ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilContainerResourceGUI.php
Go to the documentation of this file.
1<?php
2
39use ILIAS\components\ResourceStorage\Container\View\PreviewDefinition;
43
48{
49 use URLSerializer;
50
54 public const P_PATH = 'path';
58 public const P_PATHS = 'paths';
59
63 public const CMD_INDEX = 'index';
67 public const CMD_INFO = 'info';
71 public const CMD_UPLOAD = 'upload';
75 public const CMD_POST_UPLOAD = 'postUpload';
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 protected 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 if ($title !== null) {
197 $this->main_tpl->setTitle($title);
198 }
199 $description = $this->view_request->getDescription();
200 if ($description !== null) {
201 $this->main_tpl->setDescription($description);
202 }
203 }
204
205 switch ($this->ctrl->getCmd(self::CMD_INDEX)) {
206 case self::CMD_INDEX:
207 $this->index();
208 break;
209 case self::CMD_UPLOAD:
210 $this->upload();
211 break;
213 $this->postUpload();
214 break;
215 case self::CMD_REMOVE:
216 $this->remove();
217 break;
219 $this->download();
220 break;
221 case self::CMD_UNZIP:
222 $this->unzip();
223 break;
225 $this->renderConfirmRemove();
226 break;
228 $this->addDirectory();
229 break;
231 $this->downloadZIP();
232 break;
233 }
234 }
235
236 // RESOURCE COLLECTION GUI
237
238 private function index(): void
239 {
240 global $DIC;
241 $components = [];
242
243 // Add components from Actions
244 $components = array_merge(
246 $this->action_provider->getComponents()
247 );
248
249 // Add components from the selected view (currently data-table)
250 foreach ($this->view_factory->getComponentProvider($this->view_request)->getComponents() as $component) {
251 $components[] = $component;
252 }
253
254 $this->main_tpl->setContent(
255 $this->ui_renderer->render(
257 )
258 );
259 }
260
261 private function downloadZIP(): never
262 {
263 $this->irss->consume()->download(
264 $this->view_configuration->getContainer()->getIdentification()
265 )->overrideFileName($this->view_request->getTitle())->run();
266 }
267
268 private function addDirectory(): void
269 {
270 if (!$this->view_request->canUserAdministrate()) {
272 return;
273 }
274 $modal = $this->standard_action_provider->getAddDirectoryModal()->withRequest($this->http->request());
275
276 $directory_name = $modal->getData()[0] ?? '';
277 if (empty($directory_name)) {
278 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_error_adding_directory'), true);
279 $this->ctrl->redirect($this, self::CMD_INDEX);
280 return;
281 }
282 $directory_name = $this->view_request->getPath() . $directory_name;
283
284 $success = $this->irss->manageContainer()->createDirectoryInsideContainer(
285 $this->view_configuration->getContainer()->getIdentification(),
286 $directory_name
287 );
288
289 if (!$success) {
290 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_error_adding_directory'), true);
291 $this->ctrl->redirect($this, self::CMD_INDEX);
292 return;
293 }
294
295 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('msg_success_adding_directory'), true);
296 $this->ctrl->redirect($this, self::CMD_INDEX);
297 }
298
299 public function upload(): void
300 {
301 if (!$this->view_request->canUserUplaod()) {
303 return;
304 }
305 $this->upload->process();
306 if (!$this->upload->hasUploads()) {
307 return;
308 }
309 $container = $this->view_configuration->getContainer();
310
311 foreach ($this->upload->getResults() as $result) {
312 if (!$result->isOK()) {
313 continue;
314 }
315 // store to zip
316 $return = $this->irss->manageContainer()->addUploadToContainer(
317 $container->getIdentification(),
318 $result,
319 $this->view_request->getPath()
320 );
321 }
322
323 // OK
324 $upload_result = new BasicHandlerResult(
325 self::P_PATH,
326 $return ? BasicHandlerResult::STATUS_OK : BasicHandlerResult::STATUS_FAILED,
327 '-',
328 'undefined error'
329 );
330 $response = $this->http->response()->withBody(Streams::ofString(json_encode($upload_result)));
331 $this->http->saveResponse($response);
332 $this->http->sendResponse();
333 $this->http->close();
334 }
335
336 private function postUpload(): void
337 {
338 if (!$this->view_request->canUserUplaod()) {
340 return;
341 }
342 if ($this->http->request()->getParsedBody() === []) { // nothing uploaded
343 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('rids_appended_failed'), true);
344 } else {
345 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('rids_appended'), true);
346 }
347
348 $this->ctrl->redirect($this, self::CMD_INDEX);
349 }
350
351 private function download(): never
352 {
353 $paths = $this->getPathsFromRequest();
354
355 $this->view_request->getWrapper()->download(
356 $paths[0]
357 );
358 }
359
360 protected function getPathsFromRequest(): array
361 {
362 $unhash = fn(string $path): string => $this->unhash($path);
363 $unhash_array = static fn(array $paths): array => array_map(
364 $unhash,
365 $paths
366 );
367 $to_string = $this->refinery->kindlyTo()->string();
368 $to_array_of_strings = $this->refinery->kindlyTo()->listOf(
369 $to_string
370 );
371
372 // Get item from table
373 $token_name = $this->action_builder->getUrlToken()->getName();
374 if ($this->http->wrapper()->query()->has($token_name)) {
375 return $unhash_array(
376 $this->http->wrapper()->query()->retrieve(
377 $token_name,
378 $to_array_of_strings
379 ) ?? []
380 );
381 }
382
383 if ($this->http->wrapper()->post()->has('interruptive_items')) {
384 return $unhash_array(
385 $this->http->wrapper()->post()->retrieve(
386 'interruptive_items',
387 $to_array_of_strings
388 )
389 );
390 }
391
392 return [];
393 }
394
395 private function unzip(): void
396 {
397 if (!$this->view_request->canUserAdministrate()) {
399 return;
400 }
401 $paths = $this->getPathsFromRequest()[0];
402 $this->view_request->getWrapper()->unzip(
403 $paths
404 );
405
406 $this->postUpload();
407 }
408
409 private function renderConfirmRemove(): void
410 {
411 if (!$this->view_request->canUserAdministrate()) {
413 return;
414 }
415 $paths = $this->getPathsFromRequest();
416
417 $stream = Streams::ofString(
418 $this->ui_renderer->render(
419 $this->ui_factory->modal()->interruptive(
420 $this->language->txt('action_remove_zip_path'),
421 $this->language->txt('action_remove_zip_path_msg'),
422 $this->ctrl->getLinkTarget($this, self::CMD_REMOVE)
423 )->withAffectedItems(
424 array_map(fn(string $path_inside_zip): Standard => $this->ui_factory->modal()->interruptiveItem()->standard(
425 $this->hash($path_inside_zip),
426 $path_inside_zip
427 ), $paths)
428 )
429 )
430 );
431 $this->http->saveResponse($this->http->response()->withBody($stream));
432 $this->http->sendResponse();
433 $this->http->close();
434 }
435
436 private function remove(): void
437 {
438 if (!$this->view_request->canUserAdministrate()) {
440 return;
441 }
442 $paths = $this->getPathsFromRequest();
443
444 if (empty($paths)) {
445 $this->main_tpl->setOnScreenMessage('failure', $this->language->txt('msg_no_perm_read'), true);
446 $this->ctrl->redirect($this, self::CMD_INDEX);
447 return;
448 }
449
450 foreach ($paths as $path_inside_zip) {
451 $this->irss->manageContainer()->removePathInsideContainer(
452 $this->view_configuration->getContainer()->getIdentification(),
453 $path_inside_zip
454 );
455 }
456
457 $this->main_tpl->setOnScreenMessage('success', $this->language->txt('msg_paths_deleted'), true);
458 $this->ctrl->redirect($this, self::CMD_INDEX);
459 }
460
461 // REQUEST HELPERS
462
466 private function getResourceIdsFromRequest(): array
467 {
468 $token = $this->action_builder->getUrlToken();
469 $wrapper = $this->http->wrapper();
470 $to_string = $this->refinery->kindlyTo()->string();
471 $to_array_of_string = $this->refinery->to()->listOf($to_string);
472 $rid_string = null;
473
474 if ($wrapper->query()->has($token->getName())) {
475 try {
476 $rid_string = $wrapper->query()->retrieve(
477 $token->getName(),
478 $to_string
479 );
480 $rid_strings = explode(',', (string) $rid_string);
482 $rid_strings = $wrapper->query()->retrieve(
483 $token->getName(),
484 $to_array_of_string
485 );
486 }
487 }
488
489 if ($wrapper->post()->has('interruptive_items')) {
490 $rid_strings = $wrapper->post()->retrieve(
491 'interruptive_items',
492 $to_array_of_string
493 );
494 }
495
496 if ($rid_strings[0] === 'ALL_OBJECTS') {
497 return $this->view_request->getWrapper()->getResourceIdentifications();
498 }
499
500 if ($rid_strings === []) {
501 return [];
502 }
503 $resource_identifications = [];
504 foreach ($rid_strings as $rid_string) {
505 $resource_identification = $this->irss->manage()->find($this->unhash($rid_string));
506 if ($resource_identification === null) {
507 continue;
508 }
509 $resource_identifications[] = $resource_identification;
510 }
511 return $resource_identifications;
512 }
513
514
515 // UPLOAD HANDLER
516 public function getFileIdentifierParameterName(): string
517 {
518 return self::P_PATH;
519 }
520
521 public function getUploadURL(): string
522 {
523 return $this->ctrl->getLinkTarget($this, self::CMD_UPLOAD);
524 }
525
526 public function getFileRemovalURL(): string
527 {
528 return '';
529 }
530
531 public function getExistingFileInfoURL(): string
532 {
533 return $this->ctrl->getLinkTarget($this, self::CMD_INFO);
534 }
535
536 public function getInfoForExistingFiles(array $file_ids): array
537 {
538 return [];
539 }
540
541 public function getInfoResult(string $identifier): ?FileInfoResult
542 {
543 return null;
544 }
545
546 public function supportsChunkedUploads(): bool
547 {
548 return false;
549 }
550}
$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
$token
Definition: xapitoken.php:70
$response
Definition: xapitoken.php:93