ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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 ) {
52 }
53
54 public function getRows(
55 ILIAS\UI\Component\Table\DataRowBuilder $row_builder,
56 array $visible_column_ids,
58 Order $order,
59 ?array $filter_data,
60 ?array $additional_parameters,
61 ): Generator {
62 $records = $this->getRecords($range, $order);
63 foreach ($records as $record) {
64 yield $row_builder->buildDataRow((string) $record['id'], $record);
65 }
66 }
67
68 public function initRecords(): void
69 {
70 if ($this->records === null) {
71 $icons = [
72 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_not_ok.svg', '', 'small'),
73 $this->ui_factory->symbol()->icon()->custom('assets/images/standard/icon_ok.svg', '', 'small')
74 ];
75
76 $rule_objs = ilLDAPRoleAssignmentRule::_getRules($this->server_id);
78 foreach ($rule_objs as $rule) {
79 switch ($rule->getType()) {
81 $type = $this->lng->txt('ldap_role_by_attribute');
82 break;
83
85 $type = $this->lng->txt('ldap_role_by_group');
86 break;
87
89 $type = $this->lng->txt('ldap_role_by_plugin');
90 break;
91 }
92 $this->records[] = [
93 'id' => $rule->getRuleId(),
94 'type' => $type ?? '',
95 'condition' => $rule->conditionToString(),
96 'add' => $icons[(int) $rule->isAddOnUpdateEnabled()],
97 'remove' => $icons[(int) $rule->isRemoveOnUpdateEnabled()],
98 'role' => ilObject::_lookupTitle($rule->getRoleId()),
99 ];
100 }
101 }
102 }
103
104 public function getComponent(): DataTable
105 {
106 $query_params_namespace = ['ldap', 'role', 'assignment'];
107 $url_builder = new URLBuilder($this->action_url);
108 [$url_builder, $action_parameter_token, $row_id_token] = $url_builder->acquireParameters(
109 $query_params_namespace,
110 'table_action',
111 'rule_ids'
112 );
113
114 return $this->ui_factory->table()
115 ->data(
116 $this,
117 $this->lng->txt('ldap_tbl_role_ass'),
118 $this->getColumns()
119 )
120 ->withActions($this->getActions($url_builder, $action_parameter_token, $row_id_token))
121 ->withId('ldap_role_assignment_table')
122 ->withOrder(new Order('type', Order::DESC))
123 ->withRequest($this->http_request);
124 }
125
126 public function getTotalRowCount(?array $filter_data, ?array $additional_parameters): ?int
127 {
128 $this->initRecords();
129
130 return count((array) $this->records);
131 }
132
143 private function getRecords(Range $range, Order $order): array
144 {
145 $this->initRecords();
146 $records = $this->records;
147
148 if ($order) {
149 $records = $this->orderRecords($records, $order);
150 }
151
152 if ($range) {
153 $records = $this->limitRecords($records, $range);
154 }
155
156 return $records;
157 }
158
162 public function getActions(
163 URLBuilder $url_builder,
164 URLBuilderToken $action_parameter_token,
165 URLBuilderToken $row_id_token
166 ): array {
167 $actions = [];
168 $actions['delete'] = $this->ui_factory->table()->action()->multi(
169 $this->lng->txt('delete'),
170 $url_builder->withParameter($action_parameter_token, 'confirmDeleteRules'),
171 $row_id_token
172 );
173
174 $actions['edit'] = $this->ui_factory->table()->action()->single(
175 $this->lng->txt('edit'),
176 $url_builder->withParameter($action_parameter_token, 'editRoleAssignment'),
177 $row_id_token
178 );
179
180 return $actions;
181 }
182
201 private function limitRecords(array $records, Range $range): array
202 {
203 return array_slice($records, $range->getStart(), $range->getLength());
204 }
205
224 private function orderRecords(array $records, Order $order): array
225 {
226 [$order_field, $order_direction] = $order->join(
227 [],
228 fn($ret, $key, $value) => [$key, $value]
229 );
230 usort($records, static fn(array $left, array $right): int => ilStr::strCmp(
231 $left[$order_field] ?? '',
232 $right[$order_field] ?? ''
233 ));
234
235 if ($order_direction === Order::DESC) {
236 $records = array_reverse($records);
237 }
238
239 return $records;
240 }
241
245 private function getColumns(): array
246 {
247 return [
248 'type' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_rule_type')),
249 'role' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_ilias_role')),
250 'condition' => $this->ui_factory->table()->column()->text($this->lng->txt('ldap_rule_condition')),
251 'add' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('ldap_add_roles'))
252 ->withIsSortable(false),
253 'remove' => $this->ui_factory->table()->column()->statusIcon($this->lng->txt('ldap_remove_roles'))
254 ->withIsSortable(false),
255 ];
256 }
257}
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
orderRecords(array $records, Order $order)
getActions(URLBuilder $url_builder, URLBuilderToken $action_parameter_token, URLBuilderToken $row_id_token)
limitRecords(array $records, Range $range)
getRecords(Range $range, Order $order)
__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)
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....
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...
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:31
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31