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 zacksleo\yii2\backend\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
|
|
|
* - \Yii::$app->getSession()->setFlash('error', 'This is the message'); |
15
|
|
|
* - \Yii::$app->getSession()->setFlash('success', 'This is the message'); |
16
|
|
|
* - \Yii::$app->getSession()->setFlash('info', 'This is the message'); |
17
|
|
|
* |
18
|
|
|
* @author Kartik Visweswaran <[email protected]> |
19
|
|
|
* @author Alexander Makarov <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Alert extends \yii\bootstrap\Widget |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array the alert types configuration for the flash messages. |
25
|
|
|
* This array is setup as $key => $value, where: |
26
|
|
|
* - $key is the name of the session flash variable |
27
|
|
|
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) |
28
|
|
|
*/ |
29
|
|
|
public $alertTypes = [ |
30
|
|
|
'error' => 'alert-danger', |
31
|
|
|
'danger' => 'alert-danger', |
32
|
|
|
'success' => 'alert-success', |
33
|
|
|
'info' => 'alert-info', |
34
|
|
|
'warning' => 'alert-warning' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array the options for rendering the close button tag. |
39
|
|
|
*/ |
40
|
|
|
public $closeButton = []; |
41
|
|
|
|
42
|
|
|
public function init() |
43
|
|
|
{ |
44
|
|
|
parent::init(); |
45
|
|
|
|
46
|
|
|
$session = \Yii::$app->getSession(); |
|
|
|
|
47
|
|
|
$flashes = $session->getAllFlashes(); |
48
|
|
|
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; |
49
|
|
|
|
50
|
|
|
foreach ($flashes as $type => $message) { |
51
|
|
|
if (isset($this->alertTypes[$type])) { |
52
|
|
|
/* initialize css class for each alert box */ |
53
|
|
|
$this->options['class'] = $this->alertTypes[$type] . $appendCss; |
54
|
|
|
/* assign unique id to each alert box */ |
55
|
|
|
$this->options['id'] = $this->getId() . '-' . $type; |
56
|
|
|
if (is_array($message)) { |
57
|
|
|
foreach ($message as $msg) { |
58
|
|
|
echo \yii\bootstrap\Alert::widget([ |
59
|
|
|
'body' => $msg, |
60
|
|
|
'closeButton' => $this->closeButton, |
61
|
|
|
'options' => $this->options, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
} else { |
65
|
|
|
echo \yii\bootstrap\Alert::widget([ |
66
|
|
|
'body' => $message, |
67
|
|
|
'closeButton' => $this->closeButton, |
68
|
|
|
'options' => $this->options, |
69
|
|
|
]); |
70
|
|
|
} |
71
|
|
|
$session->removeFlash($type); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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: