1
|
|
|
<?php |
2
|
|
|
namespace Redaxscript\View\Helper; |
3
|
|
|
|
4
|
|
|
use Redaxscript\Html; |
5
|
|
|
use Redaxscript\Module; |
6
|
|
|
use Redaxscript\Registry; |
7
|
|
|
use function array_key_exists; |
8
|
|
|
use function array_replace_recursive; |
9
|
|
|
use function count; |
10
|
|
|
use function is_array; |
11
|
|
|
use function is_numeric; |
12
|
|
|
use function strlen; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* parent class to create a flash message |
16
|
|
|
* |
17
|
|
|
* @since 3.0.0 |
18
|
|
|
* |
19
|
|
|
* @package Redaxscript |
20
|
|
|
* @category Messenger |
21
|
|
|
* @author Henry Ruhs |
22
|
|
|
* @author Balázs Szilágyi |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
class Messenger |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* instance of the registry class |
29
|
|
|
* |
30
|
|
|
* @var Registry |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
protected $_registry; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* array of the action |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
|
41
|
|
|
protected $_actionArray = |
42
|
|
|
[ |
43
|
|
|
'text' => null, |
44
|
|
|
'route' => null, |
45
|
|
|
'url' => null |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* options of the messenger |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
protected $_optionArray = |
55
|
|
|
[ |
56
|
|
|
'className' => |
57
|
|
|
[ |
58
|
|
|
'box' => ' rs-box-note', |
59
|
|
|
'title' => 'rs-title-note', |
60
|
|
|
'list' => 'rs-list-note', |
61
|
|
|
'link' => 'rs-button-note', |
62
|
|
|
'redirect' => 'rs-meta-redirect', |
63
|
|
|
'note' => |
64
|
|
|
[ |
65
|
|
|
'success' => 'rs-is-success', |
66
|
|
|
'warning' => 'rs-is-warning', |
67
|
|
|
'error' => 'rs-is-error', |
68
|
|
|
'info' => 'rs-is-info' |
69
|
|
|
] |
70
|
|
|
] |
71
|
|
|
]; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* constructor of the class |
75
|
|
|
* |
76
|
|
|
* @since 2.4.0 |
77
|
|
|
* |
78
|
|
|
* @param Registry $registry instance of the registry class |
79
|
|
|
*/ |
80
|
|
|
|
81
|
|
|
public function __construct(Registry $registry) |
82
|
|
|
{ |
83
|
|
|
$this->_registry = $registry; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* init the class |
88
|
|
|
* |
89
|
|
|
* @since 3.0.0 |
90
|
|
|
* |
91
|
|
|
* @param array $optionArray options of the messenger |
92
|
|
|
* |
93
|
|
|
* @return self |
94
|
|
|
*/ |
95
|
|
|
|
96
|
|
|
public function init(array $optionArray = []) : self |
97
|
|
|
{ |
98
|
|
|
$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray); |
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* set the absolute redirect url |
104
|
|
|
* |
105
|
|
|
* @since 3.0.0 |
106
|
|
|
* |
107
|
|
|
* @param string $text text of the action |
108
|
|
|
* @param string $url absolute url of the action |
109
|
|
|
* |
110
|
|
|
* @return self |
111
|
|
|
*/ |
112
|
|
|
|
113
|
|
|
public function setUrl(string $text = null, string $url = null) : self |
114
|
|
|
{ |
115
|
|
|
if (strlen($text) && strlen($url)) |
116
|
|
|
{ |
117
|
|
|
$this->_actionArray['text'] = $text; |
118
|
|
|
$this->_actionArray['route'] = null; |
119
|
|
|
$this->_actionArray['url'] = $url; |
120
|
|
|
} |
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* set the relative redirect url |
126
|
|
|
* |
127
|
|
|
* @since 3.0.0 |
128
|
|
|
* |
129
|
|
|
* @param string $text text of the action |
130
|
|
|
* @param string $route relative route of the action |
131
|
|
|
* |
132
|
|
|
* @return self |
133
|
|
|
*/ |
134
|
|
|
|
135
|
|
|
public function setRoute(string $text = null, string $route = null) : self |
136
|
|
|
{ |
137
|
|
|
if (strlen($text) && strlen($route)) |
138
|
|
|
{ |
139
|
|
|
$this->_actionArray['text'] = $text; |
140
|
|
|
$this->_actionArray['route'] = $route; |
141
|
|
|
$this->_actionArray['url'] = null; |
142
|
|
|
} |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* do the redirect |
148
|
|
|
* |
149
|
|
|
* @since 3.0.0 |
150
|
|
|
* |
151
|
|
|
* @param int $timeout timeout of the redirect |
152
|
|
|
* |
153
|
|
|
* @return self |
154
|
|
|
*/ |
155
|
|
|
|
156
|
|
|
public function doRedirect(int $timeout = 2) : self |
157
|
|
|
{ |
158
|
|
|
$this->_actionArray['redirect'] = $timeout; |
159
|
|
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* success message |
164
|
|
|
* |
165
|
|
|
* @since 3.0.0 |
166
|
|
|
* |
167
|
|
|
* @param string|array $message message of the success |
168
|
|
|
* @param string $title title of the success |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
|
173
|
|
|
public function success($message = null, string $title = null) : string |
174
|
|
|
{ |
175
|
|
|
return $this->render('success', $message, $title); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* info message |
180
|
|
|
* |
181
|
|
|
* @since 3.0.0 |
182
|
|
|
* |
183
|
|
|
* @param string|array $message message of the info |
184
|
|
|
* @param string $title title of the info |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
|
189
|
|
|
public function info($message = null, string $title = null) : string |
190
|
|
|
{ |
191
|
|
|
return $this->render('info', $message, $title); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* warning message |
196
|
|
|
* |
197
|
|
|
* @since 3.0.0 |
198
|
|
|
* |
199
|
|
|
* @param string|array $message message of the warning |
200
|
|
|
* @param string $title message title of the warning |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
*/ |
204
|
|
|
|
205
|
|
|
public function warning($message = null, string $title = null) : string |
206
|
|
|
{ |
207
|
|
|
return $this->render('warning', $message, $title); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* error message |
212
|
|
|
* |
213
|
|
|
* @since 3.0.0 |
214
|
|
|
* |
215
|
|
|
* @param string|array $message message of the error |
216
|
|
|
* @param string $title title of the error |
217
|
|
|
* |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
|
221
|
|
|
public function error($message = null, string $title = null) : string |
222
|
|
|
{ |
223
|
|
|
return $this->render('error', $message, $title); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* render |
228
|
|
|
* |
229
|
|
|
* @since 3.0.0 |
230
|
|
|
* |
231
|
|
|
* @param string $type type of the flash |
232
|
|
|
* @param string|array $message message of the flash |
233
|
|
|
* @param string $title title of the flash |
234
|
|
|
* |
235
|
|
|
* @return string |
236
|
|
|
*/ |
237
|
|
|
|
238
|
|
|
public function render(string $type = null, $message = null, string $title = null) : string |
239
|
|
|
{ |
240
|
|
|
$output = Module\Hook::trigger('messengerStart'); |
241
|
|
|
|
242
|
|
|
/* html element */ |
243
|
|
|
|
244
|
|
|
$element = new Html\Element(); |
245
|
|
|
$titleElement = $title ? $element |
246
|
|
|
->copy() |
247
|
|
|
->init('h2', |
248
|
|
|
[ |
249
|
|
|
'class' => $this->_optionArray['className']['title'] . ' ' . $this->_optionArray['className']['note'][$type] |
250
|
|
|
]) |
251
|
|
|
->text($title) : null; |
252
|
|
|
$boxElement = $element |
253
|
|
|
->copy() |
254
|
|
|
->init('div', |
255
|
|
|
[ |
256
|
|
|
'class' => $this->_optionArray['className']['box'] . ' ' . $this->_optionArray['className']['note'][$type] |
257
|
|
|
]); |
258
|
|
|
|
259
|
|
|
/* create a list */ |
260
|
|
|
|
261
|
|
|
if (is_array($message) && count($message) > 1) |
262
|
|
|
{ |
263
|
|
|
$listElement = $element |
264
|
|
|
->copy() |
265
|
|
|
->init('ul', |
266
|
|
|
[ |
267
|
|
|
'class' => $this->_optionArray['className']['list'] |
268
|
|
|
]); |
269
|
|
|
$itemElement = $element->copy()->init('li'); |
270
|
|
|
|
271
|
|
|
/* collect item output */ |
272
|
|
|
|
273
|
|
|
foreach ($message as $value) |
274
|
|
|
{ |
275
|
|
|
$listElement |
276
|
|
|
->append( |
277
|
|
|
$itemElement->html($value) |
278
|
|
|
); |
279
|
|
|
} |
280
|
|
|
$boxElement->html($listElement); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/* else plain text */ |
284
|
|
|
|
285
|
|
|
else |
286
|
|
|
{ |
287
|
|
|
$boxElement->html(is_array($message) && array_key_exists(0, $message) ? $message[0] : $message); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/* collect output */ |
291
|
|
|
|
292
|
|
|
$output .= $titleElement . $boxElement . $this->_renderAction($type); |
293
|
|
|
$output .= Module\Hook::trigger('messengerEnd'); |
294
|
|
|
return $output; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* render action |
299
|
|
|
* |
300
|
|
|
* @since 3.0.0 |
301
|
|
|
* |
302
|
|
|
* @param string $type type of the flash |
303
|
|
|
* |
304
|
|
|
* @return string|null |
305
|
|
|
*/ |
306
|
|
|
|
307
|
|
|
protected function _renderAction(string $type = null) : ?string |
308
|
|
|
{ |
309
|
|
|
$output = null; |
310
|
|
|
$parameterRoute = $this->_registry->get('parameterRoute'); |
311
|
|
|
$root = $this->_registry->get('root'); |
312
|
|
|
if ($this->_actionArray['text'] && ($this->_actionArray['route'] || $this->_actionArray['url'])) |
313
|
|
|
{ |
314
|
|
|
$element = new Html\Element(); |
315
|
|
|
$output .= $element |
316
|
|
|
->copy() |
317
|
|
|
->init('a', |
318
|
|
|
[ |
319
|
|
|
'href' => $this->_actionArray['route'] ? $parameterRoute . $this->_actionArray['route'] : $this->_actionArray['url'], |
320
|
|
|
'class' => $this->_optionArray['className']['link'] . ' ' . $this->_optionArray['className']['note'][$type] |
321
|
|
|
]) |
322
|
|
|
->text($this->_actionArray['text']); |
323
|
|
|
|
324
|
|
|
/* meta redirect */ |
325
|
|
|
|
326
|
|
|
if (is_numeric($this->_actionArray['redirect'])) |
327
|
|
|
{ |
328
|
|
|
$output .= $element |
329
|
|
|
->copy() |
330
|
|
|
->init('meta', |
331
|
|
|
[ |
332
|
|
|
'class' => $this->_actionArray['redirect'] === 0 ? $this->_optionArray['className']['redirect'] : null, |
333
|
|
|
'content' => $this->_actionArray['redirect'] . ';url=' . ($this->_actionArray['route'] ? $root . '/' . $parameterRoute . $this->_actionArray['route'] : $this->_actionArray['url']), |
334
|
|
|
'http-equiv' => 'refresh' |
335
|
|
|
]); |
336
|
|
|
} |
337
|
|
|
} |
338
|
|
|
return $output; |
339
|
|
|
} |
340
|
|
|
} |