ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilFooterEntriesGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\GlobalScreen\GUI\I18n\Translator;
25use Psr\Http\Message\ServerRequestInterface;
32use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
42
50{
51 use Hasher;
52
56 public const CMD_DEFAULT = 'index';
60 public const CMD_ADD = 'add';
64 public const CMD_CREATE = 'create';
68 public const CMD_EDIT = 'edit';
72 public const CMD_UPDATE = 'update';
76 public const CMD_RESET = 'reset';
80 public const CMD_SAVE_ORDER = 'saveOrder';
84 public const CMD_CONFIRM_DELETE = 'confirmDelete';
88 public const CMD_DELETE = 'delete';
92 public const GSFO_ID = 'gsfo_entry_id';
96 public const CMD_TOGGLE_ACTIVATION = 'toggleActivation';
97 public const string CMD_SELECT_MOVE = 'selectMove';
98 public const string CMD_MOVE = 'move';
102 private ServerRequestInterface $request;
103 private Translator $translator;
104
108 private ?Group $group;
110
111 public function __construct(
112 Pons $pons,
113 ) {
114 parent::__construct($pons);
115 global $DIC;
116 $this->translator = $pons->i18n();
117 $this->dic = $DIC;
118 $this->ui_factory = $this->dic->ui()->factory();
119
120 $this->groups_repository = new GroupsRepositoryDB(
121 $this->dic->database(),
123 );
124
125 $this->repository = new EntriesRepositoryDB(
126 $this->dic->database(),
128 );
129 $this->group_token = $this->pons->in()->buildToken('group', 'id');
130 $this->entry_token = $this->pons->in()->buildToken('entry', 'id');
131 $this->group = $this->groups_repository->get(
132 $this->pons->in()->getFirstFromRequest($this->group_token)
133 );
134 $this->pons->in()->keep($this->group_token);
135 }
136
137 public function getTokensToKeep(): array
138 {
139 return [
140 $this->group_token->token(),
141 $this->entry_token->token(),
142 ];
143 }
144
146 {
147 return $this->repository->get(
148 $this->pons->in()->getFirstFromRequest($this->entry_token->token())
149 );
150 }
151
152 #[Command('read')]
153 private function index(): void
154 {
155 // Add new
156 $components = [];
157 if ($this->pons->access()->hasUserPermissionTo('write')) {
158 $components = $this->addButtons();
159 }
160 // Sync
161 $this->repository->syncWithGlobalScreen(
162 $this->dic->globalScreen()->collector()->footer()
163 );
164 // Table
165 $table = new EntriesTable(
166 $this->pons,
167 $this->group,
168 $this->repository,
169 $this->entry_token
170 );
171
172 $this->pons->out()->out(
173 $table->get(),
174 ...$components
175 );
176 }
177
178 #[Command('write')]
179 private function add(): void
180 {
181 $form = new EntryForm(
182 $this->repository,
183 $this->translator,
184 $this->group
185 );
186
187 $target = $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
188 $this->pons->out()->outAsyncAsModal(
189 $this->translator->translate('add', 'entries'),
190 $target,
191 $form->get((string) $target)
192 );
193 }
194
195 private function addButtons(): array
196 {
197 $modal = $this->ui_factory->modal()->roundtrip(
198 $this->translator->translate('add', 'entries'),
199 null
200 )->withAsyncRenderUrl(
201 (string) $this->pons->flow()->getHereAsURI(self::CMD_ADD)
202 );
203
204 $this->dic->toolbar()->addComponent(
205 $this->ui_factory
206 ->button()
207 ->primary(
208 $this->translator->translate('add', 'entries'),
209 '#'
210 )
211 ->withOnClick($modal->getShowSignal())
212 );
213
214 return [$modal];
215 }
216
217 private function saveCurrentEntry(): mixed
218 {
219 $id = $this->pons->in()->getFirstFromRequest($this->entry_token);
220 $this->pons->in()->keep($this->entry_token);
221
222 return $id;
223 }
224
225 #[Command('write')]
226 public function create(): void
227 {
228 $form = new EntryForm(
229 $this->repository,
230 $this->translator,
231 $this->group
232 );
233 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
234 if ($form->store(
235 $this->pons->in()->request(),
236 $target
237 )) {
238 $this->pons->flow()->redirect(self::CMD_DEFAULT);
239 }
240 $this->pons->out()->out(
241 $form->get(
242 $target
243 )
244 );
245 }
246
247 #[Command('write')]
248 private function edit(): void
249 {
250 $id = $this->saveCurrentEntry();
251 $entry = $this->repository->get($id);
252
253 $form = new EntryForm(
254 $this->repository,
255 $this->translator,
256 $this->group,
257 $entry
258 );
259
260 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_UPDATE);
261 $this->pons->out()->outAsyncAsModal(
262 $this->translator->translate('edit', 'entries'),
263 $target,
264 $form->get($target)
265 );
266 }
267
268 #[Command('write')]
269 public function update(): void
270 {
271 $id = $this->pons->in()->getFirstFromRequest($this->entry_token);
272 $entry = $this->repository->get($id);
273
274 $form = new EntryForm(
275 $this->repository,
276 $this->translator,
277 $this->group,
278 $entry
279 );
280 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
281 if ($form->store(
282 $this->pons->in()->request(),
283 $target
284 )) {
285 $this->pons->flow()->redirect(self::CMD_DEFAULT);
286 }
287 $this->pons->out()->out(
288 $form->get($target)
289 );
290 }
291
292 #[Command('write')]
293 private function toggleActivation(): void
294 {
295 $from_request = $this->pons->in()->getAllFromRequest($this->entry_token);
296
297 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
298 $from_request = array_map(
299 fn(TranslatableItem $item): string => $item->getId(),
300 iterator_to_array($this->repository->all())
301 );
302 }
303
304 foreach ($from_request as $id) {
305 $entry = $this->repository->get($id);
306 if ($entry === null) {
307 continue;
308 }
309 $this->repository->store($entry->withActive(!$entry->isActive()));
310 }
311
312 $this->pons->out()->success($this->translator->translate('group_activation_toggled'), true);
313 $this->pons->flow()->redirect(self::CMD_DEFAULT);
314 }
315
316 #[Command('write')]
317 private function confirmDelete(): void
318 {
319 $items = [];
320
321 $from_request = $this->pons->in()->getAllFromRequest($this->entry_token);
322
323 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
324 $from_request = array_map(
325 fn(TranslatableItem $item): string => $item->getId(),
326 iterator_to_array($this->repository->all())
327 );
328 }
329
330 foreach ($from_request as $id) {
331 $entry = $this->repository->get($id);
332 if ($entry === null) {
333 continue;
334 }
335 if ($entry->isCore()) {
336 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
337 $id,
338 $entry->getTitle(),
339 $this->translator->translate('info_not_deletable_core') .
340 $this->pons->out()->render($this->pons->out()->nok())
341 );
342 continue;
343 }
344 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
345 $this->hash($entry->getId()),
346 $entry->getTitle(),
347 $this->pons->out()->render($this->pons->out()->ok())
348 );
349 }
350
351 $this->pons->out()->outAsyncAsModal(
352 $this->translator->translate('entry_delete'),
353 $this->pons->flow()->getHereAsURI(self::CMD_DELETE),
354 ...$items
355 );
356 }
357
358 #[Command('write')]
359 private function delete(): void
360 {
361 $from_request = $this->pons->in()->getAllFromRequest($this->entry_token);
362
363 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
364 $from_request = array_map(
365 fn(TranslatableItem $item): string => $item->getId(),
366 iterator_to_array($this->repository->all())
367 );
368 }
369
370 $successful_deletions = 0;
371 foreach ($from_request as $id) {
372 $item = $this->repository->get($id);
373 if ($item === null) {
374 continue;
375 }
376 if ($item->isCore()) {
377 continue;
378 }
379 $this->repository->delete($item);
380 $successful_deletions++;
381 }
382
383 if ($successful_deletions === 0) {
384 $this->pons->out()->error($this->translator->translate('entry_deleted_failed'), true);
385 $this->pons->flow()->redirect(self::CMD_DEFAULT);
386 return;
387 }
388 $this->pons->out()->success($this->translator->translate('entry_deleted'), true);
389 $this->pons->flow()->redirect(self::CMD_DEFAULT);
390 }
391
392 #[Command('write')]
393 private function saveOrder(): void
394 {
395 foreach ($this->pons->in()->request()->getParsedBody() as $hashed_id => $position) {
396 $item = $this->repository->get($this->unhash($hashed_id));
397 $item = $item->withPosition((int) $position);
398 $this->repository->store($item);
399 }
400 $this->pons->out()->success($this->pons->i18n()->translate('order_saved'));
401 $this->pons->flow()->redirect(self::CMD_DEFAULT);
402 }
403
404 private function buildTargetSelectorForm(URI $post_url): Standard
405 {
406 // determine parent group
407 $all_groups = $this->groups_repository->all();
408
409 $selection = [];
410 foreach ($all_groups as $group) {
411 if ($this->group !== null && $group->getId() === $this->group->getId()) {
412 continue;
413 }
414 $selection[$this->hash($group->getId())] = $group->getTitle();
415 }
416
417 return $this->pons->out()->ui()->factory()->input()->container()->form()->standard(
418 (string) $post_url,
419 [
420 $this->pons->out()->ui()->factory()->input()->field()->select(
421 $this->pons->i18n()->t('target_group', 'entries'),
422 $selection
423 )->withRequired(true)
424 ]
425 );
426 }
427
428 #[Command('write')]
429 private function selectMove(): void
430 {
431 $this->pons->in()->keepTokens($this);
432
433 $post_url = $this->pons->flow()->getHereAsURI(self::CMD_MOVE);
434 $this->pons->out()->outAsyncAsModal(
435 $this->pons->i18n()->t(self::CMD_MOVE),
436 $post_url,
437 $this->buildTargetSelectorForm($post_url)
438 );
439 }
440
441 #[Command('write')]
442 private function move(): void
443 {
444 $form = $this->buildTargetSelectorForm($this->pons->flow()->getHereAsURI(self::CMD_MOVE))->withRequest(
445 $this->pons->in()->request()
446 );
447
448 if (($data = $form->getData()) === null) {
449 $this->pons->out()->error($this->pons->i18n()->t('no_parent_selected'), true);
450 $this->pons->flow()->redirect(self::CMD_DEFAULT);
451 return;
452 }
453
454 try {
455 $parent_group = $this->unhash($data[0] ?? '');
456 } catch (Throwable $e) {
457 $this->pons->out()->error($this->pons->i18n()->t('no_parent_selected'), true);
458 $this->pons->flow()->redirect(self::CMD_DEFAULT);
459 return;
460 }
461
465 $selected_item = $this->getCurrentItem();
466 $this->repository->store($selected_item->withParent($parent_group));
467 $this->pons->out()->success($this->pons->i18n()->t('item_moved'), true);
468 $this->pons->flow()->redirect(self::CMD_DEFAULT);
469 }
470
471 public function executeCommand(): bool
472 {
473 if ($this->pons->handle(ilObjFooterAdministrationGUI::TAB_SUB_ITEMS)) {
474 return true;
475 }
476 match ($this->pons->flow()->getCommand(self::CMD_DEFAULT)) {
477 self::CMD_DEFAULT => $this->index(),
478 self::CMD_ADD => $this->add(),
479 self::CMD_CREATE => $this->create(),
480 self::CMD_EDIT => $this->edit(),
481 self::CMD_UPDATE => $this->update(),
482 self::CMD_CONFIRM_DELETE => $this->confirmDelete(),
483 self::CMD_DELETE => $this->delete(),
484 self::CMD_TOGGLE_ACTIVATION => $this->toggleActivation(),
485 self::CMD_SAVE_ORDER => $this->saveOrder(),
486 self::CMD_SELECT_MOVE => $this->selectMove(),
487 self::CMD_MOVE => $this->move(),
488 default => $this->pons->out()->outString(
489 'Unknown command: ' . $this->pons->flow()->getCommand(self::CMD_DEFAULT)
490 ),
491 };
492 return true;
493 }
494
495}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$components
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:36
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
ServerRequestInterface $request
GroupsRepositoryDB $groups_repository
buildTargetSelectorForm(URI $post_url)
EntriesRepository $repository
This describes a standard form.
Definition: Standard.php:30
This is how the factory for UI elements looks.
Definition: Factory.php:38
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
button(string $caption, string $cmd)
global $DIC
Definition: shib_login.php:26