1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
*## TbButton 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
|
|
|
* @since 0.9.10 |
9
|
|
|
* |
10
|
|
|
* @author Amr Bedair <[email protected]> |
11
|
|
|
* @since v4.0.0 - upgraded to bootstrap 3.1.1 |
12
|
|
|
*/ |
13
|
|
|
Yii::import('booster.widgets.TbWidget'); |
14
|
|
|
/** |
15
|
|
|
* Bootstrap button widget. |
16
|
|
|
* |
17
|
|
|
* @see http://getbootstrap.com/css/#buttons |
18
|
|
|
* |
19
|
|
|
* @package booster.widgets.forms.buttons |
20
|
|
|
*/ |
21
|
|
|
class TbButton extends TbWidget { |
22
|
|
|
|
23
|
|
|
// Button callback types. |
24
|
|
|
const BUTTON_LINK = 'link'; |
25
|
|
|
const BUTTON_BUTTON = 'button'; |
26
|
|
|
const BUTTON_SUBMIT = 'submit'; |
27
|
|
|
const BUTTON_SUBMITLINK = 'submitLink'; |
28
|
|
|
const BUTTON_RESET = 'reset'; |
29
|
|
|
const BUTTON_AJAXLINK = 'ajaxLink'; |
30
|
|
|
const BUTTON_AJAXBUTTON = 'ajaxButton'; |
31
|
|
|
const BUTTON_AJAXSUBMIT = 'ajaxSubmit'; |
32
|
|
|
const BUTTON_INPUTBUTTON = 'inputButton'; |
33
|
|
|
const BUTTON_INPUTSUBMIT = 'inputSubmit'; |
34
|
|
|
const BUTTON_TOGGLE_RADIO = 'radio'; |
35
|
|
|
const BUTTON_TOGGLE_CHECKBOX = 'checkbox'; |
36
|
|
|
|
37
|
|
|
const CTX_LINK = 'link'; |
38
|
|
|
const CTX_LINK_CLASS = 'link'; |
39
|
|
|
|
40
|
|
|
// Button sizes. |
41
|
|
|
const SIZE_LARGE = 'large'; |
42
|
|
|
const SIZE_DEFAULT = 'default'; |
43
|
|
|
const SIZE_SMALL = 'small'; |
44
|
|
|
const SIZE_EXTRA_SMALL = 'extra_small'; |
45
|
|
|
|
46
|
|
|
protected static $sizeClasses = array( |
47
|
|
|
self::SIZE_LARGE => 'btn-lg', |
48
|
|
|
self::SIZE_DEFAULT => '', |
49
|
|
|
self::SIZE_SMALL => 'btn-sm', |
50
|
|
|
self::SIZE_EXTRA_SMALL => 'btn-xs', |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var string the button callback types. |
55
|
|
|
* Valid values are 'link', 'button', 'submit', 'submitLink', 'reset', 'ajaxLink', 'ajaxButton' and 'ajaxSubmit'. |
56
|
|
|
*/ |
57
|
|
|
public $buttonType = self::BUTTON_BUTTON; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string the button size. |
61
|
|
|
* Valid values are 'large', 'small' and 'mini'. |
62
|
|
|
*/ |
63
|
|
|
public $size; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var string the button icon, e.g. 'ok' or 'remove white'. |
67
|
|
|
*/ |
68
|
|
|
public $icon; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var string the button label. |
72
|
|
|
*/ |
73
|
|
|
public $label; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var string the button URL. |
77
|
|
|
*/ |
78
|
|
|
public $url; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var boolean indicates whether the button should span the full width of the a parent. |
82
|
|
|
*/ |
83
|
|
|
public $block = false; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var boolean indicates whether the button is active. |
87
|
|
|
*/ |
88
|
|
|
public $active = false; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var boolean indicates whether the button is disabled. |
92
|
|
|
*/ |
93
|
|
|
public $disabled = false; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var boolean indicates whether to encode the label. |
97
|
|
|
*/ |
98
|
|
|
public $encodeLabel = true; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var boolean indicates whether to enable toggle. |
102
|
|
|
*/ |
103
|
|
|
public $toggle; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @var string the loading text. |
107
|
|
|
*/ |
108
|
|
|
public $loadingText; |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @var string the complete text. |
112
|
|
|
*/ |
113
|
|
|
public $completeText; |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @var array the dropdown button items. |
117
|
|
|
*/ |
118
|
|
|
public $items; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @var array the HTML attributes for the widget container. |
122
|
|
|
*/ |
123
|
|
|
public $htmlOptions = array(); |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @var array the button ajax options (used by 'ajaxLink' and 'ajaxButton'). |
127
|
|
|
*/ |
128
|
|
|
public $ajaxOptions = array(); |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var array the HTML attributes for the dropdown menu. |
132
|
|
|
* @since 0.9.11 |
133
|
|
|
*/ |
134
|
|
|
public $dropdownOptions = array(); |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @var bool whether the button is visible or not |
138
|
|
|
* @since 0.9.11 |
139
|
|
|
*/ |
140
|
|
|
public $visible = true; |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Tooltip for button |
144
|
|
|
* @var bool |
145
|
|
|
* @since 2.1.0 |
146
|
|
|
*/ |
147
|
|
|
public $tooltip = false; |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Tooltip options |
151
|
|
|
* @var array |
152
|
|
|
* @since 2.1.0 |
153
|
|
|
*/ |
154
|
|
|
public $tooltipOptions = array(); |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
*### .init() |
158
|
|
|
* |
159
|
|
|
* Initializes the widget. |
160
|
|
|
*/ |
161
|
1 |
|
public function init() { |
162
|
|
|
|
163
|
1 |
|
if (false === $this->visible) { |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
$classes = array('btn'); |
168
|
|
|
|
169
|
1 |
|
if ($this->isValidContext()) { |
170
|
1 |
|
$classes[] = 'btn-' . $this->getContextClass(); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
1 |
|
if($this->buttonType == self::BUTTON_LINK) { |
174
|
|
|
$classes[] = 'btn-link'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$validSizes = array( |
178
|
1 |
|
self::SIZE_LARGE, |
179
|
1 |
|
self::SIZE_DEFAULT, |
180
|
1 |
|
self::SIZE_SMALL, |
181
|
|
|
self::SIZE_EXTRA_SMALL |
182
|
1 |
|
); |
183
|
|
|
|
184
|
1 |
|
if (isset($this->size) && in_array($this->size, $validSizes)) { |
185
|
|
|
$classes[] = self::$sizeClasses[$this->size]; |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
if ($this->block) { |
189
|
|
|
$classes[] = 'btn-block'; |
190
|
|
|
} |
191
|
|
|
|
192
|
1 |
|
if ($this->active) { |
193
|
|
|
$classes[] = 'active'; |
194
|
|
|
} |
195
|
|
|
|
196
|
1 |
|
if ($this->disabled) { |
197
|
|
|
$disableTypes = array( |
198
|
|
|
self::BUTTON_BUTTON, |
199
|
|
|
self::BUTTON_SUBMIT, |
200
|
|
|
self::BUTTON_RESET, |
201
|
|
|
self::BUTTON_AJAXBUTTON, |
202
|
|
|
self::BUTTON_AJAXSUBMIT, |
203
|
|
|
self::BUTTON_INPUTBUTTON, |
204
|
|
|
self::BUTTON_INPUTSUBMIT |
205
|
|
|
); |
206
|
|
|
|
207
|
|
|
if (in_array($this->buttonType, $disableTypes)) { |
208
|
|
|
$this->htmlOptions['disabled'] = 'disabled'; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$classes[] = 'disabled'; |
212
|
|
|
} |
213
|
|
|
|
214
|
1 |
|
if (!isset($this->url) && isset($this->htmlOptions['href'])) { |
215
|
|
|
$this->url = $this->htmlOptions['href']; |
216
|
|
|
unset($this->htmlOptions['href']); |
217
|
|
|
} |
218
|
|
|
|
219
|
1 |
|
if ($this->encodeLabel) { |
220
|
1 |
|
$this->label = CHtml::encode($this->label); |
221
|
1 |
|
} |
222
|
|
|
|
223
|
1 |
|
if ($this->hasDropdown()) { |
224
|
|
|
if (!isset($this->url)) { |
225
|
|
|
$this->url = '#'; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$classes[] = 'dropdown-toggle'; |
229
|
|
|
$this->label .= ' <span class="caret"></span>'; |
230
|
|
|
$this->htmlOptions['data-toggle'] = 'dropdown'; |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
if (!empty($classes)) { |
234
|
1 |
|
$classes = implode(' ', $classes); |
235
|
1 |
|
if (isset($this->htmlOptions['class'])) { |
236
|
|
|
$this->htmlOptions['class'] .= ' ' . $classes; |
237
|
|
|
} else { |
238
|
1 |
|
$this->htmlOptions['class'] = $classes; |
239
|
|
|
} |
240
|
1 |
|
} |
241
|
|
|
|
242
|
1 |
|
if (isset($this->icon)) { // no need for implode as newglyphicon only supports one icon |
243
|
|
|
if (strpos($this->icon, 'icon') === false && strpos($this->icon, 'fa') === false) { |
244
|
|
|
$this->icon = 'glyphicon glyphicon-' . $this->icon; |
245
|
|
|
$this->label = '<span class="' . $this->icon . '"></span> ' . $this->label; |
246
|
|
|
} else { // to support font awesome icons |
247
|
|
|
$this->label = '<i class="' . $this->icon . '"></i> ' . $this->label; |
248
|
|
|
} |
249
|
|
|
} |
250
|
|
|
|
251
|
1 |
|
if (!isset($this->htmlOptions['id'])) { |
252
|
1 |
|
$this->htmlOptions['id'] = $this->getId(); |
253
|
1 |
|
} |
254
|
|
|
|
255
|
1 |
|
if (isset($this->toggle)) { |
256
|
|
|
$this->htmlOptions['data-toggle'] = 'button'; |
257
|
|
|
} |
258
|
|
|
|
259
|
1 |
|
if (isset($this->loadingText)) { |
260
|
|
|
$this->htmlOptions['data-loading-text'] = $this->loadingText; |
261
|
|
|
} |
262
|
|
|
|
263
|
1 |
|
if (isset($this->completeText)) { |
264
|
|
|
$this->htmlOptions['data-complete-text'] = $this->completeText; |
265
|
|
|
} |
266
|
|
|
|
267
|
1 |
|
if (!empty($this->tooltip) && !$this->toggle) { |
|
|
|
|
268
|
|
|
if (!is_array($this->tooltipOptions)) { |
269
|
|
|
$this->tooltipOptions = array(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
$this->htmlOptions['data-toggle'] = 'tooltip'; |
273
|
|
|
foreach ($this->tooltipOptions as $key => $value) { |
274
|
|
|
$this->htmlOptions['data-' . $key] = $value; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Encode delay option |
279
|
|
|
* @link http://getbootstrap.com/2.3.2/javascript.html#tooltips |
280
|
|
|
*/ |
281
|
|
|
if (isset($this->htmlOptions['data-delay']) && is_array($this->htmlOptions['data-delay'])) { |
282
|
|
|
$this->htmlOptions['data-delay'] = CJSON::encode($this->htmlOptions['data-delay']); |
283
|
|
|
} |
284
|
|
|
} |
285
|
1 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
*### .run() |
289
|
|
|
* |
290
|
|
|
* Runs the widget. |
291
|
|
|
*/ |
292
|
1 |
|
public function run() { |
293
|
1 |
|
if (false === $this->visible) { |
294
|
|
|
return; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
// TODO: I think we have to drop this -allowing button to has items- it is the same as TbButtonGroup with bugs! |
298
|
1 |
|
if ($this->hasDropdown()) { |
299
|
|
|
echo '<div class="btn-group">'; |
300
|
|
|
echo $this->createButton(); |
301
|
|
|
|
302
|
|
|
$this->controller->widget( |
303
|
|
|
'booster.widgets.TbDropdown', |
304
|
|
|
array( |
305
|
|
|
'encodeLabel' => $this->encodeLabel, |
306
|
|
|
'items' => $this->items, |
307
|
|
|
'htmlOptions' => $this->dropdownOptions, |
308
|
|
|
'id' => isset($this->dropdownOptions['id']) ? $this->dropdownOptions['id'] : null, |
309
|
|
|
) |
310
|
|
|
); |
311
|
|
|
echo '</div>'; |
312
|
|
|
} else { |
313
|
1 |
|
echo $this->createButton(); |
314
|
|
|
} |
315
|
1 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
*### .createButton() |
319
|
|
|
* |
320
|
|
|
* Creates the button element. |
321
|
|
|
* |
322
|
|
|
* @return string the created button. |
323
|
|
|
*/ |
324
|
1 |
|
protected function createButton() { |
325
|
|
|
|
326
|
1 |
|
switch ($this->buttonType) { |
327
|
1 |
|
case self::BUTTON_LINK: |
328
|
|
|
return CHtml::link($this->label, $this->url, $this->htmlOptions); |
329
|
|
|
|
330
|
1 |
|
case self::BUTTON_SUBMIT: |
331
|
1 |
|
$this->htmlOptions['type'] = 'submit'; |
332
|
1 |
|
return CHtml::htmlButton($this->label, $this->htmlOptions); |
333
|
|
|
|
334
|
1 |
|
case self::BUTTON_RESET: |
335
|
1 |
|
$this->htmlOptions['type'] = 'reset'; |
336
|
1 |
|
return CHtml::htmlButton($this->label, $this->htmlOptions); |
337
|
|
|
|
338
|
|
|
case self::BUTTON_SUBMITLINK: |
339
|
|
|
return CHtml::linkButton($this->label, $this->htmlOptions); |
340
|
|
|
|
341
|
|
|
case self::BUTTON_AJAXLINK: |
342
|
|
|
return CHtml::ajaxLink($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions); |
343
|
|
|
|
344
|
|
|
case self::BUTTON_AJAXBUTTON: |
345
|
|
|
$this->ajaxOptions['url'] = $this->url; |
346
|
|
|
$this->htmlOptions['ajax'] = $this->ajaxOptions; |
347
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions); |
348
|
|
|
|
349
|
|
|
case self::BUTTON_AJAXSUBMIT: |
350
|
|
|
$this->ajaxOptions['type'] = isset($this->ajaxOptions['type']) ? $this->ajaxOptions['type'] : 'POST'; |
351
|
|
|
$this->ajaxOptions['url'] = $this->url; |
352
|
|
|
$this->htmlOptions['type'] = 'submit'; |
353
|
|
|
$this->htmlOptions['ajax'] = $this->ajaxOptions; |
354
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions); |
355
|
|
|
|
356
|
|
|
case self::BUTTON_INPUTBUTTON: |
357
|
|
|
return CHtml::button($this->label, $this->htmlOptions); |
358
|
|
|
|
359
|
|
|
case self::BUTTON_INPUTSUBMIT: |
360
|
|
|
$this->htmlOptions['type'] = 'submit'; |
361
|
|
|
return CHtml::button($this->label, $this->htmlOptions); |
362
|
|
|
|
363
|
|
|
case self::BUTTON_TOGGLE_RADIO: |
364
|
|
|
return $this->createToggleButton('radio'); |
365
|
|
|
|
366
|
|
|
case self::BUTTON_TOGGLE_CHECKBOX: |
367
|
|
|
return $this->createToggleButton('checkbox'); |
368
|
|
|
|
369
|
|
|
default: |
370
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions); |
371
|
|
|
} |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param string $toggleType |
376
|
|
|
* |
377
|
|
|
* @return string |
378
|
|
|
*/ |
379
|
|
|
protected function createToggleButton($toggleType) { |
380
|
|
|
|
381
|
|
|
$html = ''; |
382
|
|
|
$html .= CHtml::openTag('label', $this->htmlOptions); |
383
|
|
|
$html .= "<input type='{$toggleType}' name='{$this->id}_options' id='option_{$this->id}'> {$this->label}"; |
384
|
|
|
$html .= CHtml::closeTag('label'); |
385
|
|
|
|
386
|
|
|
return $html; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
*### .hasDropdown() |
391
|
|
|
* |
392
|
|
|
* Returns whether the button has a dropdown. |
393
|
|
|
* |
394
|
|
|
* @return bool the result. |
395
|
|
|
*/ |
396
|
1 |
|
protected function hasDropdown() { |
397
|
|
|
|
398
|
1 |
|
return isset($this->items) && !empty($this->items); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.