ILIAS  trunk Revision v13.0_alpha-52-g21c366bbdd4
class.ilADNNotificationUIFormGUI.php
Go to the documentation of this file.
1<?php
2
24
29{
30 public const F_TITLE = 'title';
31 public const F_BODY = 'body';
32 public const F_TYPE = 'type';
33 public const F_TYPE_DURING_EVENT = 'type_during_event';
34 public const F_EVENT_DATE = 'event_date';
35 public const F_DISPLAY_DATE = 'display_date';
36 public const F_PERMANENT = 'permanent';
37 public const F_POSITION = 'position';
38 public const F_ADDITIONAL_CLASSES = 'additional_classes';
39 public const F_PREVENT_LOGIN = 'prevent_login';
40 public const F_INTERRUPTIVE = 'interruptive';
41 public const F_ALLOWED_USERS = 'allowed_users';
42 public const F_DISMISSABLE = 'dismissable';
43 public const F_HAS_LANGUAGE_LIMITATION = 'has_language_limitation';
44 public const F_LIMITED_TO_LANGUAGES = 'limited_to_languages';
45 public const F_LIMIT_TO_ROLES = 'limit_to_roles';
46 public const F_LIMITED_TO_ROLE_IDS = 'limited_to_role_ids';
47 public const F_DISPLAY_DATE_START = 'display_date_start';
48 public const F_DISPLAY_DATE_END = 'display_date_end';
49 public const F_EVENT_DATE_START = 'event_date_start';
50 public const F_EVENT_DATE_END = 'event_date_end';
51 public const F_SHOW_TO_ALL_LANGUAGES = 'show_to_all_languages';
52 public const F_SHOW_TO_ALL_ROLES = 'show_to_all_roles';
53 public const F_LANGUAGES = 'languages';
54 public const F_PRESENTATION = 'presentation';
56 protected \ILIAS\UI\Factory $ui;
58 protected ?Standard $form = null;
59 protected Factory $refinery;
60 protected ilLanguage $lng;
62 protected Services $http;
63
64 public function __construct(protected ilADNNotification $notification, protected string $action)
65 {
69 global $DIC;
70 $this->ui = $DIC->ui()->factory();
71 $this->renderer = $DIC->ui()->renderer();
72 $this->http = $DIC->http();
73 $this->ctrl = $DIC->ctrl();
74 $this->lng = $DIC->language();
75 $this->refinery = $DIC->refinery();
76 $this->rbac_review = $DIC->rbac()->review();
77 $this->initForm();
78 }
79
80 protected function txt(string $var): string
81 {
82 return $this->lng->txt('msg_' . $var);
83 }
84
85 protected function infoTxt(string $var): string
86 {
87 return $this->txt($var . '_info');
88 }
89
93 protected function getDenotations(): array
94 {
95 return [
99 ];
100 }
101
102 public function getHTML(): string
103 {
104 return $this->renderer->render($this->form);
105 }
106
107 public function initForm(): void
108 {
109 $field = $this->ui->input()->field();
110 $custom_trafo = fn(callable $c): Transformation => $this->refinery->custom()->transformation($c);
111 $custom_constraint = fn(callable $c, string $error): Transformation => $this->refinery->custom()->constraint(
112 $c,
113 $error
114 );
115
116 // DENOTATION
117 $types = $this->getDenotations();
118 $denotation = $field->select($this->txt(self::F_TYPE), $types, $this->infoTxt(self::F_TYPE))
119 ->withRequired(true)
120 ->withValue($this->notification->getType())
121 ->withAdditionalTransformation(
122 $custom_trafo(function ($v): void {
123 $this->notification->setType((int) $v);
124 })
125 );
126
127 // TITLE
128 $title = $field->text($this->txt(self::F_TITLE), $this->infoTxt(self::F_TITLE))
129 ->withRequired(true)
130 ->withValue($this->notification->getTitle())
131 ->withAdditionalTransformation(
132 $custom_trafo(function ($v): void {
133 $this->notification->setTitle((string) $v);
134 })
135 );
136
137 // BODY
138 $body = $field->markdown(
140 $this->txt(self::F_BODY),
141 $this->infoTxt(self::F_BODY)
142 )
143 ->withValue($this->notification->getBody())
144 ->withAdditionalTransformation(
145 $custom_trafo(function ($v): void {
146 $this->notification->setBody((string) $v);
147 })
148 );
149
150 // PERMANENT AND DATES
151 $format = (new ILIAS\Data\Factory())->dateFormat()->standard();
152 $str = $format->toString() . ' H:i:s';
153
154 $display_date_start = $field->dateTime($this->txt(self::F_DISPLAY_DATE_START))
155 ->withUseTime(true)
156 ->withFormat($format)
157 ->withValue($this->notification->getDisplayStart()->format($str))
159 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
160 $this->notification->setDisplayStart($v ?? new DateTimeImmutable());
161 return $v;
162 })
163 );
164 $display_date_end = $field->dateTime($this->txt(self::F_DISPLAY_DATE_END))
165 ->withUseTime(true)
166 ->withFormat($format)
167 ->withValue($this->notification->getDisplayEnd()->format($str))
169 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
170 $this->notification->setDisplayEnd($v ?? new DateTimeImmutable());
171 return $v;
172 })
173 );
174 // the event is optional: without event dates, an event without duration at the display start
175 // is stored, which is never active, see https://mantis.ilias.de/view.php?id=48295
176 $has_event = $this->notification->getId() > 0
177 && $this->notification->getEventStart()->getTimestamp() < $this->notification->getEventEnd()->getTimestamp();
178 $event_date_start = $field->dateTime($this->txt(self::F_EVENT_DATE_START))
179 ->withUseTime(true)
180 ->withFormat($format)
181 ->withValue($has_event ? $this->notification->getEventStart()->format($str) : null)
183 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
184 if ($v !== null) {
185 $this->notification->setEventStart($v);
186 }
187 return $v;
188 })
189 );
190 $event_date_end = $field->dateTime($this->txt(self::F_EVENT_DATE_END))
191 ->withUseTime(true)
192 ->withFormat($format)
193 ->withValue($has_event ? $this->notification->getEventEnd()->format($str) : null)
195 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
196 if ($v !== null) {
197 $this->notification->setEventEnd($v);
198 }
199 return $v;
200 })
201 );
202
203 $type_during_event = $field->select($this->txt(self::F_TYPE_DURING_EVENT), $types)
204 ->withValue($this->notification->getTypeDuringEvent())
206 $custom_trafo(function ($v): void {
207 if ($v !== null && $v !== '') {
208 $this->notification->setTypeDuringEvent((int) $v);
209 }
210 })
211 );
212
213 $permanent = $field->switchableGroup([
214 self::F_PERMANENT . '_yes' => $field->group([], $this->txt(self::F_PERMANENT . '_yes')),
215 self::F_PERMANENT . '_no' => $field->group(
216 [
217 self::F_DISPLAY_DATE_START => $display_date_start,
218 self::F_DISPLAY_DATE_END => $display_date_end,
219 self::F_EVENT_DATE_START => $event_date_start,
220 self::F_EVENT_DATE_END => $event_date_end,
221 self::F_TYPE_DURING_EVENT => $type_during_event
222 ],
223 $this->txt(self::F_PERMANENT . '_no')
224 )
225 ], $this->txt(self::F_PERMANENT), $this->infoTxt(self::F_PERMANENT))
226 ->withValue(
227 $this->notification->isPermanent(
228 ) ? self::F_PERMANENT . '_yes' : self::F_PERMANENT . '_no'
229 )
231 $custom_trafo(function ($v) {
232 $permanent = isset($v[0]) && $v[0] === self::F_PERMANENT . '_yes';
233 $this->notification->setPermanent($permanent);
234 if (!$permanent
235 && ($v[1][self::F_EVENT_DATE_START] ?? null) === null
236 && ($v[1][self::F_EVENT_DATE_END] ?? null) === null
237 ) {
238 $this->notification->setEventStart($this->notification->getDisplayStart());
239 $this->notification->setEventEnd($this->notification->getDisplayStart());
240 }
241 return $permanent ? null : $v[1];
242 })
243 )
244 ->withAdditionalTransformation(
245 $custom_constraint(
246 static fn($v): bool => self::isValidDateConfiguration($v),
247 $this->txt('error_false_date_configuration')
248 )
249 );
250
251 // DISMISSABLE
252 $dismissable = $field->checkbox($this->txt(self::F_DISMISSABLE), $this->infoTxt(self::F_DISMISSABLE))
253 ->withValue($this->notification->getDismissable())
255 $custom_trafo(function ($v): void {
256 $this->notification->setDismissable((bool) $v);
257 })
258 );
259
260 // LIMITED TO LANGUAGES
261 $installed_languages = $this->lng->getInstalledLanguages();
262 $lang_options = [];
263 $this->lng->loadLanguageModule('meta');
264 foreach ($installed_languages as $installed_language) {
265 $lang_options[$installed_language] = $this->lng->txt("meta_l_" . $installed_language);
266 }
267
268 $limited_to_languages = $field->multiSelect('', $lang_options)->withValue(
269 array_intersect($this->notification->getLimitedToLanguages(), $installed_languages)
270 );
271
272 $lang_value = $this->notification->hasLanguageLimitation()
275
276 $languages = $field->switchableGroup([
277 self::F_SHOW_TO_ALL_LANGUAGES => $field->group(
278 [],
279 $this->txt(self::F_SHOW_TO_ALL_LANGUAGES)
280 ),
281 self::F_HAS_LANGUAGE_LIMITATION => $field->group(
282 [$limited_to_languages],
283 $this->txt(self::F_HAS_LANGUAGE_LIMITATION)
284 )->withByline($this->infoTxt(self::F_HAS_LANGUAGE_LIMITATION))
285 ], $this->txt("languages"))
286 ->withValue($lang_value)
287 ->withAdditionalTransformation(
288 $custom_trafo(function ($v): void {
289 $has_language_limitation = ($v[0] ?? null) === self::F_HAS_LANGUAGE_LIMITATION;
290 $limited_to_languages = (array) ($v[1][0] ?? []);
291 $this->notification->setHasLanguageLimitation($has_language_limitation);
292 $this->notification->setLimitedToLanguages($limited_to_languages);
293 })
294 );
295
296 // LIMITED TO ROLES
297 $available_roles = $this->getRoles(ilRbacReview::FILTER_ALL_GLOBAL);
298 $limited_to_role_ids = $field->multiSelect('', $available_roles)
299 ->withValue($this->notification->getLimitedToRoleIds());
300
301 $value = $this->notification->isLimitToRoles()
304
305 $roles = $field->switchableGroup([
306 self::F_SHOW_TO_ALL_ROLES => $field->group(
307 [],
308 $this->txt(self::F_SHOW_TO_ALL_ROLES)
309 )->withByline($this->infoTxt(self::F_SHOW_TO_ALL_ROLES)),
310 self::F_LIMIT_TO_ROLES => $field->group(
311 [$limited_to_role_ids],
312 $this->txt(self::F_LIMIT_TO_ROLES)
313 )->withByline($this->infoTxt(self::F_LIMIT_TO_ROLES))
314 ], $this->txt(self::F_PRESENTATION))
315 ->withValue($value)
316 ->withAdditionalTransformation(
317 $custom_trafo(function ($v): void {
318 $limit_to_roles = ($v[0] ?? null) === self::F_LIMIT_TO_ROLES;
319 $limited_to_role_ids = (array) ($v[1][0] ?? []);
320 $this->notification->setLimitToRoles($limit_to_roles);
321 $this->notification->setLimitedToRoleIds($limited_to_role_ids);
322 })
323 );
324
325 // COMPLETE FORM
326 $section = $field->section([
327 self::F_TYPE => $denotation,
328 self::F_TITLE => $title,
329 self::F_BODY => $body,
330 self::F_PERMANENT => $permanent,
331 self::F_DISMISSABLE => $dismissable,
332 self::F_HAS_LANGUAGE_LIMITATION => $languages,
333 self::F_LIMIT_TO_ROLES => $roles,
334 ], $this->txt('form_title'))->withAdditionalTransformation(
335 $custom_trafo(fn($v): ilADNNotification => $this->notification)
336 );
337
338 $this->form = $this->ui->input()->container()->form()->standard(
339 $this->action,
340 [$section]
341 )->withAdditionalTransformation(
342 $this->refinery->custom()->transformation(fn($v) => array_shift($v))
343 );
344 }
345
346 public function setValuesByPost(): void
347 {
348 $this->form = $this->form->withRequest($this->http->request());
349 }
350
351 protected function fillObject(): bool
352 {
353 $data = $this->form->getData();
354 if (!$data instanceof ilADNNotification) {
355 return false;
356 }
357 $this->notification = $data;
358 return true;
359 }
360
361 public function saveObject(): bool
362 {
363 if (!$this->fillObject()) {
364 return false;
365 }
366 if ($this->notification->getId() > 0) {
367 $this->notification->update();
368 } else {
369 $this->notification->create();
370 }
371
372 return $this->notification->getId() > 0;
373 }
374
378 protected function getRoles(int $filter): array
379 {
380 $opt = [];
381 foreach ($this->rbac_review->getRolesByFilter($filter) as $role) {
382 $opt[(int) $role['obj_id']] = (string) $role['title'];
383 }
384
385 return $opt;
386 }
387
394 public static function isValidDateConfiguration(?array $v): bool
395 {
396 if (is_null($v)) {
397 return true;
398 }
399 $display_start = $v[self::F_DISPLAY_DATE_START] ?? null;
400 $display_end = $v[self::F_DISPLAY_DATE_END] ?? null;
401 $event_start = $v[self::F_EVENT_DATE_START] ?? null;
402 $event_end = $v[self::F_EVENT_DATE_END] ?? null;
403
404 if ($display_start >= $display_end) {
405 return false;
406 }
407 if ($event_start === null && $event_end === null) {
408 return true;
409 }
410 if ($event_start === null || $event_end === null || $event_start >= $event_end) {
411 return false;
412 }
413 if ($event_start < $display_start) {
414 return false;
415 }
416 return $event_end <= $display_end;
417 }
418}
renderer()
Builds data types.
Definition: Factory.php:36
Class Services.
Definition: Services.php:43
static isValidDateConfiguration(?array $v)
Display dates are required, event dates are optional (both empty), but if given, the event must be wi...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
language handling
class ilRbacReview Contains Review functions of core Rbac.
A transformation is a function from one datatype to another.
This describes a standard form.
Definition: Standard.php:30
An entity that renders components to a string output.
Definition: Renderer.php:31
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$body
Definition: ltiauth.php:41
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
form(?array $class_path, string $cmd, string $submit_caption="")
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
global $DIC
Definition: shib_login.php:26
$data
Definition: storeScorm.php:45