1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Yii2 adapter for PNotify JQuery extension |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/yii2-pnotify |
7
|
|
|
* @package yii2-pnotify |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\pnotify; |
13
|
|
|
|
14
|
|
|
use Yii; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Flashes widget renders messages from session flashes. All flash messages are displayed |
19
|
|
|
* in the sequence they were assigned using setFlash. You can set message as following:. |
20
|
|
|
* |
21
|
|
|
* ```php |
22
|
|
|
* Yii::$app->getSession()->setFlash('error', 'This is the message'); |
23
|
|
|
* Yii::$app->getSession()->setFlash('success', 'This is the message'); |
24
|
|
|
* Yii::$app->getSession()->setFlash('info', 'This is the message'); |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* Multiple messages could be set as follows: |
28
|
|
|
* |
29
|
|
|
* ```php |
30
|
|
|
* Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']); |
31
|
|
|
* ``` |
32
|
|
|
*/ |
33
|
|
|
class Flashes extends \yii\bootstrap\Widget |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var array the alert types configuration for the flash messages. |
37
|
|
|
* This array is setup as $key => $value, where: |
38
|
|
|
* - $key is the name of the session flash variable |
39
|
|
|
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) |
40
|
|
|
*/ |
41
|
|
|
public $alertTypes = [ |
42
|
|
|
'error' => 'alert-danger', |
43
|
|
|
'danger' => 'alert-danger', |
44
|
|
|
'success' => 'alert-success', |
45
|
|
|
'info' => 'alert-info', |
46
|
|
|
'warning' => 'alert-warning', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var array the options for rendering the close button tag. |
51
|
|
|
*/ |
52
|
|
|
public $closeButton = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param mixed $message Flash value to be normalized |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function normalizeMessage($message) |
59
|
|
|
{ |
60
|
|
|
$res = []; |
61
|
|
|
if (is_string($message)) { |
62
|
|
|
$res['text'] = $message; |
63
|
|
|
} elseif (is_array($message)) { |
64
|
|
|
$res = $message; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $res; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function run() |
71
|
|
|
{ |
72
|
|
|
$session = Yii::$app->getSession(); |
|
|
|
|
73
|
|
|
$flashes = $session->getAllFlashes(); |
74
|
|
|
$notifications = []; |
75
|
|
|
|
76
|
|
|
foreach ($flashes as $type => $data) { |
77
|
|
|
if (isset($this->alertTypes[$type])) { |
78
|
|
|
$data = (array) $data; |
79
|
|
|
|
80
|
|
|
foreach ($data as $message) { |
81
|
|
|
$message = $this->normalizeMessage($message); |
82
|
|
|
$notifications[] = ArrayHelper::merge([ |
83
|
|
|
'type' => $type, |
84
|
|
|
], $message); |
85
|
|
|
} |
86
|
|
|
$session->removeFlash($type); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
echo PNotify::widget([ |
91
|
|
|
'notifications' => $notifications, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: