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