ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilFooterGroupsGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
33 
40 final class ilFooterGroupsGUI
41 {
42  use Hasher;
43  use UIHelper;
44 
45  public const CMD_DEFAULT = 'index';
46  public const CMD_ADD = 'add';
47  public const CMD_CREATE = 'create';
48  public const CMD_EDIT = 'edit';
49  public const CMD_UPDATE = 'update';
50  public const CMD_RESET = 'reset';
51  public const CMD_SAVE_ORDER = 'saveOrder';
52  public const GSFO_ID = 'gsfo_group_id';
53  private const CMD_CONFIRM_RESET = 'confirmReset';
58 
59  public function __construct(
60  private Container $dic,
61  private Translator $translator,
62  private ilObjFooterUIHandling $ui_handling
63  ) {
64  $this->ui_factory = $this->dic->ui()->factory();
65  $this->ctrl = $this->dic->ctrl();
66  $this->request = $this->dic->http()->request();
67  $this->repository = new GroupsRepositoryDB(
68  $this->dic->database(),
70  );
71  }
72 
73  protected function addButtons(): array
74  {
75  $modal = $this->ui_factory->modal()->roundtrip(
76  $this->translator->translate('group_add'),
77  null
78  )->withAsyncRenderUrl(
79  $this->ctrl->getLinkTarget($this, self::CMD_ADD)
80  );
81 
82  $confirm_reset = $this->ui_factory->prompt()->standard(
83  $this->ui_handling->getHereAsURI(self::CMD_CONFIRM_RESET),
84  );
85 
86  $this->dic->toolbar()->addComponent(
87  $this->ui_factory
88  ->button()
89  ->primary(
90  $this->translator->translate('group_add'),
91  '#' //$this->ctrl->getLinkTarget($this, self::CMD_ADD)
92  )
93  ->withOnClick($modal->getShowSignal())
94  ->withHelpTopics(...$this->ui_factory->helpTopics('gsfo_button_add'))
95  );
96  $this->dic->toolbar()->addComponent(
97  $this->ui_factory
98  ->button()
99  ->standard(
100  $this->translator->translate('reset_footer'),
101  '#' //'$this->ctrl->getLinkTarget($this, self::CMD_RESET)
102  )
103  ->withOnClick($confirm_reset->getShowSignal())
104  ->withHelpTopics(...$this->ui_factory->helpTopics('gsfo_button_reset'))
105  );
106 
107  return [$modal, $confirm_reset];
108  }
109 
110  protected function confirmReset(): void
111  {
112  $this->ui_handling->outAsync(
113  $this->ui_factory->prompt()->state()->show(
114  $this->ui_factory->messageBox()->confirmation(
115  $this->translator->translate('confirm_reset')
116  )->withButtons(
117  [
118  $this->ui_factory->button()->standard(
119  $this->translator->translate('reset'),
120  $this->ctrl->getLinkTarget($this, self::CMD_RESET)
121  )
122  ]
123  )
124  )
125  );
126  }
127 
128  protected function index(): void
129  {
130  // Add new
131  $components = [];
132  if ($this->ui_handling->hasPermission('write')) {
133  $components = $this->addButtons();
134  }
135  // Sync
136  $this->repository->syncWithGlobalScreen(
137  $this->dic->globalScreen()->collector()->footer()
138  );
139 
140  // Table
141  $table = new GroupsTable(
142  $this->repository,
143  new TranslationsRepositoryDB($this->dic->database()),
144  $this->translator,
145  $this->ui_handling->hasPermission('write')
146  );
147 
148  $this->ui_handling->out(
149  $table->get(
150  $this->ui_handling->getHereAsURI(self::CMD_SAVE_ORDER),
151  $this->ui_handling->buildURI(
152  $this->ctrl->getLinkTargetByClass(
153  ilFooterTranslationGUI::class,
155  )
156  )
157  ),
158  ...$components
159  );
160  }
161 
162  protected function confirmDelete(): void
163  {
164  $items = [];
165 
166  foreach ($this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID) as $id) {
167  $group = $this->repository->get($id);
168  if ($group === null) {
169  continue;
170  }
171  if ($group->isCore()) {
172  $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
173  $id,
174  $group->getTitle(),
175  $this->translator->translate('info_not_deletable_core') .
176  $this->ui_handling->render($this->nok($this->ui_factory))
177  );
178  continue;
179  }
180  if ($group->getItems() > 0) {
181  $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
182  $id,
183  $group->getTitle(),
184  $this->translator->translate('info_not_deletable_not_empty') .
185  $this->ui_handling->render($this->nok($this->ui_factory))
186  );
187  continue;
188  }
189  $items[] = $this->ui_factory->modal()->interruptiveItem()->keyValue(
190  $id,
191  $group->getTitle(),
192  $this->ui_handling->render($this->ok($this->ui_factory))
193  );
194  }
195 
196  $this->ui_handling->outAsyncAsModal(
197  $this->translator->translate('group_delete'),
198  $this->ctrl->getFormAction($this, 'delete'),
199  ...$items
200  );
201  }
202 
203  private function delete(): void
204  {
205  foreach ($this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID) as $id) {
206  $group = $this->repository->get($id);
207  $this->repository->delete($group);
208  }
209 
210  $this->ui_handling->sendMessageAndRedirect(
211  'success',
212  $this->translator->translate('group_deleted'),
213  $this->ctrl->getLinkTarget($this, self::CMD_DEFAULT)
214  );
215  }
216 
217  private function saveOrder(): void
218  {
219  foreach ($this->request->getParsedBody() as $hashed_id => $position) {
220  $this->repository->updatePositionById($this->unhash($hashed_id), (int) $position);
221  }
222  $this->ui_handling->sendMessageAndRedirect(
223  'success',
224  $this->translator->translate('order_saved'),
225  $this->ctrl->getLinkTarget($this, self::CMD_DEFAULT)
226  );
227  }
228 
229  protected function add(): void
230  {
231  $form = new GroupForm(
232  $this->repository,
233  $this->translator
234  );
235 
236  $action = $this->ctrl->getFormAction($this, self::CMD_CREATE);
237 
238  $this->ui_handling->outAsyncAsModal(
239  $this->translator->translate('group_add'),
240  $action,
241  $form->get($action)
242  );
243  }
244 
245  public function create(): void
246  {
247  $form = new GroupForm(
248  $this->repository,
249  $this->translator
250  );
251  if ($form->store(
252  $this->request,
253  $this->ctrl->getFormAction($this, self::CMD_CREATE)
254  )) {
255  $this->ctrl->redirect($this, self::CMD_DEFAULT);
256  }
257  $this->ui_handling->out(
258  $form->get(
259  $this->ctrl->getFormAction($this, self::CMD_CREATE)
260  )
261  );
262  }
263 
264  protected function edit(): void
265  {
266  $id = $this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID)[0];
267  $this->ui_handling->saveIdentificationsToRequest(
268  $this,
269  self::GSFO_ID,
270  $id
271  );
272  $group = $this->repository->get($id);
273 
274  $form = new GroupForm(
275  $this->repository,
276  $this->translator,
277  $group
278  );
279 
280  $target = $this->ctrl->getFormAction($this, self::CMD_UPDATE);
281  $this->ui_handling->outAsyncAsModal(
282  $this->translator->translate('group_edit'),
283  $target,
284  $form->get($target)
285  );
286  }
287 
288  public function update(): void
289  {
290  $id = $this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID)[0];
291  $group = $this->repository->get($id);
292 
293  $form = new GroupForm(
294  $this->repository,
295  $this->translator,
296  $group
297  );
298  if ($form->store(
299  $this->request,
300  $this->ctrl->getFormAction($this, self::CMD_CREATE)
301  )) {
302  $this->ctrl->redirect($this, self::CMD_DEFAULT);
303  }
304  $this->ui_handling->out(
305  $form->get(
306  $this->ctrl->getFormAction($this, self::CMD_CREATE)
307  )
308  );
309  }
310 
311  protected function toggleActivation(): void
312  {
313  foreach ($this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID) as $id) {
314  $group = $this->repository->get($id);
315  $this->repository->store($group->withActive(!$group->isActive()));
316  }
317 
318  $this->ui_handling->sendMessageAndRedirect(
319  'success',
320  $this->translator->translate('group_activation_toggled'),
321  $this->ctrl->getLinkTarget($this, self::CMD_DEFAULT)
322  );
323  }
324 
325  protected function reset(): void
326  {
327  $this->repository->reset(
328  $this->dic->globalScreen()->collector()->footer()
329  );
330  $entries_repo = new EntriesRepositoryDB($this->dic->database(), new ilFooterCustomGroupsProvider($this->dic));
331  $entries_repo->reset($this->dic->globalScreen()->collector()->footer());
332 
333  $translations = new TranslationsRepositoryDB($this->dic->database());
334  $translations->reset();
335 
336  $this->ui_handling->sendMessageAndRedirect(
337  'success',
338  $this->translator->translate('reset_success'),
339  $this->ctrl->getLinkTarget($this, self::CMD_DEFAULT)
340  );
341  }
342 
343  protected function editEntries(): void
344  {
345  $id = $this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID)[0];
346  $this->ui_handling->saveIdentificationsToRequest(
347  ilFooterEntriesGUI::class,
348  self::GSFO_ID,
349  $id
350  );
351  $this->ctrl->redirectByClass(ilFooterEntriesGUI::class);
352  }
353 
354 
355  // HELPERS AND NEEDED IMPLEMENATIONS
356 
357  public function executeCommand(): void
358  {
359  $this->ui_handling->requireReadable();
360 
361  $next_class = $this->ctrl->getNextClass($this) ?? '';
362  $cmd = $this->ctrl->getCmd(self::CMD_DEFAULT);
363 
364  switch (strtolower($next_class)) {
365  case strtolower(ilFooterTranslationGUI::class):
366  $item = $this->repository->get(
367  $this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID)[0]
368  );
369  $back_target = null;
370  if ($this->request->getQueryParams()['async'] ?? false) {
371  $back_target = $this->ui_handling->buildURI($this->ctrl->getLinkTarget($this, self::CMD_DEFAULT));
372  }
373  $translation = new ilFooterTranslationGUI(
374  $this->dic,
375  $this->translator,
376  $this->ui_handling,
377  $item,
378  $back_target
379  );
380 
381  $this->ctrl->forwardCommand($translation);
382 
383  return;
384  case strtolower(ilFooterEntriesGUI::class):
385  $id = $this->ui_handling->getIdentificationsFromRequest(self::GSFO_ID)[0];
386  $this->ui_handling->saveIdentificationsToRequest(
387  ilFooterEntriesGUI::class,
388  self::GSFO_ID,
389  $id
390  );
391  $group = $this->repository->get($id);
392 
393  $this->ctrl->forwardCommand(
394  new ilFooterEntriesGUI(
395  $this->dic,
396  $this->translator,
397  $this->ui_handling,
398  $group,
399  $this->repository
400  )
401  );
402  return;
403  default:
404  switch ($cmd) {
405  case self::CMD_DEFAULT:
406  $this->ui_handling->requireReadable();
407  $this->index();
408  break;
409  case 'editEntries':
410  $this->ui_handling->requireReadable();
411  $this->editEntries();
412  break;
413  case self::CMD_ADD:
414  case self::CMD_CREATE:
415  case self::CMD_EDIT:
416  case self::CMD_UPDATE:
417  default:
418  $this->ui_handling->backToMainTab();
419  $this->ui_handling->requireWritable();
420  $this->$cmd();
421  break;
422  }
423  }
424  }
425 
426 }
ServerRequestInterface $request
button(string $caption, string $cmd)
standard()
description: > This is an example, of how the Notification Slate is generated by assigning Notificat...
Definition: standard.php:22
repository()
description: > Example for rendering a repository card
Definition: repository.php:17
GroupsRepository $repository
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:35
__construct(private Container $dic, private Translator $translator, private ilObjFooterUIHandling $ui_handling)
$components
This is how the factory for UI elements looks.
Definition: Factory.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
$dic
Definition: ltiresult.php:33