-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alert.php
85 lines (69 loc) · 2.19 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
<?php
/**
* Alert
*
*@package vendor.tangniyuqi.yii2-zui
*@author tangming <[email protected]>
*@copyright DNA <http://www.Noooya.com/>
*@version 1.0.0
*@since 2017-05-18 Create
*@todo N/A
*/
namespace tangniyuqi\zui;
use yii\helpers\Json;
class Alert extends \yii\bootstrap\Widget
{
/**
* @inheritdoc
*/
public $alertTypes = [
'error' => 'icon icon-exclamation-sign',
'danger' => 'icon icon-exclamation-sign',
'success' => 'icon icon-ok-sign',
'info' => 'icon icon-info-sign',
'warning' => 'icon icon-warning-sign',
];
/**
* @inheritdoc
*/
public $closeButton = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$session = \Yii::$app->session;
$flashes = $session->getAllFlashes();
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
if(!isset($this->options['placement'])) $this->options['placement'] = 'top';
foreach ($flashes as $type => $data) {
if (isset($this->alertTypes[$type])) {
$data = (array) $data;
foreach ($data as $i => $message) {
if($type == 'error') $type = 'warning';
$this->options['type'] = $type ;// $this->alertTypes[$type] . $appendCss;
$this->options['icon'] = $this->alertTypes[$type];
if (is_array($message)) {
$message = implode($message, '<br>');
}
unset($this->options['id'],$this->options['class']);
$this->createJs($message, $this->options);
}
$session->removeFlash($type);
}
}
}
/**
* @inheritdoc
*/
public function createJs($content, $options)
{
$options = Json::encode($options);
$script = '
var flashMessage = $.zui.messager.show("'.$content.'", '.$options.');
flashMessage.show();
';
$this->view->registerJs($script, \yii\web\View::POS_READY);
}
}