ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
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 F_TITLE = 'title';
46 public const F_SIZE = 'size';
47 public const F_TYPE = 'type';
48 public const F_MODIFICATION_DATE = 'create_date';
49 public const FIELD_TITLE = 'title';
50 public const HOME = 'HOME';
51 private \ILIAS\Data\Factory $data_factory;
52 private \ILIAS\ResourceStorage\Services $irss;
54 private \ilCtrlInterface $ctrl;
58 private array $actions;
59
60 public function __construct(
61 private Request $request,
62 private Factory $ui_factory,
63 private \ilLanguage $language,
64 private Services $http,
65 private TableDataProvider $data_provider,
66 private ActionBuilder $action_builder,
67 private ViewControlBuilder $view_control_builder,
68 private UploadBuilder $upload_builder
69 ) {
70 global $DIC;
71 $this->data_factory = new \ILIAS\Data\Factory();
72 $this->ui_renderer = $DIC->ui()->renderer();
73 $this->ctrl = $DIC->ctrl();
74 $this->actions = $this->action_builder->getActionProvider()->getSingleActions(
75 $this->request
76 );
77 }
78
79 protected function buildTopActions(): Standard
80 {
81 $buttons = [];
82 if ($this->request->canUserAdministrate()) {
83 foreach ($this->action_builder->getActionProvider()->getTopActions() as $top_action) {
84 if ($top_action->getAction() instanceof Signal) {
85 $button = $this->ui_factory->button()->shy(
86 $top_action->getLabel(),
87 '#'
88 )->withOnClick($top_action->getAction());
89 } else {
90 $button = $this->ui_factory->button()->shy(
91 $top_action->getLabel(),
92 (string) $top_action->getAction()
93 );
94 }
95
96 $buttons[] = $button;
97 }
98 }
99 return $this->ui_factory->dropdown()->standard($buttons);
100 }
101
102 protected function getBreadcrumbs(): \Generator
103 {
104 $get_action = function (string $path_inside_zip): string {
105 $this->ctrl->setParameterByClass(
106 \ilContainerResourceGUI::class,
108 $this->hash($path_inside_zip)
109 );
110 return $this->ctrl->getLinkTargetByClass(
111 \ilContainerResourceGUI::class,
113 );
114 };
115
116 $links = [];
117 // Link to Root Directory
118 $links[] = $this->ui_factory->link()->standard(
119 $this->language->txt('home_directory'),
120 $get_action('./')
121 );
122
123 // Links to current directory and all parent directories
124 if ($this->request->getPath() !== './') {
125 $directories = array_filter(
126 explode('/', $this->request->getPath()),
127 static fn(string $part): bool => $part !== ''
128 );
129
130 foreach ($directories as $i => $directory) {
131 $path_inside_zip = rtrim(
132 implode('/', array_slice($directories, 0, $i + 1)),
133 '/'
134 ) . '/';
135 $links[] = $this->ui_factory->link()->standard(
136 $directory,
137 $get_action($path_inside_zip)
138 );
139 }
140 }
141 yield $this->ui_factory->divider()->horizontal();
142
143 yield $this->ui_factory->breadcrumbs($links);
144 }
145
146 public function getComponents(): \Generator
147 {
148 // build top actions here
149 $dropdown = $this->buildTopActions();
150
151 yield $this->ui_factory->panel()->standard(
152 $this->language->txt('title_manage_container'),
153 array_merge(
154 iterator_to_array($this->upload_builder->getDropZone()),
155 iterator_to_array($this->getBreadcrumbs()),
156 )
157 )->withActions($dropdown);
158
159 yield $this->buildTable();
160 }
161
165 protected function buildTable(): Data
166 {
167 return $this->ui_factory->table()->data(
168 $this,
169 $this->request->getTitle(), // we already have the title in the panel
170 [
171 self::F_TITLE => $this->ui_factory->table()->column()->text(
172 $this->language->txt(self::F_TITLE)
173 )->withIsSortable(true),
174 self::F_SIZE => $this->ui_factory->table()->column()->text(
175 $this->language->txt(self::F_SIZE)
176 )->withIsSortable(true),
177 self::F_MODIFICATION_DATE => $this->ui_factory->table()->column()->date(
178 $this->language->txt(self::F_MODIFICATION_DATE),
179 $this->data_factory->dateFormat()->germanLong()
180 )->withIsSortable(true),
181 self::F_TYPE => $this->ui_factory->table()->column()->text(
182 $this->language->txt(self::F_TYPE)
183 )->withIsSortable(true),
184 ],
185 )->withRequest(
186 $this->http->request()
187 )->withActions(
188 $this->action_builder->getActions()
189 )->withRange(
190 new Range(0, $this->request->getItemsPerPage())
191 );
192 }
193
194 public function getRows(
195 DataRowBuilder $row_builder,
196 array $visible_column_ids,
198 Order $order,
199 mixed $additional_viewcontrol_data,
200 mixed $filter_data,
201 mixed $additional_parameters
202 ): \Generator {
203 $this->initSortingAndOrdering($range, $order);
204
205 $regex_storage = [];
206
207 $entries = $this->data_provider->getEntries();
208 // cut entries
209 $entries = array_slice(
210 $entries,
211 $range->getStart(),
213 );
214
215 foreach ($entries as $entry) {
216 $is_dir = $entry instanceof Dir;
217 $path_inside_zip = $entry->getPathInsideZIP();
218
219 $entry_name = trim((string) $entry, '/');
220
221 // needed for links in table
222 $this->ctrl->setParameterByClass(
223 \ilContainerResourceGUI::class,
225 $this->hash($path_inside_zip)
226 );
227
228 $action = $this->ctrl->getLinkTargetByClass(
229 \ilContainerResourceGUI::class,
231 );
232
233 $title = $is_dir
234 ? $this->ui_renderer->render(
235 $this->ui_factory->link()->standard($entry_name, $action)
236 )
237 : $entry_name;
238
239 $data_row = $row_builder->buildDataRow(
240 $this->hash($entry->getPathInsideZIP()),
241 [
242 self::F_TITLE => $title,
243 self::F_SIZE => $is_dir ? '' : $this->formatSize($entry->getSize()),
244 self::F_TYPE => $is_dir ? '' : $entry->getMimeType(),
245 self::F_MODIFICATION_DATE => $entry->getModificationDate(),
246 ]
247 );
248
249 foreach ($this->actions as $key => $single_action) {
250 if ($is_dir && !$single_action->supportsDirectories()) {
251 $data_row = $data_row->withDisabledAction($key);
252 }
253
254 if ($single_action->getSupportedMimeTypes() !== ['*']) {
255 if ($is_dir) {
256 $data_row = $data_row->withDisabledAction($key);
257 } else {
258 if (isset($regex_storage[$key])) {
259 $regex = $regex_storage[$key];
260 } else {
261 $mime_type_quoted = [];
262 foreach ($single_action->getSupportedMimeTypes() as $mime_type) {
263 $mime_type_quoted[] = str_replace('*', '.*', preg_quote((string) $mime_type, '/'));
264 }
265
266 $regex_storage[$key] = $regex = implode('|', $mime_type_quoted);
267 }
268 if (!preg_match("/($regex)/", $entry->getMimeType())) {
269 $data_row = $data_row->withDisabledAction($key);
270 }
271 }
272 }
273 }
274 yield $data_row;
275 }
276 }
277
278 private function initSortingAndOrdering(Range $range, Order $order): void
279 {
280 $sort_field = array_keys($order->get())[0];
281 $sort_direction = $order->get()[$sort_field];
282
283 $start = $range->getStart();
284 $length = $range->getLength();
285 $this->data_provider->getViewRequest()->setPage((int) round($start / $length, 0, \RoundingMode::HalfTowardsZero));
286 $this->data_provider->getViewRequest()->setItemsPerPage($length);
287
288 switch ($sort_field . '_' . $sort_direction) {
289 case self::F_TITLE . '_' . Order::ASC:
290 $this->data_provider->getViewRequest()->setSortation(Request::BY_TITLE_ASC);
291 break;
292 case self::F_TITLE . '_' . Order::DESC:
293 $this->data_provider->getViewRequest()->setSortation(Request::BY_TITLE_DESC);
294 break;
295 case self::F_SIZE . '_' . Order::ASC:
296 $this->data_provider->getViewRequest()->setSortation(Request::BY_SIZE_ASC);
297 break;
298 case self::F_SIZE . '_' . Order::DESC:
299 $this->data_provider->getViewRequest()->setSortation(Request::BY_SIZE_DESC);
300 break;
301 case self::F_MODIFICATION_DATE . '_' . Order::ASC:
302 $this->data_provider->getViewRequest()->setSortation(Request::BY_CREATION_DATE_ASC);
303 break;
304 case self::F_MODIFICATION_DATE . '_' . Order::DESC:
305 $this->data_provider->getViewRequest()->setSortation(Request::BY_CREATION_DATE_DESC);
306 break;
307 case self::F_TYPE . '_' . Order::ASC:
308 $this->data_provider->getViewRequest()->setSortation(Request::BY_TYPE_ASC);
309 break;
310 case self::F_TYPE . '_' . Order::DESC:
311 $this->data_provider->getViewRequest()->setSortation(Request::BY_TYPE_DESC);
312 break;
313 }
314 }
315
316 public function getTotalRowCount(
317 mixed $additional_viewcontrol_data,
318 mixed $filter_data,
319 mixed $additional_parameters
320 ): ?int {
321 return $this->data_provider->getTotal();
322 }
323}
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