1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* TbButton class file.
|
4
|
|
|
* @author Christoffer Niska <[email protected]>
|
5
|
|
|
* @copyright Copyright © Christoffer Niska 2011-
|
6
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php New BSD License
|
7
|
|
|
* @package bootstrap.widgets
|
8
|
|
|
* @since 0.9.10
|
9
|
|
|
*/
|
10
|
|
|
|
11
|
|
|
/**
|
12
|
|
|
* Bootstrap button widget.
|
13
|
|
|
* @see http://twitter.github.com/bootstrap/base-css.html#buttons
|
14
|
|
|
*/
|
15
|
|
|
class TbButton extends CWidget
|
16
|
|
|
{
|
17
|
|
|
// Button callback types.
|
18
|
|
|
const BUTTON_LINK = 'link';
|
19
|
|
|
const BUTTON_BUTTON = 'button';
|
20
|
|
|
const BUTTON_SUBMIT = 'submit';
|
21
|
|
|
const BUTTON_SUBMITLINK = 'submitLink';
|
22
|
|
|
const BUTTON_RESET = 'reset';
|
23
|
|
|
const BUTTON_AJAXLINK = 'ajaxLink';
|
24
|
|
|
const BUTTON_AJAXBUTTON = 'ajaxButton';
|
25
|
|
|
const BUTTON_AJAXSUBMIT = 'ajaxSubmit';
|
26
|
|
|
const BUTTON_INPUTBUTTON = 'inputButton';
|
27
|
|
|
const BUTTON_INPUTSUBMIT = 'inputSubmit';
|
28
|
|
|
|
29
|
|
|
// Button types.
|
30
|
|
|
const TYPE_PRIMARY = 'primary';
|
31
|
|
|
const TYPE_INFO = 'info';
|
32
|
|
|
const TYPE_SUCCESS = 'success';
|
33
|
|
|
const TYPE_WARNING = 'warning';
|
34
|
|
|
const TYPE_DANGER = 'danger';
|
35
|
|
|
const TYPE_INVERSE = 'inverse';
|
36
|
|
|
const TYPE_LINK = 'link';
|
37
|
|
|
const TYPE_CLEAR = 'clear';
|
38
|
|
|
|
39
|
|
|
// Button sizes.
|
40
|
|
|
const SIZE_MINI = 'mini';
|
41
|
|
|
const SIZE_SMALL = 'small';
|
42
|
|
|
const SIZE_LARGE = 'large';
|
43
|
|
|
|
44
|
|
|
/**
|
45
|
|
|
* @var string the button callback types.
|
46
|
|
|
* Valid values are 'link', 'button', 'submit', 'submitLink', 'reset', 'ajaxLink', 'ajaxButton' and 'ajaxSubmit'.
|
47
|
|
|
*/
|
48
|
|
|
public $buttonType = self::BUTTON_LINK;
|
49
|
|
|
/**
|
50
|
|
|
* @var string the button type.
|
51
|
|
|
* Valid values are 'primary', 'info', 'success', 'warning', 'danger' and 'inverse'.
|
52
|
|
|
*/
|
53
|
|
|
public $type;
|
54
|
|
|
/**
|
55
|
|
|
* @var string the button size.
|
56
|
|
|
* Valid values are 'large', 'small' and 'mini'.
|
57
|
|
|
*/
|
58
|
|
|
public $size;
|
59
|
|
|
/**
|
60
|
|
|
* @var string the button icon, e.g. 'ok' or 'remove white'.
|
61
|
|
|
*/
|
62
|
|
|
public $icon;
|
63
|
|
|
/**
|
64
|
|
|
* @var string the button label.
|
65
|
|
|
*/
|
66
|
|
|
public $label;
|
67
|
|
|
/**
|
68
|
|
|
* @var string the button URL.
|
69
|
|
|
*/
|
70
|
|
|
public $url;
|
71
|
|
|
/**
|
72
|
|
|
* @var boolean indicates whether the button should span the full width of the a parent.
|
73
|
|
|
*/
|
74
|
|
|
public $block = false;
|
75
|
|
|
/**
|
76
|
|
|
* @var boolean indicates whether the button is active.
|
77
|
|
|
*/
|
78
|
|
|
public $active = false;
|
79
|
|
|
/**
|
80
|
|
|
* @var boolean indicates whether the button is disabled.
|
81
|
|
|
*/
|
82
|
|
|
public $disabled = false;
|
83
|
|
|
/**
|
84
|
|
|
* @var boolean indicates whether to encode the label.
|
85
|
|
|
*/
|
86
|
|
|
public $encodeLabel = true;
|
87
|
|
|
/**
|
88
|
|
|
* @var boolean indicates whether to enable toggle.
|
89
|
|
|
*/
|
90
|
|
|
public $toggle;
|
91
|
|
|
/**
|
92
|
|
|
* @var string the loading text.
|
93
|
|
|
*/
|
94
|
|
|
public $loadingText;
|
95
|
|
|
/**
|
96
|
|
|
* @var string the complete text.
|
97
|
|
|
*/
|
98
|
|
|
public $completeText;
|
99
|
|
|
/**
|
100
|
|
|
* @var array the dropdown button items.
|
101
|
|
|
*/
|
102
|
|
|
public $items;
|
103
|
|
|
/**
|
104
|
|
|
* @var array the HTML attributes for the widget container.
|
105
|
|
|
*/
|
106
|
|
|
public $htmlOptions = array();
|
107
|
|
|
/**
|
108
|
|
|
* @var array the button ajax options (used by 'ajaxLink' and 'ajaxButton').
|
109
|
|
|
*/
|
110
|
|
|
public $ajaxOptions = array();
|
111
|
|
|
/**
|
112
|
|
|
* @var array the HTML attributes for the dropdown menu.
|
113
|
|
|
* @since 0.9.11
|
114
|
|
|
*/
|
115
|
|
|
public $dropdownOptions = array();
|
116
|
|
|
|
117
|
|
|
/**
|
118
|
|
|
* Initializes the widget.
|
119
|
|
|
*/
|
120
|
|
|
public function init()
|
121
|
|
|
{
|
122
|
|
|
$classes = array('btn');
|
123
|
|
|
|
124
|
|
|
$validTypes = array(self::TYPE_LINK, self::TYPE_PRIMARY, self::TYPE_INFO, self::TYPE_SUCCESS,
|
125
|
|
|
self::TYPE_WARNING, self::TYPE_DANGER, self::TYPE_INVERSE, self::TYPE_CLEAR);
|
126
|
|
|
|
127
|
|
|
if (isset($this->type) && in_array($this->type, $validTypes))
|
128
|
|
|
$classes[] = 'btn-'.$this->type;
|
129
|
|
|
|
130
|
|
|
$validSizes = array(self::SIZE_LARGE, self::SIZE_SMALL, self::SIZE_MINI);
|
131
|
|
|
|
132
|
|
|
if (isset($this->size) && in_array($this->size, $validSizes))
|
133
|
|
|
$classes[] = 'btn-'.$this->size;
|
134
|
|
|
|
135
|
|
|
if ($this->block)
|
136
|
|
|
$classes[] = 'btn-block';
|
137
|
|
|
|
138
|
|
|
if ($this->active)
|
139
|
|
|
$classes[] = 'active';
|
140
|
|
|
|
141
|
|
|
if ($this->disabled)
|
142
|
|
|
{
|
143
|
|
|
$disableTypes = array(self::BUTTON_BUTTON, self::BUTTON_SUBMIT, self::BUTTON_RESET,
|
144
|
|
|
self::BUTTON_AJAXBUTTON, self::BUTTON_AJAXSUBMIT, self::BUTTON_INPUTBUTTON, self::BUTTON_INPUTSUBMIT);
|
145
|
|
|
|
146
|
|
|
if (in_array($this->buttonType, $disableTypes))
|
147
|
|
|
$this->htmlOptions['disabled'] = 'disabled';
|
148
|
|
|
|
149
|
|
|
$classes[] = 'disabled';
|
150
|
|
|
}
|
151
|
|
|
|
152
|
|
|
if (!isset($this->url) && isset($this->htmlOptions['href']))
|
153
|
|
|
{
|
154
|
|
|
$this->url = $this->htmlOptions['href'];
|
155
|
|
|
unset($this->htmlOptions['href']);
|
156
|
|
|
}
|
157
|
|
|
|
158
|
|
|
if ($this->encodeLabel)
|
159
|
|
|
$this->label = CHtml::encode($this->label);
|
160
|
|
|
|
161
|
|
|
if ($this->hasDropdown())
|
162
|
|
|
{
|
163
|
|
|
if (!isset($this->url))
|
164
|
|
|
$this->url = '#';
|
165
|
|
|
|
166
|
|
|
$classes[] = 'dropdown-toggle';
|
167
|
|
|
$this->label .= ' <span class="caret"></span>';
|
168
|
|
|
$this->htmlOptions['data-toggle'] = 'dropdown';
|
169
|
|
|
}
|
170
|
|
|
|
171
|
|
|
if (!empty($classes))
|
172
|
|
|
{
|
173
|
|
|
$classes = implode(' ', $classes);
|
174
|
|
|
if (isset($this->htmlOptions['class']))
|
175
|
|
|
$this->htmlOptions['class'] .= ' '.$classes;
|
176
|
|
|
else
|
177
|
|
|
$this->htmlOptions['class'] = $classes;
|
178
|
|
|
}
|
179
|
|
|
|
180
|
|
|
if (isset($this->icon))
|
181
|
|
|
{
|
182
|
|
|
if (strpos($this->icon, 'icon') === false)
|
183
|
|
|
$this->icon = 'icon-'.implode(' icon-', explode(' ', $this->icon));
|
184
|
|
|
|
185
|
|
|
$this->label = '<i class="'.$this->icon.'"></i> '.$this->label;
|
186
|
|
|
}
|
187
|
|
|
|
188
|
|
|
if (isset($this->toggle))
|
189
|
|
|
$this->htmlOptions['data-toggle'] = 'button';
|
190
|
|
|
|
191
|
|
|
if (isset($this->loadingText))
|
192
|
|
|
$this->htmlOptions['data-loading-text'] = $this->loadingText;
|
193
|
|
|
|
194
|
|
|
if (isset($this->completeText))
|
195
|
|
|
$this->htmlOptions['data-complete-text'] = $this->completeText;
|
196
|
|
|
}
|
197
|
|
|
|
198
|
|
|
/**
|
199
|
|
|
* Runs the widget.
|
200
|
|
|
*/
|
201
|
|
|
public function run()
|
202
|
|
|
{
|
203
|
|
|
echo $this->createButton();
|
204
|
|
|
|
205
|
|
|
if ($this->hasDropdown())
|
206
|
|
|
{
|
207
|
|
|
$this->controller->widget('bootstrap.widgets.TbDropdown', array(
|
208
|
|
|
'encodeLabel'=>$this->encodeLabel,
|
209
|
|
|
'items'=>$this->items,
|
210
|
|
|
'htmlOptions'=>$this->dropdownOptions,
|
211
|
|
|
));
|
212
|
|
|
}
|
213
|
|
|
}
|
214
|
|
|
|
215
|
|
|
/**
|
216
|
|
|
* Creates the button element.
|
217
|
|
|
* @return string the created button.
|
218
|
|
|
*/
|
219
|
|
|
protected function createButton()
|
220
|
|
|
{
|
221
|
|
|
switch ($this->buttonType)
|
222
|
|
|
{
|
223
|
|
|
case self::BUTTON_BUTTON:
|
224
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions);
|
225
|
|
|
|
226
|
|
View Code Duplication |
case self::BUTTON_SUBMIT:
|
|
|
|
|
227
|
|
|
$this->htmlOptions['type'] = 'submit';
|
228
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions);
|
229
|
|
|
|
230
|
|
View Code Duplication |
case self::BUTTON_RESET:
|
|
|
|
|
231
|
|
|
$this->htmlOptions['type'] = 'reset';
|
232
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions);
|
233
|
|
|
|
234
|
|
|
case self::BUTTON_SUBMITLINK:
|
235
|
|
|
return CHtml::linkButton($this->label, $this->htmlOptions);
|
236
|
|
|
|
237
|
|
|
case self::BUTTON_AJAXLINK:
|
238
|
|
|
return CHtml::ajaxLink($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions);
|
239
|
|
|
|
240
|
|
|
case self::BUTTON_AJAXBUTTON:
|
241
|
|
|
$this->ajaxOptions['url'] = $this->url;
|
242
|
|
|
$this->htmlOptions['ajax'] = $this->ajaxOptions;
|
243
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions);
|
244
|
|
|
|
245
|
|
|
case self::BUTTON_AJAXSUBMIT:
|
246
|
|
|
$this->ajaxOptions['type'] = isset($this->ajaxOptions['type']) ? $this->ajaxOptions['type'] : 'POST';
|
247
|
|
|
$this->ajaxOptions['url'] = $this->url;
|
248
|
|
|
$this->htmlOptions['type'] = 'submit';
|
249
|
|
|
$this->htmlOptions['ajax'] = $this->ajaxOptions;
|
250
|
|
|
return CHtml::htmlButton($this->label, $this->htmlOptions);
|
251
|
|
|
|
252
|
|
|
case self::BUTTON_INPUTBUTTON:
|
253
|
|
|
return CHtml::button($this->label, $this->htmlOptions);
|
254
|
|
|
|
255
|
|
|
case self::BUTTON_INPUTSUBMIT:
|
256
|
|
|
$this->htmlOptions['type'] = 'submit';
|
257
|
|
|
return CHtml::button($this->label, $this->htmlOptions);
|
258
|
|
|
|
259
|
|
|
default:
|
260
|
|
|
case self::BUTTON_LINK:
|
|
|
|
|
261
|
|
|
return CHtml::link($this->label, $this->url, $this->htmlOptions);
|
262
|
|
|
}
|
263
|
|
|
}
|
264
|
|
|
|
265
|
|
|
/**
|
266
|
|
|
* Returns whether the button has a dropdown.
|
267
|
|
|
* @return bool the result.
|
268
|
|
|
*/
|
269
|
|
|
protected function hasDropdown()
|
270
|
|
|
{
|
271
|
|
|
return isset($this->items) && !empty($this->items);
|
272
|
|
|
}
|
273
|
|
|
}
|
274
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.