|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link http://www.yiiframework.com/ |
|
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
|
5
|
|
|
* @license http://www.yiiframework.com/license/ |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace frontend\widgets; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Alert widget renders a message from session flash. All flash messages are displayed |
|
12
|
|
|
* in the sequence they were assigned using setFlash. You can set message as following: |
|
13
|
|
|
* |
|
14
|
|
|
* ```php |
|
15
|
|
|
* \Yii::$app->getSession()->setFlash('error', 'This is the message'); |
|
16
|
|
|
* \Yii::$app->getSession()->setFlash('success', 'This is the message'); |
|
17
|
|
|
* \Yii::$app->getSession()->setFlash('info', 'This is the message'); |
|
18
|
|
|
* ``` |
|
19
|
|
|
* |
|
20
|
|
|
* Multiple messages could be set as follows: |
|
21
|
|
|
* |
|
22
|
|
|
* ```php |
|
23
|
|
|
* \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']); |
|
24
|
|
|
* ``` |
|
25
|
|
|
* |
|
26
|
|
|
* @author Kartik Visweswaran <[email protected]> |
|
27
|
|
|
* @author Alexander Makarov <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class Alert extends \yii\bootstrap\Widget |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var array the alert types configuration for the flash messages. |
|
33
|
|
|
* This array is setup as $key => $value, where: |
|
34
|
|
|
* - $key is the name of the session flash variable |
|
35
|
|
|
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) |
|
36
|
|
|
*/ |
|
37
|
|
|
public $alertTypes = [ |
|
38
|
|
|
'error' => 'alert-danger', |
|
39
|
|
|
'danger' => 'alert-danger', |
|
40
|
|
|
'success' => 'alert-success', |
|
41
|
|
|
'info' => 'alert-info', |
|
42
|
|
|
'warning' => 'alert-warning' |
|
43
|
|
|
]; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var array the options for rendering the close button tag. |
|
47
|
|
|
*/ |
|
48
|
|
|
public $closeButton = []; |
|
49
|
|
|
|
|
50
|
|
|
public function init() |
|
51
|
|
|
{ |
|
52
|
|
|
parent::init(); |
|
53
|
|
|
|
|
54
|
|
|
$session = \Yii::$app->getSession(); |
|
|
|
|
|
|
55
|
|
|
$flashes = $session->getAllFlashes(); |
|
56
|
|
|
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; |
|
57
|
|
|
|
|
58
|
|
|
foreach ($flashes as $type => $data) { |
|
59
|
|
|
if (isset($this->alertTypes[$type])) { |
|
60
|
|
|
$data = (array) $data; |
|
61
|
|
|
foreach ($data as $i => $message) { |
|
62
|
|
|
/* initialize css class for each alert box */ |
|
63
|
|
|
$this->options['class'] = $this->alertTypes[$type] . $appendCss; |
|
64
|
|
|
|
|
65
|
|
|
/* assign unique id to each alert box */ |
|
66
|
|
|
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i; |
|
67
|
|
|
|
|
68
|
|
|
echo \yii\bootstrap\Alert::widget([ |
|
69
|
|
|
'body' => $message, |
|
70
|
|
|
'closeButton' => $this->closeButton, |
|
71
|
|
|
'options' => $this->options, |
|
72
|
|
|
]); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$session->removeFlash($type); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
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: