1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*## TbAlert class file. |
4
|
|
|
* |
5
|
|
|
* @author Christoffer Niska <[email protected]> |
6
|
|
|
* @copyright Copyright © Christoffer Niska 2011- |
7
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
8
|
|
|
*/ |
9
|
|
|
Yii::import('booster.widgets.TbWidget'); |
10
|
|
|
/** |
11
|
|
|
*## Bootstrap alert widget. |
12
|
|
|
* |
13
|
|
|
* Alert widget displays the messages set via CWebUser.setFlash() using the Twitter Bootstrap Alert widget. |
14
|
|
|
* |
15
|
|
|
* @see http://twitter.github.com/bootstrap/javascript.html#alerts |
16
|
|
|
* |
17
|
|
|
* @package booster.widgets.decoration |
18
|
|
|
*/ |
19
|
|
|
class TbAlert extends TbWidget { |
20
|
|
|
|
21
|
|
|
const CTX_ERROR = 'error'; |
22
|
|
|
const CTX_ERROR_CLASS = 'danger'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array The configuration for individual types of alerts. |
26
|
|
|
* |
27
|
|
|
* Here's the allowed array elements: |
28
|
|
|
* |
29
|
|
|
* 'visible' (= null) If set to false, this type of alerts will not be rendered. |
30
|
|
|
* 'fade' (= widget value) The same as a global fade property. |
31
|
|
|
* If set, alert will close itself fading away. |
32
|
|
|
* It defaults to the widget-level fade property value. |
33
|
|
|
* 'htmlOptions' (= array()) Attributes for the individual alert panels. |
34
|
|
|
* Widget-level htmlOptions was for wrapper element around them. |
35
|
|
|
* Note that the class attribute will be appended with classes required for alert to be Twitter Bootstrap alert. |
36
|
|
|
* 'closeText' (= widget value) The same as a global closeText property. |
37
|
|
|
* If set to false, close button will be removed from this type of alert. |
38
|
|
|
* It defaults to the widget-level closeText property value. |
39
|
|
|
* |
40
|
|
|
* @note Instead of full arrays you can use just the names of alert types as a values of the alerts property. |
41
|
|
|
* You can even mix the array configuration and plain names. |
42
|
|
|
* |
43
|
|
|
* Default is the array of all alert types defined as TYPE_* constants. |
44
|
|
|
* If you want no alerts to be displayed, set this property to empty array, not `null` value. |
45
|
|
|
*/ |
46
|
|
|
public $alerts; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string|boolean What to render as a button to close the alert panel. |
50
|
|
|
* |
51
|
|
|
* Default is to render a diagonal cross symbol. |
52
|
|
|
* If set to false, no close button will be rendered, making user unable to close the alert. |
53
|
|
|
*/ |
54
|
|
|
public $closeText = '×'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var boolean When set, alert will fade out using transitions when closed. Defaults to 'true' |
58
|
|
|
*/ |
59
|
|
|
public $fade = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var string[] The Javascript event handlers attached to all alert elements being rendered. |
63
|
|
|
* |
64
|
|
|
* It should be an array with elements being a javascript string containing event handler function definition (along with declaration) and indexed with the names of events. |
65
|
|
|
* This will be fed to jQuery.on verbatim. |
66
|
|
|
* |
67
|
|
|
* @volatile |
68
|
|
|
*/ |
69
|
|
|
public $events = array(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var array Traditional property to set attributes to the element wrapping all of alerts. |
73
|
|
|
*/ |
74
|
|
|
public $htmlOptions = array(); |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var string Name of the component which will be used to get alert messages. |
78
|
|
|
* |
79
|
|
|
* It should implement getFlash() method which returns alert message by its type. |
80
|
|
|
* Default is 'user'. |
81
|
|
|
*/ |
82
|
|
|
public $userComponentId = 'user'; |
83
|
|
|
|
84
|
|
|
protected static $_containerId = 0; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
*### .init() |
88
|
|
|
* |
89
|
|
|
* Initializes the widget. |
90
|
|
|
*/ |
91
|
3 |
|
public function init() { |
92
|
|
|
|
93
|
3 |
|
if (!isset($this->htmlOptions['id'])) { |
94
|
3 |
|
$this->htmlOptions['id'] = $this->getId(); |
95
|
3 |
|
} |
96
|
|
|
|
97
|
3 |
|
if (is_string($this->alerts)) { |
98
|
1 |
|
$this->alerts = array($this->alerts); |
99
|
1 |
|
} |
100
|
|
|
|
101
|
|
|
// Display all alert types by default. |
102
|
3 |
|
if (!isset($this->alerts)) { |
103
|
2 |
|
$this->alerts = array( |
104
|
2 |
|
self::CTX_SUCCESS, |
105
|
2 |
|
self::CTX_INFO, |
106
|
2 |
|
self::CTX_WARNING, |
107
|
2 |
|
self::CTX_DANGER, |
108
|
|
|
self::CTX_ERROR |
109
|
2 |
|
); |
110
|
2 |
|
} |
111
|
3 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
*### .run() |
115
|
|
|
* |
116
|
|
|
* Runs the widget. |
117
|
|
|
*/ |
118
|
|
|
public function run() { |
119
|
|
|
|
120
|
|
|
$id = $this->htmlOptions['id']; |
121
|
|
|
|
122
|
|
|
echo CHtml::openTag('div', $this->htmlOptions); |
123
|
|
|
|
124
|
|
|
foreach ($this->alerts as $type => $alert) { |
125
|
|
|
|
126
|
|
|
if (is_string($alert)) { |
127
|
|
|
$type = $alert; |
128
|
|
|
$alert = array(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (isset($alert['visible']) && $alert['visible'] === false) { |
132
|
|
|
continue; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** @var CWebUser $userComponent */ |
136
|
|
|
$userComponent = Yii::app()->getComponent($this->userComponentId); |
137
|
|
|
if (!$userComponent->hasFlash($type)) |
138
|
|
|
continue; |
139
|
|
|
|
140
|
|
|
$alertText = $userComponent->getFlash($type); |
141
|
|
|
if (empty($alertText)) { // null, '' |
142
|
|
|
continue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$this->renderSingleAlert($alert, $type, $alertText); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
echo CHtml::closeTag('div'); |
149
|
|
|
|
150
|
|
|
$id .= '_' . self::$_containerId++; |
151
|
|
|
$selector = "#{$id} .alert"; |
152
|
|
|
|
153
|
|
|
/** @var CClientScript $cs */ |
154
|
|
|
$cs = Yii::app()->getClientScript(); |
155
|
|
|
$cs->registerScript(__CLASS__ . '#' . $id, "jQuery('{$selector}').alert();"); |
156
|
|
|
|
157
|
|
|
foreach ($this->events as $name => $handler) { |
158
|
|
|
$handler = CJavaScript::encode($handler); |
159
|
|
|
$cs->registerScript( |
160
|
|
|
__CLASS__ . '#' . $id . '_' . $name, |
161
|
|
|
"jQuery('{$selector}').on('{$name}', {$handler});" |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param $alert |
168
|
|
|
* @param $context |
169
|
|
|
* @param $alertText |
170
|
|
|
* |
171
|
|
|
* @internal param $type |
172
|
|
|
*/ |
173
|
|
|
protected function renderSingleAlert($alert, $context, $alertText) { |
174
|
|
|
|
175
|
|
|
$classes = array('alert in'); |
176
|
|
|
|
177
|
|
|
if (!isset($alert['fade'])) { |
178
|
|
|
$alert['fade'] = $this->fade; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($alert['fade'] === true) { |
182
|
|
|
$classes[] = 'fade'; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
if ($this->isValidContext($context)) { |
186
|
|
|
$classes[] = 'alert-' . $this->getContextClass($context); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
if (!isset($alert['htmlOptions'])) { |
190
|
|
|
$alert['htmlOptions'] = array(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
$classes = implode(' ', $classes); |
194
|
|
|
if (isset($alert['htmlOptions']['class'])) { |
195
|
|
|
$alert['htmlOptions']['class'] .= ' ' . $classes; |
196
|
|
|
} else { |
197
|
|
|
$alert['htmlOptions']['class'] = $classes; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
echo CHtml::openTag('div', $alert['htmlOptions']); |
201
|
|
|
|
202
|
|
|
// Logic is this: if no type-specific `closeText` was defined, let's show `$this->closeText`. |
203
|
|
|
// Else, show type-specific `closeText`. Treat 'false' differently. |
204
|
|
|
if (!isset($alert['closeText'])) { |
205
|
|
|
$alert['closeText'] = (isset($this->closeText) && $this->closeText !== false) |
206
|
|
|
? $this->closeText |
207
|
|
|
: false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
// If `closeText` which is in effect now is `false` then do not show button. |
211
|
|
|
if ($alert['closeText'] !== false) { |
212
|
|
|
echo '<a href="#" class="close" data-dismiss="alert">' . $alert['closeText'] . '</a>'; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
echo $alertText; |
216
|
|
|
echo CHtml::closeTag('div'); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* only these are allowed for alerts |
221
|
|
|
* |
222
|
|
|
* @param bool $context |
223
|
|
|
* |
224
|
|
|
* @return bool |
225
|
|
|
*/ |
226
|
|
|
protected function isValidContext($context = false) { |
227
|
|
|
return in_array($context, array( |
228
|
|
|
self::CTX_SUCCESS, |
229
|
|
|
self::CTX_INFO, |
230
|
|
|
self::CTX_WARNING, |
231
|
|
|
self::CTX_DANGER, |
232
|
|
|
self::CTX_ERROR, |
233
|
|
|
)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|