ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilMDVocabulariesGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
29 use ILIAS\Data\URI;
39 
44 {
45  protected const MAX_CONFIRMATION_VALUES = 5;
46 
47  protected ilCtrl $ctrl;
48  protected HTTP $http;
51  protected ilLanguage $lng;
57  protected Refinery $refinery;
58 
61  protected Importer $importer;
62 
63  public function __construct(ilObjMDSettingsGUI $parent_obj_gui)
64  {
65  global $DIC;
66 
67  $services = new InternalServices($DIC);
68 
69  $this->vocab_manager = $services->vocabularies()->manager();
70  $this->presentation = new Presentation(
71  $services->presentation()->elements(),
72  $services->presentation()->utilities(),
73  $services->vocabularies()->presentation(),
74  $services->vocabularies()->slotHandler(),
75  $services->structure()->structure(),
76  $services->paths()->navigatorFactory(),
77  $services->paths()->pathFactory()
78  );
79  $this->importer = new Importer(
80  $services->paths()->pathFactory(),
81  $this->vocab_manager->controlledVocabularyCreator(),
82  $services->vocabularies()->slotHandler()
83  );
84 
85  $this->ctrl = $DIC->ctrl();
86  $this->http = $DIC->http();
87  $this->temp_files = $DIC->filesystem()->temp();
88  $this->lng = $DIC->language();
89  $this->tpl = $DIC->ui()->mainTemplate();
90  $this->toolbar = $DIC->toolbar();
91  $this->ui_factory = $DIC->ui()->factory();
92  $this->ui_renderer = $DIC->ui()->renderer();
93  $this->refinery = $DIC->refinery();
94 
95  $this->parent_obj_gui = $parent_obj_gui;
96  $this->access_service = new ilMDSettingsAccessService(
97  $this->parent_obj_gui->getRefId(),
98  $DIC->access()
99  );
100 
101  $this->lng->loadLanguageModule("meta");
102  }
103 
104  public function executeCommand(): void
105  {
106  $next_class = $this->ctrl->getNextClass($this);
107  $cmd = $this->ctrl->getCmd();
108 
109  if (
110  !$this->access_service->hasCurrentUserVisibleAccess() ||
111  !$this->access_service->hasCurrentUserReadAccess()
112  ) {
113  throw new ilPermissionException($this->lng->txt('no_permission'));
114  }
115 
116  switch ($next_class) {
117  case strtolower(ilMDVocabularyUploadHandlerGUI::class):
119  $this->ctrl->forwardCommand($handler);
120 
121  // no break
122  default:
123  if (!$cmd || $cmd === 'view') {
124  $cmd = 'showVocabularies';
125  }
126 
127  $this->$cmd();
128  break;
129  }
130  }
131 
132  public function showVocabularies(): void
133  {
134  $content = [];
135 
136  if ($this->access_service->hasCurrentUserWriteAccess()) {
137  $import_modal = $this->getImportModal();
138  $this->toolbar->addComponent($this->getImportButton($import_modal->getShowSignal()));
139  $content[] = $import_modal;
140  }
141 
142  $content[] = $this->getTable();
143 
144  $this->tpl->setContent($this->ui_renderer->render($content));
145  }
146 
147  public function tableAction(): void
148  {
149  $action = $this->fetchTableAction();
150  $vocab_id = $this->fetchVocabID();
151 
152  if (
153  $vocab_id === '' ||
154  ($action !== 'show_all' && !$this->access_service->hasCurrentUserWriteAccess())
155  ) {
156  $this->ctrl->redirect($this, 'showVocabularies');
157  }
158 
159  switch ($action) {
160  case 'delete':
161  $this->confirmDeleteVocabulary($vocab_id);
162  return;
163 
164  case 'activate':
165  $this->activateVocabulary($vocab_id);
166  return;
167 
168  case 'deactivate':
169  $this->deactivateVocabulary($vocab_id);
170  return;
171 
172  case 'allow_custom_input':
173  $this->allowCustomInputForVocabulary($vocab_id);
174  return;
175 
176  case 'disallow_custom_input':
177  $this->disallowCustomInputForVocabulary($vocab_id);
178  return;
179 
180  case 'show_all':
181  $this->showAllValuesModalForVocabulary($vocab_id);
182  return;
183 
184  default:
185  $this->ctrl->redirect($this, 'showVocabularies');
186  }
187  }
188 
189  public function importVocabulary(): void
190  {
191  if (!$this->access_service->hasCurrentUserWriteAccess()) {
192  $this->ctrl->redirect($this, 'showVocabularies');
193  }
194 
195  $message_type = 'failure';
196  $message_text = $this->lng->txt('md_vocab_import_upload_failed');
197 
198  $modal = $this->getImportModal()->withRequest($this->http->request());
199 
200  $upload_folder = null;
201  if ($modal->getData()) {
202  $upload_folder = (string) ($modal->getData()['file'][0] ?? null);
203  if (!$this->temp_files->hasDir($upload_folder)) {
204  $upload_folder = null;
205  }
206  }
207 
208  $file_content = null;
209  if (!is_null($upload_folder)) {
210  $files = $files = $this->temp_files->listContents($upload_folder);
211  if (count($files) === 1 && ($files[0] ?? null)?->isFile()) {
212  $file_content = $this->temp_files->read($files[0]->getPath());
213  }
214  $this->temp_files->deleteDir($upload_folder);
215  }
216 
217  if (!is_null($file_content)) {
218  $result = $this->importer->import($file_content);
219 
220  if ($result->wasSuccessful()) {
221  $message_type = 'success';
222  $message_text = $this->lng->txt('md_vocab_import_successful');
223  } else {
224  $message_type = 'failure';
225  $message_text = sprintf(
226  $this->lng->txt('md_vocab_import_invalid'),
227  implode("<br/>", $result->getErrors())
228  );
229  }
230  }
231 
232  $this->tpl->setOnScreenMessage($message_type, $message_text, true);
233  $this->ctrl->redirect($this, 'showVocabularies');
234  }
235 
236  #[NoReturn] protected function confirmDeleteVocabulary(string $vocab_id): void
237  {
238  list($url_builder, $action_parameter_token, $vocabs_id_token) = $this->getTableURLBuilderAndParameters();
239  $key = $vocabs_id_token->getName();
240 
241  $vocab = $this->vocab_manager->getVocabulary($vocab_id);
243  foreach ($this->presentation->makeValuesPresentable(
244  $vocab,
245  self::MAX_CONFIRMATION_VALUES
246  ) as $value) {
247  $value_items[] = $this->ui_factory->modal()->interruptiveItem()->standard(
248  '',
249  $value,
250  );
251  }
252 
253  $this->ctrl->setParameter($this, $key, $vocab_id);
254  $link = $this->ctrl->getLinkTarget($this, 'deleteVocabulary');
255  $this->ctrl->clearParameters($this);
256 
257  $modal = $this->ui_factory->modal()->interruptive(
258  $this->presentation->txt('md_vocab_delete_confirmation_title'),
259  $this->presentation->txtFill(
260  'md_vocab_delete_confirmation_text',
261  $this->presentation->makeSlotPresentable($vocab->slot()),
262  $vocab->source()
263  ),
264  $link
265  )->withAffectedItems($value_items);
266  echo $this->ui_renderer->renderAsync($modal);
267  exit;
268  }
269 
270  protected function deleteVocabulary(): void
271  {
272  $vocab_id = $this->fetchVocabID();
273  if ($vocab_id !== '') {
274  $this->vocab_manager->actions()->delete(
275  $this->vocab_manager->getVocabulary($vocab_id)
276  );
277  }
278  $this->tpl->setOnScreenMessage(
280  $this->lng->txt('md_vocab_deletion_successful'),
281  true
282  );
283  $this->ctrl->redirect($this, 'showVocabularies');
284  }
285 
286  protected function activateVocabulary(string $vocab_id): void
287  {
288  $this->vocab_manager->actions()->activate(
289  $this->vocab_manager->getVocabulary($vocab_id)
290  );
291  $this->tpl->setOnScreenMessage(
293  $this->lng->txt('md_vocab_update_successful'),
294  true
295  );
296  $this->ctrl->redirect($this, 'showVocabularies');
297  }
298 
299  protected function deactivateVocabulary(string $vocab_id): void
300  {
301  $this->vocab_manager->actions()->deactivate(
302  $this->vocab_manager->getVocabulary($vocab_id)
303  );
304  $this->tpl->setOnScreenMessage(
306  $this->lng->txt('md_vocab_update_successful'),
307  true
308  );
309  $this->ctrl->redirect($this, 'showVocabularies');
310  }
311 
312  protected function allowCustomInputForVocabulary(string $vocab_id): void
313  {
314  $this->vocab_manager->actions()->allowCustomInput(
315  $this->vocab_manager->getVocabulary($vocab_id)
316  );
317  $this->tpl->setOnScreenMessage(
319  $this->lng->txt('md_vocab_update_successful'),
320  true
321  );
322  $this->ctrl->redirect($this, 'showVocabularies');
323  }
324 
325  protected function disallowCustomInputForVocabulary(string $vocab_id): void
326  {
327  $this->vocab_manager->actions()->disallowCustomInput(
328  $this->vocab_manager->getVocabulary($vocab_id)
329  );
330  $this->tpl->setOnScreenMessage(
332  $this->lng->txt('md_vocab_update_successful'),
333  true
334  );
335  $this->ctrl->redirect($this, 'showVocabularies');
336  }
337 
338  #[NoReturn] protected function showAllValuesModalForVocabulary(string $vocab_id): void
339  {
340  $vocab = $this->vocab_manager->getVocabulary($vocab_id);
341  $values = $this->ui_factory->listing()->unordered(
342  $this->presentation->makeValuesPresentable($vocab)
343  );
344  $modal = $this->ui_factory->modal()->roundtrip(
345  $this->presentation->txtFill(
346  'md_vocab_all_values_title',
347  $this->presentation->makeSlotPresentable($vocab->slot()),
348  $vocab->source()
349  ),
350  [$values]
351  );
352  echo $this->ui_renderer->renderAsync($modal);
353  exit;
354  }
355 
356  protected function getTable(): DataTable
357  {
358  $column_factory = $this->ui_factory->table()->column();
359  $columns = [
360  'element' => $column_factory->text($this->lng->txt('md_vocab_element_column'))->withIsSortable(false),
361  'type' => $column_factory->status($this->lng->txt('md_vocab_type_column'))->withIsSortable(false),
362  'source' => $column_factory->text($this->lng->txt('md_vocab_source_column'))->withIsSortable(false),
363  'preview' => $column_factory->text($this->lng->txt('md_vocab_preview_column'))->withIsSortable(false),
364  'active' => $column_factory->statusIcon($this->lng->txt('md_vocab_active_column'))->withIsSortable(false),
365  'custom_input' => $column_factory->statusIcon($this->lng->txt('md_vocab_custom_input_column'))->withIsSortable(false)
366  ];
367 
368  list($url_builder, $action_parameter_token, $row_id_token) = $this->getTableURLBuilderAndParameters();
369  $actions_factory = $this->ui_factory->table()->action();
370  $actions = [];
371 
372  if ($this->access_service->hasCurrentUserWriteAccess()) {
373  $actions ['delete'] = $actions_factory->single(
374  $this->lng->txt('md_vocab_delete_action'),
375  $url_builder->withParameter($action_parameter_token, 'delete'),
376  $row_id_token
377  )->withAsync(true);
378  $actions['activate'] = $actions_factory->single(
379  $this->lng->txt('md_vocab_activate_action'),
380  $url_builder->withParameter($action_parameter_token, 'activate'),
381  $row_id_token
382  );
383  $actions['deactivate'] = $actions_factory->single(
384  $this->lng->txt('md_vocab_deactivate_action'),
385  $url_builder->withParameter($action_parameter_token, 'deactivate'),
386  $row_id_token
387  );
388  $actions['allow_custom_input'] = $actions_factory->single(
389  $this->lng->txt('md_vocab_allow_custom_input_action'),
390  $url_builder->withParameter($action_parameter_token, 'allow_custom_input'),
391  $row_id_token
392  );
393  $actions['disallow_custom_input'] = $actions_factory->single(
394  $this->lng->txt('md_vocab_disallow_custom_input_action'),
395  $url_builder->withParameter($action_parameter_token, 'disallow_custom_input'),
396  $row_id_token
397  );
398  }
399 
400  $actions['show_all'] = $actions_factory->single(
401  $this->lng->txt('md_vocab_show_all_action'),
402  $url_builder->withParameter($action_parameter_token, 'show_all'),
403  $row_id_token
404  )->withAsync(true);
405 
406  return $this->ui_factory->table()->data(
407  $this->lng->txt('md_vocab_table_title'),
408  $columns,
409  new DataRetrieval(
410  $this->vocab_manager,
411  $this->presentation,
412  $this->ui_factory
413  )
414  )->withActions($actions)->withRequest($this->http->request());
415  }
416 
417  protected function getImportModal(): RoundtripModal
418  {
419  $file_input = $this->ui_factory->input()->field()->file(
421  $this->lng->txt('md_import_file_vocab')
422  )->withAcceptedMimeTypes([MimeType::TEXT__XML])->withMaxFiles(1);
423 
424  return $this->ui_factory->modal()->roundtrip(
425  $this->lng->txt('md_import_vocab_modal'),
426  null,
427  ['file' => $file_input],
428  $this->ctrl->getLinkTarget($this, 'importVocabulary')
429  );
430  }
431 
432  protected function getImportButton(Signal $signal): Button
433  {
434  return $this->ui_factory->button()->standard(
435  $this->lng->txt('md_import_vocab'),
436  $signal
437  );
438  }
439 
440  protected function fetchTableAction(): string
441  {
442  list($url_builder, $action_parameter_token, $vocabs_id_token) = $this->getTableURLBuilderAndParameters();
443  $key = $action_parameter_token->getName();
444  if ($this->http->wrapper()->query()->has($key)) {
445  return $this->http->wrapper()->query()->retrieve(
446  $key,
447  $this->refinery->identity()
448  );
449  }
450  return '';
451  }
452 
453  protected function fetchVocabID(): string
454  {
455  list($url_builder, $action_parameter_token, $vocabs_id_token) = $this->getTableURLBuilderAndParameters();
456  $key = $vocabs_id_token->getName();
457  if ($this->http->wrapper()->query()->has($key)) {
458  return $this->http->wrapper()->query()->retrieve(
459  $key,
460  $this->refinery->kindlyTo()->listOf($this->refinery->kindlyTo()->string())
461  )[0] ?? '';
462  }
463  return '';
464  }
465 
466  protected function getTableURLBuilderAndParameters(): array
467  {
468  $url_builder = new URLBuilder(new URI(
469  rtrim(ILIAS_HTTP_PATH, '/') . '/' . $this->ctrl->getLinkTarget($this, 'tableAction')
470  ));
471  return $url_builder->acquireParameters(
472  ['metadata', 'vocab'],
473  'table_action',
474  'ids'
475  );
476  }
477 }
ilMDVocabulariesGUI: ilMDVocabularyUploadHandlerGUI
__construct(ilObjMDSettingsGUI $parent_obj_gui)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
allowCustomInputForVocabulary(string $vocab_id)
ilMDSettingsAccessService $access_service
deactivateVocabulary(string $vocab_id)
ilGlobalTemplateInterface $tpl
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:25
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:34
activateVocabulary(string $vocab_id)
$handler
Definition: oai.php:14
ilObjMDSettingsGUI $parent_obj_gui
disallowCustomInputForVocabulary(string $vocab_id)
URLBuilder.
Definition: URLBuilder.php:39