ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilFooterEntriesGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use ILIAS\GlobalScreen\GUI\I18n\Translator;
24use Psr\Http\Message\ServerRequestInterface;
31use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
39
47{
48 use Hasher;
49
53 public const CMD_DEFAULT = 'index';
57 public const CMD_ADD = 'add';
61 public const CMD_CREATE = 'create';
65 public const CMD_EDIT = 'edit';
69 public const CMD_UPDATE = 'update';
73 public const CMD_RESET = 'reset';
77 public const CMD_SAVE_ORDER = 'saveOrder';
81 public const CMD_CONFIRM_DELETE = 'confirmDelete';
85 public const CMD_DELETE = 'delete';
89 public const GSFO_ID = 'gsfo_entry_id';
93 public const CMD_TOGGLE_ACTIVATION = 'toggleActivation';
97 private ServerRequestInterface $request;
98 private Translator $translator;
99
103 private ?Group $group;
104
105 public function __construct(
106 Pons $pons,
107 ) {
108 parent::__construct($pons);
109 global $DIC;
110 $this->translator = $pons->i18n();
111 $this->dic = $DIC;
112 $this->ui_factory = $this->dic->ui()->factory();
113
114 $group_repository = new GroupsRepositoryDB(
115 $this->dic->database(),
117 );
118
119 $this->repository = new EntriesRepositoryDB(
120 $this->dic->database(),
122 );
123 $this->group_token = $this->pons->in()->buildToken('group', 'id');
124 $this->entry_token = $this->pons->in()->buildToken('entry', 'id');
125 $this->group = $group_repository->get(
126 $this->pons->in()->getFirstFromRequest($this->group_token)
127 );
128 $this->pons->in()->keep($this->group_token);
129 }
130
131 public function getTokensToKeep(): array
132 {
133 return [
134 $this->group_token->token(),
135 $this->entry_token->token(),
136 ];
137 }
138
140 {
141 return $this->repository->get(
142 $this->pons->in()->getFirstFromRequest($this->entry_token->token())
143 );
144 }
145
146 #[Command('read')]
147 private function index(): void
148 {
149 // Add new
150 $components = [];
151 if ($this->pons->access()->hasUserPermissionTo('write')) {
152 $components = $this->addButtons();
153 }
154 // Sync
155 $this->repository->syncWithGlobalScreen(
156 $this->dic->globalScreen()->collector()->footer()
157 );
158 // Table
159 $table = new EntriesTable(
160 $this->pons,
161 $this->group,
162 $this->repository,
163 $this->entry_token
164 );
165
166 $this->pons->out()->out(
167 $table->get(),
168 ...$components
169 );
170 }
171
172 #[Command('write')]
173 private function add(): void
174 {
175 $form = new EntryForm(
176 $this->repository,
177 $this->translator,
178 $this->group
179 );
180
181 $target = $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
182 $this->pons->out()->outAsyncAsModal(
183 $this->translator->translate('add', 'entries'),
184 $target,
185 $form->get((string) $target)
186 );
187 }
188
189 private function addButtons(): array
190 {
191 $modal = $this->ui_factory->modal()->roundtrip(
192 $this->translator->translate('add', 'entries'),
193 null
194 )->withAsyncRenderUrl(
195 (string) $this->pons->flow()->getHereAsURI(self::CMD_ADD)
196 );
197
198 $this->dic->toolbar()->addComponent(
199 $this->ui_factory
200 ->button()
201 ->primary(
202 $this->translator->translate('add', 'entries'),
203 '#'
204 )
205 ->withOnClick($modal->getShowSignal())
206 );
207
208 return [$modal];
209 }
210
211 private function saveCurrentEntry(): mixed
212 {
213 $id = $this->pons->in()->getFirstFromRequest($this->entry_token);
214 $this->pons->in()->keep($this->entry_token);
215
216 return $id;
217 }
218
219 #[Command('write')]
220 public function create(): void
221 {
222 $form = new EntryForm(
223 $this->repository,
224 $this->translator,
225 $this->group
226 );
227 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
228 if ($form->store(
229 $this->pons->in()->request(),
230 $target
231 )) {
232 $this->pons->flow()->redirect(self::CMD_DEFAULT);
233 }
234 $this->pons->out()->out(
235 $form->get(
236 $target
237 )
238 );
239 }
240
241 #[Command('write')]
242 private function edit(): void
243 {
244 $id = $this->saveCurrentEntry();
245 $entry = $this->repository->get($id);
246
247 $form = new EntryForm(
248 $this->repository,
249 $this->translator,
250 $this->group,
251 $entry
252 );
253
254 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_UPDATE);
255 $this->pons->out()->outAsyncAsModal(
256 $this->translator->translate('edit', 'entries'),
257 $target,
258 $form->get($target)
259 );
260 }
261
262 #[Command('write')]
263 public function update(): void
264 {
265 $id = $this->pons->in()->getFirstFromRequest($this->entry_token);
266 $entry = $this->repository->get($id);
267
268 $form = new EntryForm(
269 $this->repository,
270 $this->translator,
271 $this->group,
272 $entry
273 );
274 $target = (string) $this->pons->flow()->getHereAsURI(self::CMD_CREATE);
275 if ($form->store(
276 $this->pons->in()->request(),
277 $target
278 )) {
279 $this->pons->flow()->redirect(self::CMD_DEFAULT);
280 }
281 $this->pons->out()->out(
282 $form->get($target)
283 );
284 }
285
286 #[Command('write')]
287 private function toggleActivation(): void
288 {
289 $from_request = $this->pons->in()->getAllFromRequest($this->entry_token);
290
291 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
292 $from_request = array_map(
293 fn(TranslatableItem $item): string => $item->getId(),
294 iterator_to_array($this->repository->all())
295 );
296 }
297
298 foreach ($from_request as $id) {
299 $entry = $this->repository->get($id);
300 if ($entry === null) {
301 continue;
302 }
303 $this->repository->store($entry->withActive(!$entry->isActive()));
304 }
305
306 $this->pons->out()->success($this->translator->translate('group_activation_toggled'), true);
307 $this->pons->flow()->redirect(self::CMD_DEFAULT);
308 }
309
310 #[Command('write')]
311 private function confirmDelete(): void
312 {
313 $items = [];
314
315 $from_request = $this->pons->in()->getAllFromRequest($this->entry_token);
316
317 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
318 $from_request = array_map(
319 fn(TranslatableItem $item): string => $item->getId(),
320 iterator_to_array($this->repository->all())
321 );
322 }
323
324 foreach ($from_request as $id) {
325 $entry = $this->repository->get($id);
326 if ($entry === null) {
327 continue;
328 }
329 if ($entry->isCore()) {
330 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
331 $id,
332 $entry->getTitle(),
333 $this->translator->translate('info_not_deletable_core') .
334 $this->pons->out()->render($this->pons->out()->nok())
335 );
336 continue;
337 }
338 $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
339 $this->hash($entry->getId()),
340 $entry->getTitle(),
341 $this->pons->out()->render($this->pons->out()->ok())
342 );
343 }
344
345 $this->pons->out()->outAsyncAsModal(
346 $this->translator->translate('entry_delete'),
347 $this->pons->flow()->getHereAsURI(self::CMD_DELETE),
348 ...$items
349 );
350 }
351
352 #[Command('write')]
353 private function delete(): void
354 {
355 $from_request = $this->pons->in()->getAllFromRequest($this->entry_token);
356
357 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
358 $from_request = array_map(
359 fn(TranslatableItem $item): string => $item->getId(),
360 iterator_to_array($this->repository->all())
361 );
362 }
363
364 foreach ($from_request as $id) {
365 $item = $this->repository->get($id);
366 if ($item === null) {
367 continue;
368 }
369 if ($item->isCore()) {
370 continue;
371 }
372 $this->repository->delete($item);
373 }
374
375 $this->pons->out()->success($this->translator->translate('entry_deleted'), true);
376 $this->pons->flow()->redirect(self::CMD_DEFAULT);
377 }
378
379 private function saveOrder(): void
380 {
381 foreach ($this->pons->in()->request()->getParsedBody() as $hashed_id => $position) {
382 $item = $this->repository->get($this->unhash($hashed_id));
383 $item = $item->withPosition((int) $position);
384 $this->repository->store($item);
385 }
386 $this->pons->out()->success($this->pons->i18n()->translate('order_saved'));
387 $this->pons->flow()->redirect(self::CMD_DEFAULT);
388 }
389
390 public function executeCommand(): bool
391 {
392 if ($this->pons->handle(ilObjFooterAdministrationGUI::TAB_SUB_ITEMS)) {
393 return true;
394 }
395 match ($this->pons->flow()->getCommand(self::CMD_DEFAULT)) {
396 self::CMD_DEFAULT => $this->index(),
397 self::CMD_ADD => $this->add(),
398 self::CMD_CREATE => $this->create(),
399 self::CMD_EDIT => $this->edit(),
400 self::CMD_UPDATE => $this->update(),
401 self::CMD_CONFIRM_DELETE => $this->confirmDelete(),
402 self::CMD_DELETE => $this->delete(),
403 self::CMD_TOGGLE_ACTIVATION => $this->toggleActivation(),
404 self::CMD_SAVE_ORDER => $this->saveOrder(),
405 default => $this->pons->out()->outString(
406 'Unknown command: ' . $this->pons->flow()->getCommand(self::CMD_DEFAULT)
407 ),
408 };
409 return true;
410 }
411
412}
$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
ServerRequestInterface $request
EntriesRepository $repository
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