|
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 app\widgets; |
|
9
|
|
|
|
|
10
|
|
|
use Yii; |
|
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
|
|
|
* |
|
16
|
|
|
* ```php |
|
17
|
|
|
* Yii::$app->session->setFlash('error', 'This is the message'); |
|
18
|
|
|
* Yii::$app->session->setFlash('success', 'This is the message'); |
|
19
|
|
|
* Yii::$app->session->setFlash('info', 'This is the message'); |
|
20
|
|
|
* ``` |
|
21
|
|
|
* |
|
22
|
|
|
* Multiple messages could be set as follows: |
|
23
|
|
|
* |
|
24
|
|
|
* ```php |
|
25
|
|
|
* Yii::$app->session->setFlash('error', ['Error 1', 'Error 2']); |
|
26
|
|
|
* ``` |
|
27
|
|
|
* |
|
28
|
|
|
* @author Kartik Visweswaran <[email protected]> |
|
29
|
|
|
* @author Alexander Makarov <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Alert extends \yii\bootstrap\Widget |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var array the alert types configuration for the flash messages. |
|
35
|
|
|
* This array is setup as $key => $value, where: |
|
36
|
|
|
* - $key is the name of the session flash variable |
|
37
|
|
|
* - $value is the bootstrap alert type (i.e. danger, success, info, warning) |
|
38
|
|
|
*/ |
|
39
|
|
|
public $alertTypes = [ |
|
40
|
|
|
'error' => 'alert-danger', |
|
41
|
|
|
'danger' => 'alert-danger', |
|
42
|
|
|
'success' => 'alert-success', |
|
43
|
|
|
'info' => 'alert-info', |
|
44
|
|
|
'warning' => 'alert-warning' |
|
45
|
|
|
]; |
|
46
|
|
|
/** |
|
47
|
|
|
* @var array the options for rendering the close button tag. |
|
48
|
|
|
*/ |
|
49
|
|
|
public $closeButton = []; |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
public function init() |
|
53
|
|
|
{ |
|
54
|
|
|
parent::init(); |
|
55
|
|
|
$session = Yii::$app->session; |
|
56
|
|
|
$flashes = $session->getAllFlashes(); |
|
57
|
|
|
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; |
|
58
|
|
|
foreach ($flashes as $type => $data) { |
|
59
|
|
|
if (isset($this->alertTypes[$type])) { |
|
60
|
|
|
$data = (array)$data; |
|
61
|
|
|
$this->processTypes($this->alertTypes[$type], $data, $appendCss); |
|
62
|
|
|
$session->removeFlash($type); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param string $type |
|
69
|
|
|
* @param array $data |
|
70
|
|
|
* @param string $appendCss |
|
71
|
|
|
* @throws \Exception |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function processTypes($type, $data, $appendCss) |
|
74
|
|
|
{ |
|
75
|
|
|
foreach ($data as $i => $message) { |
|
76
|
|
|
/** initialize css class for each alert box */ |
|
77
|
|
|
$this->options['class'] = $type . $appendCss; |
|
78
|
|
|
|
|
79
|
|
|
/** assign unique id to each alert box */ |
|
80
|
|
|
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i; |
|
81
|
|
|
$this->renderFlash($message); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param array $message |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function renderFlash($message) |
|
90
|
|
|
{ |
|
91
|
|
|
echo \yii\bootstrap\Alert::widget([ |
|
92
|
|
|
'body' => $message, |
|
93
|
|
|
'closeButton' => $this->closeButton, |
|
94
|
|
|
'options' => $this->options, |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|