ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
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;
57  protected Renderer $renderer;
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 [
96  ilADNNotification::TYPE_INFO => $this->txt(self::F_TYPE . '_' . ilADNNotification::TYPE_INFO),
98  ilADNNotification::TYPE_ERROR => $this->txt(self::F_TYPE . '_' . ilADNNotification::TYPE_ERROR),
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())
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())
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())
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  $event_date_start = $field->dateTime($this->txt(self::F_EVENT_DATE_START))
175  ->withUseTime(true)
176  ->withFormat($format)
177  ->withValue($this->notification->getEventStart()->format($str))
179  $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
180  $this->notification->setEventStart($v ?? new DateTimeImmutable());
181  return $v;
182  })
183  );
184  $event_date_end = $field->dateTime($this->txt(self::F_EVENT_DATE_END))
185  ->withUseTime(true)
186  ->withFormat($format)
187  ->withValue($this->notification->getEventEnd()->format($str))
189  $custom_trafo(function (?DateTimeImmutable $v): ?\DateTimeImmutable {
190  $this->notification->setEventEnd($v ?? new DateTimeImmutable());
191  return $v;
192  })
193  );
194 
195  $type_during_event = $field->select($this->txt(self::F_TYPE_DURING_EVENT), $types)
196  ->withRequired(true)
197  ->withValue($this->notification->getTypeDuringEvent())
199  $custom_trafo(function ($v): void {
200  $this->notification->setTypeDuringEvent((int) $v);
201  })
202  );
203 
204  $permanent = $field->switchableGroup([
205  self::F_PERMANENT . '_yes' => $field->group([], $this->txt(self::F_PERMANENT . '_yes')),
206  self::F_PERMANENT . '_no' => $field->group(
207  [
208  self::F_DISPLAY_DATE_START => $display_date_start,
209  self::F_DISPLAY_DATE_END => $display_date_end,
210  self::F_EVENT_DATE_START => $event_date_start,
211  self::F_EVENT_DATE_END => $event_date_end,
212  self::F_TYPE_DURING_EVENT => $type_during_event
213  ],
214  $this->txt(self::F_PERMANENT . '_no')
215  )
216  ], $this->txt(self::F_PERMANENT), $this->infoTxt(self::F_PERMANENT))
217  ->withValue(
218  $this->notification->isPermanent(
219  ) ? self::F_PERMANENT . '_yes' : self::F_PERMANENT . '_no'
220  )
222  $custom_trafo(function ($v) {
223  $permanent = isset($v[0]) && $v[0] === self::F_PERMANENT . '_yes';
224  $this->notification->setPermanent($permanent);
225  return $permanent ? null : $v[1];
226  })
227  )
228  ->withAdditionalTransformation(
229  $custom_constraint(static function ($v): bool {
230  if (is_null($v)) {
231  return true;
232  }
234  $display_start = $v[self::F_DISPLAY_DATE_START] ?? null;
235  $display_end = $v[self::F_DISPLAY_DATE_END] ?? null;
236  $event_start = $v[self::F_EVENT_DATE_START] ?? null;
237  $event_end = $v[self::F_EVENT_DATE_END] ?? null;
238 
239  if ($display_start >= $display_end) {
240  return false;
241  }
242  if ($event_start >= $event_end) {
243  return false;
244  }
245  if ($event_start < $display_start) {
246  return false;
247  }
248  return $event_end <= $display_end;
249  }, $this->txt('error_false_date_configuration'))
250  );
251 
252  // DISMISSABLE
253  $dismissable = $field->checkbox($this->txt(self::F_DISMISSABLE), $this->infoTxt(self::F_DISMISSABLE))
254  ->withValue($this->notification->getDismissable())
256  $custom_trafo(function ($v): void {
257  $this->notification->setDismissable((bool) $v);
258  })
259  );
260 
261  // LIMITED TO LANGUAGES
262  $installed_languages = $this->lng->getInstalledLanguages();
263  $lang_options = [];
264  $this->lng->loadLanguageModule('meta');
265  foreach ($installed_languages as $installed_language) {
266  $lang_options[$installed_language] = $this->lng->txt("meta_l_" . $installed_language);
267  }
268 
269  $limited_to_languages = $field->multiSelect('', $lang_options)->withValue(
270  array_intersect($this->notification->getLimitedToLanguages(), $installed_languages)
271  );
272 
273  $lang_value = $this->notification->hasLanguageLimitation()
274  ? self::F_HAS_LANGUAGE_LIMITATION
275  : self::F_SHOW_TO_ALL_LANGUAGES;
276 
277  $languages = $field->switchableGroup([
278  self::F_SHOW_TO_ALL_LANGUAGES => $field->group(
279  [],
280  $this->txt(self::F_SHOW_TO_ALL_LANGUAGES)
281  ),
282  self::F_HAS_LANGUAGE_LIMITATION => $field->group(
283  [$limited_to_languages],
284  $this->txt(self::F_HAS_LANGUAGE_LIMITATION)
285  )->withByline($this->infoTxt(self::F_HAS_LANGUAGE_LIMITATION))
286  ], $this->txt("languages"))
287  ->withValue($lang_value)
288  ->withAdditionalTransformation(
289  $custom_trafo(function ($v): void {
290  $has_language_limitation = ($v[0] ?? null) === self::F_HAS_LANGUAGE_LIMITATION;
291  $limited_to_languages = (array) ($v[1][0] ?? []);
292  $this->notification->setHasLanguageLimitation($has_language_limitation);
293  $this->notification->setLimitedToLanguages($limited_to_languages);
294  })
295  );
296 
297  // LIMITED TO ROLES
298  $available_roles = $this->getRoles(ilRbacReview::FILTER_ALL_GLOBAL);
299  $limited_to_role_ids = $field->multiSelect('', $available_roles)
300  ->withValue($this->notification->getLimitedToRoleIds());
301 
302  $value = $this->notification->isLimitToRoles()
303  ? self::F_LIMIT_TO_ROLES
304  : self::F_SHOW_TO_ALL_ROLES;
305 
306  $roles = $field->switchableGroup([
307  self::F_SHOW_TO_ALL_ROLES => $field->group(
308  [],
309  $this->txt(self::F_SHOW_TO_ALL_ROLES)
310  )->withByline($this->infoTxt(self::F_SHOW_TO_ALL_ROLES)),
311  self::F_LIMIT_TO_ROLES => $field->group(
312  [$limited_to_role_ids],
313  $this->txt(self::F_LIMIT_TO_ROLES)
314  )->withByline($this->infoTxt(self::F_LIMIT_TO_ROLES))
315  ], $this->txt(self::F_PRESENTATION))
316  ->withValue($value)
317  ->withAdditionalTransformation(
318  $custom_trafo(function ($v): void {
319  $limit_to_roles = ($v[0] ?? null) === self::F_LIMIT_TO_ROLES;
320  $limited_to_role_ids = (array) ($v[1][0] ?? []);
321  $this->notification->setLimitToRoles($limit_to_roles);
322  $this->notification->setLimitedToRoleIds($limited_to_role_ids);
323  })
324  );
325 
326  // COMPLETE FORM
327  $section = $field->section([
328  self::F_TYPE => $denotation,
329  self::F_TITLE => $title,
330  self::F_BODY => $body,
331  self::F_PERMANENT => $permanent,
332  self::F_DISMISSABLE => $dismissable,
333  self::F_HAS_LANGUAGE_LIMITATION => $languages,
334  self::F_LIMIT_TO_ROLES => $roles,
335  ], $this->txt('form_title'))->withAdditionalTransformation(
336  $custom_trafo(fn($v): ilADNNotification => $this->notification)
337  );
338 
339  $this->form = $this->ui->input()->container()->form()->standard(
340  $this->action,
341  [$section]
342  )->withAdditionalTransformation(
343  $this->refinery->custom()->transformation(fn($v) => array_shift($v))
344  );
345  }
346 
347  public function setValuesByPost(): void
348  {
349  $this->form = $this->form->withRequest($this->http->request());
350  }
351 
352  protected function fillObject(): bool
353  {
354  $data = $this->form->getData();
355  if (!$data instanceof ilADNNotification) {
356  return false;
357  }
358  $this->notification = $data;
359  return true;
360  }
361 
362  public function saveObject(): bool
363  {
364  if (!$this->fillObject()) {
365  return false;
366  }
367  if ($this->notification->getId() > 0) {
368  $this->notification->update();
369  } else {
370  $this->notification->create();
371  }
372 
373  return $this->notification->getId() > 0;
374  }
375 
379  protected function getRoles(int $filter): array
380  {
381  $opt = [];
382  foreach ($this->rbac_review->getRolesByFilter($filter) as $role) {
383  $opt[(int) $role['obj_id']] = $role['title'] . ' (' . $role['obj_id'] . ')';
384  }
385 
386  return $opt;
387  }
388 }
renderer()
$c
Definition: deliver.php:25
notification()
description: > Example for rendring a notification glyph.
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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:22
withValue($value)
Get an input like this with another value displayed on the client side.
Definition: Group.php:61
This describes a standard form.
Definition: Standard.php:28
form( $class_path, string $cmd, string $submit_caption="")
__construct(Container $dic, ilPlugin $plugin)
A transformation is a function from one datatype to another.