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