ILIAS  release_7 Revision v7.30-3-g800a261c036
Notifications.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25use ILIAS\GlobalScreen\Scope\MainMenu\Collector\Renderer\Hasher;
28use JsonException;
29
36{
37 use Hasher;
38
39 public const ADDITIONAL_ACTION = 'additional_action';
47 protected $dic;
56 public const MODE = "mode";
60 public const MODE_OPENED = "opened";
64 public const MODE_CLOSED = "closed";
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";
84 protected $identifiers_to_handle = [];
93
94 public function __construct()
95 {
96 global $DIC;
97 $this->dic = $DIC;
98 }
99
100 public function run() : void
101 {
102 $this->notification_groups = $this->dic->globalScreen()->collector()->notifications()->getNotifications();
103 $this->administrative_notifications = $this->dic->globalScreen()->collector()->notifications(
104 )->getAdministrativeNotifications();
105 $this->identifiers_to_handle = $this->dic->http()->request()->getQueryParams()[self::NOTIFICATION_IDENTIFIERS] ?? [];
106 $this->single_identifier_to_handle = $this->dic->http()->request()->getQueryParams()[self::ITEM_ID] ?? null;
107
108 switch ($this->dic->http()->request()->getQueryParams()[self::MODE] ?? 'none') {
110 $this->handleOpened();
111 break;
113 $this->handleClosed();
114 break;
116 $this->handleRerender();
117 break;
118
119 }
120 }
121
126 private function handleOpened() : void
127 {
128 foreach ($this->notification_groups as $notification_group) {
129 foreach ($notification_group->getNotifications() as $notification) {
130 if (in_array(
131 $this->hash($notification->getProviderIdentification()->serialize()),
132 $this->identifiers_to_handle,
133 true
134 )) {
135 $notification->getOpenedCallable()();
136 }
137 }
138 if (in_array(
139 $this->hash($notification_group->getProviderIdentification()->serialize()),
140 $this->identifiers_to_handle,
141 true
142 )) {
143 $notification_group->getOpenedCallable()();
144 }
145 }
146 }
147
151 private function handleClosed() : void
152 {
153 foreach ($this->notification_groups as $notification_group) {
154 foreach ($notification_group->getNotifications() as $notification) {
155 if ($this->single_identifier_to_handle !== $this->hash(
156 $notification->getProviderIdentification()->serialize()
157 )) {
158 continue;
159 }
160 if (!$notification->hasClosedCallable()) {
161 continue;
162 }
163 $notification->getClosedCallable()();
164 }
165 }
166 foreach ($this->administrative_notifications as $administrative_notification) {
167 if ($this->single_identifier_to_handle !== $this->hash(
168 $administrative_notification->getProviderIdentification()->serialize()
169 )) {
170 continue;
171 }
172 if (!$administrative_notification->hasClosedCallable()) {
173 continue;
174 }
175 $administrative_notification->getClosedCallable()();
176 }
177 }
178
183 private function handleRerender() : void
184 {
185 $notifications = [];
186 $amount = 0;
187 foreach ($this->notification_groups as $group) {
188 $notifications[] = $group->getRenderer($this->dic->ui()->factory())->getNotificationComponentForItem(
189 $group
190 );
191 if ($group->getNewNotificationsCount() > 0) {
192 $amount++;
193 }
194 }
195 $this->dic->http()->saveResponse(
196 $this->dic->http()->response()
197 ->withBody(
199 json_encode([
200 'html' => $this->dic->ui()->renderer()->renderAsync($notifications),
201 'symbol' => $this->dic->ui()->renderer()->render(
202 $this->dic->ui()->factory()->symbol()->glyph()->notification()->withCounter(
203 $this->dic->ui()->factory()->counter()->novelty($amount)
204 )
205 )
206 ])
207 )
208 )
209 ->withHeader(ResponseHeader::CONTENT_TYPE, 'application/json')
210 );
211 $this->dic->http()->sendResponse();
212 $this->dic->http()->close();
213 }
214}
An exception for terminatinating execution or to throw for unit testing.
Customizing of pimple-DIC for ILIAS.
Definition: Container.php:19
Class Streams Stream factory which enables the user to create streams without the knowledge of the co...
Definition: Streams.php:17
static ofString($string)
Creates a new stream with an initial value.
Definition: Streams.php:25
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_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.
global $DIC
Definition: goto.php:24
Interface ResponseHeader.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: gs_content.php:1