ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilMDEditorGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\MetaData\Editor\Full\Services\Services as FullEditorServices;
29use ILIAS\GlobalScreen\Services as GlobalScreen;
30use ILIAS\UI\Factory as UIFactory;
35use ILIAS\MetaData\Editor\Full\ContentType as FullContentType;
36use ILIAS\MetaData\Editor\Digest\ContentType as DigestContentType;
41use ILIAS\MetaData\OERHarvester\Services\Services as PublishingServices;
46
53{
54 public const string SET_FOR_TREE = 'md_set_for_tree';
55 public const string PATH_FOR_TREE = 'md_path_for_tree';
56
57 protected FullEditorServices $full_editor_services;
58 protected DigestServices $digest_services;
59 protected PublishingServices $publishing_services;
60
61 protected ilCtrl $ctrl;
70 protected GlobalScreen $global_screen;
71 protected ilTabsGUI $tabs;
72 protected UIFactory $ui_factory;
73 protected XMLWriter $xml_writer;
74
75 protected int $obj_id;
76 protected int $sub_id;
77 public string $type;
78 protected int $ref_id;
79
80 public function __construct(int $obj_id, int $sub_id, string $type, int $ref_id = 0)
81 {
82 global $DIC;
83
84 $services = new InternalServices($DIC);
85
86 $this->full_editor_services = $services->editor()->fullEditor();
87 $this->digest_services = $services->editor()->digest();
88 $this->publishing_services = $services->OERHarvester();
89
90 $this->ctrl = $services->dic()->ctrl();
91 $this->tpl = $services->dic()->ui()->mainTemplate();
92 $this->ui_renderer = $services->dic()->ui()->renderer();
93 $this->presenter = $services->editor()->internal()->presenter();
94 $this->request_parser = $services->editor()->internal()->requestParser();
95 $this->repository = $services->repository()->repository();
96 $this->observer_handler = $services->editor()->internal()->observerHandler();
97 $this->access = $services->dic()->access();
98 $this->toolbar = $services->dic()->toolbar();
99 $this->global_screen = $services->dic()->globalScreen();
100 $this->tabs = $services->dic()->tabs();
101 $this->ui_factory = $services->dic()->ui()->factory();
102 $this->xml_writer = $services->xml()->standardWriter();
103
104 $this->obj_id = $obj_id;
105 $this->sub_id = $sub_id === 0 ? $obj_id : $sub_id;
106 $this->ref_id = $ref_id;
107 $this->type = $type;
108 }
109
110 public function executeCommand(): void
111 {
112 $next_class = $this->ctrl->getNextClass($this);
113
114 $cmd = $this->ctrl->getCmd();
115 switch ($next_class) {
116 case strtolower(ControlCenterGUI::class):
117 $back_link = $this->ctrl->getLinkTarget($this, 'listQuickEdit');
118 $gui = $this->publishing_services->controlCenterGUI($back_link);
119 $this->ctrl->forwardCommand($gui);
120 break;
121
122 default:
123 $valid_cmd = (Command::tryFrom($cmd) ?? Command::SHOW_DIGEST)->value;
124 $this->$valid_cmd();
125 break;
126 }
127 }
128
129 public function debug(): bool
130 {
131 $xml = $this->xml_writer->write($this->repository->getMD($this->obj_id, $this->sub_id, $this->type));
132 $dom = new DOMDocument('1.0');
133 $dom->formatOutput = true;
134 $dom->preserveWhiteSpace = false;
135 $dom->loadXML($xml->asXML());
136
137 $this->addButtonToFullEditor();
138 $this->tpl->setContent('<pre>' . htmlentities($dom->saveXML()) . '</pre>');
139 return true;
140 }
141
142 public function listSection(): void
143 {
144 $this->listQuickEdit();
145 }
146
147 public function listQuickEdit(): void
148 {
149 $digest = $this->digest_services->digest();
150 $set = $this->repository->getMD(
151 $this->obj_id,
152 $this->sub_id,
153 $this->type
154 );
155
156 $this->renderDigest($set, $digest);
157 }
158
159 public function updateQuickEdit(): void
160 {
161 $this->checkAccess();
162
163 $digest = $this->digest_services->digest();
164 $manipulator = $this->digest_services->manipulatorAdapter();
165 $set = $this->repository->getMD(
166 $this->obj_id,
167 $this->sub_id,
168 $this->type
169 );
170
171 $request = $this->request_parser->fetchRequestForForm(false);
172 if (!$manipulator->update($set, $request)) {
173 $this->tpl->setOnScreenMessage(
174 'failure',
175 $this->presenter->utilities()->txt('msg_form_save_error'),
176 true
177 );
178 $this->renderDigest($set, $digest, $request);
179 return;
180 }
181
182 $this->callListeners('General');
183 $this->callListeners('Rights');
184 $this->callListeners('Educational');
185 $this->callListeners('Lifecycle');
186
187 // Redirect here to read new title and description
188 $this->tpl->setOnScreenMessage(
189 'success',
190 $this->presenter->utilities()->txt("saved_successfully"),
191 true
192 );
193 $this->ctrl->redirect($this, 'listQuickEdit');
194 }
195
196 protected function renderDigest(
197 SetInterface $set,
198 Digest $digest,
199 ?RequestForFormInterface $request = null
200 ): void {
201 $content = $digest->getContent($set, $request);
202 $template_content = $this->getButtonToControlCenter();
203 foreach ($content as $type => $entity) {
204 switch ($type) {
205 case DigestContentType::FORM:
207 $template_content[] = $entity;
208 break;
209
210 case DigestContentType::JS_SOURCE:
211 $this->tpl->addJavaScript($entity);
212 break;
213 }
214 }
215 $this->addButtonToFullEditor();
216 $this->tpl->setContent($this->ui_renderer->render($template_content));
217 }
218
219 protected function fullEditorCreate(): void
220 {
221 $this->fullEditorEdit(true);
222 }
223
224 protected function fullEditorUpdate(): void
225 {
226 $this->fullEditorEdit(false);
227 }
228
229 protected function fullEditorEdit(bool $create): void
230 {
231 $this->checkAccess();
232
233 // get the paths from the http request
234 $base_path = $this->request_parser->fetchBasePath();
235 $action_path = $this->request_parser->fetchActionPath();
236
237 // get and prepare the MD
238 $set = $this->repository->getMD(
239 $this->obj_id,
240 $this->sub_id,
241 $this->type
242 );
243 $editor = $this->full_editor_services->fullEditor();
244 $manipulator = $this->full_editor_services->manipulatorAdapter();
245 $set = $manipulator->prepare($set, $base_path);
246
247 // update or create
248 $request = $this->request_parser->fetchRequestForForm(true);
249 $success = $manipulator->createOrUpdate(
250 $set,
251 $base_path,
252 $action_path,
253 $request
254 );
255 if (!$success) {
256 $this->tpl->setOnScreenMessage(
257 'failure',
258 $this->presenter->utilities()->txt('msg_form_save_error'),
259 true
260 );
261 $this->renderFullEditor($set, $base_path, $editor, $request);
262 return;
263 }
264
265 // call listeners
266 $this->observer_handler->callObserversByPath($action_path);
267
268 // redirect back to the full editor
269 $this->tpl->setOnScreenMessage(
270 'success',
271 $this->presenter->utilities()->txt(
272 $create ?
273 'meta_add_element_success' :
274 'meta_edit_element_success'
275 ),
276 true
277 );
278 $this->ctrl->setParameter(
279 $this,
280 Parameter::BASE_PATH->value,
281 urlencode($base_path->toString())
282 );
283 $this->ctrl->redirect($this, 'fullEditor');
284 }
285
286 protected function fullEditorDelete(): void
287 {
288 $this->checkAccess();
289
290 // get the paths from the http request
291 $base_path = $this->request_parser->fetchBasePath();
292 $delete_path = $this->request_parser->fetchActionPath();
293
294 // get the MD
295 $set = $this->repository->getMD(
296 $this->obj_id,
297 $this->sub_id,
298 $this->type
299 );
300 $editor = $this->full_editor_services->fullEditor();
301 $manipulator = $this->full_editor_services->manipulatorAdapter();
302
303 // delete
304 $base_path = $manipulator->deleteAndTrimBasePath(
305 $set,
306 $base_path,
307 $delete_path
308 );
309
310 // call listeners
311 $this->observer_handler->callObserversByPath($delete_path);
312
313 // redirect back to the full editor
314 $this->tpl->setOnScreenMessage(
315 'success',
316 $this->presenter->utilities()->txt('meta_delete_element_success'),
317 true
318 );
319 $this->ctrl->setParameter(
320 $this,
321 Parameter::BASE_PATH->value,
322 urlencode($base_path->toString())
323 );
324 $this->ctrl->redirect($this, 'fullEditor');
325 }
326
327 protected function fullEditor(): void
328 {
329 $this->setTabsForFullEditor();
330
331 // get the paths from the http request
332 $base_path = $this->request_parser->fetchBasePath();
333
334 // get and prepare the MD
335 $set = $this->repository->getMD(
336 $this->obj_id,
337 $this->sub_id,
338 $this->type
339 );
340 $editor = $this->full_editor_services->fullEditor();
341 $manipulator = $this->full_editor_services->manipulatorAdapter();
342 $set = $manipulator->prepare($set, $base_path);
343
344 // add content for element
345 $this->renderFullEditor($set, $base_path, $editor);
346 }
347
348 protected function renderFullEditor(
349 SetInterface $set,
350 PathInterface $base_path,
351 FullEditor $full_editor,
352 ?RequestForFormInterface $request = null
353 ): void {
354 // add slate with tree
355 $this->global_screen->tool()->context()->current()->addAdditionalData(
356 self::SET_FOR_TREE,
357 $set
358 );
359 $this->global_screen->tool()->context()->current()->addAdditionalData(
360 self::PATH_FOR_TREE,
361 $base_path
362 );
363
364 // render toolbar, modals and main content
365 $content = $full_editor->getContent($set, $base_path, $request);
366 $template_content = [];
367 foreach ($content as $type => $entity) {
368 switch ($type) {
369 case FullContentType::MAIN:
370 if ($entity instanceof Table) {
371 $entity = $this->ui_factory->legacy()->content(
372 $entity->getHTML()
373 );
374 }
375 $template_content[] = $entity;
376 break;
377
379 if ($modal = $entity->getModal()) {
380 $template_content[] = $modal;
381 }
382 break;
383
384 case FullContentType::TOOLBAR:
385 $this->toolbar->addComponent($entity);
386 break;
387 }
388 }
389 $this->tpl->setContent($this->ui_renderer->render($template_content));
390 }
391
392 protected function setTabsForFullEditor(): void
393 {
394 $this->tabs->clearSubTabs();
395 foreach ($this->tabs->target as $tab) {
396 if (($tab['id'] ?? null) !== $this->tabs->getActiveTab()) {
397 $this->tabs->removeTab($tab['id']);
398 }
399 }
400 $this->tabs->removeNonTabbedLinks();
401 $this->tabs->setBackTarget(
402 $this->presenter->utilities()->txt('back'),
403 $this->ctrl->getLinkTarget($this, 'listQuickEdit')
404 );
405 }
406
407 protected function addButtonToFullEditor(): void
408 {
409 $editor = $this->ui_factory->button()->standard(
410 $this->presenter->utilities()->txt('meta_button_to_full_editor_label'),
411 $this->ctrl->getLinkTarget($this, 'fullEditor')
412 );
413 $this->toolbar->addComponent($editor);
414 if (DEVMODE) {
415 $debug = $this->ui_factory->button()->standard(
416 'Debug',
417 $this->ctrl->getLinkTarget($this, 'debug')
418 );
419 $this->toolbar->addComponent($debug);
420 }
421 }
422
426 protected function getButtonToControlCenter(): array
427 {
428 // will also exclude subtypes
429 if (!$this->publishing_services->stateInfoFetcher()->isPublishingRelevantForObject(
430 $this->ref_id,
431 $this->type,
432 $this->obj_id
433 )) {
434 return [];
435 }
436 $status = $this->publishing_services->stateInfoFetcher()->getStatusForObject($this->obj_id);
437 return $this->publishing_services->controlCenterComponentFactory()->getButtonToControlCenter(
438 $status,
439 $this->ref_id,
440 $this->obj_id,
441 $this->type
442 );
443 }
444
445 protected function checkAccess(): void
446 {
447 // if there is no fixed parent (e.g. mob), then skip
448 if ($this->obj_id === 0 || $this->ref_id === 0) {
449 return;
450 }
451 if ($this->access->checkAccess(
452 'write',
453 '',
454 $this->ref_id,
455 '',
456 $this->obj_id
457 )) {
458 return;
459 }
460 throw new ilPermissionException($this->presenter->utilities()->txt('permission_denied'));
461 }
462
463 // Observer methods
464 public function addObserver(object $a_class, string $a_method, string $a_element): void
465 {
466 $this->observer_handler->addObserver($a_class, $a_method, $a_element);
467 }
468
469 public function callListeners(string $a_element): void
470 {
471 $this->observer_handler->callObservers($a_element);
472 }
473}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
getContent(SetInterface $set, PathInterface $base_path, ?RequestForFormInterface $request=null)
Definition: FullEditor.php:57
Class ilCtrl provides processing control methods.
const string PATH_FOR_TREE
FullEditorServices $full_editor_services
RequestParserInterface $request_parser
fullEditorEdit(bool $create)
GlobalScreen $global_screen
const string SET_FOR_TREE
renderFullEditor(SetInterface $set, PathInterface $base_path, FullEditor $full_editor, ?RequestForFormInterface $request=null)
addObserver(object $a_class, string $a_method, string $a_element)
renderDigest(SetInterface $set, Digest $digest, ?RequestForFormInterface $request=null)
callListeners(string $a_element)
PresenterInterface $presenter
ObserverHandler $observer_handler
ilGlobalTemplateInterface $tpl
PublishingServices $publishing_services
__construct(int $obj_id, int $sub_id, string $type, int $ref_id=0)
DigestServices $digest_services
RepositoryInterface $repository
ilToolbarGUI $toolbar
ilAccessHandler $access
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
An entity that renders components to a string output.
Definition: Renderer.php:31
Interface ilAccessHandler This interface combines all available interfaces which can be called via gl...
global $DIC
Definition: shib_login.php:26