1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @link http://www.yiiframework.com/ |
5
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
6
|
|
|
* @license http://www.yiiframework.com/license/ |
7
|
|
|
*/ |
8
|
|
|
namespace app\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 string |
33
|
|
|
*/ |
34
|
|
|
public $template = null; |
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
|
|
|
public function init() |
55
|
|
|
{ |
56
|
|
|
parent::init(); |
57
|
|
|
$session = \Yii::$app->getSession(); |
58
|
|
|
$flashes = $session->getAllFlashes(); |
59
|
|
|
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; |
60
|
|
|
|
61
|
|
|
foreach ($flashes as $type => $data) { |
62
|
|
|
if (isset($this->alertTypes[$type])) { |
63
|
|
|
$data = (array) $data; |
64
|
|
|
foreach ($data as $i => $message) { |
65
|
|
|
if ($this->template) { |
66
|
|
|
$message = $this->render($this->template, ['message' => $message, 'type' => $type]); |
67
|
|
|
} |
68
|
|
|
/* initialize css class for each alert box */ |
69
|
|
|
$this->options['class'] = $this->alertTypes[$type] . $appendCss; |
70
|
|
|
/* assign unique id to each alert box */ |
71
|
|
|
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i; |
72
|
|
|
echo \yii\bootstrap\Alert::widget([ |
73
|
|
|
'body' => $message, |
74
|
|
|
'closeButton' => $this->closeButton, |
75
|
|
|
'options' => $this->options, |
76
|
|
|
]); |
77
|
|
|
} |
78
|
|
|
$session->removeFlash($type); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|