ILIAS  trunk Revision v12.0_alpha-16-g3e876e53c80
ShortlinkAdministrationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
33use ILIAS\GlobalScreen\GUI\Hasher;
37
44{
45 use Hasher;
46
47 public const string CMD_FORM = 'form';
48 public const string CMD_SAVE = 'save';
49 public const string CMD_CONFIRM_DELETE = 'confirmDelete';
50 public const string CMD_DELETE = 'delete';
51 public const string CMD_CONFIRM_TOGGLE = 'confirmToggle';
52 public const string CMD_TOGGLE = 'toggle';
53 public const string CMD_SAVE_ORDERING = 'saveOrdering';
60
61 public function __construct(Pons $pons)
62 {
63 global $DIC;
65 $this->toolbar = $pons->out()->toolbar();
66 $this->ui_factory = $pons->out()->ui()->factory();
67 $this->repository = new RepositoryDB(
68 $DIC->database(),
70 $DIC->repositoryTree()
71 )
72 );
73 $this->link_resolver = new TargetLinkResolver(
74 $DIC['static_url']->builder(),
75 $DIC[\ILIAS\Data\Factory::class],
76 $this->ui_factory,
77 );
78 $this->token = $this->pons->in()->buildToken('alias', 'id');
79 $this->configuration = $DIC['static_url.config'];
80 }
81
82 private function getForm(): Form
83 {
84 $id_from_request = $this->pons->in()->getFirstFromRequest($this->token);
85
86 if ($this->repository->hasId($id_from_request)) {
87 $shortlink = $this->repository->getById(
88 $id_from_request
89 );
90 } else {
91 $shortlink = $this->repository->blank();
92 }
93
94 return new Form(
95 $this->pons,
96 $this->repository,
97 $this->pons->flow()->getHereAsURI(self::CMD_SAVE),
98 $shortlink
99 );
100 }
101
102 public function executeCommand(): bool
103 {
104 if ($this->pons->handle(ilObjStaticUrlServiceGUI::TAB_INDEX)) {
105 return true;
106 }
107
108 match ($cmd = $this->pons->flow()->getCommand(self::CMD_DEFAULT)) {
109 self::CMD_DEFAULT => $this->index(),
110 self::CMD_FORM => $this->form(),
111 self::CMD_SAVE => $this->save(),
112 self::CMD_CONFIRM_DELETE => $this->confirmDelete(),
113 self::CMD_DELETE => $this->delete(),
114 self::CMD_CONFIRM_TOGGLE => $this->confirmToggle(),
115 self::CMD_TOGGLE => $this->toggle(),
116 self::CMD_SAVE_ORDERING => $this->saveOrdering(),
117 default => $this->pons->out()->outString(
118 'Unknown command: ' . $cmd
119 ),
120 };
121 return true;
122 }
123
124 public function getTokensToKeep(): array
125 {
126 return [];
127 }
128
129 #[Command('write')]
130 private function save(): void
131 {
132 $form = $this->getForm();
133 if (($out_form = $form->save()) instanceof Standard) {
134 $this->pons->out()->out(
135 $out_form
136 );
137 return;
138 }
139 $this
140 ->pons
141 ->out()
142 ->success(
143 $this->pons->i18n()->t(
144 'stus_stored_sucessfully'
145 )
146 );
147
148 $this
149 ->pons
150 ->flow()
151 ->redirect(
152 self::CMD_DEFAULT
153 );
154 }
155
156 #[Command('write')]
157 private function form(): void
158 {
159 $form = $this->getForm();
160
161 $this->pons->in()->keep(
162 $this->token
163 );
164
165 $this->pons->out()->outAsyncAsModal(
166 $this->pons->i18n()->t('shortlink'),
167 $this->pons->flow()->getHereAsURI(self::CMD_SAVE),
168 ...iterator_to_array($form->get())
169 );
170 }
171
172 #[Command('read')]
173 private function index(): void
174 {
175 if ($this->pons->access()->hasUserPermissionTo('write')) {
176 $this->toolbar->addComponent(
177 $modal = $this->ui_factory->modal()->roundtrip(
178 $this->pons->i18n()->t('create_shortlink'),
179 null,
180 [],
181 null
182 )->withAsyncRenderUrl(
183 (string) $this->pons->flow()->getHereAsURI(self::CMD_FORM),
184 )
185 );
186
187 $this->toolbar->addComponent(
188 $button = $this->ui_factory->button()->primary(
189 $this->pons->i18n()->t('create_shortlink'),
190 (string) $this->pons->flow()->getHereAsURI(self::CMD_FORM), // TODO make async again
191 )->withOnClick(
192 $modal->getShowSignal()
193 )
194 );
195 }
196
197 $table = new Table(
198 $this->pons,
199 $this->repository,
200 $this->link_resolver,
201 $this->pons->flow()->getHereAsURI(self::CMD_SAVE_ORDERING),
202 $this->token,
203 $this->configuration,
204 $this->pons->access()->hasUserPermissionTo('write'),
205 );
206
207 $this->pons->out()->out(...iterator_to_array($table->get()));
208 }
209
210 #[Command('write')]
211 private function confirm(
212 string $target_command,
213 string $title,
214 string $message,
215 string $button_title,
216 ): void {
217 $items = [];
218 $from_request = $this->getAllIds();
219
220 $this->pons->out()->nok();
221 $this->pons->out()->ok();
222
223 foreach ($from_request as $id) {
224 $shortlink = $this->repository->getById($id);
225 if ($shortlink === null) {
226 continue;
227 }
228 $id = $this->hash($id);
229
230 $items[] = $this->ui_factory->modal()->interruptiveItem()->standard(
231 $id,
232 $shortlink->getAlias(),
233 );
234 }
235
236 $this->pons->out()->outAsyncAsConfirmation(
237 $title,
238 $message,
239 $button_title,
240 $this->pons->flow()->getHereAsURI($target_command),
241 ...$items
242 );
243 }
244
245 #[Command('write')]
246 private function confirmDelete(): void
247 {
248 $this->confirm(
249 self::CMD_DELETE,
250 $this->pons->i18n()->t('delete_shortlink'),
251 $this->pons->i18n()->t('delete_shortlink_msg'),
252 $this->pons->i18n()->t('delete')
253 );
254 }
255
256 #[Command('write')]
257 private function delete(): void
258 {
259 $from_request = $this->getAllIds();
260 foreach ($from_request as $id) {
261 $shortlink = $this->repository->getById($id);
262 if ($shortlink === null) {
263 continue;
264 }
265 $this->repository->delete($shortlink);
266 }
267
268 $this->pons->out()->success($this->pons->i18n()->t('shortlinks_deleted'));
269 $this->pons->flow()->redirect(self::CMD_DEFAULT);
270 }
271
272 #[Command('write')]
273 private function confirmToggle(): void
274 {
275 $this->confirm(
276 self::CMD_TOGGLE,
277 $this->pons->i18n()->t('toggle_shortlink'),
278 $this->pons->i18n()->t('toggle_shortlink_msg'),
279 $this->pons->i18n()->t('toggle')
280 );
281 }
282
283 #[Command('write')]
284 private function toggle(): void
285 {
286 $from_request = $this->getAllIds();
287 foreach ($from_request as $id) {
288 $shortlink = $this->repository->getById($id);
289 if ($shortlink === null) {
290 continue;
291 }
292 $shortlink = $shortlink->withActive(
293 !$shortlink->isActive()
294 );
295
296 $this->repository->store($shortlink);
297 }
298
299 $this->pons->out()->success($this->pons->i18n()->t('shortlinks_toggled'));
300 $this->pons->flow()->redirect(self::CMD_DEFAULT);
301 }
302
303 #[Command('write')]
304 private function saveOrdering(): void
305 {
306 foreach ($this->pons->in()->request()->getParsedBody() as $hashed_id => $position) {
307 $item = $this->repository->getById($this->unhash($hashed_id));
308 $item = $item->withPosition((int) $position);
309 $this->repository->store($item);
310 }
311 $this->pons->out()->success($this->pons->i18n()->translate('order_saved'));
312 $this->pons->flow()->redirect(self::CMD_DEFAULT);
313 }
314
315 private function getAllIds(): array
316 {
317 $from_request = $this->pons->in()->getAllFromRequest($this->token);
318 if (($from_request[0] ?? null) === Input::ALL_OBJECTS) {
319 return array_map(
320 static fn(Shortlink $s): string => $s->getId(),
321 iterator_to_array($this->repository->getAll())
322 );
323 }
324 return array_unique($from_request);
325 }
326
327}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
confirm(string $target_command, string $title, string $message, string $button_title,)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This describes a standard form.
Definition: Standard.php:30
This is how the factory for UI elements looks.
Definition: Factory.php:38
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $DIC
Definition: shib_login.php:26