ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMDCopyrightSelectionGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
30 use ILIAS\Data\URI;
34 
39 {
40  protected ilCtrl $ctrl;
42  protected ilLanguage $lng;
43  protected ilTabsGUI $tabs_gui;
46  protected Factory $refinery;
49  protected IRSS $irss;
50 
56 
57  protected ?ilMDSettings $md_settings = null;
58 
59  public function __construct(ilObjMDSettingsGUI $parent_gui)
60  {
61  global $DIC;
62 
63  $this->ctrl = $DIC->ctrl();
64  $this->lng = $DIC->language();
65  $this->tpl = $DIC->ui()->mainTemplate();
66  $this->tabs_gui = $DIC->tabs();
67  $this->toolbar_gui = $DIC->toolbar();
68  $this->http = $DIC->http();
69  $this->refinery = $DIC->refinery();
70  $this->ui_factory = $DIC->ui()->factory();
71  $this->ui_renderer = $DIC->ui()->renderer();
72  $this->irss = $DIC->resourceStorage();
73 
74  $this->parent_gui = $parent_gui;
75  $this->access_service = new ilMDSettingsAccessService(
76  $this->parent_gui->getRefId(),
77  $DIC->access()
78  );
79  $this->modal_service = new ilMDSettingsModalService(
80  $this->ui_factory
81  );
82  $this->renderer = new Renderer(
83  $DIC->ui()->factory(),
84  $DIC->resourceStorage()
85  );
86  $this->repository = new DatabaseRepository($DIC->database());
87 
88  $this->lng->loadLanguageModule("meta");
89  }
90 
91  public function executeCommand(): void
92  {
93  $next_class = $this->ctrl->getNextClass($this);
94  $cmd = $this->ctrl->getCmd();
95 
96  if (
97  !$this->access_service->hasCurrentUserVisibleAccess() ||
98  !$this->access_service->hasCurrentUserReadAccess()
99  ) {
100  throw new ilPermissionException($this->lng->txt('no_permission'));
101  }
102 
103  switch ($next_class) {
104  case strtolower(ilMDCopyrightUsageGUI::class):
105  // this command is used if copyrightUsageGUI calls getParentReturn (see ...UsageGUI->setTabs)
106  $this->ctrl->setReturn($this, 'showCopyrightSelection');
107  $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
108  $gui = new ilMDCopyrightUsageGUI($entry);
109  $this->ctrl->forwardCommand($gui);
110  break;
111 
112  case strtolower(ilMDCopyrightImageUploadHandlerGUI::class):
113  $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
114  $file_id = empty($entry?->copyrightData()?->imageFile()) ? '' : $entry?->copyrightData()?->imageFile();
116  $this->ctrl->forwardCommand($handler);
117 
118  // no break
119  default:
120  if (!$cmd || $cmd === 'view') {
121  $cmd = 'showCopyrightSettings';
122  }
123 
124  $this->$cmd();
125  break;
126  }
127  }
128 
129  protected function setCopyrightTabs(string $active_subtab): void
130  {
131  if (
132  !$this->access_service->hasCurrentUserVisibleAccess() ||
133  !$this->access_service->hasCurrentUserReadAccess()
134  ) {
135  return;
136  }
137  $this->tabs_gui->setTabActive('md_copyright');
138 
139  $this->tabs_gui->addSubTab(
140  'md_copyright_settings',
141  $this->lng->txt('settings'),
142  $this->ctrl->getLinkTarget($this, 'showCopyrightSettings')
143  );
144 
145  $this->tabs_gui->addSubTab(
146  'md_copyright_selection',
147  $this->lng->txt('md_copyright_selection'),
148  $this->ctrl->getLinkTarget($this, 'showCopyrightSelection')
149  );
150 
151  if (in_array($active_subtab, [
152  'md_copyright_settings',
153  'md_copyright_selection'
154  ])) {
155  $this->tabs_gui->activateSubTab($active_subtab);
156  return;
157  }
158  $this->tabs_gui->activateSubTab('md_copyright_settings');
159  }
160 
161  public function showCopyrightSettings(?ilPropertyFormGUI $form = null): void
162  {
163  $this->setCopyrightTabs('md_copyright_settings');
164 
165  if (!$form instanceof ilPropertyFormGUI) {
166  $form = $this->initSettingsForm();
167  }
168  $this->tpl->setContent($form->getHTML());
169  }
170 
171  public function showCopyrightSelection(
172  int $current_id = 0,
173  RoundTrip $current_modal = null
174  ): void {
175  $this->setCopyrightTabs('md_copyright_selection');
176 
177  $has_write = $this->access_service->hasCurrentUserWriteAccess();
178 
179  $table_gui = new ilMDCopyrightTableGUI($this, 'showCopyrightSelection', $has_write);
180  $table_gui->setTitle($this->lng->txt("md_copyright_selection"));
181  $table_gui->parseSelections();
182 
183  $edit_modals = [];
184  if ($has_write) {
185  foreach ($this->repository->getAllEntries() as $entry) {
186  if ($entry->id() === $current_id) {
187  $modal = $current_modal;
188  } else {
189  $modal = $this->initCopyrightEditModal($entry);
190  }
191  $table_gui->setEditModalSignal($entry->id(), $modal->getShowSignal());
192  $edit_modals[] = $modal;
193  }
194  if ($current_id === 0 && !is_null($current_modal)) {
195  $add_modal = $current_modal;
196  } else {
197  $add_modal = $this->initCopyrightEditModal();
198  }
199  $this->toolbar_gui->addComponent($this->ui_factory->button()->standard(
200  $this->lng->txt('md_copyright_add'),
201  $add_modal->getShowSignal()
202  ));
203 
204  $table_gui->addMultiCommand("confirmDeleteEntries", $this->lng->txt("delete"));
205  $table_gui->setSelectAllCheckbox("entry_id");
206  }
207 
208  $this->tpl->setContent(
209  $table_gui->getHTML() .
210  (isset($add_modal) ? $this->ui_renderer->render($add_modal) : '') .
211  $this->ui_renderer->render($edit_modals)
212  );
213  }
214 
215  public function saveCopyrightSettings(): void
216  {
217  if (!$this->access_service->hasCurrentUserWriteAccess()) {
218  $this->ctrl->redirect($this, "showCopyrightSettings");
219  }
220  $form = $this->initSettingsForm();
221  if ($form->checkInput()) {
222  $this->MDSettings()->activateCopyrightSelection((bool) $form->getInput('active'));
223  $this->MDSettings()->save();
224  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
225  $this->ctrl->redirect($this, 'showCopyrightSettings');
226  }
227  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_check_input'), true);
228  $this->showCopyrightSettings($form);
229  }
230 
231  public function saveEntry(): void
232  {
233  $modal = $this
234  ->initCopyrightEditModal(null, true)
235  ->withRequest($this->http->request());
236 
237  if ($data = $modal->getData()) {
238  $this->repository->createEntry(
239  $data['title'],
240  $data['description'],
241  (bool) $data['outdated'],
242  $data['copyright']['full_name'],
243  $data['copyright']['link'],
244  $this->extractImageFromData($data),
245  $data['copyright']['alt_text']
246  );
247  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
248 
249  $this->ctrl->redirect($this, 'showCopyrightSelection');
250  }
251  $this->showCopyrightSelection(0, $modal);
252  }
253 
254  public function confirmDeleteEntries(): void
255  {
256  $entry_ids = $this->initEntryIdFromPost();
257  if (!count($entry_ids)) {
258  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
259  $this->showCopyrightSelection();
260  return;
261  }
262 
263  $c_gui = new ilConfirmationGUI();
264  // set confirm/cancel commands
265  $c_gui->setFormAction($this->ctrl->getFormAction($this, "deleteEntries"));
266  $c_gui->setHeaderText($this->lng->txt("md_delete_cp_sure"));
267  $c_gui->setCancel($this->lng->txt("cancel"), "showCopyrightSelection");
268  $c_gui->setConfirm($this->lng->txt("confirm"), "deleteEntries");
269 
270  // add items to delete
271  foreach ($entry_ids as $entry_id) {
272  $entry = $this->repository->getEntry($entry_id);
273  $c_gui->addItem('entry_id[]', (string) $entry_id, $entry->title());
274  }
275  $this->tpl->setContent($c_gui->getHTML());
276  }
277 
278  public function deleteEntries(): bool
279  {
280  $entry_ids = $this->initEntryIdFromPost();
281  if (!count($entry_ids)) {
282  $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
283  $this->showCopyrightSelection();
284  return true;
285  }
286 
287  foreach ($entry_ids as $entry_id) {
288  $entry = $this->repository->getEntry($entry_id);
289  $this->deleteFile($entry);
290  $this->repository->deleteEntry($entry_id);
291  }
292  $this->tpl->setOnScreenMessage('success', $this->lng->txt('md_copyrights_deleted'));
293  $this->showCopyrightSelection();
294  return true;
295  }
296 
297  public function updateEntry(): void
298  {
299  $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
300  $modal = $this
301  ->initCopyrightEditModal($entry, true)
302  ->withRequest($this->http->request());
303 
304  if ($data = $modal->getData()) {
305  $this->deleteFileIfChanged($entry, $data);
306  $this->repository->updateEntry(
307  $entry->id(),
308  $data['title'],
309  $data['description'],
310  (bool) $data['outdated'],
311  $data['copyright']['full_name'],
312  $data['copyright']['link'],
313  $this->extractImageFromData($data),
314  $data['copyright']['alt_text']
315  );
316  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
317 
318  $this->ctrl->redirect($this, 'showCopyrightSelection');
319  }
320  $this->showCopyrightSelection($entry->id(), $modal);
321  }
322 
323  protected function initCopyrightEditModal(
324  EntryInterface $entry = null,
325  bool $open_on_load = false
326  ): RoundTrip {
327  $inputs = [];
328  $ff = $this->ui_factory->input()->field();
329 
330  $title = $ff
331  ->text($this->lng->txt('title'))
332  ->withValue($entry?->title() ?? '')
333  ->withRequired(true)
334  ->withMaxLength(255);
335  $inputs['title'] = $title;
336 
337  $des = $ff
338  ->textarea($this->lng->txt('description'))
339  ->withValue($entry?->description() ?? '');
340  $inputs['description'] = $des;
341 
342  $usage = $ff
343  ->radio($this->lng->txt('meta_copyright_usage'))
344  ->withOption('0', $this->lng->txt('meta_copyright_in_use'))
345  ->withOption('1', $this->lng->txt('meta_copyright_outdated'))
346  ->withValue((int) $entry?->isOutdated());
347  $inputs['outdated'] = $usage;
348 
349  $cp_data = $entry?->copyrightData();
350 
351  $full_name = $ff
352  ->text($this->lng->txt('md_copyright_full_name'))
353  ->withValue($cp_data?->fullName() ?? '');
354 
355  $link = $ff
356  ->url(
357  $this->lng->txt('md_copyright_link'),
358  $this->lng->txt('md_copyright_link_info')
359  )
360  ->withValue((string) $cp_data?->link())
362  $this->refinery->custom()->transformation(fn($v) => $v instanceof URI ? $v : null)
363  );
364 
365  $image_link = $ff
366  ->url($this->lng->txt('md_copyright_image_link'))
367  ->withValue((string) $cp_data?->imageLink())
368  ->withAdditionalTransformation($this->refinery->custom()->transformation(
369  fn($v) => $v instanceof URI ? $v : null
370  ));
371 
372  $file_id = empty($cp_data?->imageFile()) ? '' : $cp_data?->imageFile();
373  $image_file = $ff
374  ->file(
376  $this->lng->txt('md_copyright_image_file')
377  )
378  ->withMaxFiles(1)
379  ->withAcceptedMimeTypes([MimeType::IMAGE__PNG, MimeType::IMAGE__JPEG]);
380  if ($file_id !== '') {
381  $image_file = $image_file->withValue([$file_id]);
382  };
383 
384  $image_value = 'link_group';
385  if (!is_null($cp_data) && !$cp_data->isImageLink()) {
386  $image_value = 'file_group';
387  }
388  $image = $ff
389  ->switchableGroup(
390  [
391  'link_group' => $ff->group(
392  ['image_link' => $image_link],
393  $this->lng->txt('md_copyright_image_is_link')
394  ),
395  'file_group' => $ff->group(
396  ['image_file' => $image_file],
397  $this->lng->txt('md_copyright_image_is_file')
398  ),
399  ],
400  $this->lng->txt('md_copyright_image')
401  )
402  ->withValue($image_value);
403 
404  $alt_text = $ff
405  ->text(
406  $this->lng->txt('md_copyright_alt_text'),
407  $this->lng->txt('md_copyright_alt_text_info')
408  )
409  ->withValue($cp_data?->altText() ?? '');
410 
411  $cop = $ff
412  ->section(
413  [
414  'full_name' => $full_name,
415  'link' => $link,
416  'image' => $image,
417  'alt_text' => $alt_text
418  ],
419  $this->lng->txt('md_copyright_value')
420  );
421  $inputs['copyright'] = $cop;
422 
423  if (!isset($entry)) {
424  $title = $this->lng->txt('md_copyright_add');
425  $post_url = $this->ctrl->getLinkTarget($this, 'saveEntry');
426  } else {
427  $title = $this->lng->txt('md_copyright_edit');
428  $this->ctrl->setParameter($this, 'entry_id', $entry->id());
429  $post_url = $this->ctrl->getLinkTarget($this, 'updateEntry');
430  $this->ctrl->clearParameters($this);
431  }
432 
433  $modal = $this->modal_service->modalWithForm(
434  $title,
435  $post_url,
436  ...$inputs
437  );
438  if ($open_on_load) {
439  $modal = $modal->withOnLoad($modal->getShowSignal());
440  }
441  return $modal;
442  }
443 
444  protected function initSettingsForm(): ilPropertyFormGUI
445  {
446  $form = new ilPropertyFormGUI();
447  $form->setFormAction($this->ctrl->getFormAction($this));
448  $form->setTitle($this->lng->txt('md_copyright_settings'));
449 
450  if ($this->access_service->hasCurrentUserWriteAccess()) {
451  $form->addCommandButton('saveCopyrightSettings', $this->lng->txt('save'));
452  }
453 
454  $check = new ilCheckboxInputGUI($this->lng->txt('md_copyright_enabled'), 'active');
455  $check->setChecked($this->MDSettings()->isCopyrightSelectionActive());
456  $check->setValue('1');
457  $check->setInfo($this->lng->txt('md_copyright_enable_info'));
458  $form->addItem($check);
459 
461  $this->getAdministrationFormId(),
462  $form,
463  $this->parent_gui
464  );
465  return $form;
466  }
467 
468  public function saveCopyrightPosition(): bool
469  {
470  if (!$this->http->wrapper()->post()->has('order')) {
471  $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_select_one'), true);
472  $this->ctrl->redirect($this, 'showCopyrightSelection');
473  return false;
474  }
475  $positions = $this->http->wrapper()->post()->retrieve(
476  'order',
477  $this->refinery->kindlyTo()->dictOf(
478  $this->refinery->kindlyTo()->string()
479  )
480  );
481  asort($positions);
482  $ids = [];
483  foreach ($positions as $entry_id => $position_ignored) {
484  $ids[] = (int) $entry_id;
485  }
486  $this->repository->reorderEntries(...$ids);
487  $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
488  $this->ctrl->redirect($this, 'showCopyrightSelection');
489  return false;
490  }
491 
492  protected function deleteFile(EntryInterface $entry): void
493  {
494  if (($image_file = $entry->copyrightData()->imageFile()) === '') {
495  return;
496  }
497  if ($id = $this->irss->manage()->find($image_file)) {
498  $this->irss->manage()->remove($id, new ilMDCopyrightImageStakeholder());
499  }
500  }
501 
502  protected function deleteFileIfChanged(
503  EntryInterface $entry,
504  array $data
505  ): void {
506  if (($image_file = $entry->copyrightData()->imageFile()) === '') {
507  return;
508  }
509  if (
510  $data['copyright']['image'][0] === 'file_group' &&
511  $image_file === ($data['copyright']['image'][1]['image_file'][0] ?? '')
512  ) {
513  return;
514  }
515  $this->deleteFile($entry);
516  }
517 
518  protected function extractImageFromData(array $data): string|URI
519  {
520  $v = $data['copyright']['image'];
521  if ($v[0] === 'link_group') {
522  return empty($link = $v[1]['image_link']) ? '' : $link;
523  }
524  if ($v[0] === 'file_group') {
525  return $v[1]['image_file'][0] ?? '';
526  }
527  return '';
528  }
529 
530  protected function MDSettings(): ilMDSettings
531  {
532  if (!isset($this->md_settings)) {
533  $this->md_settings = ilMDSettings::_getInstance();
534  }
535  return $this->md_settings;
536  }
537 
538  protected function initEntryIdFromQuery(): int
539  {
540  $entry_id = 0;
541  if ($this->http->wrapper()->query()->has('entry_id')) {
542  $entry_id = $this->http->wrapper()->query()->retrieve(
543  'entry_id',
544  $this->refinery->kindlyTo()->int()
545  );
546  }
547  return $entry_id;
548  }
549 
550  protected function initEntryIdFromPost(): array
551  {
552  $entries = [];
553  if ($this->http->wrapper()->post()->has('entry_id')) {
554  return $this->http->wrapper()->post()->retrieve(
555  'entry_id',
556  $this->refinery->kindlyTo()->listOf(
557  $this->refinery->kindlyTo()->int()
558  )
559  );
560  }
561  return [];
562  }
563 
564  protected function getAdministrationFormId(): int
565  {
567  }
568 }
Interface GlobalHttpState.
ilMDCopyrightSelectionGUI: ilMDCopyrightUsageGUI
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilMDSettingsAccessService $access_service
global $DIC
Definition: feed.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
showCopyrightSelection(int $current_id=0, RoundTrip $current_modal=null)
static http()
Fetches the global http state from ILIAS.
deleteFileIfChanged(EntryInterface $entry, array $data)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
initCopyrightEditModal(EntryInterface $entry=null, bool $open_on_load=false)
The scope of this class is split ilias-conform URI&#39;s into components.
Definition: URI.php:18
ilMDCopyrightImageUploadHandlerGUI: ilMDCopyrightSelectionGUI
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:58
static addFieldsToForm(int $a_form_id, ilPropertyFormGUI $a_form, ilObjectGUI $a_parent_gui)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
showCopyrightSettings(?ilPropertyFormGUI $form=null)
$check
Definition: buildRTE.php:81
__construct(ilObjMDSettingsGUI $parent_gui)
$handler
Definition: index.php:18