ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
TableAdapterGUI.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Repository\Table;
22 
29 
31 {
32  use BaseGUIRequest;
33 
34  protected const STANDARD = 0;
35  protected const SINGLE = 1;
36  protected const MULTI = 2;
37  protected string $order_cmd = "";
38  protected string $last_action_key;
42  protected \ILIAS\Data\Factory $df;
43 
44  protected \ilLanguage $lng;
45  protected ?Table $table = null;
46  protected string $last_key;
47  protected \ilCtrlInterface $ctrl;
48  protected \ILIAS\DI\UIServices $ui;
49  protected array $columns = [];
50  protected array $actions = [];
51 
52  public function __construct(
53  protected string $id,
54  protected string $title,
55  protected RetrievalInterface $retrieval,
56  protected object $parent_gui,
57  protected string $parent_cmd = "tableCommand",
58  protected string $namespace = "",
59  protected string $ordering_cmd = "",
60  protected ?\Closure $active_action_closure = null,
61  protected ?\Closure $row_transformer = null,
62  protected bool $numeric_ids = true
63  ) {
64  global $DIC;
65  $this->ui = $DIC->ui();
66  $this->ctrl = $DIC->ctrl();
67  $this->http = $DIC->http();
68  $this->lng = $DIC->language();
69  $this->refinery = $DIC->refinery();
70  $this->df = new \ILIAS\Data\Factory();
71  $this->initRequest($this->http, $this->refinery);
72  if ($namespace === "") {
73  $this->namespace = $id;
74  }
75  $this->order_cmd = $ordering_cmd;
76 
77  $form_action = $this->df->uri(
78  ILIAS_HTTP_PATH . '/' .
79  $this->ctrl->getLinkTarget($this->parent_gui, $this->parent_cmd)
80  );
81  $this->url_builder = new URLBuilder($form_action);
83  $this->url_builder->acquireParameters(
84  [$this->namespace], // namespace
85  "table_action", //this is the actions's parameter name
86  "ids" //this is the parameter name to be used for row-ids
87  );
88 
89  }
90 
91  public function textColumn(
92  string $key,
93  string $title,
94  bool $sortable = false
95  ): self {
96  $column = $this->ui->factory()->table()->column()->text($title)->withIsSortable($sortable);
97  $this->addColumn($key, $column);
98  return $this;
99  }
100 
101  public function iconColumn(
102  string $key,
103  string $title,
104  bool $sortable = false
105  ): self {
106  $column = $this->ui->factory()->table()->column()->statusIcon($title)->withIsSortable($sortable);
107  $this->addColumn($key, $column);
108  return $this;
109  }
110 
111  public function singleAction(
112  string $action,
113  string $title,
114  bool $async = false
115  ): self {
116  $this->addAction(self::SINGLE, $action, $title, $async);
117  return $this;
118  }
119 
120  public function singleRedirectAction(
121  string $action,
122  string $title,
123  array $class_path,
124  string $cmd = "",
125  string $id_param = ""
126  ): self {
127  $this->addAction(self::SINGLE, $action, $title);
128  $act = $this->actions[$this->last_action_key] ?? false;
129  if ($act && $act["type"] === self::SINGLE) {
130  $act["redirect_class_path"] = $class_path;
131  $act["redirect_cmd"] = $cmd;
132  $act["redirect_id_param"] = $id_param;
133  $this->actions[$this->last_action_key] = $act;
134  }
135  return $this;
136  }
137 
138  public function standardAction(
139  string $action,
140  string $title
141  ): self {
142  $this->addAction(self::STANDARD, $action, $title);
143  return $this;
144  }
145 
146  public function multiAction(
147  string $action,
148  string $title
149  ): self {
150  $this->addAction(self::MULTI, $action, $title);
151  return $this;
152  }
153 
154  protected function addAction(int $type, string $action, string $title, bool $async = false): void
155  {
156  $this->actions[$action] = [
157  "type" => $type,
158  "action" => $action,
159  "title" => $title,
160  "async" => $async
161  ];
162  $this->last_action_key = $action;
163  }
164 
165  protected function addColumn(string $key, Column $column): void
166  {
167  if ($key === "") {
168  throw new \ilException("Missing Input Key: " . $key);
169  }
170  if (isset($this->columns[$key])) {
171  throw new \ilException("Duplicate Input Key: " . $key);
172  }
173  $this->columns[$key] = $column;
174  $this->last_key = $key;
175  }
176 
177  protected function getColumnForKey(string $key): Column
178  {
179  if (!isset($this->columns[$key])) {
180  throw new \ilException("Unknown Key: " . $key);
181  }
182  return $this->columns[$key];
183  }
184 
185  protected function getLastColumn(): ?Column
186  {
187  return $this->columns[$this->last_key] ?? null;
188  }
189 
190  protected function replaceLastColumn(Column $column): void
191  {
192  if ($this->last_key !== "") {
193  $this->columns[$this->last_key] = $column;
194  }
195  }
196 
197  protected function getItemIds(): array
198  {
199  if ($this->numeric_ids) {
200  $ids = $this->intArray($this->row_id_token->getName());
201  } else {
202  $ids = $this->strArray($this->row_id_token->getName());
203  }
204  if (count($ids) > 0) {
205  return $ids; // from table multi action
206  }
207  if ($this->numeric_ids) {
208  $ids = $this->intArray("interruptive_items"); // from confirmation
209  } else {
210  $ids = $this->strArray("interruptive_items"); // from confirmation
211  }
212  if (count($ids) > 0) {
213  return $ids;
214  }
215  return [];
216  }
217 
218  public function handleCommand(): bool
219  {
220  $action = $this->str($this->action_parameter_token->getName());
221  if ($action !== "") {
222  if ($this->actions[$action]["type"] === self::SINGLE) {
223  $id = $this->getItemIds()[0];
224  if ($this->actions[$action]["redirect_class_path"] ?? false) {
225  $path = $this->actions[$action]["redirect_class_path"];
226  if ($this->actions[$action]["redirect_id_param"] ?? false) {
227  $this->ctrl->setParameterByClass(
228  $path[count($path) - 1],
229  $this->actions[$action]["redirect_id_param"],
230  $id
231  );
232  }
233  $cmd = $this->actions[$action]["redirect_cmd"] ?? $action;
234  $this->ctrl->redirectByClass($this->actions[$action]["redirect_class_path"], $cmd);
235  }
236  $this->parent_gui->$action($id);
237  return true;
238  } else {
239  $this->parent_gui->$action($this->getItemIds());
240  return true;
241  }
242  }
243  return false;
244  }
245 
246  protected function getTable(): Table
247  {
248  $a = $this->ui->factory()->table()->action();
249 
250  if (is_null($this->table)) {
251  $columns = [];
252  foreach ($this->columns as $key => $column) {
253  $columns[$key] = $column;
254  }
255  $actions = [];
256  foreach ($this->actions as $act) {
257  switch ($act["type"]) {
258  case self::SINGLE:
259  $actions[$act["action"]] = $a->single(
260  $act["title"],
261  $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
263  );
264  break;
265  case self::STANDARD:
266  $actions[$act["action"]] = $a->standard(
267  $act["title"],
268  $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
270  );
271  break;
272  case self::MULTI:
273  $actions[$act["action"]] = $a->multi(
274  $act["title"],
275  $this->url_builder->withParameter($this->action_parameter_token, $act["action"]),
277  );
278  break;
279  }
280  if ($act["async"]) {
281  $actions[$act["action"]] = $actions[$act["action"]]->withAsync(true);
282  }
283  }
284  if ($this->order_cmd !== "") {
285  $uri = $this->df->uri(
286  ILIAS_HTTP_PATH . '/' .
287  $this->ctrl->getLinkTarget($this->parent_gui, $this->order_cmd)
288  );
289  $table_retrieval = new OrderingRetrieval(
290  $this->retrieval,
291  array_keys($actions),
292  $this->active_action_closure,
293  $this->row_transformer
294  );
295  $this->table = $this
296  ->ui
297  ->factory()
298  ->table()
299  ->ordering($table_retrieval, $uri, $this->title, $columns)
300  ->withId($this->id)
301  ->withActions($actions)
302  ->withRequest($this->http->request());
303  } else {
304  $table_retrieval = new TableRetrieval(
305  $this->retrieval,
306  array_keys($actions),
307  $this->active_action_closure,
308  $this->row_transformer
309  );
310  $this->table = $this
311  ->ui
312  ->factory()
313  ->table()
314  ->data($table_retrieval, $this->title, $columns)
315  ->withId($this->id)
316  ->withActions($actions)
317  ->withRequest($this->http->request());
318  }
319  }
320  return $this->table;
321  }
322 
323  public function getData(): ?array
324  {
325  return $this->getTable()->getData();
326  }
327 
328  public function render(): string
329  {
330  $html = $this->ui->renderer()->render($this->getTable());
331  return $html;
332  }
333 }
if($err=$client->getError()) $namespace
singleRedirectAction(string $action, string $title, array $class_path, string $cmd="", string $id_param="")
__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)
addColumn(string $key, Column $column)
textColumn(string $key, string $title, bool $sortable=false)
trait BaseGUIRequest
Base gui request wrapper.
$path
Definition: ltiservices.php:29
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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.
global $DIC
Definition: shib_login.php:26
addAction(int $type, string $action, string $title, bool $async=false)
iconColumn(string $key, string $title, bool $sortable=false)
multiAction(string $action, string $title)
standardAction(string $action, string $title)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
singleAction(string $action, string $title, bool $async=false)
URLBuilder.
Definition: URLBuilder.php:40
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:27