ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMDEditorGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
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;
41
46{
47 public const string SET_FOR_TREE = 'md_set_for_tree';
48 public const string PATH_FOR_TREE = 'md_path_for_tree';
49
52
53 protected ilCtrl $ctrl;
62 protected GlobalScreen $global_screen;
63 protected ilTabsGUI $tabs;
64 protected UIFactory $ui_factory;
65 protected XMLWriter $xml_writer;
66
67 protected int $obj_id;
68 protected int $sub_id;
69 public string $type;
70
71 public function __construct(int $obj_id, int $sub_id, string $type)
72 {
73 global $DIC;
74
75 $services = new InternalServices($DIC);
76 $this->full_editor_initiator = new FullEditorInitiator($services);
77 $this->digest_initiator = new DigestInitiator($services);
78
79 $this->ctrl = $services->dic()->ctrl();
80 $this->tpl = $services->dic()->ui()->mainTemplate();
81 $this->ui_renderer = $services->dic()->ui()->renderer();
82 $this->presenter = $services->editor()->presenter();
83 $this->request_parser = $services->editor()->requestParser();
84 $this->repository = $services->repository()->repository();
85 $this->observer_handler = $services->editor()->observerHandler();
86 $this->access = $services->dic()->access();
87 $this->toolbar = $services->dic()->toolbar();
88 $this->global_screen = $services->dic()->globalScreen();
89 $this->tabs = $services->dic()->tabs();
90 $this->ui_factory = $services->dic()->ui()->factory();
91 $this->xml_writer = $services->xml()->standardWriter();
92
93 $this->obj_id = $obj_id;
94 $this->sub_id = $sub_id === 0 ? $obj_id : $sub_id;
95 $this->type = $type;
96 }
97
98 public function executeCommand(): void
99 {
100 $next_class = $this->ctrl->getNextClass($this);
101
102 $cmd = $this->ctrl->getCmd();
103 switch ($next_class) {
104 default:
105 if (!$cmd) {
106 $cmd = "listQuickEdit";
107 }
108 $this->$cmd();
109 break;
110 }
111 }
112
113 public function debug(): bool
114 {
115 $xml = $this->xml_writer->write($this->repository->getMD($this->obj_id, $this->sub_id, $this->type));
116 $dom = new DOMDocument('1.0');
117 $dom->formatOutput = true;
118 $dom->preserveWhiteSpace = false;
119 $dom->loadXML($xml->asXML());
120
121 $this->addButtonToFullEditor();
122 $this->tpl->setContent('<pre>' . htmlentities($dom->saveXML()) . '</pre>');
123 return true;
124 }
125
126 public function listSection(): void
127 {
128 $this->listQuickEdit();
129 }
130
131 public function listQuickEdit(): void
132 {
133 $digest = $this->digest_initiator->init();
134 $set = $this->repository->getMD(
135 $this->obj_id,
136 $this->sub_id,
137 $this->type
138 );
139
140 $this->renderDigest($set, $digest);
141 }
142
143 public function updateQuickEdit(): void
144 {
145 $this->checkAccess();
146
147 $digest = $this->digest_initiator->init();
148 $set = $this->repository->getMD(
149 $this->obj_id,
150 $this->sub_id,
151 $this->type
152 );
153
154 $request = $this->request_parser->fetchRequestForForm(false);
155 if (!$digest->updateMD($set, $request)) {
156 $this->tpl->setOnScreenMessage(
157 'failure',
158 $this->presenter->utilities()->txt('msg_form_save_error'),
159 true
160 );
161 $this->renderDigest($set, $digest, $request);
162 return;
163 }
164
165 $this->callListeners('General');
166 $this->callListeners('Rights');
167 $this->callListeners('Educational');
168 $this->callListeners('Lifecycle');
169
170 // Redirect here to read new title and description
171 $this->tpl->setOnScreenMessage(
172 'success',
173 $this->presenter->utilities()->txt("saved_successfully"),
174 true
175 );
176 $this->ctrl->redirect($this, 'listQuickEdit');
177 }
178
179 protected function renderDigest(
180 SetInterface $set,
181 Digest $digest,
182 ?RequestForFormInterface $request = null
183 ): void {
184 $content = $digest->getContent($set, $request);
185 $template_content = [];
186 foreach ($content as $type => $entity) {
187 switch ($type) {
188 case DigestContentType::FORM:
190 $template_content[] = $entity;
191 break;
192
193 case DigestContentType::JS_SOURCE:
194 $this->tpl->addJavaScript($entity);
195 break;
196 }
197 }
198 $this->addButtonToFullEditor();
199 $this->tpl->setContent($this->ui_renderer->render($template_content));
200 }
201
202 protected function fullEditorCreate(): void
203 {
204 $this->fullEditorEdit(true);
205 }
206
207 protected function fullEditorUpdate(): void
208 {
209 $this->fullEditorEdit(false);
210 }
211
212 protected function fullEditorEdit(bool $create): void
213 {
214 $this->checkAccess();
215
216 // get the paths from the http request
217 $base_path = $this->request_parser->fetchBasePath();
218 $action_path = $this->request_parser->fetchActionPath();
219
220 // get and prepare the MD
221 $set = $this->repository->getMD(
222 $this->obj_id,
223 $this->sub_id,
224 $this->type
225 );
226 $editor = $this->full_editor_initiator->init();
227 $set = $editor->manipulateMD()->prepare($set, $base_path);
228
229 // update or create
230 $request = $this->request_parser->fetchRequestForForm(true);
231 $success = $editor->manipulateMD()->createOrUpdate(
232 $set,
233 $base_path,
234 $action_path,
235 $request
236 );
237 if (!$success) {
238 $this->tpl->setOnScreenMessage(
239 'failure',
240 $this->presenter->utilities()->txt('msg_form_save_error'),
241 true
242 );
243 $this->renderFullEditor($set, $base_path, $editor, $request);
244 return;
245 }
246
247 // call listeners
248 $this->observer_handler->callObserversByPath($action_path);
249
250 // redirect back to the full editor
251 $this->tpl->setOnScreenMessage(
252 'success',
253 $this->presenter->utilities()->txt(
254 $create ?
255 'meta_add_element_success' :
256 'meta_edit_element_success'
257 ),
258 true
259 );
260 $this->ctrl->setParameter(
261 $this,
262 Parameter::BASE_PATH->value,
263 urlencode($base_path->toString())
264 );
265 $this->ctrl->redirect($this, 'fullEditor');
266 }
267
268 protected function fullEditorDelete(): void
269 {
270 $this->checkAccess();
271
272 // get the paths from the http request
273 $base_path = $this->request_parser->fetchBasePath();
274 $delete_path = $this->request_parser->fetchActionPath();
275
276 // get the MD
277 $set = $this->repository->getMD(
278 $this->obj_id,
279 $this->sub_id,
280 $this->type
281 );
282 $editor = $this->full_editor_initiator->init();
283
284 // delete
285 $base_path = $editor->manipulateMD()->deleteAndTrimBasePath(
286 $set,
287 $base_path,
288 $delete_path
289 );
290
291 // call listeners
292 $this->observer_handler->callObserversByPath($delete_path);
293
294 // redirect back to the full editor
295 $this->tpl->setOnScreenMessage(
296 'success',
297 $this->presenter->utilities()->txt('meta_delete_element_success'),
298 true
299 );
300 $this->ctrl->setParameter(
301 $this,
302 Parameter::BASE_PATH->value,
303 urlencode($base_path->toString())
304 );
305 $this->ctrl->redirect($this, 'fullEditor');
306 }
307
308 protected function fullEditor(): void
309 {
310 $this->setTabsForFullEditor();
311
312 // get the paths from the http request
313 $base_path = $this->request_parser->fetchBasePath();
314
315 // get and prepare the MD
316 $set = $this->repository->getMD(
317 $this->obj_id,
318 $this->sub_id,
319 $this->type
320 );
321 $editor = $this->full_editor_initiator->init();
322 $set = $editor->manipulateMD()->prepare($set, $base_path);
323
324 // add content for element
325 $this->renderFullEditor($set, $base_path, $editor);
326 }
327
328 protected function renderFullEditor(
329 SetInterface $set,
330 PathInterface $base_path,
331 FullEditor $full_editor,
332 ?RequestForFormInterface $request = null
333 ): void {
334 // add slate with tree
335 $this->global_screen->tool()->context()->current()->addAdditionalData(
336 self::SET_FOR_TREE,
337 $set
338 );
339 $this->global_screen->tool()->context()->current()->addAdditionalData(
340 self::PATH_FOR_TREE,
341 $base_path
342 );
343
344 // render toolbar, modals and main content
345 $content = $full_editor->getContent($set, $base_path, $request);
346 $template_content = [];
347 foreach ($content as $type => $entity) {
348 switch ($type) {
349 case FullContentType::MAIN:
350 if ($entity instanceof Table) {
351 $entity = $this->ui_factory->legacy()->content(
352 $entity->getHTML()
353 );
354 }
355 $template_content[] = $entity;
356 break;
357
359 if ($modal = $entity->getModal()) {
360 $template_content[] = $modal;
361 }
362 break;
363
364 case FullContentType::TOOLBAR:
365 $this->toolbar->addComponent($entity);
366 break;
367 }
368 }
369 $this->tpl->setContent($this->ui_renderer->render($template_content));
370 }
371
372 protected function setTabsForFullEditor(): void
373 {
374 $this->tabs->clearSubTabs();
375 foreach ($this->tabs->target as $tab) {
376 if (($tab['id'] ?? null) !== $this->tabs->getActiveTab()) {
377 $this->tabs->removeTab($tab['id']);
378 }
379 }
380 $this->tabs->removeNonTabbedLinks();
381 $this->tabs->setBackTarget(
382 $this->presenter->utilities()->txt('back'),
383 $this->ctrl->getLinkTarget($this, 'listQuickEdit')
384 );
385 }
386
387 protected function addButtonToFullEditor(): void
388 {
389 $editor = $this->ui_factory->button()->standard(
390 $this->presenter->utilities()->txt('meta_button_to_full_editor_label'),
391 $this->ctrl->getLinkTarget($this, 'fullEditor')
392 );
393 $this->toolbar->addComponent($editor);
394 if (DEVMODE) {
395 $debug = $this->ui_factory->button()->standard(
396 'Debug',
397 $this->ctrl->getLinkTarget($this, 'debug')
398 );
399 $this->toolbar->addComponent($debug);
400 }
401 }
402
403 protected function checkAccess(): void
404 {
405 // if there is no fixed parent (e.g. mob), then skip
406 if ($this->obj_id === 0) {
407 return;
408 }
409 $ref_ids = ilObject::_getAllReferences($this->obj_id);
410 // if there are no references (e.g. in workspace), then skip
411 if (empty($ref_ids)) {
412 return;
413 }
414 foreach ($ref_ids as $ref_id) {
415 if ($this->access->checkAccess(
416 'write',
417 '',
418 $ref_id,
419 '',
420 $this->obj_id
421 )) {
422 return;
423 }
424 }
425 throw new ilPermissionException($this->presenter->utilities()->txt('permission_denied'));
426 }
427
428 // Observer methods
429 public function addObserver(object $a_class, string $a_method, string $a_element): void
430 {
431 $this->observer_handler->addObserver($a_class, $a_method, $a_element);
432 }
433
434 public function callListeners(string $a_element): void
435 {
436 $this->observer_handler->callObservers($a_element);
437 }
438}
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:79
Class ilCtrl provides processing control methods.
const string PATH_FOR_TREE
FullEditorInitiator $full_editor_initiator
RequestParserInterface $request_parser
fullEditorEdit(bool $create)
GlobalScreen $global_screen
const string SET_FOR_TREE
__construct(int $obj_id, int $sub_id, string $type)
renderFullEditor(SetInterface $set, PathInterface $base_path, FullEditor $full_editor, ?RequestForFormInterface $request=null)
DigestInitiator $digest_initiator
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
RepositoryInterface $repository
ilToolbarGUI $toolbar
ilAccessHandler $access
static _getAllReferences(int $id)
get all reference ids for object ID
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...
$ref_id
Definition: ltiauth.php:66
global $DIC
Definition: shib_login.php:26