HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbButton.php
Go to the documentation of this file.
1 <?php
13 Yii::import('booster.widgets.TbWidget');
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 = [
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 
58 
63  public $size;
64 
68  public $icon;
69 
73  public $label;
74 
78  public $url;
79 
83  public $block = false;
84 
88  public $active = false;
89 
93  public $disabled = false;
94 
98  public $encodeLabel = true;
99 
103  public $toggle;
104 
108  public $loadingText;
109 
114 
118  public $items;
119 
123  public $htmlOptions = array();
124 
128  public $ajaxOptions = array();
129 
134  public $dropdownOptions = array();
135 
140  public $visible = true;
141 
147  public $tooltip = false;
148 
154  public $tooltipOptions = array();
155 
161  public function init() {
162 
163  if (false === $this->visible) {
164  return;
165  }
166 
167  $classes = array('btn');
168 
169  if ($this->isValidContext()) {
170  $classes[] = 'btn-' . $this->getContextClass();
171  }
172 
173  $validSizes = array(
174  self::SIZE_LARGE,
175  self::SIZE_DEFAULT,
176  self::SIZE_SMALL,
177  self::SIZE_EXTRA_SMALL
178  );
179 
180  if (isset($this->size) && in_array($this->size, $validSizes)) {
181  $classes[] = self::$sizeClasses[$this->size];
182  }
183 
184  if ($this->block) {
185  $classes[] = 'btn-block';
186  }
187 
188  if ($this->active) {
189  $classes[] = 'active';
190  }
191 
192  if ($this->disabled) {
193  $disableTypes = array(
194  self::BUTTON_BUTTON,
195  self::BUTTON_SUBMIT,
196  self::BUTTON_RESET,
197  self::BUTTON_AJAXBUTTON,
198  self::BUTTON_AJAXSUBMIT,
199  self::BUTTON_INPUTBUTTON,
200  self::BUTTON_INPUTSUBMIT
201  );
202 
203  if (in_array($this->buttonType, $disableTypes)) {
204  $this->htmlOptions['disabled'] = 'disabled';
205  }
206 
207  $classes[] = 'disabled';
208  }
209 
210  if (!isset($this->url) && isset($this->htmlOptions['href'])) {
211  $this->url = $this->htmlOptions['href'];
212  unset($this->htmlOptions['href']);
213  }
214 
215  if ($this->encodeLabel) {
216  $this->label = CHtml::encode($this->label);
217  }
218 
219  if ($this->hasDropdown()) {
220  if (!isset($this->url)) {
221  $this->url = '#';
222  }
223 
224  $classes[] = 'dropdown-toggle';
225  $this->label .= ' <span class="caret"></span>';
226  $this->htmlOptions['data-toggle'] = 'dropdown';
227  }
228 
229  if (!empty($classes)) {
230  $classes = implode(' ', $classes);
231  if (isset($this->htmlOptions['class'])) {
232  $this->htmlOptions['class'] .= ' ' . $classes;
233  } else {
234  $this->htmlOptions['class'] = $classes;
235  }
236  }
237 
238  if (isset($this->icon)) { // no need for implode as newglyphicon only supports one icon
239  if (strpos($this->icon, 'icon') === false && strpos($this->icon, 'fa') === false) {
240  $this->icon = 'glyphicon glyphicon-' . $this->icon; // implode(' glyphicon-', explode(' ', $this->icon));
241  $this->label = '<span class="' . $this->icon . '"></span> ' . $this->label;
242  } else { // to support font awesome icons
243  $this->label = '<i class="' . $this->icon . '"></i> ' . $this->label;
244  }
245  }
246 
247  if (!isset($this->htmlOptions['id'])) {
248  $this->htmlOptions['id'] = $this->getId();
249  }
250 
251  if (isset($this->toggle)) {
252  $this->htmlOptions['data-toggle'] = 'button';
253  }
254 
255  if (isset($this->loadingText)) {
256  $this->htmlOptions['data-loading-text'] = $this->loadingText;
257  }
258 
259  if (isset($this->completeText)) {
260  $this->htmlOptions['data-complete-text'] = $this->completeText;
261  }
262 
263  if (!empty($this->tooltip) && !$this->toggle) {
264  if (!is_array($this->tooltipOptions)) {
265  $this->tooltipOptions = array();
266  }
267 
268  $this->htmlOptions['data-toggle'] = 'tooltip';
269  foreach ($this->tooltipOptions as $key => $value) {
270  $this->htmlOptions['data-' . $key] = $value;
271  }
272 
277  if (isset($this->htmlOptions['data-delay']) && is_array($this->htmlOptions['data-delay'])) {
278  $this->htmlOptions['data-delay'] = CJSON::encode($this->htmlOptions['data-delay']);
279  }
280  }
281  }
282 
288  public function run() {
289  if (false === $this->visible) {
290  return;
291  }
292 
293  if ($this->hasDropdown()) {
294 
295  echo $this->createButton();
296 
297  $this->controller->widget(
298  'booster.widgets.TbDropdown',
299  array(
300  'encodeLabel' => $this->encodeLabel,
301  'items' => $this->items,
302  'htmlOptions' => $this->dropdownOptions,
303  'id' => isset($this->dropdownOptions['id']) ? $this->dropdownOptions['id'] : null,
304  )
305  );
306  } else {
307  echo $this->createButton();
308  }
309  }
310 
318  protected function createButton() {
319 
320  switch ($this->buttonType) {
321  case self::BUTTON_LINK:
322  return CHtml::link($this->label, $this->url, $this->htmlOptions);
323 
324  case self::BUTTON_SUBMIT:
325  $this->htmlOptions['type'] = 'submit';
326  return CHtml::htmlButton($this->label, $this->htmlOptions);
327 
328  case self::BUTTON_RESET:
329  $this->htmlOptions['type'] = 'reset';
330  return CHtml::htmlButton($this->label, $this->htmlOptions);
331 
332  case self::BUTTON_SUBMITLINK:
333  return CHtml::linkButton($this->label, $this->htmlOptions);
334 
335  case self::BUTTON_AJAXLINK:
336  return CHtml::ajaxLink($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions);
337 
338  case self::BUTTON_AJAXBUTTON:
339  $this->ajaxOptions['url'] = $this->url;
340  $this->htmlOptions['ajax'] = $this->ajaxOptions;
341  return CHtml::htmlButton($this->label, $this->htmlOptions);
342 
343  case self::BUTTON_AJAXSUBMIT:
344  $this->ajaxOptions['type'] = isset($this->ajaxOptions['type']) ? $this->ajaxOptions['type'] : 'POST';
345  $this->ajaxOptions['url'] = $this->url;
346  $this->htmlOptions['type'] = 'submit';
347  $this->htmlOptions['ajax'] = $this->ajaxOptions;
348  return CHtml::htmlButton($this->label, $this->htmlOptions);
349 
350  case self::BUTTON_INPUTBUTTON:
351  return CHtml::button($this->label, $this->htmlOptions);
352 
353  case self::BUTTON_INPUTSUBMIT:
354  $this->htmlOptions['type'] = 'submit';
355  return CHtml::button($this->label, $this->htmlOptions);
356 
357  case self::BUTTON_TOGGLE_RADIO:
358  return $this->createToggleButton('radio');
359 
360  case self::BUTTON_TOGGLE_CHECKBOX:
361  return $this->createToggleButton('checkbox');
362 
363  default:
364  case self::BUTTON_BUTTON:
365  return CHtml::htmlButton($this->label, $this->htmlOptions);
366  }
367  }
368 
369  protected function createToggleButton($toggleType) {
370 
371  $html = '';
372  $html .= CHtml::openTag('label', $this->htmlOptions);
373  $html .= "<input type='{$toggleType}' name='{$this->id}_options' id='option_{$this->id}'> {$this->label}";
374  $html .= CHtml::closeTag('label');
375 
376  return $html;
377  }
378 
386  protected function hasDropdown() {
387 
388  return isset($this->items) && !empty($this->items);
389  }
390 }