forked from dmstr/yii2-adminlte-asset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlert.php
106 lines (95 loc) · 3.27 KB
/
Alert.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace dmstr\widgets;
use yii\bootstrap\Alert as BootstrapAlert;
use yii\bootstrap\Widget;
/**
* Alert widget renders a message from session flash for AdminLTE alerts. All flash messages are displayed
* in the sequence they were assigned using setFlash. You can set message as following:
*
* ```php
* \Yii::$app->getSession()->setFlash('error', '<b>Alert!</b> Danger alert preview. This alert is dismissable.');
* ```
*
* Multiple messages could be set as follows:
*
* ```php
* \Yii::$app->getSession()->setFlash('error', ['Error 1', 'Error 2']);
* ```
*
* @author Evgeniy Tkachenko <et.coder@gmail.com>
*/
class Alert extends Widget
{
/**
* @var array the alert types configuration for the flash messages.
* This array is setup as $key => $value, where:
* - $key is the name of the session flash variable
* - $value is the array:
* - class of alert type (i.e. danger, success, info, warning)
* - icon for alert AdminLTE
*/
public $alertTypes = [
'error' => [
'class' => 'alert-danger',
'icon' => '<i class="icon fa fa-ban"></i>',
],
'danger' => [
'class' => 'alert-danger',
'icon' => '<i class="icon fa fa-ban"></i>',
],
'success' => [
'class' => 'alert-success',
'icon' => '<i class="icon fa fa-check"></i>',
],
'info' => [
'class' => 'alert-info',
'icon' => '<i class="icon fa fa-info"></i>',
],
'warning' => [
'class' => 'alert-warning',
'icon' => '<i class="icon fa fa-warning"></i>',
],
];
/**
* @var array the options for rendering the close button tag.
*/
public $closeButton = [];
/**
* @var boolean whether to removed flash messages during AJAX requests
*/
public $isAjaxRemoveFlash = true;
/**
* Initializes the widget.
* This method will register the bootstrap asset bundle. If you override this method,
* make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
$session = \Yii::$app->getSession();
$flashes = $session->getAllFlashes();
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
foreach ($flashes as $type => $data) {
if (isset($this->alertTypes[$type])) {
$data = (array) $data;
foreach ($data as $message) {
$this->options['class'] = $this->alertTypes[$type]['class'] . $appendCss;
$this->options['id'] = $this->getId() . '-' . $type;
echo BootstrapAlert::widget([
'body' => $this->alertTypes[$type]['icon'] . $message,
'closeButton' => $this->closeButton,
'options' => $this->options,
]);
}
if ($this->isAjaxRemoveFlash && !\Yii::$app->request->isAjax) {
$session->removeFlash($type);
}
}
}
}
}