ILIAS  release_8 Revision v8.24
class.ilADNNotificationUIFormGUI.php
Go to the documentation of this file.
1<?php
2
22
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_LIMIT_TO_ROLES = 'limit_to_roles';
44 public const F_LIMITED_TO_ROLE_IDS = 'limited_to_role_ids';
45 public const F_DISPLAY_DATE_START = 'display_date_start';
46 public const F_DISPLAY_DATE_END = 'display_date_end';
47 public const F_EVENT_DATE_START = 'event_date_start';
48 public const F_EVENT_DATE_END = 'event_date_end';
49 public const F_SHOW_TO_ALL_ROLES = 'show_to_all_roles';
50 public const F_PRESENTATION = 'presentation';
51 protected string $action;
52
55 protected \ILIAS\UI\Factory $ui;
56 protected \ILIAS\UI\Renderer $renderer;
58 protected Factory $refinery;
59 protected ilLanguage $lng;
61 protected Services $http;
62
63 public function __construct(ilADNNotification $notification, string $action)
64 {
68 global $DIC;
69 $this->ui = $DIC->ui()->factory();
70 $this->renderer = $DIC->ui()->renderer();
71 $this->http = $DIC->http();
72 $this->ctrl = $DIC->ctrl();
73 $this->lng = $DIC->language();
75 $this->action = $action;
76 $this->refinery = $DIC->refinery();
77 $this->rbac_review = $DIC->rbac()->review();
78 $this->initForm();
79 }
80
81 protected function txt(string $var): string
82 {
83 return $this->lng->txt('msg_' . $var);
84 }
85
86 protected function infoTxt(string $var): string
87 {
88 return $this->txt($var . '_info');
89 }
90
94 protected function getDenotations(): array
95 {
96 return [
100 ];
101 }
102
103 public function getHTML(): string
104 {
105 return $this->renderer->render($this->form);
106 }
107
108 public function initForm(): void
109 {
110 $field = $this->ui->input()->field();
111 $custom_trafo = fn (callable $c) => $this->refinery->custom()->transformation($c);
112 $custom_constraint = fn (callable $c, string $error) => $this->refinery->custom()->constraint($c, $error);
113
114 // DENOTATION
115 $types = $this->getDenotations();
116 $denotation = $field->select($this->txt(self::F_TYPE), $types, $this->infoTxt(self::F_TYPE))
117 ->withRequired(true)
118 ->withValue($this->notification->getType())
119 ->withAdditionalTransformation(
120 $custom_trafo(function ($v): void {
121 $this->notification->setType((int) $v);
122 })
123 );
124
125 // TITLE
126 $title = $field->text($this->txt(self::F_TITLE), $this->infoTxt(self::F_TITLE))
127 ->withRequired(true)
128 ->withValue($this->notification->getTitle())
129 ->withAdditionalTransformation(
130 $custom_trafo(function ($v): void {
131 $this->notification->setTitle((string) $v);
132 })
133 );
134
135 // BODY
136 $body = $field->textarea($this->txt(self::F_BODY), $this->infoTxt(self::F_BODY))
137 ->withValue($this->notification->getBody())
138 ->withAdditionalTransformation(
139 $custom_trafo(function ($v): void {
140 $this->notification->setBody((string) $v);
141 })
142 );
143
144 // PERMANENT AND DATES
145 $format = (new ILIAS\Data\Factory())->dateFormat()->standard();
146 $str = $format->toString() . ' H:i:s';
147
148 $display_date_start = $field->dateTime($this->txt(self::F_DISPLAY_DATE_START))
149 ->withUseTime(true)
150 ->withFormat($format)
151 ->withValue($this->notification->getDisplayStart()->format($str))
153 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
154 $this->notification->setDisplayStart($v ?? new DateTimeImmutable());
155 return $v;
156 })
157 );
158 $display_date_end = $field->dateTime($this->txt(self::F_DISPLAY_DATE_END))
159 ->withUseTime(true)
160 ->withFormat($format)
161 ->withValue($this->notification->getDisplayEnd()->format($str))
163 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
164 $this->notification->setDisplayEnd($v ?? new DateTimeImmutable());
165 return $v;
166 })
167 );
168 $event_date_start = $field->dateTime($this->txt(self::F_EVENT_DATE_START))
169 ->withUseTime(true)
170 ->withFormat($format)
171 ->withValue($this->notification->getEventStart()->format($str))
173 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
174 $this->notification->setEventStart($v ?? new DateTimeImmutable());
175 return $v;
176 })
177 );
178 $event_date_end = $field->dateTime($this->txt(self::F_EVENT_DATE_END))
179 ->withUseTime(true)
180 ->withFormat($format)
181 ->withValue($this->notification->getEventEnd()->format($str))
183 $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
184 $this->notification->setEventEnd($v ?? new DateTimeImmutable());
185 return $v;
186 })
187 );
188
189 $type_during_event = $field->select($this->txt(self::F_TYPE_DURING_EVENT), $types)
190 ->withRequired(true)
191 ->withValue($this->notification->getTypeDuringEvent())
193 $custom_trafo(function ($v): void {
194 $this->notification->setTypeDuringEvent((int) $v);
195 })
196 );
197
198 $permanent = $field->switchableGroup([
199 self::F_PERMANENT . '_yes' => $field->group([], $this->txt(self::F_PERMANENT . '_yes')),
200 self::F_PERMANENT . '_no' => $field->group(
201 [
202 self::F_DISPLAY_DATE_START => $display_date_start,
203 self::F_DISPLAY_DATE_END => $display_date_end,
204 self::F_EVENT_DATE_START => $event_date_start,
205 self::F_EVENT_DATE_END => $event_date_end,
206 self::F_TYPE_DURING_EVENT => $type_during_event
207 ],
208 $this->txt(self::F_PERMANENT . '_no')
209 )
210 ], $this->txt(self::F_PERMANENT), $this->infoTxt(self::F_PERMANENT))
211 ->withValue(
212 $this->notification->isPermanent(
213 ) ? self::F_PERMANENT . '_yes' : self::F_PERMANENT . '_no'
214 )
216 $custom_trafo(function ($v) {
217 $permanent = isset($v[0]) && $v[0] === self::F_PERMANENT . '_yes';
218 $this->notification->setPermanent($permanent);
219 return $permanent ? null : $v[1];
220 })
221 )
222 ->withAdditionalTransformation(
223 $custom_constraint(static function ($v): bool {
224 if (is_null($v)) {
225 return true;
226 }
230 $display_start = $v[self::F_DISPLAY_DATE_START];
231 $display_end = $v[self::F_DISPLAY_DATE_END];
232 $event_start = $v[self::F_EVENT_DATE_START];
233 $event_end = $v[self::F_EVENT_DATE_END];
234
235 if ($display_start >= $display_end) {
236 return false;
237 }
238 if ($event_start >= $event_end) {
239 return false;
240 }
241 if ($event_start < $display_start) {
242 return false;
243 }
244 return $event_end <= $display_end;
245 }, $this->txt('error_false_date_configuration'))
246 );
247
248 // DISMISSABLE
249 $dismissable = $field->checkbox($this->txt(self::F_DISMISSABLE), $this->infoTxt(self::F_DISMISSABLE))
250 ->withValue($this->notification->getDismissable())
252 $custom_trafo(function ($v): void {
253 $this->notification->setDismissable((bool) $v);
254 })
255 );
256
257 // LIMITED TO ROLES
258 $available_roles = $this->getRoles(ilRbacReview::FILTER_ALL_GLOBAL);
259 $limited_to_role_ids = $field->multiSelect('', $available_roles)
260 ->withValue($this->notification->getLimitedToRoleIds());
261
262 $value = $this->notification->isLimitToRoles()
265
266 $roles = $field->switchableGroup([
267 self::F_SHOW_TO_ALL_ROLES => $field->group(
268 [],
269 $this->txt(self::F_SHOW_TO_ALL_ROLES)
270 )->withByline($this->infoTxt(self::F_SHOW_TO_ALL_ROLES)),
271 self::F_LIMIT_TO_ROLES => $field->group(
272 [$limited_to_role_ids],
273 $this->txt(self::F_LIMIT_TO_ROLES)
274 )->withByline($this->infoTxt(self::F_LIMIT_TO_ROLES))
275 ], $this->txt(self::F_PRESENTATION))
276 ->withValue($value)
277 ->withAdditionalTransformation(
278 $custom_trafo(function ($v): void {
279 $limit_to_roles = ($v[0] ?? null) === self::F_LIMIT_TO_ROLES;
280 $limited_to_role_ids = (array) ($v[1][0] ?? []);
281 $this->notification->setLimitToRoles($limit_to_roles);
282 $this->notification->setLimitedToRoleIds($limited_to_role_ids);
283 })
284 );
285
286 // COMPLETE FORM
287 $section = $field->section([
288 self::F_TYPE => $denotation,
289 self::F_TITLE => $title,
290 self::F_BODY => $body,
291 self::F_PERMANENT => $permanent,
292 self::F_DISMISSABLE => $dismissable,
293 self::F_LIMIT_TO_ROLES => $roles,
294 ], $this->txt('form_title'))->withAdditionalTransformation(
295 $custom_trafo(function ($v): ilADNNotification {
296 return $this->notification;
297 })
298 );
299
300 $this->form = $this->ui->input()->container()->form()->standard(
301 $this->action,
302 [$section]
303 )->withAdditionalTransformation(
304 $this->refinery->custom()->transformation(function ($v) {
305 return array_shift($v);
306 })
307 );
308 }
309
310 public function setValuesByPost(): void
311 {
312 $this->form = $this->form->withRequest($this->http->request());
313 }
314
315 protected function fillObject(): bool
316 {
317 $data = $this->form->getData();
318 if (!$data instanceof ilADNNotification) {
319 return false;
320 }
321 $this->notification = $data;
322 return true;
323 }
324
325 public function saveObject(): bool
326 {
327 if (!$this->fillObject()) {
328 return false;
329 }
330 if ($this->notification->getId() > 0) {
331 $this->notification->update();
332 } else {
333 $this->notification->create();
334 }
335
336 return $this->notification->getId() > 0;
337 }
338
342 protected function getRoles(int $filter): array
343 {
344 $opt = [];
345 foreach ($this->rbac_review->getRolesByFilter($filter) as $role) {
346 $opt[(int) $role['obj_id']] = $role['title'] . ' (' . $role['obj_id'] . ')';
347 }
348
349 return $opt;
350 }
351}
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:32
Builds data types.
Definition: Factory.php:21
Class Services.
Definition: Services.php:38
Class ilADNNotificationUIFormGUI.
ILIAS UI Component Input Container Form Standard $form
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.
$c
Definition: cli.php:38
global $DIC
Definition: feed.php:28
This describes a standard form.
Definition: Standard.php:27
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$format
Definition: metadata.php:235
static http()
Fetches the global http state from ILIAS.
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
form( $class_path, string $cmd)
withAdditionalTransformation(Transformation $trafo)
@inheritDoc
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59