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