-
Notifications
You must be signed in to change notification settings - Fork 29
/
Alert.php
executable file
·126 lines (112 loc) · 3.91 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @link https://github.com/MacGyer/yii2-materializecss
* @copyright Copyright (c) 2016 ... MacGyer for pluspunkt coding
* @license https://github.com/MacGyer/yii2-materializecss/blob/master/LICENSE
*/
namespace macgyer\yii2materializecss\widgets;
use macgyer\yii2materializecss\lib\BaseWidget;
use macgyer\yii2materializecss\lib\Html;
use Yii;
use yii\helpers\ArrayHelper;
/**
* Alert renders Yii's session flash messages.
*
* All flash messages are displayed in the sequence they were assigned using
* [yii\web\Session::setFlash()](http://www.yiiframework.com/doc-2.0/yii-web-session.html#setFlash()-detail).
*
* You can set messages as follows:
*
* ```php
* Yii::$app->session->setFlash('error', 'This is the message');
* Yii::$app->session->setFlash('success', 'This is the message');
* Yii::$app->session->setFlash('info', 'This is the message');
* ```
*
* Multiple messages can be set by specifying an array:
*
* ```php
* Yii::$app->session->setFlash('error', ['Error 1', 'Error 2']);
* ```
*
* @author Christoph Erdmann <yii2-materializecss@pluspunkt-coding.de>
* @package widgets
*/
class Alert extends BaseWidget
{
/**
* @var array the default alert levels.
* This array is setup as $key => $value, where:
* - $key is the name of the session flash variable
* - $value is the alert type (will be uses as CSS class)
*
* To define own alert levels and their corresponding CSS classes or to overrride the
* default levels use [[alertLevels]]
*/
protected $predefinedAlertLevels = [
'error' => 'error',
'danger' => 'danger',
'success' => 'success',
'info' => 'info',
'warning' => 'warning',
'default' => 'default',
];
/**
* @var array custom alert levels
*/
public $alertLevels = [];
/**
* @var array the HTML attributes for the widget container tag.
* @see [yii\helpers\BaseHtml::renderTagAttributes()](http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#renderTagAttributes()-detail) for details on
* how attributes are being rendered.
*/
public $options = [];
/**
* Initializes the widget.
*
* @uses [yii\helper\BaseArrayHelper::merge()](http://www.yiiframework.com/doc-2.0/yii-helpers-basearrayhelper.html#merge()-detail)
*/
public function init()
{
parent::init();
$this->alertLevels = ArrayHelper::merge($this->predefinedAlertLevels, $this->alertLevels);
}
/**
* Executes the widget.
*
* @uses [yii\web\Session](http://www.yiiframework.com/doc-2.0/yii-web-session.html)
*/
public function run()
{
$flashes = Yii::$app->session->getAllFlashes();
$appendCss = isset($this->options['class']) ? ' ' . $this->options['class'] : '';
foreach ($flashes as $type => $data) {
if (isset($this->alertLevels[$type])) {
$data = (array) $data;
foreach ($data as $i => $message) {
/* initialize css class for each alert box */
$this->options['class'] = 'alert ' . $this->alertLevels[$type] . $appendCss;
/* assign unique id to each alert box */
$this->options['id'] = $this->getId() . '-' . $type . '-' . $i;
echo $this->renderHtml($message, $this->options);
}
Yii::$app->session->removeFlash($type);
}
}
}
/**
* Renders a single flash message.
* @param string $message the content of the message
* @param array $options the HTML attributes for the container tag
* @return string
*/
private function renderHtml($message, $options = [])
{
$html = Html::beginTag('div', $options);
$html .= '<div class="card-panel">';
$html .= $message;
$html .= '</div>';
$html .= Html::endTag('div');
return $html;
}
}