ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFooterGroupsGUI.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;
35use ILIAS\GlobalScreen\GUI\Hasher;
39
48{
49 use Hasher;
50
54 public const CMD_TOGGLE_ACTIVATION = 'toggleActivation';
55
59 public const CMD_ADD = 'add';
60
64 public const CMD_CREATE = 'create';
65
69 public const CMD_EDIT = 'edit';
70
74 public const CMD_UPDATE = 'update';
75
79 public const CMD_RESET = 'reset';
80
84 public const CMD_SAVE_ORDER = 'saveOrder';
85
89 public const GSFO_ID = 'gsfo_group_id';
90
94 public const CMD_CONFIRM_RESET = 'confirmReset';
98 public const CMD_CONFIRM_DELETE = 'confirmDelete';
102 public const CMD_DELETE = 'delete';
106 private ServerRequestInterface $request;
107
110 private Translator $translator;
111
112 public function __construct(
113 Pons $pons,
114 ) {
115 parent::__construct($pons);
116 global $DIC;
117 $this->dic = $DIC;
118 $this->translator = $pons->i18n();
119 $this->ui_factory = $this->dic->ui()->factory();
120 $this->repository = new GroupsRepositoryDB(
121 $this->dic->database(),
122 new ilFooterCustomGroupsProvider($this->dic)
123 );
124 $this->group_token = $this->pons->in()->buildToken('group', 'id');
125 }
126
127 public function getTokensToKeep(): array
128 {
129 return [
130 $this->group_token->token(),
131 ];
132 }
133
135 {
136 return $this->repository->get(
137 $this->pons->in()->getFirstFromRequest($this->group_token)
138 );
139 }
140
141 private function addButtons(): array
142 {
143 $modal = $this->ui_factory
144 ->modal()
145 ->roundtrip(
146 $this->translator->translate('group_add'),
147 null
148 )
149 ->withAsyncRenderUrl(
150 (string) $this->pons->flow()->getHereAsURI(self::CMD_ADD)
151 );
152
153 $confirm_reset = $this->ui_factory
154 ->prompt()
155 ->standard(
156 $this->pons->flow()->getHereAsURI(self::CMD_CONFIRM_RESET),
157 );
158
159 $this->dic
160 ->toolbar()
161 ->addComponent(
162 $this->ui_factory
163 ->button()
164 ->primary(
165 $this->translator->translate('group_add'),
166 '#'
167 )
168 ->withOnClick($modal->getShowSignal())
169 ->withHelpTopics(
170 ...$this->ui_factory->helpTopics('gsfo_button_add')
171 )
172 );
173 $this->dic
174 ->toolbar()
175 ->addComponent(
176 $this->ui_factory
177 ->button()
178 ->standard(
179 $this->translator->translate('reset_footer'),
180 '#'
181 )
182 ->withOnClick($confirm_reset->getShowSignal())
183 ->withHelpTopics(
184 ...$this->ui_factory->helpTopics('gsfo_button_reset')
185 )
186 );
187
188 return [$modal, $confirm_reset];
189 }
190
191 #[Command('read')]
192 private function index(): void
193 {
194 // Add new
195 $components = [];
196 if ($this->pons->access()->hasUserPermissionTo('write')) {
197 $components = $this->addButtons();
198 }
199 // Sync
200 $this->repository->syncWithGlobalScreen(
201 $this->dic->globalScreen()->collector()->footer()
202 );
203
204 // Table
205 $table = new GroupsTable(
206 $this->pons,
207 $this->repository,
208 $this->group_token
209 );
210
211 $this->pons->out()->out(
212 $table->get(),
213 ...$components
214 );
215 }
216
217 #[Command('write')]
218 private function confirmReset(): void
219 {
220 $this->pons->out()->outAsync(
221 $this->ui_factory->prompt()->state()->show(
222 $this->ui_factory->messageBox()->confirmation(
223 $this->translator->translate('confirm_reset')
224 )->withButtons(
225 [
226 $this->ui_factory->button()->standard(
227 $this->translator->translate('reset'),
228 (string) $this->pons->flow()->getHereAsURI(self::CMD_RESET)
229 )
230 ]
231 )
232 )
233 );
234 }
235
236 #[Command('write')]
237 private function toggleActivation(): void
238 {
239 $from_request = $this->pons->in()->getAllFromRequest($this->group_token);
240
241 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
242 $from_request = array_map(
243 fn(TranslatableItem $item): string => $item->getId(),
244 iterator_to_array($this->repository->all())
245 );
246 }
247
248 foreach ($from_request as $id) {
249 $group = $this->repository->get($id);
250 if ($group === null) {
251 continue;
252 }
253 $this->repository->store($group->withActive(!$group->isActive()));
254 }
255
256 $this->pons->out()->success($this->translator->translate('group_activation_toggled'));
257 $this->pons->flow()->redirect(self::CMD_DEFAULT);
258 }
259
260 #[Command('write')]
261 private function confirmDelete(): void
262 {
263 $items = [];
264
265 $nok = $this->pons->out()->nok();
266 $ok = $this->pons->out()->ok();
267
268 $from_request = $this->pons->in()->getAllFromRequest($this->group_token);
269
270 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
271 $from_request = array_map(
272 fn(TranslatableItem $item): string => $item->getId(),
273 iterator_to_array($this->repository->all())
274 );
275 }
276
277 foreach ($from_request as $id) {
278 $group = $this->repository->get($id);
279 $id = $this->hash($id);
280 if ($group === null) {
281 continue;
282 }
283 if ($group->isCore()) {
284 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
285 $id,
286 $group->getTitle(),
287 $this->translator->translate('info_not_deletable_core') .
288 $this->pons->out()->ui()->renderer()->render(
289 $nok
290 )
291 );
292 continue;
293 }
294 if ($group->getItems() > 0) {
295 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
296 $id,
297 $group->getTitle(),
298 $this->translator->translate('info_not_deletable_not_empty') .
299 $this->pons->out()->ui()->renderer()->render($nok)
300 );
301 continue;
302 }
303
304 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
305 $id,
306 $group->getTitle(),
307 $this->pons->out()->ui()->renderer()->render($ok)
308 );
309 }
310
311 $this->pons->out()->outAsyncAsModal(
312 $this->pons->i18n()->translate('group_delete'),
313 (string) $this->pons->flow()->getHereAsURI(self::CMD_DELETE),
314 ...$items
315 );
316 }
317
318 #[Command('write')]
319 private function delete(): void
320 {
321 $from_request = $this->pons->in()->getAllFromRequest($this->group_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 $group = $this->repository->get($id);
332 if ($group === null) {
333 continue;
334 }
335 if ($group->isCore()) {
336 continue;
337 }
338 $this->repository->delete($group);
339 }
340
341 $this->pons->out()->success($this->translator->translate('group_deleted'));
342 $this->pons->flow()->redirect(self::CMD_DEFAULT);
343 }
344
345 #[Command('write')]
346 private function add(): void
347 {
348 $form = new GroupForm(
349 $this->repository,
350 $this->translator
351 );
352
353 $action = (string) $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
354
355 $this->pons->out()->outAsyncAsModal(
356 $this->translator->translate('group_add'),
357 $action,
358 $form->get($action)
359 );
360 }
361
362 #[Command('write')]
363 public function create(): void
364 {
365 $form = new GroupForm(
366 $this->repository,
367 $this->translator
368 );
369 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
370 if ($form->store(
371 $this->pons->in()->request(),
372 $target
373 )) {
374 $this->pons->flow()->redirect(self::CMD_DEFAULT);
375 }
376 $this->pons->out()->out(
377 $form->get(
378 $target
379 )
380 );
381 }
382
383 #[Command('write')]
384 private function edit(): void
385 {
386 $id = $this->pons->in()->getFirstFromRequest($this->group_token);
387 $this->pons->in()->keepTokens($this);
388
389 $group = $this->repository->get($id);
390
391 $form = new GroupForm(
392 $this->repository,
393 $this->translator,
394 $group
395 );
396
397 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_UPDATE);
398 $this->pons->out()->outAsyncAsModal(
399 $this->translator->translate('group_edit'),
400 $target,
401 $form->get($target)
402 );
403 }
404
405 #[Command('write')]
406 public function update(): void
407 {
408 $id = $this->pons->in()->getFirstFromRequest($this->group_token);
409
410 $group = $this->repository->get($id);
411
412 $form = new GroupForm(
413 $this->repository,
414 $this->translator,
415 $group
416 );
417
418 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_EDIT);
419 if ($form->store(
420 $this->pons->in()->request(),
421 $target
422 )) {
423 $this->pons->flow()->redirect(self::CMD_DEFAULT);
424 }
425 $this->pons->out()->out(
426 $form->get(
427 $target
428 )
429 );
430 }
431
432 #[Command('write')]
433 private function reset(): never
434 {
435 $this->repository->reset(
436 $this->dic->globalScreen()->collector()->footer()
437 );
438 $entries_repo = new EntriesRepositoryDB($this->dic->database(), new ilFooterCustomGroupsProvider($this->dic));
439 $entries_repo->reset($this->dic->globalScreen()->collector()->footer());
440
441 $translations = new TranslationsRepositoryDB($this->dic->database());
442 $translations->reset();
443
444 $this->pons->out()->success($this->translator->translate('reset_success'));
445 $this->pons->flow()->redirect(self::CMD_DEFAULT);
446 }
447
448 #[Command('write')]
449 private function saveOrder(): void
450 {
451 foreach ($this->pons->in()->request()->getParsedBody() as $hashed_id => $position) {
452 $item = $this->repository->get($this->unhash($hashed_id));
453 $item = $item->withPosition((int) $position);
454 $this->repository->store($item);
455 }
456 $this->pons->out()->success($this->pons->i18n()->translate('order_saved'));
457 $this->pons->flow()->redirect(self::CMD_DEFAULT);
458 }
459
460 public function executeCommand(): bool
461 {
462 if ($this->pons->handle(ilObjFooterAdministrationGUI::TAB_GROUPS)) {
463 return true;
464 }
465 match ($this->pons->flow()->getCommand(self::CMD_DEFAULT)) {
466 self::CMD_DEFAULT => $this->index(),
467 self::CMD_ADD => $this->add(),
468 self::CMD_CREATE => $this->create(),
469 self::CMD_EDIT => $this->edit(),
470 self::CMD_UPDATE => $this->update(),
471 self::CMD_CONFIRM_RESET => $this->confirmReset(),
472 self::CMD_CONFIRM_DELETE => $this->confirmDelete(),
473 self::CMD_DELETE => $this->delete(),
474 self::CMD_TOGGLE_ACTIVATION => $this->toggleActivation(),
475 self::CMD_RESET => $this->reset(),
476 self::CMD_SAVE_ORDER => $this->saveOrder(),
477 default => $this->pons->out()->outString(
478 'Unknown command: ' . $this->pons->flow()->getCommand(self::CMD_DEFAULT)
479 ),
480 };
481 return true;
482 }
483
484}
$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
GroupsRepository $repository
ServerRequestInterface $request
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