ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ConfigurationGUI.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\User\Settings;
22
24use ILIAS\User\RedirectOnMissingWrite;
26use ILIAS\UI\Factory as UIFactory;
27use ILIAS\UI\Renderer as UIRenderer;
28use ILIAS\UI\Component\Modal\RoundTrip as RoundTripModal;
29use ILIAS\UI\Component\Table\Data as DataTable;
37use ILIAS\Refinery\Factory as Refinery;
39use ILIAS\HTTP\Services as HttpService;
41use Psr\Http\Message\ServerRequestInterface;
42
44{
45 use RedirectOnMissingWrite;
46
47 private readonly URLBuilder $url_builder;
50
51 private array $available_settings;
52
53 public function __construct(
54 private readonly \ILIAS\Language\Language $lng,
55 private readonly \ilCtrl $ctrl,
56 private readonly \ilAccess $access,
57 private readonly \ilGlobalTemplateInterface $tpl,
58 private readonly UIFactory $ui_factory,
59 private readonly UIRenderer $ui_renderer,
60 private readonly Refinery $refinery,
61 private readonly ServerRequestInterface $request,
62 private readonly RequestWrapper $request_wrapper,
63 private readonly HttpService $http,
64 private readonly Repository $user_settings_repository
65 ) {
66 $this->available_settings = $this->user_settings_repository->get();
67
68 $url_builder = new URLBuilder(new URI(ILIAS_HTTP_PATH . '/' . $this->ctrl->getLinkTargetByClass(self::class, 'action')));
69 [
74 ['user', 'settings'],
75 'table_action',
76 'setting'
77 );
78 }
79
80 public function executeCommand(): void
81 {
82 $this->redirectOnMissingWrite($this->access, $this->ctrl, $this->tpl, $this->lng);
83 $cmd = $this->ctrl->getCmd() . 'Cmd';
84 $this->$cmd();
85 }
86
87 public function showCmd(?RoundTripModal $modal = null): void
88 {
89 $content = [
90 $this->buildTable()
91 ];
92
93 if ($modal !== null) {
94 $content[] = $modal;
95 }
96
97 $this->tpl->setContent(
98 $this->ui_renderer->render($content)
99 );
100 }
101
102 public function actionCmd(): void
103 {
104 $this->http->saveResponse(
105 $this->http->response()->withBody(
107 $this->ui_renderer->renderAsync($this->buildEditModal())
108 )
109 )
110 );
111 $this->http->sendResponse();
112 $this->http->close();
113 }
114
115 public function saveCmd(): void
116 {
117 $modal = $this->buildEditModal()->withRequest($this->request);
118 $data = $modal->getData();
119 if ($data === null) {
120 $this->showCmd(
121 $modal->withOnLoad($modal->getShowSignal())
122 );
123 return;
124 }
125
126 $this->user_settings_repository->storeConfiguration($data['setting']);
127 $this->available_settings = $this->user_settings_repository->get();
128 $this->tpl->setOnScreenMessage('success', $this->lng->txt('usr_settings_saved'));
129 $this->showCmd();
130 }
131
132 public function getRows(
133 DataRowBuilder $row_builder,
134 array $visible_column_ids,
136 Order $order,
137 ?array $filter_data,
138 ?array $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 ?array $filter_data,
151 ?array $additional_parameters
152 ): ?int {
153 return count($this->available_settings);
154 }
155
156 private function buildTable(): DataTable
157 {
158 return $this->ui_factory->table()->data(
159 $this,
160 $this->lng->txt('user_settings'),
161 $this->getColumns()
162 )->withActions([
163 $this->ui_factory->table()->action()->single(
164 $this->lng->txt('edit_setting'),
165 $this->url_builder,
166 $this->setting_id_token
167 )->withAsync(true)
168 ])->withRequest($this->request);
169 }
170
171 private function getColumns(): array
172 {
173 $cf = $this->ui_factory->table()->column();
174 return [
175 'field' => $cf->text($this->lng->txt('user_field'))->withIsSortable(true),
176 'changeable_by_user' => $cf->boolean(
177 $this->lng->txt(
178 PropertyAttributes::ChangeableByUser->value
179 ),
180 $this->ui_factory->symbol()->glyph()->checked(),
181 $this->ui_factory->symbol()->glyph()->unchecked()
182 )->withIsSortable(true),
183 'changeable_in_local_user_administration' => $cf->boolean(
184 $this->lng->txt(
185 PropertyAttributes::ChangeableInLocalUserAdministration->value
186 ),
187 $this->ui_factory->symbol()->glyph()->checked(),
188 $this->ui_factory->symbol()->glyph()->unchecked()
189 )->withIsSortable(true),
190 'export' => $cf->boolean(
191 $this->lng->txt(
192 PropertyAttributes::Export->value
193 ),
194 $this->ui_factory->symbol()->glyph()->checked(),
195 $this->ui_factory->symbol()->glyph()->unchecked()
196 )->withIsSortable(true)
197 ];
198 }
199
200 private function buildEditModal(): RoundTripModal
201 {
202 $identifier = $this->retrieveIdentifierFromQuery();
203 $this->ctrl->setParameterByClass(self::class, $this->setting_id_token->getName(), $identifier);
204 return $this->ui_factory->modal()->roundtrip(
205 $this->lng->txt('edit_setting'),
206 null,
207 $this->user_settings_repository->getByIdentifier(
208 $identifier
209 )->getForm(
210 $this->lng,
211 $this->ui_factory->input()->field(),
212 $this->refinery
213 ),
214 $this->ctrl->getFormActionByClass(self::class, 'save')
215 );
216 }
217
218 private function retrieveIdentifierFromQuery(): string
219 {
220 $identifier = $this->request_wrapper->retrieve(
221 $this->setting_id_token->getName(),
222 $this->refinery->byTrying([
223 $this->refinery->kindlyTo()->string(),
224 $this->refinery->kindlyTo()->listOf(
225 $this->refinery->kindlyTo()->string()
226 )
227 ])
228 );
229
230 if (is_array($identifier)) {
231 return $identifier[0];
232 }
233 return $identifier;
234 }
235
236 private function sortRows(Order $order): void
237 {
238 $order_array = $order->get();
239 $key = array_key_first($order_array);
240 $factor = array_shift($order_array) === 'ASC' ? 1 : -1;
241 if ($key === 'field') {
242 usort(
243 $this->available_settings,
244 fn(Setting $v1, Setting $v2): int =>
245 $factor * ($v1->getLabel($this->lng) <=> $v2->getLabel($this->lng))
246 );
247 }
248
249 if ($key === 'export') {
250 usort(
251 $this->available_settings,
252 fn(Setting $v1, Setting $v2): int =>
253 $factor * ($v1->export() <=> $v2->export())
254 );
255 }
256
257 if ($key === 'changeable_by_user') {
258 usort(
259 $this->available_settings,
260 fn(Setting $v1, Setting $v2): int =>
261 $factor * ($v1->isChangeableByUser() <=> $v2->isChangeableByUser())
262 );
263 }
264
265 if ($key === 'changeable_in_local_user_administration') {
266 usort(
267 $this->available_settings,
268 fn(Setting $v1, Setting $v2): int =>
270 );
271 }
272 }
273}
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
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...
__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 Repository $user_settings_repository)
getRows(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....
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:31
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