ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
Handler.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
24 use ilCtrl;
29 use ILIAS\Export\ExportHandler\Factory as ilExportHandler;
30 use ILIAS\Export\ExportHandler\I\Consumer\Context\HandlerInterface as ilExportHandlerConsumerContextInterface;
31 use ILIAS\Export\ExportHandler\I\Consumer\ExportOption\CollectionInterface as ilExportHandlerConsumerExportOptionCollectionInterface;
32 use ILIAS\Export\ExportHandler\I\Consumer\File\Identifier\CollectionInterface as ilExportHandlerConsumerFileIdentifierCollectionInterface;
33 use ILIAS\Export\ExportHandler\I\Table\HandlerInterface as ilExportHandlerTableInterface;
34 use ILIAS\Export\ExportHandler\I\Table\RowId\CollectionInterface as ilExportHandlerTableRowCollectionInterface;
40 use ilLanguage;
41 use ilObjUser;
43 
44 class Handler implements ilExportHandlerTableInterface
45 {
46  protected const ALL_OBJECTS = "ALL_OBJECTS";
47  protected const TABLE_COL_LNG_TYPE = 'exp_type';
48  protected const TABLE_COL_LNG_FILE = 'exp_file';
49  protected const TABLE_COL_LNG_SIZE = 'exp_size';
50  protected const TABLE_COL_LNG_TIMESTAMP = 'exp_timestamp';
51  protected const TABLE_COL_LNG_PUBLIC_ACCESS = 'exp_public_access';
52  protected const TABLE_ID = "export";
53  protected const ROW_ID = "row_ids";
54  protected const TABLE_ACTION_ID = "table_action";
55  protected const ACTION_DELETE = "delete";
56  protected const ACTION_DOWNLOAD = "download";
57  protected const ACTION_PUBLIC_ACCESS = "enable_pa";
58  protected const ACTION_CONFIRM_DELETE = "delete_selected";
59 
63  protected ilObjUser $user;
64  protected ilCtrl $ctrl;
65  protected ilLanguage $lng;
66  protected ilExportHandler $export_handler;
71  protected ilExportHandlerConsumerExportOptionCollectionInterface $export_options;
72  protected ilDataTable $table;
73  protected ilExportHandlerConsumerContextInterface $context;
74 
75  public function __construct(
76  ilUIServices $ui_services,
77  ilHTTPServices $http_services,
78  ilRefineryFactory $refinery,
79  ilObjUser $user,
80  ilLanguage $lng,
81  ilCtrl $ctrl,
82  ilExportHandler $export_handler,
83  ilDataFactory $data_factory
84  ) {
85  $this->http_services = $http_services;
86  $this->ui_services = $ui_services;
87  $this->refinery = $refinery;
88  $this->lng = $lng;
89  $this->lng->loadLanguageModule("export");
90  $this->user = $user;
91  $this->ctrl = $ctrl;
92  $this->export_handler = $export_handler;
93  $this->data_factory = $data_factory;
94  }
95 
96  protected function getColumns(): array
97  {
98  if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
99  $format = $this->data_factory->dateFormat()->withTime12($this->user->getDateFormat());
100  } else {
101  $format = $this->data_factory->dateFormat()->withTime24($this->user->getDateFormat());
102  }
103  return [
104  self::TABLE_COL_TYPE => $this->ui_services->factory()->table()->column()->text(
105  $this->lng->txt(self::TABLE_COL_LNG_TYPE)
106  )->withHighlight(true),
107  self::TABLE_COL_FILE => $this->ui_services->factory()->table()->column()->text(
108  $this->lng->txt(self::TABLE_COL_LNG_FILE)
109  )->withHighlight(true),
110  self::TABLE_COL_SIZE => $this->ui_services->factory()->table()->column()->number(
111  $this->lng->txt(self::TABLE_COL_LNG_SIZE)
112  )
113  ->withHighlight(true)
114  ->withDecimals(4),
115  self::TABLE_COL_TIMESTAMP => $this->ui_services->factory()->table()->column()->date(
116  $this->lng->txt(self::TABLE_COL_LNG_TIMESTAMP),
117  $format
118  ),
119  self::TABLE_COL_PUBLIC_ACCESS => $this->ui_services->factory()->table()->column()->status(
120  $this->lng->txt(self::TABLE_COL_LNG_PUBLIC_ACCESS),
121  )
122  ];
123  }
124 
125  protected function getActions(): array
126  {
127  $this->url_builder = new URLBuilder($this->data_factory->uri($this->http_services->request()->getUri()->__toString()));
128  list($this->url_builder, $this->action_parameter_token, $this->row_id_token) =
129  $this->url_builder->acquireParameters(
130  ['datatable', self::TABLE_ID],
131  self::TABLE_ACTION_ID,
132  self::ROW_ID
133  );
134  return [
135  self::ACTION_PUBLIC_ACCESS => $this->ui_services->factory()->table()->action()->single(
136  $this->lng->txt('exp_toggle_public_access'),
137  $this->url_builder->withParameter($this->action_parameter_token, self::ACTION_PUBLIC_ACCESS),
139  ),
140  self::ACTION_DOWNLOAD => $this->ui_services->factory()->table()->action()->single(
141  $this->lng->txt('download'),
142  $this->url_builder->withParameter($this->action_parameter_token, self::ACTION_DOWNLOAD),
144  ),
145  self::ACTION_DELETE => $this->ui_services->factory()->table()->action()->standard(
146  $this->lng->txt('delete'),
147  $this->url_builder->withParameter($this->action_parameter_token, self::ACTION_DELETE),
149  )->withAsync()
150  ];
151  }
152 
156  #[NoReturn] protected function showDeleteModal(array $ids_sorted): void
157  {
158  $items = [];
159  $ids = [];
160  foreach ($ids_sorted as $export_option_id => $file_identifiers) {
161  $export_option = $this->export_options->getById($export_option_id);
162  foreach ($export_option->getFileSelection($this->context, $file_identifiers) as $file_info) {
163  $table_row_id = $this->export_handler->table()->rowId()->handler()
164  ->withExportOptionId($export_option_id)
165  ->withFileIdentifier($file_info->getFileIdentifier());
166  $ids[] = $table_row_id->getCompositId();
167  $items[] = $this->ui_services->factory()->modal()->interruptiveItem()->standard(
168  $table_row_id->getCompositId(),
169  $file_info->getFileName()
170  );
171  }
172  }
173  echo($this->ui_services->renderer()->renderAsync([
174  $this->ui_services->factory()->modal()->interruptive(
175  $this->lng->txt('confirm'),
176  $this->lng->txt('exp_really_delete'),
177  (string) $this->url_builder
178  ->withParameter(
179  $this->action_parameter_token,
180  self::ACTION_CONFIRM_DELETE
181  )->withParameter(
182  $this->row_id_token,
183  $ids
184  )->buildURI()
185  )->withAffectedItems($items)
186  ]));
187  exit();
188  }
189 
193  protected function deleteItems(array $ids_sorted): void
194  {
195  $object_id = $this->data_factory->objId($this->context->exportObject()->getId());
196  foreach ($ids_sorted as $export_option_id => $file_identifiers) {
197  $export_option = $this->export_options->getById($export_option_id);
198  if (
199  $this->export_handler->publicAccess()->handler()->hasPublicAccessFile($object_id) and
200  $this->export_handler->publicAccess()->handler()->getPublicAccessFileExportOptionId($object_id) === $export_option->getExportOptionId() and
201  in_array($this->export_handler->publicAccess()->handler()->getPublicAccessFileIdentifier($object_id), $file_identifiers->toStringArray())
202  ) {
203  $this->export_handler->publicAccess()->handler()->removePublicAccessFile($object_id);
204  }
205  $export_option->onDeleteFiles(
206  $this->context,
207  $file_identifiers
208  );
209  }
210  }
211 
215  protected function markAsPublicAccess(array $ids_sorted): void
216  {
217  $pa_repository = $this->export_handler->publicAccess()->repository()->handler();
218  $pa_repository_element_factory = $this->export_handler->publicAccess()->repository()->element();
219  $pa_repository_key_factory = $this->export_handler->publicAccess()->repository()->key();
220  $pa_repository_values_factory = $this->export_handler->publicAccess()->repository()->values();
221  $obj_id = $this->data_factory->objId($this->context->exportObject()->getId());
222  foreach ($ids_sorted as $export_option_id => $file_identifiers) {
223  $export_option = $this->export_options->getById($export_option_id);
224  $type_allowed = $export_option->isPublicAccessPossible();
225  foreach ($export_option->getFileSelection($this->context, $file_identifiers) as $file_info) {
226  $key = $pa_repository_key_factory->handler()
227  ->withObjectId($obj_id);
228  if ($file_info->getPublicAccessEnabled() and $pa_repository->hasElement($key)) {
229  $pa_repository->deleteElement($key);
230  continue;
231  }
232  if (
233  !$export_option->isPublicAccessPossible() or
234  !$file_info->getPublicAccessPossible() or
235  !$type_allowed
236  ) {
237  continue;
238  }
239  $values = $pa_repository_values_factory->handler()
240  ->withIdentification($file_info->getFileIdentifier())
241  ->withExportOptionId($export_option->getExportOptionId());
242  $element = $pa_repository_element_factory->handler()
243  ->withKey($key)
244  ->withValues($values);
245  $pa_repository->storeElement($element);
246  }
247  }
248  $this->ctrl->redirect($this->context->exportGUIObject(), $this->context->exportGUIObject()::CMD_LIST_EXPORT_FILES);
249  }
250 
254  protected function downloadItems(array $ids_sorted): void
255  {
256  foreach ($ids_sorted as $export_option_id => $file_identifiers) {
257  $export_option = $this->export_options->getById($export_option_id);
258  $export_option->onDownloadFiles(
259  $this->context,
260  $file_identifiers
261  );
262  }
263  }
264 
265  protected function initTable(): void
266  {
267  if (isset($this->table)) {
268  return;
269  }
270  $this->table = $this->ui_services->factory()->table()->data(
271  $this->export_handler->table()->dataRetrieval()
272  ->withExportOptions($this->export_options)
273  ->withExportObject($this->context->exportObject())
274  ->withExportGUI($this->context->exportGUIObject()),
275  $this->lng->txt("exp_export_files"),
276  $this->getColumns(),
277  )
278  ->withId(self::TABLE_ID)
279  ->withActions($this->getActions())
280  ->withRequest($this->http_services->request());
281  }
282 
286  protected function readIdsFromExportOptions(): array
287  {
288  $ids_sorted = [];
289  $key = $this->export_handler->repository()->key()->handler()
290  ->withObjectId($this->data_factory->objId($this->context->exportObject()->getId()));
291  $key_collection = $this->export_handler->repository()->key()->collection()
292  ->withElement($key);
293  $elements = $this->export_handler->repository()->handler()->getElements($key_collection);
294  foreach ($this->export_options as $export_option) {
295  $export_option_id = $export_option->getExportOptionId();
296  $ids_sorted[$export_option_id] = $this->export_handler->consumer()->file()->identifier()->collection();
297  foreach ($export_option->getFiles($this->context) as $file_info) {
298  $file_identifier = $this->export_handler->consumer()->file()->identifier()->handler()
299  ->withIdentifier($file_info->getFileIdentifier());
300  $ids_sorted[$export_option_id] = $ids_sorted[$export_option_id]
301  ->withElement($file_identifier);
302  }
303  }
304  return $ids_sorted;
305  }
306 
310  protected function readIdsFromQuery(): array
311  {
312  $tokens = $this->http_services->wrapper()->query()->retrieve(
313  $this->row_id_token->getName(),
314  $this->refinery->custom()->transformation(fn($v) => $v)
315  );
316  $composit_ids = is_array($tokens) ? $tokens : [$tokens];
317  $ids_sorted = [];
318  foreach ($composit_ids as $composit_id) {
319  $table_row_id = $this->export_handler->table()->rowId()->handler()
320  ->withCompositId($composit_id);
321  $file_identifier = $this->export_handler->consumer()->file()->identifier()->handler()
322  ->withIdentifier($table_row_id->getFileIdentifier());
323  $export_option = $this->export_options->getById($table_row_id->getExportOptionId());
324  if (!isset($ids_sorted[$table_row_id->getExportOptionId()])) {
325  $ids_sorted[$table_row_id->getExportOptionId()] = $this->export_handler->consumer()->file()->identifier()->collection();
326  }
327  $ids_sorted[$table_row_id->getExportOptionId()] = $ids_sorted[$table_row_id->getExportOptionId()]
328  ->withElement($file_identifier);
329  }
330  return $ids_sorted;
331  }
332 
333  public function handleCommands(): void
334  {
335  $this->initTable();
336  if (!$this->http_services->wrapper()->query()->has($this->action_parameter_token->getName())) {
337  return;
338  }
339  $action = $this->http_services->wrapper()->query()->retrieve(
340  $this->action_parameter_token->getName(),
341  $this->refinery->to()->string()
342  );
343  $tokens = $this->http_services->wrapper()->query()->retrieve(
344  $this->row_id_token->getName(),
345  $this->refinery->custom()->transformation(fn($v) => $v)
346  );
347  $all_entries = ($tokens[0] ?? "") === self::ALL_OBJECTS;
348  $ids_sorted = [];
349  if ($all_entries) {
350  $ids_sorted = $this->readIdsFromExportOptions();
351  }
352  if (!$all_entries) {
353  $ids_sorted = $this->readIdsFromQuery();
354  }
355  switch ($action) {
356  case self::ACTION_PUBLIC_ACCESS:
357  $this->markAsPublicAccess($ids_sorted);
358  break;
359  case self::ACTION_DOWNLOAD:
360  $this->downloadItems($ids_sorted);
361  break;
362  case self::ACTION_DELETE:
363  $this->showDeleteModal($ids_sorted);
364  break;
365  case self::ACTION_CONFIRM_DELETE:
366  $this->deleteItems($ids_sorted);
367  break;
368  }
369  }
370 
371  public function getHTML(): string
372  {
373  $this->initTable();
374  return $this->ui_services->renderer()->render([$this->table]);
375  }
376 
377  public function withExportOptions(
378  ilExportHandlerConsumerExportOptionCollectionInterface $export_options
379  ): ilExportHandlerTableInterface {
380  $clone = clone $this;
381  $clone->export_options = $export_options;
382  return $clone;
383  }
384 
385  public function withContext(ilExportHandlerConsumerContextInterface $context): ilExportHandlerTableInterface
386  {
387  $clone = clone $this;
388  $clone->context = $context;
389  return $clone;
390  }
391 }
withContext(ilExportHandlerConsumerContextInterface $context)
Definition: Handler.php:385
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilURLBuilderToken $action_parameter_token
Definition: Handler.php:69
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(ilUIServices $ui_services, ilHTTPServices $http_services, ilRefineryFactory $refinery, ilObjUser $user, ilLanguage $lng, ilCtrl $ctrl, ilExportHandler $export_handler, ilDataFactory $data_factory)
Definition: Handler.php:75
ilExportHandlerConsumerExportOptionCollectionInterface $export_options
Definition: Handler.php:71
withExportOptions(ilExportHandlerConsumerExportOptionCollectionInterface $export_options)
Definition: Handler.php:377
URLBuilder.
Definition: URLBuilder.php:40
exit
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
ilExportHandlerConsumerContextInterface $context
Definition: Handler.php:73