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