ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilContainerResourceGUI.php
Go to the documentation of this file.
1<?php
2
38use ILIAS\components\ResourceStorage\Container\View\PreviewDefinition;
41
46{
47 use URLSerializer;
48
52 public const P_PATH = 'path';
56 public const P_PATHS = 'paths';
57
61 public const CMD_INDEX = 'index';
65 public const CMD_INFO = 'info';
69 public const CMD_UPLOAD = 'upload';
73 public const CMD_POST_UPLOAD = 'postUpload';
74
75
79 public const CMD_REMOVE = 'remove';
83 public const CMD_DOWNLOAD = 'download';
87 public const CMD_DOWNLOAD_ZIP = 'downloadZIP';
88
92 public const CMD_UNZIP = 'unzip';
96 public const CMD_RENDER_CONFIRM_REMOVE = 'renderConfirmRemove';
100 public const ADD_DIRECTORY = 'addDirectory';
101
110 private \ILIAS\ResourceStorage\Services $irss;
113 private \ILIAS\UI\Factory $ui_factory;
116 private PreviewDefinition $preview_definition;
119
120 final public function __construct(
121 private Configuration $view_configuration
122 ) {
123 global $DIC;
124 $this->ctrl = $DIC->ctrl();
125 $this->main_tpl = $DIC->ui()->mainTemplate();
126 $this->ui_renderer = $DIC->ui()->renderer();
127 $this->ui_factory = $DIC->ui()->factory();
128 $this->http = $DIC->http();
129 $this->refinery = $DIC->refinery();
130 $this->language = $DIC->language();
131 $this->language->loadLanguageModule('irss');
132 $this->irss = $DIC->resourceStorage();
133 $this->upload = $DIC->upload();
134
135 $this->view_request = new Request(
136 $DIC->ctrl(),
137 $DIC->http()->wrapper()->query(),
138 $this->view_configuration
139 );
140
141 // to store paramaters needed in GUI
142 $this->view_request->init($this);
143
144 $this->action_provider = new CombinedActionProvider(
145 $this->standard_action_provider = new StandardActionProvider($this->view_request),
146 $this->view_configuration->getActionProvider()
147 );
148
149 $data_provider = new TableDataProvider($this->view_request);
150
151 $this->action_builder = new ActionBuilder(
152 $this->view_request,
153 $this->ctrl,
154 $DIC->ui()->factory(),
155 $DIC->language(),
156 $this->irss,
157 $this->action_provider
158 );
159
161 $this->view_request,
162 $data_provider,
163 $this->ctrl,
164 $DIC->ui()->factory(),
165 $DIC->language()
166 );
167
168 $upload_builder = new UploadBuilder(
169 $this->view_request,
170 $this->ctrl,
171 $DIC->ui()->factory(),
172 $DIC->language(),
173 $this
174 );
175
176 $this->view_factory = new ViewFactory(
177 $data_provider,
178 $this->action_builder,
180 $upload_builder
181 );
182 }
183
184 // CMD CLASS
185
186 private function abortWithPermissionDenied(): void
187 {
188 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_no_perm_read'), true);
189 $this->ctrl->redirect($this, self::CMD_INDEX);
190 }
191
192 public function executeCommand(): void
193 {
194 if ($this->view_request->handleViewTitle()) {
195 $title = $this->view_request->getTitle();
196 $this->main_tpl->setTitle($title);
197 $description = $this->view_request->getDescription();
198 if ($description !== null) {
199 $this->main_tpl->setDescription($description);
200 }
201 }
202
203 switch ($this->ctrl->getCmd(self::CMD_INDEX)) {
204 case self::CMD_INDEX:
205 $this->index();
206 break;
207 case self::CMD_UPLOAD:
208 $this->upload();
209 break;
211 $this->postUpload();
212 break;
213 case self::CMD_REMOVE:
214 $this->remove();
215 break;
217 $this->download();
218 break;
219 case self::CMD_UNZIP:
220 $this->unzip();
221 break;
223 $this->renderConfirmRemove();
224 break;
226 $this->addDirectory();
227 break;
229 $this->downloadZIP();
230 break;
231 }
232 }
233
234 // RESOURCE COLLECTION GUI
235
236 private function index(): void
237 {
238 global $DIC;
239 $components = [];
240
241 // Add components from Actions
242 $components = array_merge(
244 $this->action_provider->getComponents()
245 );
246
247 // Add components from the selected view (currently data-table)
248 foreach ($this->view_factory->getComponentProvider($this->view_request)->getComponents() as $component) {
249 $components[] = $component;
250 }
251
252 $this->main_tpl->setContent(
253 $this->ui_renderer->render(
255 )
256 );
257 }
258
259 private function downloadZIP(): never
260 {
261 $this->irss->consume()->download(
262 $this->view_configuration->getContainer()->getIdentification()
263 )->overrideFileName($this->view_request->getTitle())->run();
264 }
265
266 private function addDirectory(): void
267 {
268 if (!$this->view_request->canUserAdministrate()) {
270 return;
271 }
272 $modal = $this->standard_action_provider->getAddDirectoryModal()->withRequest($this->http->request());
273
274 $directory_name = $modal->getData()[0] ?? '';
275 if (empty($directory_name)) {
276 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_error_adding_directory'), true);
277 $this->ctrl->redirect($this, self::CMD_INDEX);
278 return;
279 }
280 $directory_name = $this->view_request->getPath() . $directory_name;
281
282 $success = $this->irss->manageContainer()->createDirectoryInsideContainer(
283 $this->view_configuration->getContainer()->getIdentification(),
284 $directory_name
285 );
286
287 if (!$success) {
288 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_error_adding_directory'), true);
289 $this->ctrl->redirect($this, self::CMD_INDEX);
290 return;
291 }
292
293 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('msg_success_adding_directory'), true);
294 $this->ctrl->redirect($this, self::CMD_INDEX);
295 }
296
297 public function upload(): void
298 {
299 if (!$this->view_request->canUserUplaod()) {
301 return;
302 }
303 $this->upload->process();
304 if (!$this->upload->hasUploads()) {
305 return;
306 }
307 $container = $this->view_configuration->getContainer();
308
309 foreach ($this->upload->getResults() as $result) {
310 if (!$result->isOK()) {
311 continue;
312 }
313 // store to zip
314 $return = $this->irss->manageContainer()->addUploadToContainer(
315 $container->getIdentification(),
316 $result,
317 $this->view_request->getPath()
318 );
319 }
320
321 // OK
322 $upload_result = new BasicHandlerResult(
323 self::P_PATH,
324 $return ? BasicHandlerResult::STATUS_OK : BasicHandlerResult::STATUS_FAILED,
325 '-',
326 'undefined error'
327 );
328 $response = $this->http->response()->withBody(Streams::ofString(json_encode($upload_result)));
329 $this->http->saveResponse($response);
330 $this->http->sendResponse();
331 $this->http->close();
332 }
333
334 private function postUpload(): void
335 {
336 if (!$this->view_request->canUserUplaod()) {
338 return;
339 }
340 if ($this->http->request()->getParsedBody() === []) { // nothing uploaded
341 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('rids_appended_failed'), true);
342 } else {
343 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('rids_appended'), true);
344 }
345
346 $this->ctrl->redirect($this, self::CMD_INDEX);
347 }
348
349 private function download(): never
350 {
351 $paths = $this->getPathsFromRequest();
352
353 $this->view_request->getWrapper()->download(
354 $paths[0]
355 );
356 }
357
358 private function getPathsFromRequest(): array
359 {
360 $unhash = fn(string $path): string => $this->unhash($path);
361 $unhash_array = static fn(array $paths): array => array_map(
362 $unhash,
363 $paths
364 );
365 $to_string = $this->refinery->kindlyTo()->string();
366 $to_array_of_strings = $this->refinery->kindlyTo()->listOf(
367 $to_string
368 );
369
370 // Get item from table
371 $token_name = $this->action_builder->getUrlToken()->getName();
372 if ($this->http->wrapper()->query()->has($token_name)) {
373 return $unhash_array(
374 $this->http->wrapper()->query()->retrieve(
375 $token_name,
376 $to_array_of_strings
377 ) ?? []
378 );
379 }
380
381 if ($this->http->wrapper()->post()->has('interruptive_items')) {
382 return $unhash_array(
383 $this->http->wrapper()->post()->retrieve(
384 'interruptive_items',
385 $to_array_of_strings
386 )
387 );
388 }
389
390 return [];
391 }
392
393 private function unzip(): void
394 {
395 if (!$this->view_request->canUserAdministrate()) {
397 return;
398 }
399 $paths = $this->getPathsFromRequest()[0];
400 $this->view_request->getWrapper()->unzip(
401 $paths
402 );
403
404 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('rids_appended'), true);
405 $this->ctrl->redirect($this, self::CMD_INDEX);
406 }
407
408 private function renderConfirmRemove(): void
409 {
410 if (!$this->view_request->canUserAdministrate()) {
412 return;
413 }
414 $paths = $this->getPathsFromRequest();
415
416 $stream = Streams::ofString(
417 $this->ui_renderer->render(
418 $this->ui_factory->modal()->interruptive(
419 $this->language->txt('action_remove_zip_path'),
420 $this->language->txt('action_remove_zip_path_msg'),
421 $this->ctrl->getLinkTarget($this, self::CMD_REMOVE)
422 )->withAffectedItems(
423 array_map(fn(string $path_inside_zip): Standard => $this->ui_factory->modal()->interruptiveItem()->standard(
424 $this->hash($path_inside_zip),
425 $path_inside_zip
426 ), $paths)
427 )
428 )
429 );
430 $this->http->saveResponse($this->http->response()->withBody($stream));
431 $this->http->sendResponse();
432 $this->http->close();
433 }
434
435 private function remove(): void
436 {
437 if (!$this->view_request->canUserAdministrate()) {
439 return;
440 }
441 $paths = $this->getPathsFromRequest();
442
443 if (empty($paths)) {
444 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_no_perm_read'), true);
445 $this->ctrl->redirect($this, self::CMD_INDEX);
446 return;
447 }
448
449 foreach ($paths as $path_inside_zip) {
450 $this->irss->manageContainer()->removePathInsideContainer(
451 $this->view_configuration->getContainer()->getIdentification(),
452 $path_inside_zip
453 );
454 }
455
456 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('msg_paths_deleted'), true);
457 $this->ctrl->redirect($this, self::CMD_INDEX);
458 }
459
460
461 // UPLOAD HANDLER
462 public function getFileIdentifierParameterName(): string
463 {
464 return self::P_PATH;
465 }
466
467 public function getUploadURL(): string
468 {
469 return $this->ctrl->getLinkTarget($this, self::CMD_UPLOAD);
470 }
471
472 public function getFileRemovalURL(): string
473 {
474 return '';
475 }
476
477 public function getExistingFileInfoURL(): string
478 {
479 return $this->ctrl->getLinkTarget($this, self::CMD_INFO);
480 }
481
482 public function getInfoForExistingFiles(array $file_ids): array
483 {
484 return [];
485 }
486
487 public function getInfoResult(string $identifier): ?FileInfoResult
488 {
489 return null;
490 }
491
492 public function supportsChunkedUploads(): bool
493 {
494 return false;
495 }
496}
$components
Builds data types.
Definition: Factory.php:36
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
Class Services.
Definition: Services.php:38
__construct(private Configuration $view_configuration)
StandardActionProvider $standard_action_provider
ilGlobalTemplateInterface $main_tpl
ILIAS ResourceStorage Services $irss
language handling
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$path
Definition: ltiservices.php:30
static http()
Fetches the global http state from ILIAS.
global $DIC
Definition: shib_login.php:26
$container
@noRector
Definition: wac.php:37
$response
Definition: xapitoken.php:90