ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
LDAPRoleAssignmentTable.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25use ILIAS\UI\Factory as UIFactory;
29use Psr\Http\Message\ServerRequestInterface;
30use ILIAS\UI\Component\Table\Data as DataTable;
32
34{
43 private ?array $records = null;
44
45 public function __construct(
46 private readonly ServerRequestInterface $http_request,
47 private readonly ilLanguage $lng,
48 private readonly UIFactory $ui_factory,
49 private readonly \ILIAS\Data\URI $action_url,
50 private readonly int $server_id,
51 private readonly bool $has_write_access
52 ) {
53 }
54
55 public function getRows(
56 ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
57 array $visible_column_ids,
59 Order $order,
60 mixed $additional_viewcontrol_data,
61 mixed $filter_data,
62 mixed $additional_parameters
63 ): Generator {
64 $records = $this->getRecords($range, $order);
65 foreach ($records as $record) {
66 yield $row_builder->buildDataRow((string) $record['id'], $record);
67 }
68 }
69
70 public function initRecords(): void
71 {
72 if ($this->records === null) {
73 $icons = [
74 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small'),
75 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small')
76 ];
77
78 $rule_objs = ilLDAPRoleAssignmentRule::_getRules($this->server_id);
80 foreach ($rule_objs as $rule) {
81 switch ($rule->getType()) {
83 $type = $this->lng->txt('ldap_role_by_attribute');
84 break;
85
87 $type = $this->lng->txt('ldap_role_by_group');
88 break;
89
91 $type = $this->lng->txt('ldap_role_by_plugin');
92 break;
93 }
94 $this->records[] = [
95 'id' => $rule->getRuleId(),
96 'type' => $type ?? '',
97 'condition' => $rule->conditionToString(),
98 'add' => $icons[(int) $rule->isAddOnUpdateEnabled()],
99 'remove' => $icons[(int) $rule->isRemoveOnUpdateEnabled()],
100 'role' => ilObject::_lookupTitle($rule->getRoleId()),
101 ];
102 }
103 }
104 }
105
106 public function getComponent(): DataTable
107 {
108 $query_params_namespace = ['ldap', 'role', 'assignment'];
109 $url_builder = new URLBuilder($this->action_url);
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 ->withRange(new Range(0, 100))
126 ->withRequest($this->http_request);
127 }
128
129 public function getTotalRowCount(
130 mixed $additional_viewcontrol_data,
131 mixed $filter_data,
132 mixed $additional_parameters
133 ): ?int {
134 $this->initRecords();
135
136 return count((array) $this->records);
137 }
138
149 private function getRecords(Range $range, Order $order): array
150 {
151 $this->initRecords();
152 $records = $this->records;
153
154 if ($order) {
155 $records = $this->orderRecords($records, $order);
156 }
157
158 if ($range) {
159 $records = $this->limitRecords($records, $range);
160 }
161
162 return $records;
163 }
164
168 public function getActions(
169 URLBuilder $url_builder,
170 URLBuilderToken $action_parameter_token,
171 URLBuilderToken $row_id_token
172 ): array {
173 $actions = [];
174 if ($this->has_write_access) {
175 $actions['delete'] = $this->ui_factory->table()->action()->multi(
176 $this->lng->txt('delete'),
177 $url_builder->withParameter($action_parameter_token, 'confirmDeleteRules'),
178 $row_id_token
179 );
180 }
181
182 $actions['edit'] = $this->ui_factory->table()->action()->single(
183 $this->has_write_access ? $this->lng->txt('edit') : $this->lng->txt('view'),
184 $url_builder->withParameter($action_parameter_token, 'editRoleAssignment'),
185 $row_id_token
186 );
187
188 return $actions;
189 }
190
209 private function limitRecords(array $records, Range $range): array
210 {
211 return array_slice($records, $range->getStart(), $range->getLength());
212 }
213
232 private function orderRecords(array $records, Order $order): array
233 {
234 [$order_field, $order_direction] = $order->join(
235 [],
236 fn($ret, $key, $value) => [$key, $value]
237 );
238 usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
239 $left[$order_field] ?? '',
240 $right[$order_field] ?? ''
241 ));
242
243 if ($order_direction === Order::DESC) {
244 $records = array_reverse($records);
245 }
246
247 return $records;
248 }
249
253 private function getColumns(): array
254 {
255 return [
256 'type' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_rule_type')),
257 'role' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_ilias_role')),
258 'condition' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_rule_condition')),
259 'add' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('ldap_add_roles'))
260 ->withIsSortable(false),
261 'remove' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('ldap_remove_roles'))
262 ->withIsSortable(false),
263 ];
264 }
265}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
join($init, callable $fn)
Definition: Order.php:75
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
withParameter(URLBuilderToken $token, string|array $value)
Change an acquired parameter's value if the supplied token is valid.
Definition: URLBuilder.php:166
__construct(private readonly ServerRequestInterface $http_request, private readonly ilLanguage $lng, private readonly UIFactory $ui_factory, private readonly \ILIAS\Data\URI $action_url, private readonly int $server_id, private readonly bool $has_write_access)
orderRecords(array $records, Order $order)
getTotalRowCount(mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
Mainly for the purpose of pagination-support, it is important to know about the total number of recor...
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
limitRecords(array $records, Range $range)
getRows(ILIAS\UI\Component\Table\DataRowBuilder $row_builder, array $visible_column_ids, Range $range, Order $order, mixed $additional_viewcontrol_data, mixed $filter_data, mixed $additional_parameters)
This is called by the table to retrieve rows; map data-records to rows using the $row_builder e....
getRecords(Range $range, Order $order)
static _getRules($a_server_id)
Get all rules.
language handling
static _lookupTitle(int $obj_id)
static strCmp(string $a, string $b)
Definition: class.ilStr.php:87
This describes how an icon could be modified during construction of UI.
Definition: Icon.php:29
A Column describes the form of presentation for a certain aspect of data, i.e.
Definition: Column.php:28
This describes a Data Table.
Definition: Data.php:33
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31