PageRenderTime 30ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/bootstrap/widgets/TbButton.php

https://bitbucket.org/sonnylazuardi/savary
PHP | 272 lines | 151 code | 37 blank | 84 comment | 23 complexity | dacaeede5850e446843bbbeb159d60a6 MD5 | raw file
  1. <?php
  2. /**
  3. * TbButton class file.
  4. * @author Christoffer Niska <ChristofferNiska@gmail.com>
  5. * @copyright Copyright &copy; 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. * Bootstrap button widget.
  12. * @see http://twitter.github.com/bootstrap/base-css.html#buttons
  13. */
  14. class TbButton extends CWidget
  15. {
  16. // Button callback types.
  17. const BUTTON_LINK = 'link';
  18. const BUTTON_BUTTON = 'button';
  19. const BUTTON_SUBMIT = 'submit';
  20. const BUTTON_SUBMITLINK = 'submitLink';
  21. const BUTTON_RESET = 'reset';
  22. const BUTTON_AJAXLINK = 'ajaxLink';
  23. const BUTTON_AJAXBUTTON = 'ajaxButton';
  24. const BUTTON_AJAXSUBMIT = 'ajaxSubmit';
  25. const BUTTON_INPUTBUTTON = 'inputButton';
  26. const BUTTON_INPUTSUBMIT = 'inputSubmit';
  27. // Button types.
  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. // Button sizes.
  36. const SIZE_MINI = 'mini';
  37. const SIZE_SMALL = 'small';
  38. const SIZE_LARGE = 'large';
  39. /**
  40. * @var string the button callback types.
  41. * Valid values are 'link', 'button', 'submit', 'submitLink', 'reset', 'ajaxLink', 'ajaxButton' and 'ajaxSubmit'.
  42. */
  43. public $buttonType = self::BUTTON_LINK;
  44. /**
  45. * @var string the button type.
  46. * Valid values are 'primary', 'info', 'success', 'warning', 'danger' and 'inverse'.
  47. */
  48. public $type;
  49. /**
  50. * @var string the button size.
  51. * Valid values are 'large', 'small' and 'mini'.
  52. */
  53. public $size;
  54. /**
  55. * @var string the button icon, e.g. 'ok' or 'remove white'.
  56. */
  57. public $icon;
  58. /**
  59. * @var string the button label.
  60. */
  61. public $label;
  62. /**
  63. * @var string the button URL.
  64. */
  65. public $url;
  66. /**
  67. * @var boolean indicates whether the button should span the full width of the a parent.
  68. */
  69. public $block = false;
  70. /**
  71. * @var boolean indicates whether the button is active.
  72. */
  73. public $active = false;
  74. /**
  75. * @var boolean indicates whether the button is disabled.
  76. */
  77. public $disabled = false;
  78. /**
  79. * @var boolean indicates whether to encode the label.
  80. */
  81. public $encodeLabel = true;
  82. /**
  83. * @var boolean indicates whether to enable toggle.
  84. */
  85. public $toggle;
  86. /**
  87. * @var string the loading text.
  88. */
  89. public $loadingText;
  90. /**
  91. * @var string the complete text.
  92. */
  93. public $completeText;
  94. /**
  95. * @var array the dropdown button items.
  96. */
  97. public $items;
  98. /**
  99. * @var array the HTML attributes for the widget container.
  100. */
  101. public $htmlOptions = array();
  102. /**
  103. * @var array the button ajax options (used by 'ajaxLink' and 'ajaxButton').
  104. */
  105. public $ajaxOptions = array();
  106. /**
  107. * @var array the HTML attributes for the dropdown menu.
  108. * @since 0.9.11
  109. */
  110. public $dropdownOptions = array();
  111. /**
  112. * Initializes the widget.
  113. */
  114. public function init()
  115. {
  116. $classes = array('btn');
  117. $validTypes = array(self::TYPE_LINK, self::TYPE_PRIMARY, self::TYPE_INFO, self::TYPE_SUCCESS,
  118. self::TYPE_WARNING, self::TYPE_DANGER, self::TYPE_INVERSE);
  119. if (isset($this->type) && in_array($this->type, $validTypes))
  120. $classes[] = 'btn-'.$this->type;
  121. $validSizes = array(self::SIZE_LARGE, self::SIZE_SMALL, self::SIZE_MINI);
  122. if (isset($this->size) && in_array($this->size, $validSizes))
  123. $classes[] = 'btn-'.$this->size;
  124. if ($this->block)
  125. $classes[] = 'btn-block';
  126. if ($this->active)
  127. $classes[] = 'active';
  128. if ($this->disabled)
  129. {
  130. $disableTypes = array(self::BUTTON_BUTTON, self::BUTTON_SUBMIT, self::BUTTON_RESET,
  131. self::BUTTON_AJAXBUTTON, self::BUTTON_AJAXSUBMIT, self::BUTTON_INPUTBUTTON, self::BUTTON_INPUTSUBMIT);
  132. if (in_array($this->buttonType, $disableTypes))
  133. $this->htmlOptions['disabled'] = 'disabled';
  134. $classes[] = 'disabled';
  135. }
  136. if (!isset($this->url) && isset($this->htmlOptions['href']))
  137. {
  138. $this->url = $this->htmlOptions['href'];
  139. unset($this->htmlOptions['href']);
  140. }
  141. if ($this->encodeLabel)
  142. $this->label = CHtml::encode($this->label);
  143. if ($this->hasDropdown())
  144. {
  145. if (!isset($this->url))
  146. $this->url = '#';
  147. $classes[] = 'dropdown-toggle';
  148. $this->label .= ' <span class="caret"></span>';
  149. $this->htmlOptions['data-toggle'] = 'dropdown';
  150. }
  151. if (!empty($classes))
  152. {
  153. $classes = implode(' ', $classes);
  154. if (isset($this->htmlOptions['class']))
  155. $this->htmlOptions['class'] .= ' '.$classes;
  156. else
  157. $this->htmlOptions['class'] = $classes;
  158. }
  159. if (isset($this->icon))
  160. {
  161. if (strpos($this->icon, 'icon') === false)
  162. $this->icon = 'icon-'.implode(' icon-', explode(' ', $this->icon));
  163. $this->label = '<i class="'.$this->icon.'"></i> '.$this->label;
  164. }
  165. if (isset($this->toggle))
  166. $this->htmlOptions['data-toggle'] = 'button';
  167. if (isset($this->loadingText))
  168. $this->htmlOptions['data-loading-text'] = $this->loadingText;
  169. if (isset($this->completeText))
  170. $this->htmlOptions['data-complete-text'] = $this->completeText;
  171. }
  172. /**
  173. * Runs the widget.
  174. */
  175. public function run()
  176. {
  177. echo $this->createButton();
  178. if ($this->hasDropdown())
  179. {
  180. $this->controller->widget('bootstrap.widgets.TbDropdown', array(
  181. 'encodeLabel'=>$this->encodeLabel,
  182. 'items'=>$this->items,
  183. 'htmlOptions'=>$this->dropdownOptions,
  184. ));
  185. }
  186. }
  187. /**
  188. * Creates the button element.
  189. * @return string the created button.
  190. */
  191. protected function createButton()
  192. {
  193. switch ($this->buttonType)
  194. {
  195. case self::BUTTON_BUTTON:
  196. return CHtml::htmlButton($this->label, $this->htmlOptions);
  197. case self::BUTTON_SUBMIT:
  198. $this->htmlOptions['type'] = 'submit';
  199. return CHtml::htmlButton($this->label, $this->htmlOptions);
  200. case self::BUTTON_RESET:
  201. $this->htmlOptions['type'] = 'reset';
  202. return CHtml::htmlButton($this->label, $this->htmlOptions);
  203. case self::BUTTON_SUBMITLINK:
  204. return CHtml::linkButton($this->label, $this->htmlOptions);
  205. case self::BUTTON_AJAXLINK:
  206. return CHtml::ajaxLink($this->label, $this->url, $this->ajaxOptions, $this->htmlOptions);
  207. case self::BUTTON_AJAXBUTTON:
  208. $this->ajaxOptions['url'] = $this->url;
  209. $this->htmlOptions['ajax'] = $this->ajaxOptions;
  210. return CHtml::htmlButton($this->label, $this->htmlOptions);
  211. case self::BUTTON_AJAXSUBMIT:
  212. $this->ajaxOptions['type'] = isset($this->ajaxOptions['type']) ? $this->ajaxOptions['type'] : 'POST';
  213. $this->ajaxOptions['url'] = $this->url;
  214. $this->htmlOptions['type'] = 'submit';
  215. $this->htmlOptions['ajax'] = $this->ajaxOptions;
  216. return CHtml::htmlButton($this->label, $this->htmlOptions);
  217. case self::BUTTON_INPUTBUTTON:
  218. return CHtml::button($this->label, $this->htmlOptions);
  219. case self::BUTTON_INPUTSUBMIT:
  220. $this->htmlOptions['type'] = 'submit';
  221. return CHtml::button($this->label, $this->htmlOptions);
  222. default:
  223. case self::BUTTON_LINK:
  224. return CHtml::link($this->label, $this->url, $this->htmlOptions);
  225. }
  226. }
  227. /**
  228. * Returns whether the button has a dropdown.
  229. * @return bool the result.
  230. */
  231. protected function hasDropdown()
  232. {
  233. return isset($this->items) && !empty($this->items);
  234. }
  235. }