ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
TableAdapterGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
32{
34
35 protected const int STANDARD = 0;
36 protected const int SINGLE = 1;
37 protected const int MULTI = 2;
38 protected string $order_cmd = "";
39 protected string $last_action_key;
43 protected \ILIAS\Data\Factory $df;
44
45 protected \ilLanguage $lng;
46 protected ?Table $table = null;
47 protected string $last_key;
48 protected \ilCtrlInterface $ctrl;
49 protected \ILIAS\DI\UIServices $ui;
50 protected \ilObjUser $user;
51 protected array $columns = [];
52 protected array $actions = [];
53 protected array $filter_data = [];
54
55 public function __construct(
56 protected string $id,
57 protected string $title,
58 protected RetrievalInterface $retrieval,
59 protected object $parent_gui,
60 protected string $parent_cmd = "tableCommand",
61 protected string $namespace = "",
62 protected string $ordering_cmd = "",
63 protected ?\Closure $active_action_closure = null,
64 protected ?\Closure $row_transformer = null,
65 protected bool $numeric_ids = true
66 ) {
67 global $DIC;
68 $this->ui = $DIC->ui();
69 $this->ctrl = $DIC->ctrl();
70 $this->http = $DIC->http();
71 $this->lng = $DIC->language();
72 $this->refinery = $DIC->refinery();
73 $this->user = $DIC->user();
74 $this->df = new \ILIAS\Data\Factory();
75 $this->initRequest($this->http, $this->refinery);
76 if ($namespace === "") {
77 $this->namespace = $id;
78 }
79 $this->order_cmd = $ordering_cmd;
80
81 $form_action = $this->df->uri(
82 ILIAS_HTTP_PATH . '/' .
83 $this->ctrl->getLinkTarget($this->parent_gui, $this->parent_cmd)
84 );
85 $this->url_builder = new URLBuilder($form_action);
87 $this->url_builder->acquireParameters(
88 [$this->namespace], // namespace
89 "table_action", //this is the actions's parameter name
90 "ids" //this is the parameter name to be used for row-ids
91 );
92
93 }
94
95 public function textColumn(
96 string $key,
97 string $title,
98 bool $sortable = false
99 ): self {
100 $column = $this->ui->factory()->table()->column()->text($title)->withIsSortable($sortable);
101 $this->addColumn($key, $column);
102 return $this;
103 }
104
105 public function iconColumn(
106 string $key,
107 string $title,
108 bool $sortable = false
109 ): self {
110 $column = $this->ui->factory()->table()->column()->statusIcon($title)->withIsSortable($sortable);
111 $this->addColumn($key, $column);
112 return $this;
113 }
114
115 public function dateColumn(
116 string $key,
117 string $title,
118 bool $sortable = false
119 ): self {
120 $column = $this->ui->factory()->table()->column()->date($title, $this->user->getDateTimeFormat())->withIsSortable($sortable);
121 $this->addColumn($key, $column);
122 return $this;
123 }
124
125 public function linkColumn(
126 string $key,
127 string $title,
128 bool $sortable = false
129 ): self {
130 $column = $this->ui->factory()->table()->column()->link($title)->withIsSortable($sortable);
131 $this->addColumn($key, $column);
132 return $this;
133 }
134
135 public function linkListingColumn(
136 string $key,
137 string $title,
138 bool $sortable = false
139 ): self {
140 $column = $this->ui->factory()->table()->column()->linkListing($title)->withIsSortable($sortable);
141 $this->addColumn($key, $column);
142 return $this;
143 }
144
145 public function singleAction(
146 string $action,
147 string $title,
148 bool $async = false
149 ): self {
150 $this->addAction(self::SINGLE, $action, $title, $async);
151 return $this;
152 }
153
154 public function singleRedirectAction(
155 string $action,
156 string $title,
157 array $class_path,
158 string $cmd = "",
159 string $id_param = "",
160 bool $async = false
161 ): self {
162 $this->addAction(self::SINGLE, $action, $title, $async);
163 $act = $this->actions[$this->last_action_key] ?? false;
164 if ($act && $act["type"] === self::SINGLE) {
165 $act["redirect_class_path"] = $class_path;
166 $act["redirect_cmd"] = $cmd;
167 $act["redirect_id_param"] = $id_param;
168 $this->actions[$this->last_action_key] = $act;
169 }
170 return $this;
171 }
172
173 public function standardAction(
174 string $action,
175 string $title
176 ): self {
177 $this->addAction(self::STANDARD, $action, $title);
178 return $this;
179 }
180
181 public function multiAction(
182 string $action,
183 string $title
184 ): self {
185 $this->addAction(self::MULTI, $action, $title);
186 return $this;
187 }
188
192 public function filterData(array $filter_data): self
193 {
194 $this->filter_data = $filter_data;
195 return $this;
196 }
197
198 protected function addAction(int $type, string $action, string $title, bool $async = false): void
199 {
200 $this->actions[$action] = [
201 "type" => $type,
202 "action" => $action,
203 "title" => $title,
204 "async" => $async
205 ];
206 $this->last_action_key = $action;
207 }
208
209 protected function addColumn(string $key, Column $column): void
210 {
211 if ($key === "") {
212 throw new \ilException("Missing Input Key: " . $key);
213 }
214 if (isset($this->columns[$key])) {
215 throw new \ilException("Duplicate Input Key: " . $key);
216 }
217 $this->columns[$key] = $column;
218 $this->last_key = $key;
219 }
220
221 protected function getColumnForKey(string $key): Column
222 {
223 if (!isset($this->columns[$key])) {
224 throw new \ilException("Unknown Key: " . $key);
225 }
226 return $this->columns[$key];
227 }
228
229 protected function getLastColumn(): ?Column
230 {
231 return $this->columns[$this->last_key] ?? null;
232 }
233
234 protected function replaceLastColumn(Column $column): void
235 {
236 if ($this->last_key !== "") {
237 $this->columns[$this->last_key] = $column;
238 }
239 }
240
241 protected function getItemIds(): array
242 {
243 if ($this->numeric_ids) {
244 $ids = $this->intArray($this->row_id_token->getName());
245 } else {
246 $ids = $this->strArray($this->row_id_token->getName());
247 }
248 if (count($ids) > 0) {
249 return $ids; // from table multi action
250 }
251 if ($this->numeric_ids) {
252 $ids = $this->intArray("interruptive_items"); // from confirmation
253 } else {
254 $ids = $this->strArray("interruptive_items"); // from confirmation
255 }
256 if (count($ids) > 0) {
257 return $ids;
258 }
259 return [];
260 }
261
262 public function handleCommand(): bool
263 {
264 $action = $this->str($this->action_parameter_token->getName());
265 if ($action !== "") {
266 if ($this->actions[$action]["type"] === self::SINGLE) {
267 $id = $this->getItemIds()[0];
268 if ($this->actions[$action]["redirect_class_path"] ?? false) {
269 $path = $this->actions[$action]["redirect_class_path"];
270 if ($this->actions[$action]["redirect_id_param"] ?? false) {
271 $this->ctrl->setParameterByClass(
272 $path[count($path) - 1],
273 $this->actions[$action]["redirect_id_param"],
274 $id
275 );
276 }
277 $cmd = $this->actions[$action]["redirect_cmd"] ?? $action;
278 $this->ctrl->redirectByClass($this->actions[$action]["redirect_class_path"], $cmd);
279 }
280 $this->parent_gui->$action($id);
281 return true;
282 } else {
283 $this->parent_gui->$action($this->getItemIds());
284 return true;
285 }
286 }
287 return false;
288 }
289
290 protected function getTable(): Table
291 {
292 $a = $this->ui->factory()->table()->action();
293
294 if (is_null($this->table)) {
295 $columns = [];
296 foreach ($this->columns as $key => $column) {
297 $columns[$key] = $column;
298 }
299 $actions = [];
300 foreach ($this->actions as $act) {
301 switch ($act["type"]) {
302 case self::SINGLE:
303 $actions[$act["action"]] = $a->single(
304 $act["title"],
305 $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
306 $this->row_id_token
307 );
308 break;
309 case self::STANDARD:
310 $actions[$act["action"]] = $a->standard(
311 $act["title"],
312 $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
313 $this->row_id_token
314 );
315 break;
316 case self::MULTI:
317 $actions[$act["action"]] = $a->multi(
318 $act["title"],
319 $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
320 $this->row_id_token
321 );
322 break;
323 }
324 if ($act["async"]) {
325 $actions[$act["action"]] = $actions[$act["action"]]->withAsync(true);
326 }
327 }
328 if ($this->order_cmd !== "") {
329 $uri = $this->df->uri(
330 ILIAS_HTTP_PATH . '/' .
331 $this->ctrl->getLinkTarget($this->parent_gui, $this->order_cmd)
332 );
333 $table_retrieval = new OrderingRetrieval(
334 $this->retrieval,
335 array_keys($actions),
336 $this->active_action_closure,
337 $this->row_transformer
338 );
339 $this->table = $this
340 ->ui
341 ->factory()
342 ->table()
343 ->ordering($table_retrieval, $uri, $this->title, $columns)
344 ->withId($this->id)
345 ->withActions($actions)
346 ->withRequest($this->http->request());
347 } else {
348 $table_retrieval = new TableRetrieval(
349 $this->retrieval,
350 array_keys($actions),
351 $this->active_action_closure,
352 $this->row_transformer
353 );
354 $this->table = $this
355 ->ui
356 ->factory()
357 ->table()
358 ->data($table_retrieval, $this->title, $columns)
359 ->withId($this->id)
360 ->withActions($actions)
361 ->withRequest($this->http->request())
362 ->withFilter($this->filter_data);
363 }
364 }
365 return $this->table;
366 }
367
368 public function getData(): ?array
369 {
370 return $this->getTable()->getData();
371 }
372
373 public function render(): string
374 {
375 $html = $this->ui->renderer()->render($this->getTable());
376 return $html;
377 }
378
380 string $modal_title,
381 string $modal_message,
382 string $delete_cmd,
383 array $items
384 ): void {
385 $f = $this->ui->factory();
386 $r = $this->ui->renderer();
387 $del_items = [];
388 foreach ($items as $id => $title) {
389 if (is_array($title)) {
390 $key = $title[0] ?? "";
391 $val = $title[1] ?? "";
392 } else {
393 $key = $title;
394 $val = "";
395 }
396 $del_items[] = $f->modal()->interruptiveItem()->keyValue((string) $id, $key, $val);
397 }
398 $action = $this->ctrl->getLinkTarget($this->parent_gui, $delete_cmd);
399
400 echo($r->renderAsync([
401 $f->modal()->interruptive(
402 $modal_title,
403 $modal_message,
404 $action
405 )->withAffectedItems($del_items)
406 ]));
407 exit();
408 }
409
410
411}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
factory()
textColumn(string $key, string $title, bool $sortable=false)
singleAction(string $action, string $title, bool $async=false)
filterData(array $filter_data)
Not applied if the table supports ordering.
renderDeletionConfirmation(string $modal_title, string $modal_message, string $delete_cmd, array $items)
dateColumn(string $key, string $title, bool $sortable=false)
addAction(int $type, string $action, string $title, bool $async=false)
standardAction(string $action, string $title)
__construct(protected string $id, protected string $title, protected RetrievalInterface $retrieval, protected object $parent_gui, protected string $parent_cmd="tableCommand", protected string $namespace="", protected string $ordering_cmd="", protected ?\Closure $active_action_closure=null, protected ?\Closure $row_transformer=null, protected bool $numeric_ids=true)
singleRedirectAction(string $action, string $title, array $class_path, string $cmd="", string $id_param="", bool $async=false)
multiAction(string $action, string $title)
linkListingColumn(string $key, string $title, bool $sortable=false)
addColumn(string $key, Column $column)
linkColumn(string $key, string $title, bool $sortable=false)
iconColumn(string $key, string $title, bool $sortable=false)
trait BaseGUIRequest
Base gui request wrapper.
exit
if($err=$client->getError()) $namespace
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
$path
Definition: ltiservices.php:30
static http()
Fetches the global http state from ILIAS.
initRequest(HTTP\Services $http, Refinery\Factory $refinery, ?array $passed_query_params=null, ?array $passed_post_data=null)
Query params and post data parameters are used for testing.
link(string $caption, string $href, bool $new_viewport=false)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26