ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
Table.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use iljQueryUtil;
24use Closure;
28use ilTable2GUI;
30use InvalidArgumentException;
33
34class Table extends ilTable2GUI implements TableSelection
35{
37 private readonly array $columns;
39 private array $sel = [];
40
41 public function __construct(?object $gui, string $command, TableInterface $table)
42 {
43 global $DIC;
44 $apply = fn($proc) => fn(array $args) => $proc(...$args);
45 $translate = fn(string $txt, ...$args) => [$txt, ...$args];
46
47 $id = substr(md5($table->name()), 0, 30);
48 $this->setId($id);
49 $this->columns = array_map($apply($translate), $table->columns());
50 $this->setFormName($id);
51 $config = new SmoothTableConfig($this);
52 $table->config($config);
53 parent::__construct($gui, $command);
54 $config->flush();
55 $this->setFormAction($this->ctrl->getFormAction($this->getParentObject(), $command));
56 $this->setRowTemplate('legacy-table-row.html', 'components/ILIAS/LegalDocuments');
57 array_map($apply($this->addColumn(...)), $this->visibleColumns());
58 $this->setShowRowsSelector(false);
59 $this->setExternalSorting(true);
60 $this->setExternalSegmentation(true);
61 iljQueryUtil::initjQuery($DIC->ui()->mainTemplate());
62 $DIC->ui()->mainTemplate()->addJavaScript('assets/js/Form.js');
64 $this->setData($table->rows($this));
65 }
66
67 public function setMaxCount(int $a_max_count): void
68 {
69 $this->setShowRowsSelector(true);
70 parent::setMaxCount($a_max_count);
71 }
72
73 public function setSelectableColumns(string ...$names): void
74 {
75 $this->sel = array_merge($this->sel, $names);
76 }
77
78 public function selection(): array
79 {
80 return array_flip($this->sel);
81 }
82
83 public function getSelectableColumns(): array
84 {
85 return array_map(fn($x) => ['txt' => $x[0]], array_intersect_key(
86 $this->columns,
87 $this->selection()
88 ));
89 }
90
91 public function filter(): array
92 {
93 return array_column(array_map(
94 fn($input) => [
95 $input->getPostVar(),
96 $input->getValue()
97 ],
98 $this->filterInputs()
99 ), 1, 0);
100 }
101
102 public function render(): string
103 {
104 return parent::render() . $this->renderModals();
105 }
106
107 protected function isColumnVisible(int $index): bool
108 {
109 return true;
110 }
111
112 private function visibleColumns(): array
113 {
114 $restore_key_order = fn($array) => array_intersect_key($this->columns, $array);
115 $base = array_diff_key($this->columns, $this->selection());
116
117 return $restore_key_order(array_merge($base, array_intersect_key($this->columns, $this->getSelectedColumns())));
118 }
119
123 private function resetParameters(array $parameters): void
124 {
125 $this->applyParamters(array_map(static fn(): string => '', $parameters));
126 }
127
131 private function applyParamters(array $parameters): void
132 {
133 foreach ($parameters as $key => $value) {
134 $this->ctrl->setParameter($this->getParentObject(), $key, $value);
135 }
136 }
137
138 protected function fillRow(array $a_set): void
139 {
140 $this->requireKeys(array_keys($this->columns), $a_set);
141 $set = array_intersect_key($a_set, $this->visibleColumns());
142 $this->tpl->setVariable('VALUE', join('', array_map($this->tableCellOfField(...), $set)));
143 }
144
145 protected function tableCellOfField($x): string
146 {
147 return sprintf('<td>%s</td>', $this->asString($x));
148 }
149
153 private function asString($x): string
154 {
155 $is_component = fn($x): bool => $x instanceof Component;
156
157 if ($is_component($x) || (is_array($x) && array_filter($x, fn($x) => !$is_component($x)) === [])) {
158 global $DIC;
159 return $DIC->ui()->renderer()->render($this->removeModals($x));
160 } elseif ($x instanceof Closure) {
161 return $x();
162 } elseif (is_string($x)) {
163 return htmlentities($x);
164 }
165
166 throw new InvalidArgumentException('Value must be either: Component|list<Component>|Closure|string. Given: ' . var_export($x, true));
167 }
168
169 protected function txt(string $key): string
170 {
171 return $key === '' ? '' : $this->lng->txt($key);
172 }
173
178 private function requireKeys(array $required, array $given): void
179 {
180 $given = array_keys($given);
181 $missing = $this->intersect($required, $this->diff($required, $given));
182 if ([] !== $missing) {
183 throw new InvalidArgumentException('Missing keys: ' . join(', ', $missing));
184 }
185 }
186
187 private function diff(array $a, array $b): array
188 {
189 return array_filter($a, fn($x) => !$this->has($x, $b));
190 }
191
192 private function intersect(array $a, array $b): array
193 {
194 return array_filter($a, fn($x) => $this->has($x, $b));
195 }
196
197 private function has($x, array $array): bool
198 {
199 return in_array($x, $array, true);
200 }
201
202 private function filterInputs(): array
203 {
204 return [
205 ...$this->getFilterItems(),
206 ...$this->getFilterItems(true),
207 ];
208 }
209
210 public function setupFilter(string $reset_command): void
211 {
212 global $DIC;
213 $this->initFilter();
214 $this->setFilterCommand($this->getParentCmd());
215 $this->setResetCommand($reset_command);
217 if ($DIC->ctrl()->getCmd() === $reset_command) {
218 $this->resetFilter();
219 } elseif (strtoupper($DIC->http()->request()->getMethod()) === 'POST') {
220 $this->writeFilterToSession();
221 } else {
222 $read = static fn(ilFormPropertyGUI $x) => $x->readFromSession();
223 array_map($read, $this->getFilterItems());
224 array_map($read, array_filter($this->getFilterItems(true), fn($x) => $this->isFilterSelected($x->getPostVar())));
225 }
226 }
227
228 private function renderModals(): string
229 {
230 global $DIC;
231
232 return $DIC->ui()->renderer()->render($this->flatMap(
233 fn($x) => $this->flatMap(fn($x) => array_filter($this->asArray($x), $this->isModal(...)), $x),
234 $this->getData()
235 ));
236 }
237
238 private function removeModals($x): array
239 {
240 return array_filter($this->asArray($x), fn($x) => !$this->isModal($x));
241 }
242
243 private function flatMap(callable $proc, array $a): array
244 {
245 return array_merge(...array_values(array_map($proc, $a)));
246 }
247
248 private function asArray($x): array
249 {
250 return is_array($x) ? $x : [$x];
251 }
252
253 private function isModal($x): bool
254 {
255 return $x instanceof Modal;
256 }
257}
requireKeys(array $required, array $given)
Definition: Table.php:178
__construct(?object $gui, string $command, TableInterface $table)
Definition: Table.php:41
resetParameters(array $parameters)
Definition: Table.php:123
setMaxCount(int $a_max_count)
Definition: Table.php:67
getSelectableColumns()
Get selectable columns.
Definition: Table.php:83
diff(array $a, array $b)
Definition: Table.php:187
setupFilter(string $reset_command)
Definition: Table.php:210
flatMap(callable $proc, array $a)
Definition: Table.php:243
intersect(array $a, array $b)
Definition: Table.php:192
applyParamters(array $parameters)
Definition: Table.php:131
fillRow(array $a_set)
Standard Version of Fill Row.
Definition: Table.php:138
setSelectableColumns(string ... $names)
Definition: Table.php:73
This class takes care of the order in which the methods must be called.
This class represents a property in a property form.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isFilterSelected(string $a_col)
Is given filter selected?
setShowRowsSelector(bool $a_value)
Toggle rows-per-page selector.
setFilterCommand(string $a_val, string $a_caption="")
determineOffsetAndOrder(bool $a_omit_offset=false)
setExternalSegmentation(bool $a_val)
setFormName(string $a_name="")
setFormAction(string $a_form_action, bool $a_multipart=false)
addColumn(string $a_text, string $a_sort_field="", string $a_width="", bool $a_is_checkbox_action_column=false, string $a_class="", string $a_tooltip="", bool $a_tooltip_with_html=false)
getFilterItems(bool $a_optionals=false)
setExternalSorting(bool $a_val)
setRowTemplate(string $a_template, string $a_template_dir="")
Set row template.
setId(string $a_val)
setResetCommand(string $a_val, string $a_caption="")
setData(array $a_data)
Set table data.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static initjQuery(?ilGlobalTemplateInterface $a_tpl=null)
inits and adds the jQuery JS-File to the global or a passed template
A component is the most general form of an entity in the UI.
Definition: Component.php:28
This describes commonalities between the different modals.
Definition: Modal.php:35
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26