ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMMBaseGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22use ILIAS\GlobalScreen\GUI\I18n\Translator;
26use ILIAS\GlobalScreen\GUI\Hasher;
29
30abstract class ilMMBaseGUI implements SupportsTranslationGUI
31{
32 use Hasher;
33
37 public const CMD_DEFAULT = 'index';
41 public const CMD_ADD = 'add';
45 public const CMD_CREATE = 'create';
49 public const CMD_EDIT = 'edit';
53 public const CMD_DELETE = 'delete';
57 public const CMD_CONFIRM_DELETE = 'confirmDelete';
61 public const CMD_UPDATE = 'update';
65 public const CMD_ACTIVATE = 'activate';
69 public const CMD_DEACTIVATE = 'deactivate';
73 public const CMD_SAVE_ORDER = 'saveOrder';
74
76 protected UIServices $ui;
77 protected Translator $lng;
78 protected Access $access;
79 protected Flow $flow;
81
82 public function __construct(protected Pons $pons)
83 {
84 $this->ui = $this->pons->out()->ui();
85 $this->lng = $this->pons->i18n();
86 $this->repository = new ilMMItemRepository();
87 $this->access = $this->pons->access();
88 $this->flow = $this->pons->flow();
89 $this->toolbar = $this->pons->out()->toolbar();
90 }
91
92 protected function keepTokens(): void
93 {
94 $this->pons->in()->keepTokens($this);
95 }
96
97 abstract public function getCurrentItem(): ilMMItemFacadeInterface;
98
102 abstract public function getMutlipleItems(): array;
103
104 abstract public function getTokensToKeep(): array;
105
106 public function executeCommand(): bool
107 {
108 match ($cmd = $this->pons->flow()->getCommand(self::CMD_DEFAULT)) {
109 self::CMD_DEFAULT => $this->index(),
110 self::CMD_ADD => $this->add(),
111 self::CMD_EDIT => $this->edit(),
112 self::CMD_CREATE => $this->create(),
113 self::CMD_UPDATE => $this->update(),
114 self::CMD_ACTIVATE => $this->activate(),
115 self::CMD_DEACTIVATE => $this->deactivate(),
116 self::CMD_CONFIRM_DELETE => $this->confirmDelete(),
117 self::CMD_DELETE => $this->delete(),
118 self::CMD_SAVE_ORDER => $this->saveOrder(),
119 default => $this->pons->out()->outString('Command not found:' . $cmd)
120 };
121 return true;
122 }
123
124 #[Command('write')]
125 private function add(): void
126 {
127 $this->form(false);
128 }
129
130 #[Command('write')]
131 private function edit(): void
132 {
133 $this->form(false);
134 }
135
136 #[Command('write')]
137 private function create(): void
138 {
139 $this->form(true);
140 }
141
142 #[Command('write')]
143 private function update(): void
144 {
145 $this->form(true);
146 }
147
148 #[Command('write')]
149 protected function delete(): void
150 {
151 foreach ($this->getMutlipleItems() as $item) {
152 if ($item->isDeletable()) {
153 $this->repository->deleteItem($item);
154 }
155 }
156 $this->pons->out()->success($this->lng->txt("msg_topitem_deleted"), true);
157 $this->pons->flow()->redirect(self::CMD_DEFAULT);
158 }
159
160 #[Command('write')]
161 protected function confirmDelete(): void
162 {
163 $this->keepTokens();
164 $items = [];
165 foreach ($this->getMutlipleItems() as $item) {
166 $items[] = $this->ui->factory()->modal()->interruptiveItem()->standard(
167 $this->hash($item->identification()->serialize()),
168 $item->getDefaultTitle()
169 );
170 }
171
172 $this->pons->out()->outAsyncAsConfirmation(
173 $this->lng->txt('delete'),
174 $this->lng->txt('confirm_delete'),
175 $this->lng->txt('delete'),
176 $this->flow->getHereAsURI(self::CMD_DELETE),
177 ...$items
178 );
179 }
180
181 #[Command('write')]
182 protected function activate(): void
183 {
184 $this->toggle(true);
185 }
186
187 #[Command('write')]
188 protected function deactivate(): void
189 {
190 $this->toggle(false);
191 }
192
193 #[Command('write')]
194 protected function toggle(bool $activation): void
195 {
196 $not_changed = [];
197 $changed = [];
198
199 foreach ($this->getMutlipleItems() as $item) {
200 if (!$item->canBeDeactivated()) {
201 $not_changed[] = $item->getDefaultTitle();
202 continue;
203 }
204 $item->setActiveStatus($activation);
205 $item->update();
206 $changed[] = $item->getDefaultTitle();
207 }
208 if ($changed !== []) {
209 $this->pons->out()->success(
210 $this->lng->t('msg_success'),
211 true
212 );
213 }
214
215 if ($not_changed !== []) {
216 $this->pons->out()->error(
217 $this->lng->t('msg_not_changed', null, [implode(', ', $not_changed)]),
218 true
219 );
220 }
221 $this->flow->redirect(self::CMD_DEFAULT);
222 }
223
224 #[Command('write')]
225 protected function saveOrder(): void
226 {
227 foreach ($this->pons->in()->request()->getParsedBody() as $hashed_id => $position) {
228 $item = $this->repository->getItemFacadeForIdentificationString($this->unhash($hashed_id));
229 $item->setPosition((int) $position);
230 $this->repository->updateItem($item);
231 }
232 $this->pons->out()->success($this->pons->i18n()->translate('order_saved'));
233 $this->pons->flow()->redirect(self::CMD_DEFAULT);
234 }
235
236}
Provides fluid interface to RBAC services.
Definition: UIServices.php:25
UIServices $ui
Translator $lng
ilMMItemRepository $repository
const CMD_CONFIRM_DELETE
ilToolbarGUI $toolbar
toggle(bool $activation)
__construct(protected Pons $pons)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface ilMMItemFacadeInterface.
form( $class_path, string $cmd, string $submit_caption="")