ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
LDAPRoleAssignmentTable.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
33 
35 {
44  private ?array $records = null;
45 
46  public function __construct(
47  private readonly ServerRequestInterface $http_request,
48  private readonly ilLanguage $lng,
49  private readonly UIFactory $ui_factory,
50  private readonly DataFactory $data_factory,
51  private readonly int $server_id,
52  ) {
53  }
54 
55  public function getRows(
56  ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
57  array $visible_column_ids,
58  Range $range,
59  Order $order,
60  ?array $filter_data,
61  ?array $additional_parameters,
62  ): Generator {
63  $records = $this->getRecords($range, $order);
64  foreach ($records as $record) {
65  yield $row_builder->buildDataRow((string) $record['id'], $record);
66  }
67  }
68 
69  public function initRecords(): void
70  {
71  if ($this->records === null) {
72  $icons = [
73  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small'),
74  $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small')
75  ];
76 
77  $rule_objs = ilLDAPRoleAssignmentRule::_getRules($this->server_id);
79  foreach ($rule_objs as $rule) {
80  switch ($rule->getType()) {
82  $type = $this->lng->txt('ldap_role_by_attribute');
83  break;
84 
86  $type = $this->lng->txt('ldap_role_by_group');
87  break;
88 
90  $type = $this->lng->txt('ldap_role_by_plugin');
91  break;
92  }
93  $this->records[] = [
94  'id' => $rule->getRuleId(),
95  'type' => $type ?? '',
96  'condition' => $rule->conditionToString(),
97  'add' => $icons[(int) $rule->isAddOnUpdateEnabled()],
98  'remove' => $icons[(int) $rule->isRemoveOnUpdateEnabled()],
99  'role' => ilObject::_lookupTitle($rule->getRoleId()),
100  ];
101  }
102  }
103  }
104 
105  public function getComponent(): DataTable
106  {
107  $query_params_namespace = ['ldap', 'role', 'assignment'];
108  $table_uri = $this->data_factory->uri($this->http_request->getUri()->__toString());
109  $url_builder = new URLBuilder($table_uri);
110  [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
111  $query_params_namespace,
112  'table_action',
113  'rule_ids'
114  );
115 
116  return $this->ui_factory->table()
117  ->data(
118  $this,
119  $this->lng->txt('ldap_tbl_role_ass'),
120  $this->getColumns()
121  )
122  ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
123  ->withId('ldap_role_assignment_table')
124  ->withOrder(new Order('type', Order::DESC))
125  ->withRequest($this->http_request);
126  }
127 
128  public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
129  {
130  $this->initRecords();
131 
132  return count((array) $this->records);
133  }
134 
145  private function getRecords(Range $range, Order $order): array
146  {
147  $this->initRecords();
148  $records = $this->records;
149 
150  if ($order) {
151  $records = $this->orderRecords($records, $order);
152  }
153 
154  if ($range) {
155  $records = $this->limitRecords($records, $range);
156  }
157 
158  return $records;
159  }
160 
164  public function getActions(
165  URLBuilder $url_builder,
166  URLBuilderToken $action_parameter_token,
167  URLBuilderToken $row_id_token
168  ): array {
169  $actions = [];
170  $actions['delete'] = $this->ui_factory->table()->action()->multi(
171  $this->lng->txt('delete'),
172  $url_builder->withParameter($action_parameter_token, 'confirmDeleteRules'),
173  $row_id_token
174  );
175 
176  $actions['edit'] = $this->ui_factory->table()->action()->single(
177  $this->lng->txt('edit'),
178  $url_builder->withParameter($action_parameter_token, 'editRoleAssignment'),
179  $row_id_token
180  );
181 
182  return $actions;
183  }
184 
203  private function limitRecords(array $records, Range $range): array
204  {
205  return array_slice($records, $range->getStart(), $range->getLength());
206  }
207 
226  private function orderRecords(array $records, Order $order): array
227  {
228  [$order_field, $order_direction] = $order->join(
229  [],
230  fn($ret, $key, $value) => [$key, $value]
231  );
232  usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
233  $left[$order_field] ?? '',
234  $right[$order_field] ?? ''
235  ));
236 
237  if ($order_direction === Order::DESC) {
238  $records = array_reverse($records);
239  }
240 
241  return $records;
242  }
243 
247  private function getColumns(): array
248  {
249  return [
250  'type' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_rule_type')),
251  'role' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_ilias_role')),
252  'condition' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_rule_condition')),
253  'add' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('ldap_add_roles'))
254  ->withIsSortable(false),
255  'remove' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('ldap_remove_roles'))
256  ->withIsSortable(false),
257  ];
258  }
259 }
limitRecords(array $records, Range $range)
join($init, callable $fn)
Definition: Order.php:75
Interface Observer Contains several chained tasks and infos about them.
getRows(ILIAS\UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, ?array $filter_data, ?array $additional_parameters,)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e...
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
static _getRules($a_server_id)
Get all rules.
Both the subject and the direction need to be specified when expressing an order. ...
Definition: Order.php:28
__construct(private readonly ServerRequestInterface $http_request, private readonly ilLanguage $lng, private readonly UIFactory $ui_factory, private readonly DataFactory $data_factory, private readonly int $server_id,)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
static _lookupTitle(int $obj_id)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
orderRecords(array $records, Order $order)
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter&#39;s value if the supplied token is valid.
Definition: URLBuilder.php:166
global $lng
Definition: privfeed.php:31
getRecords(Range $range, Order $order)
URLBuilder.
Definition: URLBuilder.php:40
getTotalRowCount(?array $filter_data, ?array $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:28