ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilADNNotificationUIFormGUI.php
Go to the documentation of this file.
1 <?php
2 
23 
28 {
29  public const F_TITLE = 'title';
30  public const F_BODY = 'body';
31  public const F_TYPE = 'type';
32  public const F_TYPE_DURING_EVENT = 'type_during_event';
33  public const F_EVENT_DATE = 'event_date';
34  public const F_DISPLAY_DATE = 'display_date';
35  public const F_PERMANENT = 'permanent';
36  public const F_POSITION = 'position';
37  public const F_ADDITIONAL_CLASSES = 'additional_classes';
38  public const F_PREVENT_LOGIN = 'prevent_login';
39  public const F_INTERRUPTIVE = 'interruptive';
40  public const F_ALLOWED_USERS = 'allowed_users';
41  public const F_DISMISSABLE = 'dismissable';
42  public const F_HAS_LANGUAGE_LIMITATION = 'has_language_limitation';
43  public const F_LIMITED_TO_LANGUAGES = 'limited_to_languages';
44  public const F_LIMIT_TO_ROLES = 'limit_to_roles';
45  public const F_LIMITED_TO_ROLE_IDS = 'limited_to_role_ids';
46  public const F_DISPLAY_DATE_START = 'display_date_start';
47  public const F_DISPLAY_DATE_END = 'display_date_end';
48  public const F_EVENT_DATE_START = 'event_date_start';
49  public const F_EVENT_DATE_END = 'event_date_end';
50  public const F_SHOW_TO_ALL_LANGUAGES = 'show_to_all_languages';
51  public const F_SHOW_TO_ALL_ROLES = 'show_to_all_roles';
52  public const F_LANGUAGES = 'languages';
53  public const F_PRESENTATION = 'presentation';
55  protected \ILIAS\UI\Factory $ui;
56  protected \ILIAS\UI\Renderer $renderer;
57  protected ?\ILIAS\UI\Component\Input\Container\Form\Standard $form = null;
58  protected Factory $refinery;
59  protected ilLanguage $lng;
61  protected Services $http;
62 
63  public function __construct(protected ilADNNotification $notification, protected 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();
74  $this->refinery = $DIC->refinery();
75  $this->rbac_review = $DIC->rbac()->review();
76  $this->initForm();
77  }
78 
79  protected function txt(string $var): string
80  {
81  return $this->lng->txt('msg_' . $var);
82  }
83 
84  protected function infoTxt(string $var): string
85  {
86  return $this->txt($var . '_info');
87  }
88 
92  protected function getDenotations(): array
93  {
94  return [
95  ilADNNotification::TYPE_INFO => $this->txt(self::F_TYPE . '_' . ilADNNotification::TYPE_INFO),
97  ilADNNotification::TYPE_ERROR => $this->txt(self::F_TYPE . '_' . ilADNNotification::TYPE_ERROR),
98  ];
99  }
100 
101  public function getHTML(): string
102  {
103  return $this->renderer->render($this->form);
104  }
105 
106  public function initForm(): void
107  {
108  $field = $this->ui->input()->field();
109  $custom_trafo = fn(callable $c): Transformation => $this->refinery->custom()->transformation($c);
110  $custom_constraint = fn(callable $c, string $error): Transformation => $this->refinery->custom()->constraint(
111  $c,
112  $error
113  );
114 
115  // DENOTATION
116  $types = $this->getDenotations();
117  $denotation = $field->select($this->txt(self::F_TYPE), $types, $this->infoTxt(self::F_TYPE))
118  ->withRequired(true)
119  ->withValue($this->notification->getType())
121  $custom_trafo(function ($v): void {
122  $this->notification->setType((int) $v);
123  })
124  );
125 
126  // TITLE
127  $title = $field->text($this->txt(self::F_TITLE), $this->infoTxt(self::F_TITLE))
128  ->withRequired(true)
129  ->withValue($this->notification->getTitle())
131  $custom_trafo(function ($v): void {
132  $this->notification->setTitle((string) $v);
133  })
134  );
135 
136  // BODY
137  $body = $field->markdown(
139  $this->txt(self::F_BODY),
140  $this->infoTxt(self::F_BODY)
141  )
142  ->withValue($this->notification->getBody())
144  $custom_trafo(function ($v): void {
145  $this->notification->setBody((string) $v);
146  })
147  );
148 
149  // PERMANENT AND DATES
150  $format = (new ILIAS\Data\Factory())->dateFormat()->standard();
151  $str = $format->toString() . ' H:i:s';
152 
153  $display_date_start = $field->dateTime($this->txt(self::F_DISPLAY_DATE_START))
154  ->withUseTime(true)
155  ->withFormat($format)
156  ->withValue($this->notification->getDisplayStart()->format($str))
158  $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
159  $this->notification->setDisplayStart($v ?? new DateTimeImmutable());
160  return $v;
161  })
162  );
163  $display_date_end = $field->dateTime($this->txt(self::F_DISPLAY_DATE_END))
164  ->withUseTime(true)
165  ->withFormat($format)
166  ->withValue($this->notification->getDisplayEnd()->format($str))
168  $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
169  $this->notification->setDisplayEnd($v ?? new DateTimeImmutable());
170  return $v;
171  })
172  );
173  $event_date_start = $field->dateTime($this->txt(self::F_EVENT_DATE_START))
174  ->withUseTime(true)
175  ->withFormat($format)
176  ->withValue($this->notification->getEventStart()->format($str))
178  $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
179  $this->notification->setEventStart($v ?? new DateTimeImmutable());
180  return $v;
181  })
182  );
183  $event_date_end = $field->dateTime($this->txt(self::F_EVENT_DATE_END))
184  ->withUseTime(true)
185  ->withFormat($format)
186  ->withValue($this->notification->getEventEnd()->format($str))
188  $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
189  $this->notification->setEventEnd($v ?? new DateTimeImmutable());
190  return $v;
191  })
192  );
193 
194  $type_during_event = $field->select($this->txt(self::F_TYPE_DURING_EVENT), $types)
195  ->withRequired(true)
196  ->withValue($this->notification->getTypeDuringEvent())
198  $custom_trafo(function ($v): void {
199  $this->notification->setTypeDuringEvent((int) $v);
200  })
201  );
202 
203  $permanent = $field->switchableGroup([
204  self::F_PERMANENT . '_yes' => $field->group([], $this->txt(self::F_PERMANENT . '_yes')),
205  self::F_PERMANENT . '_no' => $field->group(
206  [
207  self::F_DISPLAY_DATE_START => $display_date_start,
208  self::F_DISPLAY_DATE_END => $display_date_end,
209  self::F_EVENT_DATE_START => $event_date_start,
210  self::F_EVENT_DATE_END => $event_date_end,
211  self::F_TYPE_DURING_EVENT => $type_during_event
212  ],
213  $this->txt(self::F_PERMANENT . '_no')
214  )
215  ], $this->txt(self::F_PERMANENT), $this->infoTxt(self::F_PERMANENT))
216  ->withValue(
217  $this->notification->isPermanent(
218  ) ? self::F_PERMANENT . '_yes' : self::F_PERMANENT . '_no'
219  )
221  $custom_trafo(function ($v) {
222  $permanent = isset($v[0]) && $v[0] === self::F_PERMANENT . '_yes';
223  $this->notification->setPermanent($permanent);
224  return $permanent ? null : $v[1];
225  })
226  )
227  ->withAdditionalTransformation(
228  $custom_constraint(static function ($v): bool {
229  if (is_null($v)) {
230  return true;
231  }
233  $display_start = $v[self::F_DISPLAY_DATE_START] ?? null;
234  $display_end = $v[self::F_DISPLAY_DATE_END] ?? null;
235  $event_start = $v[self::F_EVENT_DATE_START] ?? null;
236  $event_end = $v[self::F_EVENT_DATE_END] ?? null;
237 
238  if ($display_start >= $display_end) {
239  return false;
240  }
241  if ($event_start >= $event_end) {
242  return false;
243  }
244  if ($event_start < $display_start) {
245  return false;
246  }
247  return $event_end <= $display_end;
248  }, $this->txt('error_false_date_configuration'))
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()
273  ? self::F_HAS_LANGUAGE_LIMITATION
274  : self::F_SHOW_TO_ALL_LANGUAGES;
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()
302  ? self::F_LIMIT_TO_ROLES
303  : self::F_SHOW_TO_ALL_ROLES;
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(function ($v): ilADNNotification {
336  return $this->notification;
337  })
338  );
339 
340  $this->form = $this->ui->input()->container()->form()->standard(
341  $this->action,
342  [$section]
343  )->withAdditionalTransformation(
344  $this->refinery->custom()->transformation(function ($v) {
345  return array_shift($v);
346  })
347  );
348  }
349 
350  public function setValuesByPost(): void
351  {
352  $this->form = $this->form->withRequest($this->http->request());
353  }
354 
355  protected function fillObject(): bool
356  {
357  $data = $this->form->getData();
358  if (!$data instanceof ilADNNotification) {
359  return false;
360  }
361  $this->notification = $data;
362  return true;
363  }
364 
365  public function saveObject(): bool
366  {
367  if (!$this->fillObject()) {
368  return false;
369  }
370  if ($this->notification->getId() > 0) {
371  $this->notification->update();
372  } else {
373  $this->notification->create();
374  }
375 
376  return $this->notification->getId() > 0;
377  }
378 
382  protected function getRoles(int $filter): array
383  {
384  $opt = [];
385  foreach ($this->rbac_review->getRolesByFilter($filter) as $role) {
386  $opt[(int) $role['obj_id']] = $role['title'] . ' (' . $role['obj_id'] . ')';
387  }
388 
389  return $opt;
390  }
391 }
ILIAS UI Component Input Container Form Standard $form
$c
Definition: deliver.php:9
notification()
description: > Example for rendring a notification glyph.
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...
global $DIC
Definition: shib_login.php:25
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:59
form( $class_path, string $cmd, string $submit_caption="")
__construct(Container $dic, ilPlugin $plugin)
A transformation is a function from one datatype to another.