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