ILIAS  release_8 Revision v8.23
Notifications.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
22 
28 use JsonException;
29 
36 {
37  use Hasher;
38 
39  public const ADDITIONAL_ACTION = 'additional_action';
43  private ?string $additional_action = null;
44  protected Container $dic;
48  protected array $notification_groups;
52  public const MODE = "mode";
56  public const MODE_OPENED = "opened";
60  public const MODE_CLOSED = "closed";
64  public const MODE_HANDLE_TOAST_ACTION = "toast_action";
68  public const MODE_RERENDER = "rerender";
72  public const ITEM_ID = "item_id";
76  public const NOTIFICATION_IDENTIFIERS = "notification_identifiers";
80  public const NOTIFY_ENDPOINT = "src/GlobalScreen/Client/notify.php";
81  protected array $identifiers_to_handle = [];
82  protected ?string $single_identifier_to_handle = null;
83  protected array $administrative_notifications = [];
87  private array $toasts = [];
88 
89  public function __construct()
90  {
91  global $DIC;
92  $this->dic = $DIC;
93  }
94 
95  public function run(): void
96  {
100  global $DIC;
101  $this->notification_groups = $DIC->globalScreen()->collector()->notifications()->getNotifications();
102  $this->administrative_notifications = $DIC->globalScreen()->collector()->notifications(
103  )->getAdministrativeNotifications();
104  $this->identifiers_to_handle = $DIC->http()->request()->getQueryParams()[self::NOTIFICATION_IDENTIFIERS] ?? [];
105  $this->single_identifier_to_handle = $DIC->http()->request()->getQueryParams()[self::ITEM_ID] ?? null;
106  $this->toasts = $DIC->globalScreen()->collector()->toasts()->getToasts();
107 
108  $query = $DIC->http()->wrapper()->query();
109 
110  $this->additional_action = $query->has(self::ADDITIONAL_ACTION)
111  ? $query->retrieve(
112  self::ADDITIONAL_ACTION,
113  $DIC->refinery()->kindlyTo()->string()
114  )
115  : null;
116 
117  $mode = 'none';
118  if ($query->has(self::MODE)) {
119  $mode = $query->retrieve(self::MODE, $DIC->refinery()->to()->string());
120  }
121 
122  switch ($mode) {
123  case self::MODE_OPENED:
124  $this->handleOpened();
125  break;
126  case self::MODE_CLOSED:
127  $this->handleClosed();
128  break;
129  case self::MODE_RERENDER:
130  $this->handleRerender();
131  break;
132  case self::MODE_HANDLE_TOAST_ACTION:
133  $this->handleToastAction();
134  break;
135  }
136  }
137 
138  private function handleToastAction(): void
139  {
140  foreach ($this->toasts as $toast) {
141  if ($this->hash($toast->getProviderIdentification()->serialize()) === $this->single_identifier_to_handle) {
142  foreach ($toast->getAllToastActions() as $toast_action) {
143  if ($toast_action->getIdentifier() === $this->additional_action) {
144  $callable = $toast_action->getAction();
145  $callable();
146  return;
147  }
148  }
149  }
150  }
151  }
152 
157  private function handleOpened(): void
158  {
159  foreach ($this->notification_groups as $notification_group) {
160  foreach ($notification_group->getNotifications() as $notification) {
161  if (in_array(
162  $this->hash($notification->getProviderIdentification()->serialize()),
163  $this->identifiers_to_handle,
164  true
165  )) {
166  $notification->getOpenedCallable()();
167  }
168  }
169  if (in_array(
170  $this->hash($notification_group->getProviderIdentification()->serialize()),
171  $this->identifiers_to_handle,
172  true
173  )) {
174  $notification_group->getOpenedCallable()();
175  }
176  }
177  }
178 
182  private function handleClosed(): void
183  {
184  foreach ($this->notification_groups as $notification_group) {
185  foreach ($notification_group->getNotifications() as $notification) {
186  if ($this->single_identifier_to_handle !== $this->hash(
187  $notification->getProviderIdentification()->serialize()
188  )) {
189  continue;
190  }
191  if (!$notification->hasClosedCallable()) {
192  continue;
193  }
194  $notification->getClosedCallable()();
195  }
196  }
197  foreach ($this->administrative_notifications as $administrative_notification) {
198  if ($this->single_identifier_to_handle !== $this->hash(
199  $administrative_notification->getProviderIdentification()->serialize()
200  )) {
201  continue;
202  }
203  if (!$administrative_notification->hasClosedCallable()) {
204  continue;
205  }
206  $administrative_notification->getClosedCallable()();
207  }
208  }
209 
214  private function handleRerender(): void
215  {
216  $notifications = [];
217  $amount = 0;
218  foreach ($this->notification_groups as $group) {
219  $notifications[] = $group->getRenderer($this->dic->ui()->factory())->getNotificationComponentForItem(
220  $group
221  );
222  if ($group->getNewNotificationsCount() > 0) {
223  $amount++;
224  }
225  }
226  $this->dic->http()->saveResponse(
227  $this->dic->http()->response()
228  ->withBody(
230  json_encode([
231  'html' => $this->dic->ui()->renderer()->renderAsync($notifications),
232  'symbol' => $this->dic->ui()->renderer()->render(
233  $this->dic->ui()->factory()->symbol()->glyph()->notification()->withCounter(
234  $this->dic->ui()->factory()->counter()->novelty($amount)
235  )
236  )
237  ], JSON_THROW_ON_ERROR)
238  )
239  )
240  ->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json')
241  );
242  $this->dic->http()->sendResponse();
243  $this->dic->http()->close();
244  }
245 }
array $notification_groups
Collected set of collected notifications.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: gs_content.php:1
const MODE_CLOSED
Value of the MODE GET param, if the Notification Center has been closed.
const MODE_HANDLE_TOAST_ACTION
Value of the MODE GET param, if a ToastLik has been klicked.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:31
const ITEM_ID
NAME of the GET param, to indicate the item ID of the closed item.
global $DIC
Definition: feed.php:28
handleOpened()
Loops through all available open callable provided by the notification providers. ...
const NOTIFICATION_IDENTIFIERS
Used to read the identifiers out of the GET param later.
const MODE_OPENED
Value of the MODE GET param, if the Notification Center has been opened.
const NOTIFY_ENDPOINT
Location of the endpoint handling async notification requests.
const MODE
Name of the GET param used in the async calls.
$query
static ofString(string $string)
Creates a new stream with an initial value.
Definition: Streams.php:43
handleClosed()
Runs the closed callable if such a callable is provided.
const MODE_RERENDER
Value of the MODE GET param, if the Notification Center should be rerendered.