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