ILIAS  trunk Revision v11.0_alpha-1689-g66c127b4ae8
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
DocumentTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
23 use Closure;
24 use Generator;
26 use ilCtrlInterface;
28 use ILIAS\Data\URI;
44 use ilObjUser;
47 
49 {
50  public const CMD_EDIT_DOCUMENT = 'editDocument';
51  public const CMD_DELETE_DOCUMENT = 'deleteDocument';
52  public const CMD_DELETE_DOCUMENTS = 'deleteDocuments';
53  public const CMD_ADD_CRITERION = 'addCriterion';
54 
56  private readonly Factory $data_factory;
57  private readonly ilCtrlInterface $ctrl;
58  private readonly Ordering $table;
59  private readonly Renderer $ui_renderer;
60  private ilObjUser $user;
61  private \DateTimeZone $usr_timezone;
62 
63  public function __construct(
64  private readonly Closure $criterion_as_component,
65  private readonly DocumentRepository $repository,
66  private readonly UI $ui,
67  private readonly DocumentModal $modal,
68  private readonly object $gui,
69  private readonly ?EditLinks $edit_links = null,
71  ?Factory $data_factory = null,
72  ?ilCtrlInterface $ctrl = null,
73  ?Renderer $ui_renderer = null,
74  ?ilObjUser $user = null
75  ) {
76  global $DIC;
77  $this->request = $request ?: $DIC->http()->request();
78  $this->data_factory = $data_factory ?: new Factory();
79  $this->ctrl = $ctrl ?: $DIC->ctrl();
80  $this->ui_renderer = $ui_renderer ?: $DIC->ui()->renderer();
81  $this->user = $user ?: $DIC->user();
82  $this->usr_timezone = new \DateTimeZone($this->user->getTimeZone());
83 
84  $this->table = $this->buildTable();
85  }
86 
87  private function buildTable(): Ordering
88  {
89  $uiTable = $this->ui->create()->table();
90 
91  if ((int) $this->user->getTimeFormat() === ilCalendarSettings::TIME_FORMAT_12) {
92  $date_format = $this->data_factory->dateFormat()->withTime12($this->user->getDateFormat());
93  } else {
94  $date_format = $this->data_factory->dateFormat()->withTime24($this->user->getDateFormat());
95  }
96 
97  $table = $uiTable->ordering(
98  $this,
99  (new URI((string) $this->request->getUri()))->withParameter('cmd', 'saveOrder'),
100  $this->ui->txt('tbl_docs_title'),
101  [
102  'title' => $uiTable->column()->text($this->ui->txt('tbl_docs_head_title')),
103  'created' => $uiTable->column()->date($this->ui->txt('tbl_docs_head_created'), $date_format),
104  'change' => $uiTable->column()->date($this->ui->txt('tbl_docs_head_last_change'), $date_format),
105  'criteria' => $uiTable->column()->text($this->ui->txt('tbl_docs_head_criteria')),
106  ],
107  )
108  ->withId('legalDocsTable')
109  ->withRequest($this->request);
110 
111  if ($this->edit_links) {
112  $table = $table->withActions($this->buildTableActions());
113  }
114 
115  return $table;
116  }
117 
118  public function getRows(
119  OrderingRowBuilder $row_builder,
120  array $visible_column_ids
121  ): Generator {
122  foreach ($this->repository->all() as $document) {
123  yield $this->buildTableRow($row_builder, $document);
124  }
125  }
126 
127  private function buildTableRow(OrderingRowBuilder $row_builder, Document $document): OrderingRow
128  {
129  $criterion_components = [];
130  foreach ($document->criteria() as $criterion) {
131  $criterion_components[] = $this->ui->create()->legacy()->content('<div style="display: flex; gap: 1rem;">');
132  $criterion_components[] = $this->criterionName($criterion);
133 
134  if ($this->edit_links) {
135  $delete_modal = $this->ui->create()->modal()->interruptive(
136  $this->ui->txt('doc_detach_crit_confirm_title'),
137  $this->ui->txt('doc_sure_detach_crit'),
138  $this->edit_links->deleteCriterion($document, $criterion)
139  );
140 
141  $dropdown = $this->ui->create()->dropdown()->standard([
142  $this->ui->create()->button()->shy(
143  $this->ui->txt('edit'),
144  $this->edit_links->editCriterion($document, $criterion)
145  ),
146  $this->ui->create()->button()->shy(
147  $this->ui->txt('delete'),
148  ''
149  )->withOnClick($delete_modal->getShowSignal())
150  ]);
151 
152  $criterion_components[] = $delete_modal;
153  $criterion_components[] = $dropdown;
154  }
155 
156  $criterion_components[] = $this->ui->create()->legacy()->content('</div>');
157  }
158 
159  $table_row = [
160  'id' => $document->id(),
161  'title' => $this->ui_renderer->render($this->modal->create($document->content())),
162  'created' => $document->meta()->creation()->time()->setTimezone($this->usr_timezone),
163  'change' => $document->meta()->lastModification()->time()->setTimezone($this->usr_timezone),
164  'criteria' => $this->ui_renderer->render($criterion_components),
165  ];
166  return $row_builder->buildOrderingRow((string) $document->id(), $table_row);
167  }
168 
169  public function criterionName(Criterion $criterion): Component
170  {
171  return ($this->criterion_as_component)($criterion->content());
172  }
173 
174  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
175  {
176  return $this->repository->countAll();
177  }
178 
179  public function render(): string
180  {
181  return $this->ui_renderer->render($this->table);
182  }
183 
187  private function buildTableActions(): array
188  {
189  return [
190  self::CMD_DELETE_DOCUMENTS => $this->buildTableAction(self::CMD_DELETE_DOCUMENTS, $this->ui->txt('delete'), true),
191  self::CMD_EDIT_DOCUMENT => $this->buildTableAction(self::CMD_EDIT_DOCUMENT, $this->ui->txt('edit')),
192  self::CMD_ADD_CRITERION => $this->buildTableAction(
193  self::CMD_ADD_CRITERION,
194  $this->ui->txt('tbl_docs_action_add_criterion')
195  ),
196  self::CMD_DELETE_DOCUMENT => $this->buildTableAction(self::CMD_DELETE_DOCUMENT, $this->ui->txt('delete')),
197  ];
198  }
199 
200  private function buildTableAction(string $cmd, string $title, bool $multi = false): Single|Multi
201  {
202  $uri = $this->data_factory->uri(
203  ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTarget($this->gui, $cmd)
204  );
205 
211  [
212  $url_builder,
213  $action_parameter_token,
214  $row_id_token
215  ] = (new URLBuilder($uri))->acquireParameters(
216  ['legal_document'],
217  'action',
218  'id'
219  );
220 
221  if ($multi) {
222  return $this->ui->create()->table()->action()->multi(
223  $title,
224  $url_builder->withParameter($action_parameter_token, $cmd),
225  $row_id_token
226  );
227  }
228 
229  return $this->ui->create()->table()->action()->single(
230  $title,
231  $url_builder->withParameter($action_parameter_token, $cmd),
232  $row_id_token
233  );
234  }
235 }
getRows(OrderingRowBuilder $row_builder, array $visible_column_ids)
This is called by the (ordering-)table to retrieve rows; map data-records to rows using the $row_buil...
repository()
description: > Example for rendering a repository card
Definition: repository.php:33
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
This describes a Table to specify the order of its data (rows).
Definition: Ordering.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:22
Builds data types.
Definition: Factory.php:35
buildTableRow(OrderingRowBuilder $row_builder, Document $document)
__construct(private readonly Closure $criterion_as_component, private readonly DocumentRepository $repository, private readonly UI $ui, private readonly DocumentModal $modal, private readonly object $gui, private readonly ?EditLinks $edit_links=null, ServerRequestInterface|RequestInterface|null $request=null, ?Factory $data_factory=null, ?ilCtrlInterface $ctrl=null, ?Renderer $ui_renderer=null, ?ilObjUser $user=null)
modal(string $title="", string $cancel_label="")
buildOrderingRow(string $id, array $record)
URLBuilder.
Definition: URLBuilder.php:40
readonly ServerRequestInterface RequestInterface $request