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