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 cornernote\ace\widgets; |
9
|
|
|
|
10
|
|
|
use \yii\bootstrap\Alert as BootstrapAlert; |
11
|
|
|
use \yii\bootstrap\Widget; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Alert widget renders a message from session flash for AdminLTE alerts. All flash messages are displayed |
15
|
|
|
* in the sequence they were assigned using setFlash. You can set message as following: |
16
|
|
|
* |
17
|
|
|
* ```php |
18
|
|
|
* \Yii::$app->getSession()->setFlash('error', '<b>Alert!</b> Danger alert preview. This alert is dismissable.'); |
19
|
|
|
* ``` |
20
|
|
|
* |
21
|
|
|
* Multiple messages could be set as follows: |
22
|
|
|
* |
23
|
|
|
* ```php |
24
|
|
|
* \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']); |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* @author Evgeniy Tkachenko <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class Alert extends 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 array: |
36
|
|
|
* - class of alert type (i.e. danger, success, info, warning) |
37
|
|
|
* - icon for alert AdminLTE |
38
|
|
|
*/ |
39
|
|
|
public $alertTypes = [ |
40
|
|
|
'error' => [ |
41
|
|
|
'class' => 'alert-danger', |
42
|
|
|
'icon' => '<i class="icon fa fa-ban"></i>', |
43
|
|
|
], |
44
|
|
|
'danger' => [ |
45
|
|
|
'class' => 'alert-danger', |
46
|
|
|
'icon' => '<i class="icon fa fa-ban"></i>', |
47
|
|
|
], |
48
|
|
|
'success' => [ |
49
|
|
|
'class' => 'alert-success', |
50
|
|
|
'icon' => '<i class="icon fa fa-check"></i>', |
51
|
|
|
], |
52
|
|
|
'info' => [ |
53
|
|
|
'class' => 'alert-info', |
54
|
|
|
'icon' => '<i class="icon fa fa-info"></i>', |
55
|
|
|
], |
56
|
|
|
'warning' => [ |
57
|
|
|
'class' => 'alert-warning', |
58
|
|
|
'icon' => '<i class="icon fa fa-warning"></i>', |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array the options for rendering the close button tag. |
64
|
|
|
*/ |
65
|
|
|
public $closeButton = [ |
66
|
|
|
'label' => '<i class="fa fa-times"></i>', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Initializes the widget. |
71
|
|
|
* This method will register the bootstrap asset bundle. If you override this method, |
72
|
|
|
* make sure you call the parent implementation first. |
73
|
|
|
*/ |
74
|
|
|
public function init() |
75
|
|
|
{ |
76
|
|
|
parent::init(); |
77
|
|
|
|
78
|
|
|
$session = \Yii::$app->getSession(); |
79
|
|
|
$flashes = $session->getAllFlashes(); |
80
|
|
|
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : ''; |
81
|
|
|
|
82
|
|
|
foreach ($flashes as $type => $data) { |
83
|
|
|
if (isset($this->alertTypes[$type])) { |
84
|
|
|
$data = (array)$data; |
85
|
|
|
foreach ($data as $message) { |
86
|
|
|
|
87
|
|
|
$this->options['class'] = $this->alertTypes[$type]['class'] . $appendCss; |
88
|
|
|
$this->options['id'] = $this->getId() . '-' . $type; |
89
|
|
|
|
90
|
|
|
echo BootstrapAlert::widget([ |
91
|
|
|
'body' => $this->alertTypes[$type]['icon'] . ' ' . $message, |
92
|
|
|
'closeButton' => $this->closeButton, |
93
|
|
|
'options' => $this->options, |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$session->removeFlash($type); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|