ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
class.ConfigurationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\User\Settings;
22
23use ILIAS\User\RedirectOnMissingWrite;
25use ILIAS\UI\Factory as UIFactory;
26use ILIAS\UI\Renderer as UIRenderer;
27use ILIAS\UI\Component\Modal\RoundTrip as RoundTripModal;
28use ILIAS\UI\Component\Table\Data as DataTable;
36use ILIAS\Refinery\Factory as Refinery;
38use ILIAS\HTTP\Services as HttpService;
40use Psr\Http\Message\ServerRequestInterface;
41
43{
44 use RedirectOnMissingWrite;
45
46 private readonly URLBuilder $url_builder;
49
50 private array $available_settings;
51
52 public function __construct(
53 private readonly \ILIAS\Language\Language $lng,
54 private readonly \ilCtrl $ctrl,
55 private readonly \ilAccess $access,
56 private readonly \ilGlobalTemplateInterface $tpl,
57 private readonly UIFactory $ui_factory,
58 private readonly UIRenderer $ui_renderer,
59 private readonly Refinery $refinery,
60 private readonly ServerRequestInterface $request,
61 private readonly RequestWrapper $request_wrapper,
62 private readonly HttpService $http,
63 private readonly ConfigurationRepository $user_settings_config_repo
64 ) {
65 $this->available_settings = $this->user_settings_config_repo->get();
66
67 $url_builder = new URLBuilder(new URI(ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(self::class, 'action')));
68 [
73 ['user', 'settings'],
74 'table_action',
75 'setting'
76 );
77 }
78
79 public function executeCommand(): void
80 {
81 $this->redirectOnMissingWrite($this->access, $this->ctrl, $this->tpl, $this->lng);
82 $cmd = $this->ctrl->getCmd() . 'Cmd';
83 $this->$cmd();
84 }
85
86 public function showCmd(?RoundTripModal $modal = null): void
87 {
88 $content = [
89 $this->buildTable()
90 ];
91
92 if ($modal !== null) {
93 $content[] = $modal;
94 }
95
96 $this->tpl->setContent(
97 $this->ui_renderer->render($content)
98 );
99 }
100
101 public function actionCmd(): void
102 {
103 $this->http->saveResponse(
104 $this->http->response()->withBody(
106 $this->ui_renderer->renderAsync($this->buildEditModal())
107 )
108 )
109 );
110 $this->http->sendResponse();
111 $this->http->close();
112 }
113
114 public function saveCmd(): void
115 {
116 $modal = $this->buildEditModal()->withRequest($this->request);
117 $data = $modal->getData();
118 if ($data === null) {
119 $this->showCmd(
120 $modal->withOnLoad($modal->getShowSignal())
121 );
122 return;
123 }
124
125 $this->user_settings_config_repo->storeConfiguration($data['setting']);
126 $this->available_settings = $this->user_settings_config_repo->get();
127 $this->tpl->setOnScreenMessage('success', $this->lng->txt('usr_settings_saved'));
128 $this->showCmd();
129 }
130
131 public function getRows(
132 DataRowBuilder $row_builder,
133 array $visible_column_ids,
135 Order $order,
136 mixed $additional_viewcontrol_data,
137 mixed $filter_data,
138 mixed $additional_parameters
139 ): \Generator {
140 $this->sortRows($order);
141 foreach ($this->available_settings as $setting) {
142 yield $setting->getTableRow(
143 $row_builder,
144 $this->lng
145 );
146 }
147 }
148
149 public function getTotalRowCount(
150 mixed $additional_viewcontrol_data,
151 mixed $filter_data,
152 mixed $additional_parameters
153 ): ?int {
154 return count($this->available_settings);
155 }
156
157 private function buildTable(): DataTable
158 {
159 return $this->ui_factory->table()->data(
160 $this,
161 $this->lng->txt('user_settings'),
162 $this->getColumns()
163 )->withActions([
164 $this->ui_factory->table()->action()->single(
165 $this->lng->txt('edit_setting'),
166 $this->url_builder,
167 $this->setting_id_token
168 )->withAsync(true)
169 ])->withRequest($this->request);
170 }
171
172 private function getColumns(): array
173 {
174 $cf = $this->ui_factory->table()->column();
175 $icon_checked = $this->ui_factory->symbol()->icon()
176 ->custom('assets/images/standard/icon_checked.svg', '', 'small');
177 $icon_unchecked = $this->ui_factory->symbol()->icon()
178 ->custom('assets/images/standard/icon_unchecked.svg', '', 'small');
179 return [
180 'field' => $cf->text($this->lng->txt('user_field'))->withIsSortable(true),
181 'changeable_by_user' => $cf->boolean(
182 $this->lng->txt(
183 PropertyAttributes::ChangeableByUser->value
184 ),
185 $icon_checked,
186 $icon_unchecked
187 )->withIsSortable(true),
188 'changeable_in_local_user_administration' => $cf->boolean(
189 $this->lng->txt(
190 PropertyAttributes::ChangeableInLocalUserAdministration->value
191 ),
192 $icon_checked,
193 $icon_unchecked
194 )->withIsSortable(true),
195 'export' => $cf->boolean(
196 $this->lng->txt(
197 PropertyAttributes::Export->value
198 ),
199 $icon_checked,
200 $icon_unchecked
201 )->withIsSortable(true)
202 ];
203 }
204
205 private function buildEditModal(): RoundTripModal
206 {
207 $identifier = $this->retrieveIdentifierFromQuery();
208 $this->ctrl->setParameterByClass(self::class, $this->setting_id_token->getName(), $identifier);
209 return $this->ui_factory->modal()->roundtrip(
210 $this->lng->txt('edit_setting'),
211 null,
212 $this->user_settings_config_repo->getByIdentifier(
213 $identifier
214 )->getForm(
215 $this->lng,
216 $this->ui_factory->input()->field(),
217 $this->refinery
218 ),
219 $this->ctrl->getFormActionByClass(self::class, 'save')
220 );
221 }
222
223 private function retrieveIdentifierFromQuery(): string
224 {
225 $identifier = $this->request_wrapper->retrieve(
226 $this->setting_id_token->getName(),
227 $this->refinery->byTrying([
228 $this->refinery->kindlyTo()->string(),
229 $this->refinery->kindlyTo()->listOf(
230 $this->refinery->kindlyTo()->string()
231 )
232 ])
233 );
234
235 if (is_array($identifier)) {
236 return $identifier[0];
237 }
238 return $identifier;
239 }
240
241 private function sortRows(Order $order): void
242 {
243 $order_array = $order->get();
244 $key = array_key_first($order_array);
245 $factor = array_shift($order_array) === 'ASC' ? 1 : -1;
246 if ($key === 'field') {
247 usort(
248 $this->available_settings,
249 fn(Setting $v1, Setting $v2): int =>
250 $factor * ($v1->getLabel($this->lng) <=> $v2->getLabel($this->lng))
251 );
252 }
253
254 if ($key === 'export') {
255 usort(
256 $this->available_settings,
257 fn(Setting $v1, Setting $v2): int =>
258 $factor * ($v1->export() <=> $v2->export())
259 );
260 }
261
262 if ($key === 'changeable_by_user') {
263 usort(
264 $this->available_settings,
265 fn(Setting $v1, Setting $v2): int =>
266 $factor * ($v1->isChangeableByUser() <=> $v2->isChangeableByUser())
267 );
268 }
269
270 if ($key === 'changeable_in_local_user_administration') {
271 usort(
272 $this->available_settings,
273 fn(Setting $v1, Setting $v2): int =>
275 );
276 }
277 }
278}
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Builds data types.
Definition: Factory.php:36
Both the subject and the direction need to be specified when expressing an order.
Definition: Order.php:29
A simple class to express a naive range of whole positive numbers.
Definition: Range.php:29
The scope of this class is split ilias-conform URI's into components.
Definition: URI.php:35
Stream factory which enables the user to create streams without the knowledge of the concrete class.
Definition: Streams.php:32
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:41
Class Services.
Definition: Services.php:38
acquireParameters(array $namespace, string ... $names)
Definition: URLBuilder.php:138
getRows(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....
__construct(private readonly \ILIAS\Language\Language $lng, private readonly \ilCtrl $ctrl, private readonly \ilAccess $access, private readonly \ilGlobalTemplateInterface $tpl, private readonly UIFactory $ui_factory, private readonly UIRenderer $ui_renderer, private readonly Refinery $refinery, private readonly ServerRequestInterface $request, private readonly RequestWrapper $request_wrapper, private readonly HttpService $http, private readonly ConfigurationRepository $user_settings_config_repo)
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...
getLabel(Language $lng)
Definition: Setting.php:49
ilSetting $setting
Definition: class.ilias.php:68
Class ilAccessHandler Checks access for ILIAS objects.
Class ilCtrl provides processing control methods.
$http
Definition: deliver.php:30
return['delivery_method'=> 'php',]
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface RequestWrapper.
This describes a Data Table.
Definition: Data.php:33
An entity that renders components to a string output.
Definition: Renderer.php:31
static http()
Fetches the global http state from ILIAS.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31