1 <?php
2 3 4 5 6 7 8 9
10
11 12 13 14
15 class TbButton extends CWidget
16 {
17
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
27
28 const TYPE_PRIMARY = 'primary';
29 const TYPE_INFO = 'info';
30 const TYPE_SUCCESS = 'success';
31 const TYPE_WARNING = 'warning';
32 const TYPE_DANGER = 'danger';
33 const TYPE_INVERSE = 'inverse';
34 const TYPE_LINK = 'link';
35
36
37 const SIZE_MINI = 'mini';
38 const SIZE_SMALL = 'small';
39 const SIZE_LARGE = 'large';
40
41 42 43 44
45 public $buttonType = self::BUTTON_LINK;
46 47 48 49
50 public $type;
51 52 53 54
55 public $size;
56 57 58
59 public $icon;
60 61 62
63 public $label;
64 65 66
67 public $url;
68 69 70
71 public $block = false;
72 73 74
75 public $active = false;
76 77 78
79 public $disabled = false;
80 81 82
83 public $encodeLabel = true;
84 85 86
87 public $toggle;
88 89 90
91 public $loadingText;
92 93 94
95 public $completeText;
96 97 98
99 public $items;
100 101 102
103 public $htmlOptions = array();
104 105 106
107 public $ajaxOptions = array();
108 109 110 111
112 public $dropdownOptions = array();
113
114 115 116
117 public function init()
118 {
119 $classes = array('btn');
120
121 $validTypes = array(self::TYPE_LINK, self::TYPE_PRIMARY, self::TYPE_INFO, self::TYPE_SUCCESS,
122 self::TYPE_WARNING, self::TYPE_DANGER, self::TYPE_INVERSE);
123
124 if (isset($this->type) && in_array($this->type, $validTypes))
125 $classes[] = 'btn-'.$this->type;
126
127 $validSizes = array(self::SIZE_LARGE, self::SIZE_SMALL, self::SIZE_MINI);
128
129 if (isset($this->size) && in_array($this->size, $validSizes))
130 $classes[] = 'btn-'.$this->size;
131
132 if ($this->block)
133 $classes[] = 'btn-block';
134
135 if ($this->active)
136 $classes[] = 'active';
137
138 if ($this->disabled)
139 $classes[] = 'disabled';
140
141 if ($this->encodeLabel)
142 $this->label = CHtml::encode($this->label);
143
144 if ($this->hasDropdown())
145 {
146 if (!isset($this->url))
147 $this->url = '#';
148
149 $classes[] = 'dropdown-toggle';
150 $this->label .= ' <span class="caret"></span>';
151 $this->htmlOptions['data-toggle'] = 'dropdown';
152 }
153
154 if (!empty($classes))
155 {
156 $classes = implode(' ', $classes);
157 if (isset($this->htmlOptions['class']))
158 $this->htmlOptions['class'] .= ' '.$classes;
159 else
160 $this->htmlOptions['class'] = $classes;
161 }
162
163 if (isset($this->icon))
164 {
165 if (strpos($this->icon, 'icon') === false)
166 $this->icon = 'icon-'.implode(' icon-', explode(' ', $this->icon));
167
168 $this->label = '<i class="'.$this->icon.'"></i> '.$this->label;
169 }
170
171 if (isset($this->toggle))
172 $this->htmlOptions['data-toggle'] = 'button';
173
174 if (isset($this->loadingText))
175 $this->htmlOptions['data-loading-text'] = $this->loadingText;
176
177 if (isset($this->completeText))
178 $this->htmlOptions['data-complete-text'] = $this->completeText;
179 }
180
181 182 183
184 public function run()
185 {
186 echo $this->createButton();
187
188 if ($this->hasDropdown())
189 {
190 $this->controller->widget('bootstrap.widgets.TbDropdown', array(
191 'encodeLabel'=>$this->encodeLabel,
192 'items'=>$this->items,
193 'htmlOptions'=>$this->dropdownOptions,
194 ));
195 }
196 }
197
198 199 200 201
202 protected function createButton()
203 {
204 switch ($this->buttonType)
205 {
206 case self::BUTTON_BUTTON:
207 return CHtml::htmlButton($this->label, $this->htmlOptions);
208
209 case self::BUTTON_SUBMIT:
210 $this->htmlOptions['type'] = 'submit';
211 return CHtml::htmlButton($this->label, $this->htmlOptions);
212
213 case self::BUTTON_RESET:
214 $this->htmlOptions['type'] = 'reset';
215 return CHtml::htmlButton($this->label, $this->htmlOptions);
216
217 case self::BUTTON_SUBMITLINK:
218 return CHtml::linkButton($this->label, $this->htmlOptions);
219
220 case self::BUTTON_AJAXLINK:
221 return CHtml::ajaxLink($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions);
222
223 case self::BUTTON_AJAXBUTTON:
224 $this->ajaxOptions['url'] = $this->url;
225 $this->htmlOptions['ajax'] = $this->ajaxOptions;
226 return CHtml::htmlButton($this->label, $this->htmlOptions);
227
228 case self::BUTTON_AJAXSUBMIT:
229 $this->ajaxOptions['type'] = 'POST';
230 $this->ajaxOptions['url'] = $this->url;
231 $this->htmlOptions['type'] = 'submit';
232 $this->htmlOptions['ajax'] = $this->ajaxOptions;
233 return CHtml::htmlButton($this->label, $this->htmlOptions);
234
235 default:
236 case self::BUTTON_LINK:
237 return CHtml::link($this->label, $this->url, $this->htmlOptions);
238 }
239 }
240
241 242 243 244
245 protected function hasDropdown()
246 {
247 return isset($this->items) && !empty($this->items);
248 }
249 }
250