ILIAS  trunk Revision v12.0_alpha-1540-g00f839d5fa1
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 bool $async = false
185 ): self {
186 $this->addAction(self::MULTI, $action, $title, $async);
187 return $this;
188 }
189
193 public function filterData(array $filter_data): self
194 {
195 $this->filter_data = $filter_data;
196 return $this;
197 }
198
199 protected function addAction(int $type, string $action, string $title, bool $async = false): void
200 {
201 $this->actions[$action] = [
202 "type" => $type,
203 "action" => $action,
204 "title" => $title,
205 "async" => $async
206 ];
207 $this->last_action_key = $action;
208 }
209
210 protected function addColumn(string $key, Column $column): void
211 {
212 if ($key === "") {
213 throw new \ilException("Missing Input Key: " . $key);
214 }
215 if (isset($this->columns[$key])) {
216 throw new \ilException("Duplicate Input Key: " . $key);
217 }
218 $this->columns[$key] = $column;
219 $this->last_key = $key;
220 }
221
222 protected function getColumnForKey(string $key): Column
223 {
224 if (!isset($this->columns[$key])) {
225 throw new \ilException("Unknown Key: " . $key);
226 }
227 return $this->columns[$key];
228 }
229
230 protected function getLastColumn(): ?Column
231 {
232 return $this->columns[$this->last_key] ?? null;
233 }
234
235 protected function replaceLastColumn(Column $column): void
236 {
237 if ($this->last_key !== "") {
238 $this->columns[$this->last_key] = $column;
239 }
240 }
241
242 public function getItemIds(): array
243 {
244 if ($this->numeric_ids) {
245 $ids = $this->intArray($this->row_id_token->getName());
246 } else {
247 $ids = $this->strArray($this->row_id_token->getName());
248 }
249 if (count($ids) > 0) {
250 return $ids; // from table multi action
251 }
252 if ($this->numeric_ids) {
253 $ids = $this->intArray("interruptive_items"); // from confirmation
254 } else {
255 $ids = $this->strArray("interruptive_items"); // from confirmation
256 }
257 if (count($ids) > 0) {
258 return $ids;
259 }
260 return [];
261 }
262
263 public function handleCommand(): bool
264 {
265 $action = $this->str($this->action_parameter_token->getName());
266 if ($action !== "") {
267 if ($this->actions[$action]["type"] === self::SINGLE) {
268 $id = $this->getItemIds()[0];
269 if ($this->actions[$action]["redirect_class_path"] ?? false) {
270 $path = $this->actions[$action]["redirect_class_path"];
271 if ($this->actions[$action]["redirect_id_param"] ?? false) {
272 $this->ctrl->setParameterByClass(
273 $path[count($path) - 1],
274 $this->actions[$action]["redirect_id_param"],
275 $id
276 );
277 }
278 $cmd = $this->actions[$action]["redirect_cmd"] ?? $action;
279 $this->ctrl->redirectByClass($this->actions[$action]["redirect_class_path"], $cmd);
280 }
281 $this->parent_gui->$action($id);
282 return true;
283 } else {
284 $this->parent_gui->$action($this->getItemIds());
285 return true;
286 }
287 }
288 return false;
289 }
290
291 protected function getTable(): Table
292 {
293 $a = $this->ui->factory()->table()->action();
294
295 if (is_null($this->table)) {
296 $columns = [];
297 foreach ($this->columns as $key => $column) {
298 $columns[$key] = $column;
299 }
300 $actions = [];
301 foreach ($this->actions as $act) {
302 switch ($act["type"]) {
303 case self::SINGLE:
304 $actions[$act["action"]] = $a->single(
305 $act["title"],
306 $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
307 $this->row_id_token
308 );
309 break;
310 case self::STANDARD:
311 $actions[$act["action"]] = $a->standard(
312 $act["title"],
313 $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
314 $this->row_id_token
315 );
316 break;
317 case self::MULTI:
318 $actions[$act["action"]] = $a->multi(
319 $act["title"],
320 $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
321 $this->row_id_token
322 );
323 break;
324 }
325 if ($act["async"]) {
326 $actions[$act["action"]] = $actions[$act["action"]]->withAsync(true);
327 }
328 }
329 if ($this->order_cmd !== "") {
330 $uri = $this->df->uri(
331 ILIAS_HTTP_PATH . '/' .
332 $this->ctrl->getLinkTarget($this->parent_gui, $this->order_cmd)
333 );
334 $table_retrieval = new OrderingRetrieval(
335 $this->retrieval,
336 array_keys($actions),
337 $this->active_action_closure,
338 $this->row_transformer
339 );
340 $this->table = $this
341 ->ui
342 ->factory()
343 ->table()
344 ->ordering($table_retrieval, $uri, $this->title, $columns)
345 ->withId($this->id)
346 ->withActions($actions)
347 ->withRequest($this->http->request());
348 } else {
349 $table_retrieval = new TableRetrieval(
350 $this->retrieval,
351 array_keys($actions),
352 $this->active_action_closure,
353 $this->row_transformer
354 );
355 $this->table = $this
356 ->ui
357 ->factory()
358 ->table()
359 ->data($table_retrieval, $this->title, $columns)
360 ->withId($this->id)
361 ->withActions($actions)
362 ->withRequest($this->http->request())
363 ->withFilter($this->filter_data);
364 }
365 }
366 return $this->table;
367 }
368
369 public function getData(): ?array
370 {
371 return $this->getTable()->getData();
372 }
373
374 public function render(): string
375 {
376 $html = $this->ui->renderer()->render($this->getTable());
377 return $html;
378 }
379
381 string $modal_title,
382 string $modal_message,
383 string $delete_cmd,
384 array $items
385 ): void {
386 $f = $this->ui->factory();
387 $r = $this->ui->renderer();
388 $del_items = [];
389 foreach ($items as $id => $title) {
390 if (is_array($title)) {
391 $key = $title[0] ?? "";
392 $val = $title[1] ?? "";
393 } else {
394 $key = $title;
395 $val = "";
396 }
397 $del_items[] = $f->modal()->interruptiveItem()->keyValue((string) $id, $key, $val);
398 }
399 $action = $this->ctrl->getLinkTarget($this->parent_gui, $delete_cmd);
400
401 echo($r->renderAsync([
402 $f->modal()->interruptive(
403 $modal_title,
404 $modal_message,
405 $action
406 )->withAffectedItems($del_items)
407 ]));
408 exit();
409 }
410
411
412}
$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, bool $async=false)
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