ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 ?array $filter_data,
200 ?array $additional_parameters
201 ): \Generator {
202 $this->initSortingAndOrdering($range, $order);
203
204 $regex_storage = [];
205
206 $entries = $this->data_provider->getEntries();
207 // cut entries
208 $entries = array_slice(
209 $entries,
210 $range->getStart(),
212 );
213
214 foreach ($entries as $entry) {
215 $is_dir = $entry instanceof Dir;
216 $path_inside_zip = $entry->getPathInsideZIP();
217
218 $entry_name = trim((string) $entry, '/');
219
220 // needed for links in table
221 $this->ctrl->setParameterByClass(
222 \ilContainerResourceGUI::class,
224 $this->hash($path_inside_zip)
225 );
226
227 $action = $this->ctrl->getLinkTargetByClass(
228 \ilContainerResourceGUI::class,
230 );
231
232 $title = $is_dir
233 ? $this->ui_renderer->render(
234 $this->ui_factory->link()->standard($entry_name, $action)
235 )
236 : $entry_name;
237
238 $data_row = $row_builder->buildDataRow(
239 $this->hash($entry->getPathInsideZIP()),
240 [
241 self::F_TITLE => $title,
242 self::F_SIZE => $is_dir ? '' : $this->formatSize($entry->getSize()),
243 self::F_TYPE => $is_dir ? '' : $entry->getMimeType(),
244 self::F_MODIFICATION_DATE => $entry->getModificationDate(),
245 ]
246 );
247
248 foreach ($this->actions as $key => $single_action) {
249 if ($is_dir && !$single_action->supportsDirectories()) {
250 $data_row = $data_row->withDisabledAction($key);
251 }
252
253 if ($single_action->getSupportedMimeTypes() !== ['*']) {
254 if ($is_dir) {
255 $data_row = $data_row->withDisabledAction($key);
256 } else {
257 if (isset($regex_storage[$key])) {
258 $regex = $regex_storage[$key];
259 } else {
260 $mime_type_quoted = [];
261 foreach ($single_action->getSupportedMimeTypes() as $mime_type) {
262 $mime_type_quoted[] = str_replace('*', '.*', preg_quote((string) $mime_type, '/'));
263 }
264
265 $regex_storage[$key] = $regex = implode('|', $mime_type_quoted);
266 }
267 if (!preg_match("/($regex)/", $entry->getMimeType())) {
268 $data_row = $data_row->withDisabledAction($key);
269 }
270 }
271 }
272 }
273 yield $data_row;
274 }
275 }
276
277 private function initSortingAndOrdering(Range $range, Order $order): void
278 {
279 $sort_field = array_keys($order->get())[0];
280 $sort_direction = $order->get()[$sort_field];
281
282 $start = $range->getStart();
283 $length = $range->getLength();
284 $this->data_provider->getViewRequest()->setPage((int) round($start / $length, 0, \RoundingMode::HalfTowardsZero));
285 $this->data_provider->getViewRequest()->setItemsPerPage($length);
286
287 switch ($sort_field . '_' . $sort_direction) {
288 case self::F_TITLE . '_' . Order::ASC:
289 $this->data_provider->getViewRequest()->setSortation(Request::BY_TITLE_ASC);
290 break;
291 case self::F_TITLE . '_' . Order::DESC:
292 $this->data_provider->getViewRequest()->setSortation(Request::BY_TITLE_DESC);
293 break;
294 case self::F_SIZE . '_' . Order::ASC:
295 $this->data_provider->getViewRequest()->setSortation(Request::BY_SIZE_ASC);
296 break;
297 case self::F_SIZE . '_' . Order::DESC:
298 $this->data_provider->getViewRequest()->setSortation(Request::BY_SIZE_DESC);
299 break;
300 case self::F_MODIFICATION_DATE . '_' . Order::ASC:
301 $this->data_provider->getViewRequest()->setSortation(Request::BY_CREATION_DATE_ASC);
302 break;
303 case self::F_MODIFICATION_DATE . '_' . Order::DESC:
304 $this->data_provider->getViewRequest()->setSortation(Request::BY_CREATION_DATE_DESC);
305 break;
306 case self::F_TYPE . '_' . Order::ASC:
307 $this->data_provider->getViewRequest()->setSortation(Request::BY_TYPE_ASC);
308 break;
309 case self::F_TYPE . '_' . Order::DESC:
310 $this->data_provider->getViewRequest()->setSortation(Request::BY_TYPE_DESC);
311 break;
312 }
313 }
314
315 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
316 {
317 return $this->data_provider->getTotal();
318 }
319}
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, ?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....
__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)
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...
language handling
$http
Definition: deliver.php:30
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:31
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