ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
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 (!$this->access_service->hasCurrentUserReadAccess()) {
90 throw new ilPermissionException($this->lng->txt('no_permission'));
91 }
92
93 switch ($next_class) {
94 case strtolower(ilMDCopyrightUsageGUI::class):
95 // this command is used if copyrightUsageGUI calls getParentReturn (see ...UsageGUI->setTabs)
96 $this->ctrl->setReturn($this, 'showCopyrightSelection');
97 $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
98 $gui = new ilMDCopyrightUsageGUI($entry);
99 $this->ctrl->forwardCommand($gui);
100 break;
101
102 case strtolower(ilMDCopyrightImageUploadHandlerGUI::class):
103 $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
104 $file_id = empty($entry?->copyrightData()?->imageFile()) ? '' : $entry?->copyrightData()?->imageFile();
106 $this->ctrl->forwardCommand($handler);
107
108 // no break
109 default:
110 if (!$cmd || $cmd === 'view') {
111 $cmd = 'showCopyrightSelection';
112 }
113
114 $this->$cmd();
115 break;
116 }
117 }
118
119 public function showCopyrightSelection(
120 int $current_id = 0,
121 ?RoundTrip $current_modal = null
122 ): void {
123 $has_write = $this->access_service->hasCurrentUserWriteAccess();
124
125 $table_gui = new ilMDCopyrightTableGUI(
126 $this->repository,
127 $this->renderer,
128 $this,
129 'showCopyrightSelection',
130 $has_write
131 );
132 $table_gui->setTitle($this->lng->txt("md_copyright_selection"));
133 $table_gui->parseSelections();
134
135 $edit_modals = [];
136 if ($has_write) {
137 foreach ($this->repository->getAllEntries() as $entry) {
138 if ($entry->id() === $current_id) {
139 $modal = $current_modal;
140 } else {
141 $modal = $this->initCopyrightEditModal($entry);
142 }
143 $table_gui->setEditModalSignal($entry->id(), $modal->getShowSignal());
144 $edit_modals[] = $modal;
145 }
146 if ($current_id === 0 && !is_null($current_modal)) {
147 $add_modal = $current_modal;
148 } else {
149 $add_modal = $this->initCopyrightEditModal();
150 }
151 $this->toolbar_gui->addComponent($this->ui_factory->button()->standard(
152 $this->lng->txt('md_copyright_add'),
153 $add_modal->getShowSignal()
154 ));
155
156 $table_gui->addMultiCommand("confirmDeleteEntries", $this->lng->txt("delete"));
157 $table_gui->setSelectAllCheckbox("entry_id");
158 }
159
160 $this->tpl->setContent(
161 $table_gui->getHTML() .
162 (isset($add_modal) ? $this->ui_renderer->render($add_modal) : '') .
163 $this->ui_renderer->render($edit_modals)
164 );
165 }
166
167 public function saveEntry(): void
168 {
169 $modal = $this
170 ->initCopyrightEditModal(null, true)
171 ->withRequest($this->http->request());
172
173 if ($data = $modal->getData()) {
174 $this->repository->createEntry(
175 $data['title'],
176 $data['description'],
177 (bool) $data['outdated'],
178 $data['copyright']['full_name'],
179 $data['copyright']['link'],
180 $this->extractImageFromData($data),
181 $data['copyright']['alt_text']
182 );
183 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
184
185 $this->ctrl->redirect($this, 'showCopyrightSelection');
186 }
187 $this->showCopyrightSelection(0, $modal);
188 }
189
190 public function confirmDeleteEntries(): void
191 {
192 $entry_ids = $this->initEntryIdFromPost();
193 if (!count($entry_ids)) {
194 $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
195 $this->showCopyrightSelection();
196 return;
197 }
198
199 $c_gui = new ilConfirmationGUI();
200 // set confirm/cancel commands
201 $c_gui->setFormAction($this->ctrl->getFormAction($this, "deleteEntries"));
202 $c_gui->setHeaderText($this->lng->txt("md_delete_cp_sure"));
203 $c_gui->setCancel($this->lng->txt("cancel"), "showCopyrightSelection");
204 $c_gui->setConfirm($this->lng->txt("confirm"), "deleteEntries");
205
206 // add items to delete
207 foreach ($entry_ids as $entry_id) {
208 $entry = $this->repository->getEntry($entry_id);
209 $c_gui->addItem('entry_id[]', (string) $entry_id, $entry->title());
210 }
211 $this->tpl->setContent($c_gui->getHTML());
212 }
213
214 public function deleteEntries(): bool
215 {
216 $entry_ids = $this->initEntryIdFromPost();
217 if (!count($entry_ids)) {
218 $this->tpl->setOnScreenMessage('info', $this->lng->txt('select_one'));
219 $this->showCopyrightSelection();
220 return true;
221 }
222
223 foreach ($entry_ids as $entry_id) {
224 $entry = $this->repository->getEntry($entry_id);
225 $this->deleteFile($entry);
226 $this->repository->deleteEntry($entry_id);
227 }
228 $this->tpl->setOnScreenMessage('success', $this->lng->txt('md_copyrights_deleted'));
229 $this->showCopyrightSelection();
230 return true;
231 }
232
233 public function updateEntry(): void
234 {
235 $entry = $this->repository->getEntry($this->initEntryIdFromQuery());
236 $modal = $this
237 ->initCopyrightEditModal($entry, true)
238 ->withRequest($this->http->request());
239
240 if ($data = $modal->getData()) {
241 $this->deleteFileIfChanged($entry, $data);
242 $this->repository->updateEntry(
243 $entry->id(),
244 $data['title'],
245 $data['description'],
246 (bool) $data['outdated'],
247 $data['copyright']['full_name'],
248 $data['copyright']['link'],
249 $this->extractImageFromData($data),
250 $data['copyright']['alt_text']
251 );
252 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'), true);
253
254 $this->ctrl->redirect($this, 'showCopyrightSelection');
255 }
256 $this->showCopyrightSelection($entry->id(), $modal);
257 }
258
259 protected function initCopyrightEditModal(
260 ?EntryInterface $entry = null,
261 bool $open_on_load = false
262 ): RoundTrip {
263 $inputs = [];
264 $ff = $this->ui_factory->input()->field();
265
266 $title = $ff
267 ->text($this->lng->txt('title'))
268 ->withValue($entry?->title() ?? '')
269 ->withRequired(true)
270 ->withMaxLength(255);
271 $inputs['title'] = $title;
272
273 $des = $ff
274 ->textarea($this->lng->txt('description'))
275 ->withValue($entry?->description() ?? '');
276 $inputs['description'] = $des;
277
278 $usage = $ff
279 ->radio($this->lng->txt('meta_copyright_usage'))
280 ->withOption('0', $this->lng->txt('meta_copyright_in_use'))
281 ->withOption('1', $this->lng->txt('meta_copyright_outdated'))
282 ->withValue((int) $entry?->isOutdated());
283 $inputs['outdated'] = $usage;
284
285 $cp_data = $entry?->copyrightData();
286
287 $full_name = $ff
288 ->text($this->lng->txt('md_copyright_full_name'))
289 ->withValue($cp_data?->fullName() ?? '');
290
291 $link = $ff
292 ->url(
293 $this->lng->txt('md_copyright_link'),
294 $this->lng->txt('md_copyright_link_info')
295 )
296 ->withValue((string) $cp_data?->link())
297 ->withAdditionalTransformation(
298 $this->refinery->custom()->transformation(fn($v) => $v instanceof URI ? $v : null)
299 );
300
301 $image_link = $ff
302 ->url($this->lng->txt('md_copyright_image_link'))
303 ->withValue((string) $cp_data?->imageLink())
304 ->withAdditionalTransformation($this->refinery->custom()->transformation(
305 fn($v) => $v instanceof URI ? $v : null
306 ));
307
308 $file_id = empty($cp_data?->imageFile()) ? '' : $cp_data?->imageFile();
309 $image_file = $ff
310 ->file(
312 $this->lng->txt('md_copyright_image_file')
313 )
314 ->withMaxFiles(1)
315 ->withAcceptedMimeTypes([MimeType::IMAGE__PNG, MimeType::IMAGE__JPEG]);
316 if ($file_id !== '') {
317 $image_file = $image_file->withValue([$file_id]);
318 };
319
320 $image_value = 'link_group';
321 if (!is_null($cp_data) && !$cp_data->isImageLink()) {
322 $image_value = 'file_group';
323 }
324 $image = $ff
325 ->switchableGroup(
326 [
327 'link_group' => $ff->group(
328 ['image_link' => $image_link],
329 $this->lng->txt('md_copyright_image_is_link')
330 ),
331 'file_group' => $ff->group(
332 ['image_file' => $image_file],
333 $this->lng->txt('md_copyright_image_is_file')
334 ),
335 ],
336 $this->lng->txt('md_copyright_image')
337 )
338 ->withValue($image_value);
339
340 $alt_text = $ff
341 ->text(
342 $this->lng->txt('md_copyright_alt_text'),
343 $this->lng->txt('md_copyright_alt_text_info')
344 )
345 ->withValue($cp_data?->altText() ?? '');
346
347 $cop = $ff
348 ->section(
349 [
350 'full_name' => $full_name,
351 'link' => $link,
352 'image' => $image,
353 'alt_text' => $alt_text
354 ],
355 $this->lng->txt('md_copyright_value')
356 );
357 $inputs['copyright'] = $cop;
358
359 if (!isset($entry)) {
360 $title = $this->lng->txt('md_copyright_add');
361 $post_url = $this->ctrl->getLinkTarget($this, 'saveEntry');
362 } else {
363 $title = $this->lng->txt('md_copyright_edit');
364 $this->ctrl->setParameter($this, 'entry_id', $entry->id());
365 $post_url = $this->ctrl->getLinkTarget($this, 'updateEntry');
366 $this->ctrl->clearParameters($this);
367 }
368
369 $modal = $this->ui_factory->modal()->roundtrip(
370 $title,
371 null,
372 $inputs,
373 $post_url
374 );
375 if ($open_on_load) {
376 $modal = $modal->withOnLoad($modal->getShowSignal());
377 }
378 return $modal;
379 }
380
381 public function saveCopyrightPosition(): bool
382 {
383 if (!$this->http->wrapper()->post()->has('order')) {
384 $this->tpl->setOnScreenMessage('failure', $this->lng->txt('err_select_one'), true);
385 $this->ctrl->redirect($this, 'showCopyrightSelection');
386 return false;
387 }
388 $positions = $this->http->wrapper()->post()->retrieve(
389 'order',
390 $this->refinery->kindlyTo()->dictOf(
391 $this->refinery->kindlyTo()->string()
392 )
393 );
394 asort($positions);
395 $ids = [];
396 foreach ($positions as $entry_id => $position_ignored) {
397 $ids[] = (int) $entry_id;
398 }
399 $this->repository->reorderEntries(...$ids);
400 $this->tpl->setOnScreenMessage('success', $this->lng->txt('settings_saved'));
401 $this->ctrl->redirect($this, 'showCopyrightSelection');
402 return false;
403 }
404
405 protected function deleteFile(EntryInterface $entry): void
406 {
407 if (($image_file = $entry->copyrightData()->imageFile()) === '') {
408 return;
409 }
410 if ($id = $this->irss->manage()->find($image_file)) {
411 $this->irss->manage()->remove($id, new ilMDCopyrightImageStakeholder());
412 }
413 }
414
415 protected function deleteFileIfChanged(
416 EntryInterface $entry,
417 array $data
418 ): void {
419 if (($image_file = $entry->copyrightData()->imageFile()) === '') {
420 return;
421 }
422 if (
423 $data['copyright']['image'][0] === 'file_group' &&
424 $image_file === ($data['copyright']['image'][1]['image_file'][0] ?? '')
425 ) {
426 return;
427 }
428 $this->deleteFile($entry);
429 }
430
431 protected function extractImageFromData(array $data): string|URI
432 {
433 $v = $data['copyright']['image'];
434 if ($v[0] === 'link_group') {
435 return empty($link = $v[1]['image_link']) ? '' : $link;
436 }
437 if ($v[0] === 'file_group') {
438 return $v[1]['image_file'][0] ?? '';
439 }
440 return '';
441 }
442
443 protected function initEntryIdFromQuery(): int
444 {
445 $entry_id = 0;
446 if ($this->http->wrapper()->query()->has('entry_id')) {
447 $entry_id = $this->http->wrapper()->query()->retrieve(
448 'entry_id',
449 $this->refinery->kindlyTo()->int()
450 );
451 }
452 return $entry_id;
453 }
454
455 protected function initEntryIdFromPost(): array
456 {
457 $entries = [];
458 if ($this->http->wrapper()->post()->has('entry_id')) {
459 return $this->http->wrapper()->post()->retrieve(
460 'entry_id',
461 $this->refinery->kindlyTo()->listOf(
462 $this->refinery->kindlyTo()->int()
463 )
464 );
465 }
466 return [];
467 }
468}
$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:31
if(!file_exists('../ilias.ini.php'))
global $DIC
Definition: shib_login.php:26