ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
RequestToDataTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
34 
39 {
40  use Formatter;
41  use URLSerializer;
42 
43  public const F_TITLE = 'title';
44  public const F_SIZE = 'size';
45  public const F_TYPE = 'type';
46  public const F_MODIFICATION_DATE = 'create_date';
47  public const FIELD_TITLE = 'title';
48  public const HOME = 'HOME';
49  private \ILIAS\Data\Factory $data_factory;
50  private \ILIAS\ResourceStorage\Services $irss;
51  private \ILIAS\UI\Renderer $ui_renderer;
52  private \ilCtrlInterface $ctrl;
56  private array $actions;
57 
58  public function __construct(
59  private Request $request,
60  private Factory $ui_factory,
61  private \ilLanguage $language,
62  private Services $http,
63  private TableDataProvider $data_provider,
64  private ActionBuilder $action_builder,
65  private ViewControlBuilder $view_control_builder,
66  private UploadBuilder $upload_builder
67  ) {
68  global $DIC;
69  $this->data_factory = new \ILIAS\Data\Factory();
70  $this->ui_renderer = $DIC->ui()->renderer();
71  $this->ctrl = $DIC->ctrl();
72  $this->actions = $this->action_builder->getActionProvider()->getSingleActions(
73  $this->request
74  );
75  }
76 
77  protected function buildTopActions(): \ILIAS\UI\Component\Dropdown\Standard
78  {
79  $buttons = [];
80  if ($this->request->canUserAdministrate()) {
81  foreach ($this->action_builder->getActionProvider()->getTopActions() as $top_action) {
82  if ($top_action->getAction() instanceof Signal) {
83  $button = $this->ui_factory->button()->shy(
84  $top_action->getLabel(),
85  '#'
86  )->withOnClick($top_action->getAction());
87  } else {
88  $button = $this->ui_factory->button()->shy(
89  $top_action->getLabel(),
90  (string) $top_action->getAction()
91  );
92  }
93 
94  $buttons[] = $button;
95  }
96  }
97  return $this->ui_factory->dropdown()->standard($buttons);
98  }
99 
100  protected function getBreadcrumbs(): \Generator
101  {
102  $get_action = function (string $path_inside_zip): string {
103  $this->ctrl->setParameterByClass(
104  \ilContainerResourceGUI::class,
106  $this->hash($path_inside_zip)
107  );
108  return $this->ctrl->getLinkTargetByClass(
109  \ilContainerResourceGUI::class,
111  );
112  };
113 
114  $links = [];
115  // Link to Root Directory
116  $links[] = $this->ui_factory->link()->standard(
117  $this->language->txt('home_directory'),
118  $get_action('./')
119  );
120 
121  // Links to current directory and all parent directories
122  if ($this->request->getPath() !== './') {
123  $directories = array_filter(
124  explode('/', $this->request->getPath()),
125  static fn(string $part) => $part !== ''
126  );
127 
128  foreach ($directories as $i => $directory) {
129  $path_inside_zip = rtrim(
130  implode('/', array_slice($directories, 0, $i + 1)),
131  '/'
132  ) . '/';
133  $links[] = $this->ui_factory->link()->standard(
134  $directory,
135  $get_action($path_inside_zip)
136  );
137  }
138  }
139  yield $this->ui_factory->divider()->horizontal();
140 
141  yield $this->ui_factory->breadcrumbs($links);
142  }
143 
144  public function getComponents(): \Generator
145  {
146  // build top actions here
147  $dropdown = $this->buildTopActions();
148 
149  yield $this->ui_factory->panel()->standard(
150  $this->language->txt('title_manage_container'),
151  array_merge(
152  iterator_to_array($this->upload_builder->getDropZone()),
153  iterator_to_array($this->getBreadcrumbs()),
154  )
155  )->withActions($dropdown);
156 
157  yield $this->buildTable();
158  }
159 
163  protected function buildTable(): \ILIAS\UI\Component\Table\Data
164  {
165  return $this->ui_factory->table()->data(
166  $this->request->getTitle(), // we already have the title in the panel
167  [
168  self::F_TITLE => $this->ui_factory->table()->column()->text(
169  $this->language->txt(self::F_TITLE)
170  )->withIsSortable(true),
171  self::F_SIZE => $this->ui_factory->table()->column()->text(
172  $this->language->txt(self::F_SIZE)
173  )->withIsSortable(true),
174  self::F_MODIFICATION_DATE => $this->ui_factory->table()->column()->date(
175  $this->language->txt(self::F_MODIFICATION_DATE),
176  $this->data_factory->dateFormat()->germanLong()
177  )->withIsSortable(true),
178  self::F_TYPE => $this->ui_factory->table()->column()->text(
179  $this->language->txt(self::F_TYPE)
180  )->withIsSortable(true),
181  ],
182  $this
183  )->withRequest(
184  $this->http->request()
185  )->withActions(
186  $this->action_builder->getActions()
187  )->withRange(
188  new Range(0, $this->request->getItemsPerPage())
189  );
190  }
191 
192  public function getRows(
193  DataRowBuilder $row_builder,
194  array $visible_column_ids,
195  Range $range,
196  Order $order,
197  ?array $filter_data,
198  ?array $additional_parameters
199  ): \Generator {
200  $this->initSortingAndOrdering($range, $order);
201 
202  $regex_storage = [];
203 
204  $entries = $this->data_provider->getEntries();
205  // cut entries
206  $entries = array_slice(
207  $entries,
208  $range->getStart(),
209  $range->getLength()
210  );
211 
212  foreach ($entries as $entry) {
213  $is_dir = $entry instanceof \ILIAS\components\ResourceStorage\Container\Wrapper\Dir;
214  $path_inside_zip = $entry->getPathInsideZIP();
215 
216  $entry_name = trim((string) $entry, '/');
217 
218  // needed for links in table
219  $this->ctrl->setParameterByClass(
220  \ilContainerResourceGUI::class,
222  $this->hash($path_inside_zip)
223  );
224 
225  $action = $this->ctrl->getLinkTargetByClass(
226  \ilContainerResourceGUI::class,
228  );
229 
230  $title = $is_dir
231  ? $this->ui_renderer->render(
232  $this->ui_factory->link()->standard($entry_name, $action)
233  )
234  : $entry_name;
235 
236  $data_row = $row_builder->buildDataRow(
237  $this->hash($entry->getPathInsideZIP()),
238  [
239  self::F_TITLE => $title,
240  self::F_SIZE => $is_dir ? '' : $this->formatSize($entry->getSize()),
241  self::F_TYPE => $is_dir ? '' : $entry->getMimeType(),
242  self::F_MODIFICATION_DATE => $entry->getModificationDate(),
243  ]
244  );
245 
246  foreach ($this->actions as $key => $single_action) {
247  if ($is_dir && !$single_action->supportsDirectories()) {
248  $data_row = $data_row->withDisabledAction($key);
249  }
250 
251  if ($single_action->getSupportedMimeTypes() !== ['*']) {
252  if ($is_dir) {
253  $data_row = $data_row->withDisabledAction($key);
254  } else {
255  if (isset($regex_storage[$key])) {
256  $regex = $regex_storage[$key];
257  } else {
258  $mime_type_quoted = [];
259  foreach ($single_action->getSupportedMimeTypes() as $mime_type) {
260  $mime_type_quoted[] = str_replace('*', '.*', preg_quote($mime_type, '/'));
261  }
262 
263  $regex_storage[$key] = $regex = implode('|', $mime_type_quoted);
264  }
265  if (!preg_match("/($regex)/", $entry->getMimeType())) {
266  $data_row = $data_row->withDisabledAction($key);
267  }
268  }
269  }
270  }
271  yield $data_row;
272  }
273  }
274 
275  private function initSortingAndOrdering(Range $range, Order $order): void
276  {
277  $sort_field = array_keys($order->get())[0];
278  $sort_direction = $order->get()[$sort_field];
279 
280  $start = $range->getStart();
281  $length = $range->getLength();
282  $this->data_provider->getViewRequest()->setPage((int) round($start / $length, 0, PHP_ROUND_HALF_DOWN));
283  $this->data_provider->getViewRequest()->setItemsPerPage($length);
284 
285  switch ($sort_field . '_' . $sort_direction) {
286  case self::F_TITLE . '_' . Order::ASC:
287  $this->data_provider->getViewRequest()->setSortation(Request::BY_TITLE_ASC);
288  break;
289  case self::F_TITLE . '_' . Order::DESC:
290  $this->data_provider->getViewRequest()->setSortation(Request::BY_TITLE_DESC);
291  break;
292  case self::F_SIZE . '_' . Order::ASC:
293  $this->data_provider->getViewRequest()->setSortation(Request::BY_SIZE_ASC);
294  break;
295  case self::F_SIZE . '_' . Order::DESC:
296  $this->data_provider->getViewRequest()->setSortation(Request::BY_SIZE_DESC);
297  break;
298  case self::F_MODIFICATION_DATE . '_' . Order::ASC:
299  $this->data_provider->getViewRequest()->setSortation(Request::BY_CREATION_DATE_ASC);
300  break;
301  case self::F_MODIFICATION_DATE . '_' . Order::DESC:
302  $this->data_provider->getViewRequest()->setSortation(Request::BY_CREATION_DATE_DESC);
303  break;
304  case self::F_TYPE . '_' . Order::ASC:
305  $this->data_provider->getViewRequest()->setSortation(Request::BY_TYPE_ASC);
306  break;
307  case self::F_TYPE . '_' . Order::DESC:
308  $this->data_provider->getViewRequest()->setSortation(Request::BY_TYPE_DESC);
309  break;
310  }
311  }
312 
313  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
314  {
315  return $this->data_provider->getTotal();
316  }
317 }
getRows(DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e...
Interface Observer Contains several chained tasks and infos about them.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$http
Definition: deliver.php:14
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:12
buildDataRow(string $id, array $record)
static http()
Fetches the global http state from ILIAS.
This is how the factory for UI elements looks.
Definition: Factory.php:37
global $DIC
Definition: shib_login.php:25
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
__construct(private Request $request, private Factory $ui_factory, private \ilLanguage $language, private Services $http, private TableDataProvider $data_provider, private ActionBuilder $action_builder, private ViewControlBuilder $view_control_builder, private UploadBuilder $upload_builder)
language()
description: > Example for rendring a language glyph.
Definition: language.php:25
const DESC
Definition: Order.php:15
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28