1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* TFlashMessages.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:FlashMessages! |
9
|
|
|
* @subpackage common |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 01.02.15 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace IPub\FlashMessages; |
16
|
|
|
|
17
|
|
|
use Nette; |
18
|
|
|
use Nette\Localization; |
19
|
|
|
|
20
|
|
|
use Kdyby; |
21
|
|
|
use Kdyby\Translation; |
22
|
|
|
|
23
|
|
|
use IPub; |
24
|
|
|
use IPub\FlashMessages\Adapters; |
25
|
|
|
use IPub\FlashMessages\Entities; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Flash message notifier |
29
|
|
|
* |
30
|
|
|
* @package iPublikuj:FlashMessages! |
31
|
|
|
* @subpackage common |
32
|
|
|
* |
33
|
|
|
* @author Adam Kadlec <[email protected]> |
34
|
|
|
*/ |
35
|
|
|
class FlashNotifier extends Nette\Object |
36
|
1 |
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var SessionStorage |
39
|
|
|
*/ |
40
|
|
|
protected $sessionStorage; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Localization\ITranslator |
44
|
|
|
*/ |
45
|
|
|
protected $translator; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param SessionStorage $sessionStorage |
49
|
|
|
* @param Localization\ITranslator $translator |
50
|
|
|
*/ |
51
|
|
|
public function __construct(SessionStorage $sessionStorage, Localization\ITranslator $translator = NULL) |
52
|
|
|
{ |
53
|
1 |
|
$this->sessionStorage = $sessionStorage; |
54
|
1 |
|
$this->translator = $translator; |
55
|
1 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Flash a success message |
59
|
|
|
* |
60
|
1 |
|
* @param string $message |
61
|
|
|
* @param string|null $title |
62
|
|
|
* |
63
|
|
|
* @return Entities\IMessage |
64
|
|
|
*/ |
65
|
|
View Code Duplication |
public function success($message, $title = NULL) |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$args = func_get_args(); |
68
|
|
|
array_splice($args, 1, 0, ['success']); |
69
|
|
|
|
70
|
|
|
return call_user_func_array([$this, 'setMessage'], $args); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Flash an information message |
75
|
1 |
|
* |
76
|
|
|
* @param string $message |
77
|
|
|
* @param string|null $title |
78
|
|
|
* |
79
|
|
|
* @return Entities\IMessage |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function info($message, $title = NULL) |
|
|
|
|
82
|
|
|
{ |
83
|
1 |
|
$args = func_get_args(); |
84
|
1 |
|
array_splice($args, 1, 0, ['info']); |
85
|
|
|
|
86
|
1 |
|
return call_user_func_array([$this, 'setMessage'], $args); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Flash a warning message |
91
|
|
|
* |
92
|
|
|
* @param string $message |
93
|
|
|
* @param string|null $title |
94
|
|
|
* |
95
|
|
|
* @return Entities\IMessage |
96
|
|
|
*/ |
97
|
|
View Code Duplication |
public function warning($message, $title = NULL) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$args = func_get_args(); |
100
|
|
|
array_splice($args, 1, 0, ['warning']); |
101
|
|
|
|
102
|
|
|
return call_user_func_array([$this, 'setMessage'], $args); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Flash an error message |
107
|
|
|
* |
108
|
|
|
* @param string $message |
109
|
|
|
* @param string|null $title |
110
|
|
|
* |
111
|
|
|
* @return Entities\IMessage |
112
|
|
|
*/ |
113
|
|
View Code Duplication |
public function error($message, $title = NULL) |
|
|
|
|
114
|
|
|
{ |
115
|
|
|
$args = func_get_args(); |
116
|
|
|
array_splice($args, 1, 0, ['danger']); |
117
|
|
|
|
118
|
|
|
return call_user_func_array([$this, 'setMessage'], $args); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Add an "important" flash to the session |
123
|
|
|
* |
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function important() |
127
|
|
|
{ |
128
|
|
|
$this->sessionStorage->set(SessionStorage::KEY_IMPORTANT, TRUE); |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Flash an overlay modal |
135
|
|
|
* |
136
|
|
|
* @param string $message |
137
|
|
|
* @param string $title |
138
|
|
|
* |
139
|
|
|
* @return Entities\IMessage |
140
|
|
|
*/ |
141
|
|
|
public function overlay($message, $title = NULL) |
|
|
|
|
142
|
|
|
{ |
143
|
1 |
|
$args = func_get_args(); |
144
|
|
|
|
145
|
1 |
|
$level = $args[1]; |
146
|
|
|
|
147
|
1 |
|
if (is_string($level) === FALSE || $level === NULL |
148
|
1 |
|
|| !in_array($level, [Entities\IMessage::LEVEL_ERROR, Entities\IMessage::LEVEL_INFO, Entities\IMessage::LEVEL_SUCCESS, Entities\IMessage::LEVEL_WARNING]) |
149
|
1 |
|
) { |
150
|
1 |
|
array_splice($args, 1, 0, ['info']); |
151
|
1 |
|
} |
152
|
|
|
|
153
|
1 |
|
array_splice($args, 3, 0, [TRUE]); |
154
|
|
|
|
155
|
1 |
|
return call_user_func_array([$this, 'setMessage'], $args); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param string $message |
160
|
|
|
* @param string $level |
161
|
|
|
* @param string $title |
162
|
|
|
* @param boolean $overlay |
163
|
|
|
* @param int|null $count |
164
|
|
|
* @param array $parameters |
165
|
|
|
* |
166
|
|
|
* @return Entities\IMessage |
167
|
|
|
*/ |
168
|
|
|
public function message($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) |
169
|
|
|
{ |
170
|
1 |
|
return $this->setMessage($message, $level, $title, $overlay, $count, $parameters); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Flash a general message |
175
|
|
|
* |
176
|
|
|
* @param string $message |
177
|
|
|
* @param string $level |
178
|
|
|
* @param string $title |
179
|
|
|
* @param boolean $overlay |
180
|
|
|
* @param int|null $count |
181
|
|
|
* @param array $parameters |
182
|
|
|
* |
183
|
|
|
* @return Entities\IMessage |
184
|
|
|
*/ |
185
|
|
|
public function setMessage($message, $level = 'info', $title = NULL, $overlay = FALSE, $count = NULL, array $parameters = []) |
|
|
|
|
186
|
|
|
{ |
187
|
1 |
|
$args = func_get_args(); |
188
|
|
|
// Remove message |
189
|
1 |
|
unset($args[0]); |
190
|
|
|
// Remove level |
191
|
1 |
|
unset($args[1]); |
192
|
|
|
|
193
|
1 |
|
$title = $this->checkForAttribute($args, 'title', NULL); |
194
|
1 |
|
$overlay = $this->checkForAttribute($args, 'overlay', FALSE); |
195
|
1 |
|
$count = $this->checkForAttribute($args, 'count', NULL); |
196
|
1 |
|
$parameters = $this->checkForAttribute($args, 'parameters', []); |
197
|
|
|
|
198
|
|
|
// Support for Kdyby/Translation |
199
|
1 |
|
if ($message instanceof Translation\Phrase) { |
200
|
|
|
$phrase = new Adapters\KdybyPhraseAdapter($message); |
201
|
|
|
|
202
|
|
|
// Default phrase adapter |
203
|
1 |
|
} else if (!$message instanceof Adapters\IPhraseAdapter) { |
204
|
1 |
|
$phrase = new Adapters\DefaultPhraseAdapter($message, $count, $parameters); |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
// Support for Kdyby/Translation |
208
|
1 |
|
if ($title instanceof Translation\Phrase) { |
209
|
|
|
$titlePhrase = new Adapters\KdybyPhraseAdapter($title); |
210
|
|
|
|
211
|
|
|
// Default phrase adapter |
212
|
1 |
|
} else if (!$title instanceof Adapters\IPhraseAdapter && $title !== NULL) { |
213
|
1 |
|
$titlePhrase = new Adapters\DefaultPhraseAdapter($title, $count, $parameters); |
214
|
|
|
|
215
|
1 |
|
} else { |
216
|
1 |
|
$titlePhrase = NULL; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Get all stored messages |
220
|
1 |
|
$messages = $this->sessionStorage->get(SessionStorage::KEY_MESSAGES, []); |
221
|
|
|
|
222
|
|
|
// Create flash message |
223
|
1 |
|
$flash = (new Entities\Message($this->translator, $phrase, $titlePhrase)) |
|
|
|
|
224
|
1 |
|
->setLevel($level) |
225
|
1 |
|
->setOverlay($overlay); |
226
|
|
|
|
227
|
1 |
|
if (!$this->translator instanceof Localization\ITranslator) { |
228
|
1 |
|
$flash->setMessage($message); |
229
|
1 |
|
$flash->setTitle($title); |
230
|
1 |
|
} |
231
|
|
|
|
232
|
1 |
|
if ($this->checkUnique($flash, $messages) === FALSE) { |
233
|
1 |
|
$messages[] = $flash; |
234
|
1 |
|
} |
235
|
|
|
|
236
|
|
|
// Store messages in session |
237
|
1 |
|
$this->sessionStorage->set(SessionStorage::KEY_MESSAGES, $messages); |
238
|
|
|
|
239
|
1 |
|
return $flash; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param Entities\IMessage $flash |
244
|
|
|
* @param Entities\IMessage[] $messages |
245
|
|
|
* |
246
|
|
|
* @return bool |
247
|
|
|
*/ |
248
|
|
|
private function checkUnique(Entities\IMessage $flash, array $messages) |
249
|
|
|
{ |
250
|
1 |
|
foreach ($messages as $member) { |
251
|
1 |
|
if ((string) $member === (string) $flash) { |
252
|
1 |
|
return TRUE; |
253
|
|
|
} |
254
|
1 |
|
} |
255
|
|
|
|
256
|
1 |
|
return FALSE; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param array $attributes |
261
|
|
|
* @param string $type |
262
|
|
|
* @param mixed $default |
263
|
|
|
* |
264
|
|
|
* @return mixed |
265
|
|
|
*/ |
266
|
|
|
private function checkForAttribute(array $attributes, $type, $default) |
267
|
|
|
{ |
268
|
1 |
|
foreach($attributes as $attribute) { |
269
|
|
|
switch($type) |
270
|
|
|
{ |
271
|
1 |
|
case 'title': |
272
|
1 |
|
if (is_string($attribute) === TRUE || $attribute instanceof Translation\Phrase || $attribute instanceof Adapters\IPhraseAdapter) { |
273
|
1 |
|
return $attribute; |
274
|
|
|
} |
275
|
1 |
|
break; |
276
|
|
|
|
277
|
1 |
|
case 'overlay': |
278
|
1 |
|
if (is_bool($attribute) === TRUE) { |
279
|
1 |
|
return $attribute; |
280
|
|
|
} |
281
|
1 |
|
break; |
282
|
|
|
|
283
|
1 |
|
case 'count': |
284
|
1 |
|
if (is_numeric($attribute) === TRUE) { |
285
|
1 |
|
return $attribute; |
286
|
|
|
} |
287
|
1 |
|
break; |
288
|
|
|
|
289
|
1 |
|
case 'parameters': |
290
|
1 |
|
if (is_array($attribute) === TRUE) { |
291
|
1 |
|
return $attribute; |
292
|
|
|
} |
293
|
1 |
|
break; |
294
|
|
|
} |
295
|
1 |
|
} |
296
|
|
|
|
297
|
|
|
// Return default |
298
|
1 |
|
return $default; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.