ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilADNNotificationUIFormGUI.php
Go to the documentation of this file.
1 <?php
2 
4 
11 {
12  public const F_TITLE = 'title';
13  public const F_BODY = 'body';
14  public const F_TYPE = 'type';
15  public const F_TYPE_DURING_EVENT = 'type_during_event';
16  public const F_EVENT_DATE = 'event_date';
17  public const F_DISPLAY_DATE = 'display_date';
18  public const F_PERMANENT = 'permanent';
19  public const F_POSITION = 'position';
20  public const F_ADDITIONAL_CLASSES = 'additional_classes';
21  public const F_PREVENT_LOGIN = 'prevent_login';
22  public const F_INTERRUPTIVE = 'interruptive';
23  public const F_ALLOWED_USERS = 'allowed_users';
24  public const F_DISMISSABLE = 'dismissable';
25  public const F_LIMIT_TO_ROLES = 'limit_to_roles';
26  public const F_LIMITED_TO_ROLE_IDS = 'limited_to_role_ids';
27  public const F_DISPLAY_DATE_START = 'display_date_start';
28  public const F_DISPLAY_DATE_END = 'display_date_end';
29  public const F_EVENT_DATE_START = 'event_date_start';
30  public const F_EVENT_DATE_END = 'event_date_end';
31  public const F_SHOW_TO_ALL_ROLES = 'show_to_all_roles';
32  public const F_PRESENTATION = 'presentation';
33  private $refinery;
37  protected $notification;
41  protected static $tags = ['a', 'strong', 'ol', 'ul', 'li', 'p'];
42 
46  protected $ctrl;
50  protected $ui;
54  protected $renderer;
58  protected $form;
62  protected $action;
66  protected $request;
70  protected $lng;
71 
77  public function __construct(ilADNNotification $notification, string $action)
78  {
82  global $DIC;
83  $this->ui = $DIC->ui()->factory();
84  $this->renderer = $DIC->ui()->renderer();
85  $this->request = $DIC->http()->request();
86  $this->ctrl = $DIC->ctrl();
87  $this->lng = $DIC->language();
88  $this->notification = $notification;
89  $this->action = $action;
90  $this->refinery = $DIC->refinery();
91  $this->initForm();
92  }
93 
94  protected $called = [];
95 
100  protected function txt(string $var) : string
101  {
102  return $this->lng->txt('msg_' . $var);
103  }
104 
109  protected function infoTxt(string $var) : ?string
110  {
111  return $this->txt($var . '_info');
112  }
113 
117  protected function getDenotations() : array
118  {
119  return [
120  ilADNNotification::TYPE_INFO => $this->txt(self::F_TYPE . '_' . ilADNNotification::TYPE_INFO),
122  ilADNNotification::TYPE_ERROR => $this->txt(self::F_TYPE . '_' . ilADNNotification::TYPE_ERROR),
123  ];
124  }
125 
126  public function getHTML() : string
127  {
128  return $this->renderer->render($this->form);
129  }
130 
131  public function initForm() : void
132  {
133  $field = $this->ui->input()->field();
134  $custom_trafo = function (callable $c) {
135  return $this->refinery->custom()->transformation($c);
136  };
137  $custom_constraint = function (callable $c, string $error) {
138  return $this->refinery->custom()->constraint($c, $error);
139  };
140 
141  // DENOTATION
142  $types = $this->getDenotations();
143  $denotation = $field->select($this->txt(self::F_TYPE), $types, $this->infoTxt(self::F_TYPE))
144  ->withRequired(true)
145  ->withValue($this->notification->getType())
146  ->withAdditionalTransformation(
147  $custom_trafo(function ($v) : void {
148  $this->notification->setType((int) $v);
149  })
150  );
151 
152  // TITLE
153  $title = $field->text($this->txt(self::F_TITLE), $this->infoTxt(self::F_TITLE))
154  ->withRequired(true)
155  ->withValue($this->notification->getTitle())
156  ->withAdditionalTransformation(
157  $custom_trafo(function ($v) : void {
158  $this->notification->setTitle((string) $v);
159  })
160  );
161 
162  // BODY
163  $body = $field->textarea($this->txt(self::F_BODY), $this->infoTxt(self::F_BODY))
164  ->withValue($this->notification->getBody())
165  ->withAdditionalTransformation(
166  $custom_trafo(function ($v) : void {
167  $this->notification->setBody((string) $v);
168  })
169  );
170 
171  // PERMANENT AND DATES
172  $format = (new ILIAS\Data\Factory())->dateFormat()->standard();
173  $str = $format->toString() . ' H:i:s';
174 
175  $display_date_start = $field->dateTime($this->txt(self::F_DISPLAY_DATE_START))
176  ->withUseTime(true)
177  ->withFormat($format)
178  ->withValue($this->notification->getDisplayStart()->format($str))
179  ->withAdditionalTransformation(
180  $custom_trafo(function (?DateTimeImmutable $v) : ?\DateTimeImmutable {
181  $this->notification->setDisplayStart($v ?? new DateTimeImmutable());
182  return $v;
183  })
184  );
185  $display_date_end = $field->dateTime($this->txt(self::F_DISPLAY_DATE_END))
186  ->withUseTime(true)
187  ->withFormat($format)
188  ->withValue($this->notification->getDisplayEnd()->format($str))
189  ->withAdditionalTransformation(
190  $custom_trafo(function (?DateTimeImmutable $v) : ?\DateTimeImmutable {
191  $this->notification->setDisplayEnd($v ?? new DateTimeImmutable());
192  return $v;
193  })
194  );
195  $event_date_start = $field->dateTime($this->txt(self::F_EVENT_DATE_START))
196  ->withUseTime(true)
197  ->withFormat($format)
198  ->withValue($this->notification->getEventStart()->format($str))
199  ->withAdditionalTransformation(
200  $custom_trafo(function (?DateTimeImmutable $v) : ?\DateTimeImmutable {
201  $this->notification->setEventStart($v ?? new DateTimeImmutable());
202  return $v;
203  })
204  );
205  $event_date_end = $field->dateTime($this->txt(self::F_EVENT_DATE_END))
206  ->withUseTime(true)
207  ->withFormat($format)
208  ->withValue($this->notification->getEventEnd()->format($str))
209  ->withAdditionalTransformation(
210  $custom_trafo(function (?DateTimeImmutable $v) : ?\DateTimeImmutable {
211  $this->notification->setEventEnd($v ?? new DateTimeImmutable());
212  return $v;
213  })
214  );
215 
216  $type_during_event = $field->select($this->txt(self::F_TYPE_DURING_EVENT), $types)
217  ->withRequired(true)
218  ->withValue($this->notification->getTypeDuringEvent())
219  ->withAdditionalTransformation(
220  $custom_trafo(function ($v) : void {
221  $this->notification->setTypeDuringEvent((int) $v);
222  })
223  );
224 
225  $permanent = $field->switchableGroup([
226  self::F_PERMANENT . '_yes' => $field->group([], $this->txt(self::F_PERMANENT . '_yes')),
227  self::F_PERMANENT . '_no' => $field->group(
228  [
229  self::F_DISPLAY_DATE_START => $display_date_start,
230  self::F_DISPLAY_DATE_END => $display_date_end,
231  self::F_EVENT_DATE_START => $event_date_start,
232  self::F_EVENT_DATE_END => $event_date_end,
233  self::F_TYPE_DURING_EVENT => $type_during_event
234  ],
235  $this->txt(self::F_PERMANENT . '_no')
236  )
237  ], $this->txt(self::F_PERMANENT), $this->infoTxt(self::F_PERMANENT))
238  ->withValue(
239  $this->notification->isPermanent(
240  ) ? self::F_PERMANENT . '_yes' : self::F_PERMANENT . '_no'
241  )
242  ->withAdditionalTransformation(
243  $custom_trafo(function ($v) {
244  $permanent = isset($v[0]) && $v[0] === self::F_PERMANENT . '_yes';
245  $this->notification->setPermanent($permanent);
246  return $permanent ? null : $v[1];
247  })
248  )
249  ->withAdditionalTransformation(
250  $custom_constraint(static function ($v) : bool {
251  if (is_null($v)) {
252  return true;
253  }
257  $display_start = $v[self::F_DISPLAY_DATE_START];
258  $display_end = $v[self::F_DISPLAY_DATE_END];
259  $event_start = $v[self::F_EVENT_DATE_START];
260  $event_end = $v[self::F_EVENT_DATE_END];
261 
262  if ($display_start >= $display_end) {
263  return false;
264  }
265  if ($event_start >= $event_end) {
266  return false;
267  }
268  if ($event_start < $display_start) {
269  return false;
270  }
271  return $event_end <= $display_end;
272  }, $this->txt('error_false_date_configuration'))
273  );
274 
275  // DISMISSABLE
276  $dismissable = $field->checkbox($this->txt(self::F_DISMISSABLE), $this->infoTxt(self::F_DISMISSABLE))
277  ->withValue($this->notification->getDismissable())
278  ->withAdditionalTransformation(
279  $custom_trafo(function ($v) : void {
280  $this->notification->setDismissable((bool) $v);
281  })
282  );
283 
284  // LIMITED TO ROLES
285  $available_roles = $this->getRoles(ilRbacReview::FILTER_ALL_GLOBAL);
286  $limited_to_role_ids = $field->multiSelect('', $available_roles)
287  ->withValue($this->notification->getLimitedToRoleIds());
288 
289  $value = $this->notification->isLimitToRoles()
290  ? self::F_LIMIT_TO_ROLES
291  : self::F_SHOW_TO_ALL_ROLES;
292 
293  $roles = $field->switchableGroup([
294  self::F_SHOW_TO_ALL_ROLES => $field->group(
295  [],
296  $this->txt(self::F_SHOW_TO_ALL_ROLES)
297  )->withByline($this->infoTxt(self::F_SHOW_TO_ALL_ROLES)),
298  self::F_LIMIT_TO_ROLES => $field->group(
299  [$limited_to_role_ids],
300  $this->txt(self::F_LIMIT_TO_ROLES)
301  )->withByline($this->infoTxt(self::F_LIMIT_TO_ROLES))
302  ], $this->txt(self::F_PRESENTATION))
303  ->withValue($value)
304  ->withAdditionalTransformation(
305  $custom_trafo(function ($v) : void {
306  $limit_to_roles = ($v[0] ?? null) === self::F_LIMIT_TO_ROLES;
307  $limited_to_role_ids = (array) $v[1][0] ?? [];
308  $this->notification->setLimitToRoles($limit_to_roles);
309  $this->notification->setLimitedToRoleIds($limited_to_role_ids);
310  })
311  );
312 
313  // COMPLETE FORM
314  $section = $field->section([
315  self::F_TYPE => $denotation,
316  self::F_TITLE => $title,
317  self::F_BODY => $body,
318  self::F_PERMANENT => $permanent,
319  self::F_DISMISSABLE => $dismissable,
320  self::F_LIMIT_TO_ROLES => $roles,
321  ], $this->txt('form_title'))->withAdditionalTransformation(
322  $custom_trafo(function ($v) : ilADNNotification {
323  return $this->notification;
324  })
325  );
326 
327  $this->form = $this->ui->input()->container()->form()->standard(
328  $this->action,
329  [$section]
330  )->withAdditionalTransformation(
331  $this->refinery->custom()->transformation(function ($v) {
332  return array_shift($v);
333  })
334  );
335  }
336 
337  public function setValuesByPost() : void
338  {
339  global $DIC;
340  $this->form = $this->form->withRequest($DIC->http()->request());
341  }
342 
343  public function fillForm() : void
344  {
345  }
346 
350  protected function fillObject() : bool
351  {
352  $this->notification = $this->form->getData();
353  return $this->notification instanceof ilADNNotification;
354  }
355 
356  public function saveObject() : int
357  {
358  if (!$this->fillObject()) {
359  return false;
360  }
361  if ($this->notification->getId() > 0) {
362  $this->notification->update();
363  } else {
364  $this->notification->create();
365  }
366 
367  return $this->notification->getId();
368  }
369 
374  protected function getRoles($filter) : array
375  {
376  global $DIC;
377  $opt = [];
378  foreach ($DIC->rbac()->review()->getRolesByFilter($filter) as $role) {
379  $opt[$role['obj_id']] = $role['title'] . ' (' . $role['obj_id'] . ')';
380  }
381 
382  return $opt;
383  }
384 }
$c
Definition: cli.php:37
$section
Definition: Utf8Test.php:83
global $DIC
Definition: goto.php:24
Class ilADNNotificationUIFormGUI.
$format
Definition: metadata.php:218
notification()
Definition: notification.php:2
ui()
Definition: ui.php:5
__construct(Container $dic, ilPlugin $plugin)