forked from kartik-v/yii2-widget-alert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlertBlock.php
183 lines (167 loc) · 6.03 KB
/
AlertBlock.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014
* @package yii2-widgets
* @subpackage yii2-widget-alert
* @version 1.1.0
*/
namespace kartik\alert;
use Yii;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use kartik\base\Config;
use kartik\growl\Growl;
/**
* Alert block widget that groups multiple `\kartik\widget\Alert` or `kartik\widget\Growl` widgets as one single block.
* You can choose to automatically read and display session flash messages (which is the default setting) or setup
* your own block of custom alerts.
*
* @author Kartik Visweswaran <[email protected]>
* @since 1.0
*/
class AlertBlock extends \yii\bootstrap\Widget
{
const TYPE_ALERT = 'alert';
const TYPE_GROWL = 'growl';
/**
* @var string the type of alert to use. Can be one of `TYPE_ALERT` or `TYPE_GROWL`.
* Defaults to `TYPE_ALERT`.
*/
public $type = self::TYPE_ALERT;
/**
* @var integer time in milliseconds to delay the fade out of each alert. If set to `0` or `false`, alerts
* will never fade out and will be always displayed. This defaults to `2000` ms for `TYPE_ALERT` and
* `1000` ms for `TYPE_GROWL`.
*/
public $delay;
/**
* @var bool whether to automatically use messages set via `Yii::$app->session->setFlash()`. Defaults to `true`.
* If set to `false`, you would need to pass the `body` setting within `alertSetting` array.
*/
public $useSessionFlash = true;
/**
* @var array the alert types configuration for the alert messages. This array is setup as $alert => $settings, where:
* - $alert: string is the name of the session flash variable (e.g. error, success, info, warning)
* - $settings: array, the `\kartik\widgets\Alert` or `\kartik\widgets\Growl` widget settings depending
* on what `type` has been set.
* @see \kartik\widgets\Alert
* @see \kartik\widgets\Growl
*/
public $alertSettings = [];
/**
* @var array the options for rendering the close button tag. This will be overridden by the `closeButton` setting
* within the `alertSettings` configuration.
*/
public $closeButton = [];
/**
* Initialize the alert block widget
*/
public function init()
{
parent::init();
$this->initOptions();
echo Html::beginTag('div', $this->options) . "\n";
}
/**
* Runs the widget
*/
public function run()
{
parent::run();
if ($this->useSessionFlash) {
$this->renderFlashAlerts();
} else {
$this->renderAlerts();
}
echo "\n" . Html::endTag('div');
}
/**
* Initializes options and settings
* @throws InvalidConfigException
*/
protected function initOptions()
{
if ($this->type == self::TYPE_GROWL) {
Config::checkDependency('growl\Growl', 'yii2-widget-growl', 'for rendering Growl notifications in the alert block');
}
if (empty($this->options['id'])) {
$this->options['id'] = $this->getId();
}
if (!isset($this->delay)) {
$this->delay = ($this->type == self::TYPE_ALERT) ? 2000 : 1200;
}
if (empty($this->alertSettings) && $this->type == self::TYPE_GROWL) {
$this->alertSettings = [
'error' => ['type' => Growl::TYPE_DANGER],
'success' => ['type' => Growl::TYPE_SUCCESS],
'info' => ['type' => Growl::TYPE_INFO],
'warning' => ['type' => Growl::TYPE_WARNING],
'growl' => ['type' => Growl::TYPE_GROWL]
];
} elseif (empty($this->alertSettings)) {
$this->alertSettings = [
'error' => ['type' => Alert::TYPE_DANGER],
'success' => ['type' => Alert::TYPE_SUCCESS],
'info' => ['type' => Alert::TYPE_INFO],
'warning' => ['type' => Alert::TYPE_WARNING],
'primary' => ['type' => Alert::TYPE_PRIMARY],
'default' => ['type' => Alert::TYPE_DEFAULT]
];
}
}
/**
* Renders alerts from session flash
*/
public function renderFlashAlerts()
{
$type = ($this->type == self::TYPE_GROWL) ? self::TYPE_GROWL : self::TYPE_ALERT;
$session = Yii::$app->getSession();
$flashes = $session->getAllFlashes();
$delay = $this->delay;
foreach ($flashes as $alert => $message) {
$message = (array) $message;
foreach ($message as $mess) {
if (!empty($this->alertSettings[$alert])) {
$settings = $this->alertSettings[$alert];
$settings['body'] = $mess;
if (empty($settings['closeButton'])) {
$settings['closeButton'] = $this->closeButton;
}
$settings['delay'] = $delay;
$delay += $this->delay;
echo ($type == self::TYPE_GROWL) ? Growl::widget($settings) : Alert::widget($settings);
$session->removeFlash($alert);
}
}
}
/**
* Renders manually set alerts
*/
public function renderAlerts()
{
$type = ($this->type == self::TYPE_GROWL) ? self::TYPE_GROWL : self::TYPE_ALERT;
foreach ($this->alertSettings as $alert => $settings) {
if (!empty($settings['body'])) {
echo ($type == self::TYPE_GROWL) ? Growl::widget($settings) : Alert::widget($settings);
}
}
}
/**
* Renders the close button.
*
* @return string the rendering result
*/
protected function renderCloseButton()
{
if ($this->closeButton !== null) {
$tag = ArrayHelper::remove($this->closeButton, 'tag', 'button');
$label = ArrayHelper::remove($this->closeButton, 'label', '×');
if ($tag === 'button' && !isset($this->closeButton['type'])) {
$this->closeButton['type'] = 'button';
}
return Html::tag($tag, $label, $this->closeButton);
} else {
return null;
}
}
}