1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Notification;
use Piwik\Notification;
use Piwik\Session;
use Piwik\Session\SessionNamespace;
/**
* Posts and removes UI notifications (see {@link Piwik\Notification} to learn more).
*
*/
class Manager
{
/**
* @var SessionNamespace
*/
private static $session = null;
/**
* @var Notification[]
*/
private static $notifications = array();
/**
* Posts a notification that will be shown in Piwik's status bar. If a notification with the same ID
* has been posted and has not been closed/removed, it will be replaced with `$notification`.
*
* @param string $id A unique identifier for this notification. The ID must be a valid HTML
* element ID. It can only contain alphanumeric characters (underscores can
* be used).
* @param Notification $notification The notification to post.
* @api
*/
public static function notify($id, Notification $notification)
{
self::checkId($id);
self::removeOldestNotificationsIfThereAreTooMany();
self::addNotification($id, $notification);
}
/**
* Removes a posted notification by ID.
*
* @param string $id The notification ID, see {@link notify()}.
*/
public static function cancel($id)
{
self::checkId($id);
self::removeNotification($id);
}
/**
* Removes all temporary notifications.
*
* Call this method after the notifications have been
* displayed to make sure temporary notifications won't be displayed twice.
*/
public static function cancelAllNonPersistent()
{
foreach (static::getAllNotifications() as $id => $notification) {
if (Notification::TYPE_PERSISTENT != $notification->type) {
static::removeNotification($id);
}
}
}
/**
* Determine all notifications that needs to be displayed. They are sorted by priority. Highest priorities first.
* @return \ArrayObject
*/
public static function getAllNotificationsToDisplay()
{
$notifications = static::getAllNotifications();
uasort($notifications, function ($n1, $n2) {
/** @var Notification $n1 */ /** @var Notification $n2 */
if ($n1->getPriority() == $n2->getPriority()) {
return 0;
}
return $n1->getPriority() > $n2->getPriority() ? -1 : 1;
});
return $notifications;
}
/**
* @param $id
* @throws \Exception In case id is empty or if id contains non word characters
*/
private static function checkId($id)
{
if (empty($id)) {
throw new \Exception('Notification ID is empty.');
}
if (!preg_match('/^(\w)*$/', $id)) {
throw new \Exception('Invalid Notification ID given. Only word characters (AlNum + underscore) allowed.');
}
}
private static function addNotification($id, Notification $notification)
{
self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification);
// we store all kinda notifications here so in case the session is not enabled or disabled later there is still
// a chance it gets delivered to the UI during the same request.
self::$notifications[$id] = $notification;
}
private static function saveNotificationAcrossUiRequestsIfNeeded($id, Notification $notification)
{
$isPersistent = $notification->type === Notification::TYPE_PERSISTENT;
if ($isPersistent && self::isSessionEnabled()) {
$session = static::getSession();
$session->notifications[$id] = $notification;
}
}
private static function removeOldestNotificationsIfThereAreTooMany()
{
if (!self::isSessionEnabled()) {
return;
}
$maxNotificationsInSession = 30;
$session = static::getSession();
while (count($session->notifications) >= $maxNotificationsInSession) {
array_shift($session->notifications);
}
}
private static function getAllNotifications()
{
if (!self::isSessionEnabled()) {
return array();
}
$notifications = self::$notifications;
foreach ($notifications as $id => $notification) {
// we copy them over to the session if possible and persist it in case the session was not yet
// writable / enabled at the time the notification was added.
self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification);
}
if (self::isSessionEnabled()) {
$session = static::getSession();
foreach ($session->notifications as $id => $notification) {
$notifications[$id] = $notification;
}
}
return $notifications;
}
private static function removeNotification($id)
{
if (array_key_exists($id, self::$notifications)) {
unset(self::$notifications[$id]);
}
if (self::isSessionEnabled()) {
$session = static::getSession();
if (array_key_exists($id, $session->notifications)) {
unset($session->notifications[$id]);
}
}
}
private static function isSessionEnabled()
{
return Session::isWritable() && Session::isReadable();
}
/**
* @return SessionNamespace
*/
private static function getSession()
{
if (!isset(static::$session)) {
static::$session = new SessionNamespace('notification');
}
if (empty(static::$session->notifications) && self::isSessionEnabled()) {
static::$session->notifications = array();
}
return static::$session;
}
}
|