-
Notifications
You must be signed in to change notification settings - Fork 22
/
FlashAlerts.php
150 lines (127 loc) · 4.18 KB
/
FlashAlerts.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace insolita\wgadminlte;
use yii\bootstrap\Widget;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* Flash messages with AdminLte style and support multiple flash similar style
*
* @example
* add in layout
* <?=\insolita\wgadminlte\FlashAlerts::widget([]);?>
* In controllers
* Yii::$app->session->setFlash('warning','Message1',true);
* Yii::$app->session->setFlash('warning-order','Message2');
* Yii::$app->session->setFlash('info1','Message3',true);
* Yii::$app->session->setFlash('info2','Message4',true);
*/
class FlashAlerts extends Widget
{
/**
* @deprecated use LteConst instead
*/
const TYPE_DANGER = 'danger';
/**
* @deprecated use LteConst instead
*/
const TYPE_INFO = 'info';
/**
* @deprecated use LteConst instead
*/
const TYPE_SUCCESS = 'success';
/**
* @deprecated use LteConst instead
*/
const TYPE_WARNING = 'warning';
/**@var boolean - Show close button for alert* */
public $closable = true;
/**@var boolean - Encode flash messages?* */
public $encode = true;
/**@var boolean - Wrap message in <b> tag?* */
public $bold = true;
public $successIcon = '<i class="fa fa-lg fa-smile-o"></i>';
public $errorIcon = '<i class="fa fa-lg fa-frown-o"></i>';
public $warningIcon = '<i class="fa fa-lg fa-volume-up"></i>';
public $infoIcon = '<i class="fa fa-lg fa-info-circle"></i>';
public $successTitle = 'Успешно!';
public $errorTitle = 'Ошибка!';
public $warningTitle = 'Внимание!';
public $infoTitle = 'К сведению!';
private $classes;
private $styleParts;
private $icons;
/**
* @var
*/
private $titles;
/**
*
*/
public function init()
{
$this->classes = ['success' => 'success', 'error' => 'danger', 'info' => 'info', 'warning' => 'warning'];
$this->styleParts = array_keys($this->classes);
$this->icons = [
'success' => $this->successIcon,
'error' => $this->errorIcon,
'info' => $this->infoIcon,
'warning' => $this->warningIcon,
];
$this->titles = [
'success' => $this->successTitle,
'error' => $this->errorTitle,
'info' => $this->infoTitle,
'warning' => $this->warningTitle,
];
}
/**
* @return string
*/
public function run()
{
$allflash = \Yii::$app->session->getAllFlashes();
$messages = '';
foreach ($allflash as $key => $message) {
$flashStyle = 'info';
foreach ($this->styleParts as $kp) {
if (strpos($key, $kp) !== false) {
$flashStyle = $kp;
break;
}
}
Html::addCssClass($this->options, 'alert');
Html::addCssClass($this->options, 'alert-' . ArrayHelper::getValue($this->classes, $flashStyle, 'info'));
if ($this->closable) {
Html::addCssClass($this->options, 'alert-dismissable');
}
if (is_array($message)) {
foreach ($message as $submess) {
$messages .= $this->buildMessage($flashStyle, $submess);
}
} elseif ($message) {
$messages .= $this->buildMessage($flashStyle, $message);
}
}
return $messages;
}
/**
* @param $flashStyle
* @param $text
*
* @return string
*/
protected function buildMessage($flashStyle, $text)
{
$text = !$this->encode ? $text : Html::encode($text);
return Html::tag(
'div',
ArrayHelper::getValue($this->icons, $flashStyle, '')
. (!$this->closable ? ''
: '<button class="close" aria-hidden="true" data-dismiss="alert" type="button">x</button>')
. (isset($this->titles[$flashStyle]) ? Html::tag('b', $this->titles[$flashStyle]) : '') . ' '
. (!$this->bold ? $text : Html::tag('b', $text))
,
$this->options
);
}
}