|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FFMVC\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use FFMVC\{Helpers, Models}; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Notifications Helper Class. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Vijay Mahrra <[email protected]> |
|
11
|
|
|
* @copyright (c) Copyright 2006-2015 Vijay Mahrra |
|
12
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
|
13
|
|
|
*/ |
|
14
|
|
|
class Notifications extends \Prefab |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
public static $types = [ |
|
18
|
|
|
'success', |
|
19
|
|
|
'danger', |
|
20
|
|
|
'warning', |
|
21
|
|
|
'info', |
|
22
|
|
|
'debug' |
|
23
|
|
|
]; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* initialise notifications |
|
27
|
|
|
* |
|
28
|
|
|
* @param bool $saveState |
|
29
|
|
|
* @param array $types |
|
30
|
|
|
* @return array $notifications |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function init(bool $saveState = true, array $types = []): array |
|
33
|
|
|
{ |
|
34
|
|
|
$f3 = \Base::instance(); |
|
35
|
|
|
$cli = $f3->get('CLI'); |
|
36
|
|
|
$notifications = $f3->get('notifications'); |
|
37
|
|
|
|
|
38
|
|
|
if (empty($notifications)) { |
|
39
|
|
|
$notifications = []; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!empty($types)) { |
|
43
|
|
|
static::$types = $types; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
foreach (static::$types as $type) { |
|
47
|
|
|
if (!array_key_exists($type, $notifications)) { |
|
48
|
|
|
$notifications[$type] = []; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$f3->set('notifications', $notifications); |
|
53
|
|
|
|
|
54
|
|
|
if (!$cli) { |
|
55
|
|
|
$f3->set('notifications_save_state', $saveState); // save notifications in session? |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $notifications; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Clear all notifications |
|
64
|
|
|
*/ |
|
65
|
|
|
public static function clear() |
|
66
|
|
|
{ |
|
67
|
|
|
$f3 = \Base::instance(); |
|
68
|
|
|
$notifications = []; |
|
69
|
|
|
|
|
70
|
|
|
foreach (static::$types as $type) { |
|
71
|
|
|
$notifications[$type] = []; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$f3->set('notifications', $notifications); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Keep state between requests? |
|
80
|
|
|
* |
|
81
|
|
|
* @param bool $boolean |
|
82
|
|
|
* @return boolean |
|
83
|
|
|
*/ |
|
84
|
|
|
public static function saveState(bool $boolean = true): bool |
|
85
|
|
|
{ |
|
86
|
|
|
$f3 = \Base::instance(); |
|
87
|
|
|
if (empty($f3->get('CLI'))) { |
|
88
|
|
|
$f3 = \Base::instance(); |
|
89
|
|
|
$f3->set('notifications_save_state', $boolean); |
|
90
|
|
|
|
|
91
|
|
|
return true; |
|
92
|
|
|
} else { |
|
93
|
|
|
|
|
94
|
|
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function __destruct() |
|
99
|
|
|
{ |
|
100
|
|
|
$f3 = \Base::instance(); |
|
101
|
|
|
if (empty($f3->get('CLI'))) { |
|
102
|
|
|
$f3 = \Base::instance(); |
|
103
|
|
|
|
|
104
|
|
|
// save persistent notifications |
|
105
|
|
|
$notifications = empty($f3->get('notifications_save_state')) ? null : $f3->get('notifications'); |
|
106
|
|
|
$f3->set('SESSION.notifications', $notifications); |
|
107
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* add a notification, default type is notification |
|
114
|
|
|
* |
|
115
|
|
|
* @param null|string $notification |
|
116
|
|
|
* @param null|string $type |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function add(string $notification, string $type = null) |
|
119
|
|
|
{ |
|
120
|
|
|
$f3 = \Base::instance(); |
|
121
|
|
|
$notifications = $f3->get('notifications'); |
|
122
|
|
|
if (null === $notifications) { |
|
123
|
|
|
$notifications = self::init(); |
|
124
|
|
|
} |
|
125
|
|
|
$type = (empty($type) || !in_array($type, static::$types)) ? 'info' : $type; |
|
126
|
|
|
|
|
127
|
|
|
// don't repeat notifications! |
|
128
|
|
|
if (!in_array($notification, $notifications[$type]) && is_string($notification)) { |
|
129
|
|
|
$notifications[$type][] = $notification; |
|
130
|
|
|
} |
|
131
|
|
|
$f3->set('notifications', $notifications); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* add multiple notifications by type |
|
137
|
|
|
* |
|
138
|
|
|
* @param array $notificationsList |
|
139
|
|
|
*/ |
|
140
|
|
|
public function addMultiple(array $notificationsList) |
|
141
|
|
|
{ |
|
142
|
|
|
$f3 = \Base::instance(); |
|
143
|
|
|
$notifications = $f3->get('notifications'); |
|
144
|
|
|
|
|
145
|
|
|
foreach ($notificationsList as $type => $list) { |
|
146
|
|
|
foreach ($list as $notification) { |
|
147
|
|
|
$notifications[$type][] = $notification; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$f3->set('notifications', $notifications); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* return notifications of given type or all types, return false if none, clearing stack |
|
157
|
|
|
* |
|
158
|
|
|
* @param null|string $type |
|
159
|
|
|
* @param bool $clear |
|
160
|
|
|
* @return boolean|array |
|
161
|
|
|
*/ |
|
162
|
|
|
public static function get(string $type = null, bool $clear = true) |
|
163
|
|
|
{ |
|
164
|
|
|
$f3 = \Base::instance(); |
|
165
|
|
|
$notifications = $f3->get('notifications'); |
|
166
|
|
|
|
|
167
|
|
|
if (!empty($type)) { |
|
168
|
|
|
if (in_array($type, static::$types)) { |
|
169
|
|
|
|
|
170
|
|
|
$i = count($notifications[$type]); |
|
171
|
|
|
if (0 < $i) { |
|
172
|
|
|
return $notifications[$type]; |
|
173
|
|
|
} else { |
|
174
|
|
|
return false; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
} else { |
|
178
|
|
|
return false; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// return false if there actually are no notifications in the session |
|
183
|
|
|
$i = 0; |
|
184
|
|
|
foreach (static::$types as $type) { |
|
185
|
|
|
$i += count($notifications[$type]); |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
if (0 == $i) { |
|
189
|
|
|
return false; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
// order return by order of type array above |
|
193
|
|
|
// i.e. success, error, warning and then informational notifications last |
|
194
|
|
|
$return = []; |
|
195
|
|
|
foreach (static::$types as $type) { |
|
196
|
|
|
$return[$type] = $notifications[$type]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
// clear all notifications |
|
200
|
|
|
if (!empty($clear)) { |
|
201
|
|
|
static::clear(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
return $return; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|