ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMDCopyrightConfigurationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
23use ILIAS\UI\Factory as UIFactory;
24use ILIAS\UI\Renderer as UIRenderer;
35
40{
41 protected ilCtrl $ctrl;
43 protected ilLanguage $lng;
46 protected Factory $refinery;
47 protected UIFactory $ui_factory;
48 protected UIRenderer $ui_renderer;
49 protected IRSS $irss;
50
55
57 {
58 global $DIC;
59
60 $this->ctrl = $DIC->ctrl();
61 $this->lng = $DIC->language();
62 $this->tpl = $DIC->ui()->mainTemplate();
63 $this->toolbar_gui = $DIC->toolbar();
64 $this->http = $DIC->http();
65 $this->refinery = $DIC->refinery();
66 $this->ui_factory = $DIC->ui()->factory();
67 $this->ui_renderer = $DIC->ui()->renderer();
68 $this->irss = $DIC->resourceStorage();
69
70 $this->parent_obj_gui = $parent_obj_gui;
71 $this->access_service = new ilMDSettingsAccessService(
72 $this->parent_obj_gui->getRefId(),
73 $DIC->access()
74 );
75 $this->renderer = new Renderer(
76 $DIC->ui()->factory(),
77 $DIC->resourceStorage()
78 );
79 $this->repository = new DatabaseRepository(new Wrapper($DIC->database()));
80
81 $this->lng->loadLanguageModule("meta");
82 }
83
84 public function executeCommand(): void
85 {
86 $next_class = $this->ctrl->getNextClass($this);
87 $cmd = $this->ctrl->getCmd();
88
89 if (
90 !$this->access_service->hasCurrentUserVisibleAccess() ||
91 !$this->access_service->hasCurrentUserReadAccess()
92 ) {
93 throw new ilPermissionException($this->lng->txt('no_permission'));
94 }
95
96 switch ($next_class) {
97 case strtolower(ilMDCopyrightUsageGUI::class):
98 // this command is used if copyrightUsageGUI calls getParentReturn (see ...UsageGUI->setTabs)
99 $this->ctrl->setReturn($this, 'showCopyrightSelection');
100 $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
101 $gui = new ilMDCopyrightUsageGUI($entry);
102 $this->ctrl->forwardCommand($gui);
103 break;
104
105 case strtolower(ilMDCopyrightImageUploadHandlerGUI::class):
106 $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
107 $file_id = empty($entry?->copyrightData()?->imageFile()) ? '' : $entry?->copyrightData()?->imageFile();
109 $this->ctrl->forwardCommand($handler);
110
111 // no break
112 default:
113 if (!$cmd || $cmd === 'view') {
114 $cmd = 'showCopyrightSelection';
115 }
116
117 $this->$cmd();
118 break;
119 }
120 }
121
122 public function showCopyrightSelection(
123 int $current_id = 0,
124 ?RoundTrip $current_modal = null
125 ): void {
126 $has_write = $this->access_service->hasCurrentUserWriteAccess();
127
128 $table_gui = new ilMDCopyrightTableGUI(
129 $this->repository,
130 $this->renderer,
131 $this,
132 'showCopyrightSelection',
133 $has_write
134 );
135 $table_gui->setTitle($this->lng->txt("md_copyright_selection"));
136 $table_gui->parseSelections();
137
138 $edit_modals = [];
139 if ($has_write) {
140 foreach ($this->repository->getAllEntries() as $entry) {
141 if ($entry->id() === $current_id) {
142 $modal = $current_modal;
143 } else {
144 $modal = $this->initCopyrightEditModal($entry);
145 }
146 $table_gui->setEditModalSignal($entry->id(), $modal->getShowSignal());
147 $edit_modals[] = $modal;
148 }
149 if ($current_id === 0 && !is_null($current_modal)) {
150 $add_modal = $current_modal;
151 } else {
152 $add_modal = $this->initCopyrightEditModal();
153 }
154 $this->toolbar_gui->addComponent($this->ui_factory->button()->standard(
155 $this->lng->txt('md_copyright_add'),
156 $add_modal->getShowSignal()
157 ));
158
159 $table_gui->addMultiCommand("confirmDeleteEntries", $this->lng->txt("delete"));
160 $table_gui->setSelectAllCheckbox("entry_id");
161 }
162
163 $this->tpl->setContent(
164 $table_gui->getHTML() .
165 (isset($add_modal) ? $this->ui_renderer->render($add_modal) : '') .
166 $this->ui_renderer->render($edit_modals)
167 );
168 }
169
170 public function saveEntry(): void
171 {
172 $modal = $this
173 ->initCopyrightEditModal(null, true)
174 ->withRequest($this->http->request());
175
176 if ($data = $modal->getData()) {
177 $this->repository->createEntry(
178 $data['title'],
179 $data['description'],
180 (bool) $data['outdated'],
181 $data['copyright']['full_name'],
182 $data['copyright']['link'],
183 $this->extractImageFromData($data),
184 $data['copyright']['alt_text']
185 );
186 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
187
188 $this->ctrl->redirect($this, 'showCopyrightSelection');
189 }
190 $this->showCopyrightSelection(0, $modal);
191 }
192
193 public function confirmDeleteEntries(): void
194 {
195 $entry_ids = $this->initEntryIdFromPost();
196 if (!count($entry_ids)) {
197 $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
198 $this->showCopyrightSelection();
199 return;
200 }
201
202 $c_gui = new ilConfirmationGUI();
203 // set confirm/cancel commands
204 $c_gui->setFormAction($this->ctrl->getFormAction($this, "deleteEntries"));
205 $c_gui->setHeaderText($this->lng->txt("md_delete_cp_sure"));
206 $c_gui->setCancel($this->lng->txt("cancel"), "showCopyrightSelection");
207 $c_gui->setConfirm($this->lng->txt("confirm"), "deleteEntries");
208
209 // add items to delete
210 foreach ($entry_ids as $entry_id) {
211 $entry = $this->repository->getEntry($entry_id);
212 $c_gui->addItem('entry_id[]', (string) $entry_id, $entry->title());
213 }
214 $this->tpl->setContent($c_gui->getHTML());
215 }
216
217 public function deleteEntries(): bool
218 {
219 $entry_ids = $this->initEntryIdFromPost();
220 if (!count($entry_ids)) {
221 $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
222 $this->showCopyrightSelection();
223 return true;
224 }
225
226 foreach ($entry_ids as $entry_id) {
227 $entry = $this->repository->getEntry($entry_id);
228 $this->deleteFile($entry);
229 $this->repository->deleteEntry($entry_id);
230 }
231 $this->tpl->setOnScreenMessage('success', $this->lng->txt('md_copyrights_deleted'));
232 $this->showCopyrightSelection();
233 return true;
234 }
235
236 public function updateEntry(): void
237 {
238 $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
239 $modal = $this
240 ->initCopyrightEditModal($entry, true)
241 ->withRequest($this->http->request());
242
243 if ($data = $modal->getData()) {
244 $this->deleteFileIfChanged($entry, $data);
245 $this->repository->updateEntry(
246 $entry->id(),
247 $data['title'],
248 $data['description'],
249 (bool) $data['outdated'],
250 $data['copyright']['full_name'],
251 $data['copyright']['link'],
252 $this->extractImageFromData($data),
253 $data['copyright']['alt_text']
254 );
255 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
256
257 $this->ctrl->redirect($this, 'showCopyrightSelection');
258 }
259 $this->showCopyrightSelection($entry->id(), $modal);
260 }
261
262 protected function initCopyrightEditModal(
263 ?EntryInterface $entry = null,
264 bool $open_on_load = false
265 ): RoundTrip {
266 $inputs = [];
267 $ff = $this->ui_factory->input()->field();
268
269 $title = $ff
270 ->text($this->lng->txt('title'))
271 ->withValue($entry?->title() ?? '')
272 ->withRequired(true)
273 ->withMaxLength(255);
274 $inputs['title'] = $title;
275
276 $des = $ff
277 ->textarea($this->lng->txt('description'))
278 ->withValue($entry?->description() ?? '');
279 $inputs['description'] = $des;
280
281 $usage = $ff
282 ->radio($this->lng->txt('meta_copyright_usage'))
283 ->withOption('0', $this->lng->txt('meta_copyright_in_use'))
284 ->withOption('1', $this->lng->txt('meta_copyright_outdated'))
285 ->withValue((int) $entry?->isOutdated());
286 $inputs['outdated'] = $usage;
287
288 $cp_data = $entry?->copyrightData();
289
290 $full_name = $ff
291 ->text($this->lng->txt('md_copyright_full_name'))
292 ->withValue($cp_data?->fullName() ?? '');
293
294 $link = $ff
295 ->url(
296 $this->lng->txt('md_copyright_link'),
297 $this->lng->txt('md_copyright_link_info')
298 )
299 ->withValue((string) $cp_data?->link())
300 ->withAdditionalTransformation(
301 $this->refinery->custom()->transformation(fn($v) => $v instanceof URI ? $v : null)
302 );
303
304 $image_link = $ff
305 ->url($this->lng->txt('md_copyright_image_link'))
306 ->withValue((string) $cp_data?->imageLink())
307 ->withAdditionalTransformation($this->refinery->custom()->transformation(
308 fn($v) => $v instanceof URI ? $v : null
309 ));
310
311 $file_id = empty($cp_data?->imageFile()) ? '' : $cp_data?->imageFile();
312 $image_file = $ff
313 ->file(
315 $this->lng->txt('md_copyright_image_file')
316 )
317 ->withMaxFiles(1)
318 ->withAcceptedMimeTypes([MimeType::IMAGE__PNG, MimeType::IMAGE__JPEG]);
319 if ($file_id !== '') {
320 $image_file = $image_file->withValue([$file_id]);
321 };
322
323 $image_value = 'link_group';
324 if (!is_null($cp_data) && !$cp_data->isImageLink()) {
325 $image_value = 'file_group';
326 }
327 $image = $ff
328 ->switchableGroup(
329 [
330 'link_group' => $ff->group(
331 ['image_link' => $image_link],
332 $this->lng->txt('md_copyright_image_is_link')
333 ),
334 'file_group' => $ff->group(
335 ['image_file' => $image_file],
336 $this->lng->txt('md_copyright_image_is_file')
337 ),
338 ],
339 $this->lng->txt('md_copyright_image')
340 )
341 ->withValue($image_value);
342
343 $alt_text = $ff
344 ->text(
345 $this->lng->txt('md_copyright_alt_text'),
346 $this->lng->txt('md_copyright_alt_text_info')
347 )
348 ->withValue($cp_data?->altText() ?? '');
349
350 $cop = $ff
351 ->section(
352 [
353 'full_name' => $full_name,
354 'link' => $link,
355 'image' => $image,
356 'alt_text' => $alt_text
357 ],
358 $this->lng->txt('md_copyright_value')
359 );
360 $inputs['copyright'] = $cop;
361
362 if (!isset($entry)) {
363 $title = $this->lng->txt('md_copyright_add');
364 $post_url = $this->ctrl->getLinkTarget($this, 'saveEntry');
365 } else {
366 $title = $this->lng->txt('md_copyright_edit');
367 $this->ctrl->setParameter($this, 'entry_id', $entry->id());
368 $post_url = $this->ctrl->getLinkTarget($this, 'updateEntry');
369 $this->ctrl->clearParameters($this);
370 }
371
372 $modal = $this->ui_factory->modal()->roundtrip(
373 $title,
374 null,
375 $inputs,
376 $post_url
377 );
378 if ($open_on_load) {
379 $modal = $modal->withOnLoad($modal->getShowSignal());
380 }
381 return $modal;
382 }
383
384 public function saveCopyrightPosition(): bool
385 {
386 if (!$this->http->wrapper()->post()->has('order')) {
387 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_select_one'), true);
388 $this->ctrl->redirect($this, 'showCopyrightSelection');
389 return false;
390 }
391 $positions = $this->http->wrapper()->post()->retrieve(
392 'order',
393 $this->refinery->kindlyTo()->dictOf(
394 $this->refinery->kindlyTo()->string()
395 )
396 );
397 asort($positions);
398 $ids = [];
399 foreach ($positions as $entry_id => $position_ignored) {
400 $ids[] = (int) $entry_id;
401 }
402 $this->repository->reorderEntries(...$ids);
403 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
404 $this->ctrl->redirect($this, 'showCopyrightSelection');
405 return false;
406 }
407
408 protected function deleteFile(EntryInterface $entry): void
409 {
410 if (($image_file = $entry->copyrightData()->imageFile()) === '') {
411 return;
412 }
413 if ($id = $this->irss->manage()->find($image_file)) {
414 $this->irss->manage()->remove($id, new ilMDCopyrightImageStakeholder());
415 }
416 }
417
418 protected function deleteFileIfChanged(
419 EntryInterface $entry,
420 array $data
421 ): void {
422 if (($image_file = $entry->copyrightData()->imageFile()) === '') {
423 return;
424 }
425 if (
426 $data['copyright']['image'][0] === 'file_group' &&
427 $image_file === ($data['copyright']['image'][1]['image_file'][0] ?? '')
428 ) {
429 return;
430 }
431 $this->deleteFile($entry);
432 }
433
434 protected function extractImageFromData(array $data): string|URI
435 {
436 $v = $data['copyright']['image'];
437 if ($v[0] === 'link_group') {
438 return empty($link = $v[1]['image_link']) ? '' : $link;
439 }
440 if ($v[0] === 'file_group') {
441 return $v[1]['image_file'][0] ?? '';
442 }
443 return '';
444 }
445
446 protected function initEntryIdFromQuery(): int
447 {
448 $entry_id = 0;
449 if ($this->http->wrapper()->query()->has('entry_id')) {
450 $entry_id = $this->http->wrapper()->query()->retrieve(
451 'entry_id',
452 $this->refinery->kindlyTo()->int()
453 );
454 }
455 return $entry_id;
456 }
457
458 protected function initEntryIdFromPost(): array
459 {
460 $entries = [];
461 if ($this->http->wrapper()->post()->has('entry_id')) {
462 return $this->http->wrapper()->post()->retrieve(
463 'entry_id',
464 $this->refinery->kindlyTo()->listOf(
465 $this->refinery->kindlyTo()->int()
466 )
467 );
468 }
469 return [];
470 }
471}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
renderer()
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Mime type determination.
Definition: MimeType.php:30
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilCtrl provides processing control methods.
language handling
@ilCtrl_Calls ilMDCopyrightConfigurationGUI: ilMDCopyrightUsageGUI, ilMDCopyrightImageUploadHandlerGU...
deleteFileIfChanged(EntryInterface $entry, array $data)
showCopyrightSelection(int $current_id=0, ?RoundTrip $current_modal=null)
__construct(ilObjMDSettingsGUI $parent_obj_gui)
initCopyrightEditModal(?EntryInterface $entry=null, bool $open_on_load=false)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface GlobalHttpState.
An entity that renders components to a string output.
Definition: Renderer.php:31
static http()
Fetches the global http state from ILIAS.
link(string $caption, string $href, bool $new_viewport=false)
$handler
Definition: oai.php:29
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26