button.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * HTML class for an <input type="button" /> elements
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: button.php,v 1.6 2009/04/04 21:34:02 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * HTML class for an <input type="button" /> elements
  25. *
  26. * @category HTML
  27. * @package HTML_QuickForm
  28. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  29. * @author Bertrand Mansion <bmansion@mamasam.com>
  30. * @version Release: 3.2.11
  31. * @since 1.0
  32. */
  33. class HTML_QuickForm_button extends HTML_QuickForm_input
  34. {
  35. private $icon;
  36. private $style;
  37. private $size;
  38. private $class;
  39. private $columnsSize;
  40. /**
  41. * @param string $name input name example 'submit'
  42. * @param string $text button text to show
  43. * @param string $icon icons based in font-awesome
  44. * @param string $style i.e default|primary|success|info|warning|danger|link
  45. * @param string $size large|default|small|extra-small
  46. * @param string $class
  47. * @param array $attributes
  48. */
  49. public function __construct(
  50. $name,
  51. $text,
  52. $icon = 'check',
  53. $style = 'default',
  54. $size = 'default',
  55. $class = null,
  56. $attributes = array()
  57. ) {
  58. $this->setIcon($icon);
  59. $this->setStyle($style);
  60. $this->setSize($size);
  61. $this->setClass($class);
  62. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  63. $this->setColumnsSize($columnsSize);
  64. parent::__construct(
  65. $name,
  66. null,
  67. $attributes
  68. );
  69. $this->_persistantFreeze = false;
  70. $this->setValue($text);
  71. $this->setType('submit');
  72. }
  73. /**
  74. * @return string
  75. */
  76. public function toHtml()
  77. {
  78. if ($this->_flagFrozen) {
  79. return $this->getFrozenHtml();
  80. } else {
  81. $value = null;
  82. if (isset($this->_attributes['value'])) {
  83. $value = $this->_attributes['value'];
  84. unset($this->_attributes['value']);
  85. }
  86. unset($this->_attributes['class']);
  87. $icon = $this->getIcon();
  88. if (!empty($icon)) {
  89. $icon = '<i class="' . $this->getIcon() . '"></i> ';
  90. }
  91. $class = $this->getClass().' '.$this->getStyle().' '.$this->getSize();
  92. return
  93. $this->_getTabs() . '
  94. <button class="'.$class.'" ' . $this->_getAttrString($this->_attributes) . '>'.
  95. $icon.
  96. $value.
  97. '</button>';
  98. }
  99. }
  100. /**
  101. * @return mixed
  102. */
  103. public function getClass()
  104. {
  105. return $this->class;
  106. }
  107. /**
  108. * @param mixed $class
  109. */
  110. public function setClass($class)
  111. {
  112. $this->class = $class;
  113. }
  114. /**
  115. * @return mixed
  116. */
  117. public function getIcon()
  118. {
  119. return $this->icon;
  120. }
  121. /**
  122. * @param mixed $icon
  123. */
  124. public function setIcon($icon)
  125. {
  126. $this->icon = !empty($icon) ? 'fa fa-'.$icon : null;
  127. }
  128. /**
  129. * @return mixed
  130. */
  131. public function getStyle()
  132. {
  133. return $this->style;
  134. }
  135. /**
  136. * @param mixed $style
  137. */
  138. public function setStyle($style)
  139. {
  140. $style = !empty($style) ? 'btn btn-'.$style : null;
  141. $this->style = $style;
  142. }
  143. /**
  144. * @return mixed
  145. */
  146. public function getSize()
  147. {
  148. return $this->size;
  149. }
  150. /**
  151. * @return null
  152. */
  153. public function getColumnsSize()
  154. {
  155. return $this->columnsSize;
  156. }
  157. /**
  158. * @param null $columnsSize
  159. */
  160. public function setColumnsSize($columnsSize)
  161. {
  162. $this->columnsSize = $columnsSize;
  163. }
  164. /**
  165. * @param mixed $size
  166. */
  167. public function setSize($size)
  168. {
  169. switch ($size) {
  170. case 'large':
  171. $size = 'btn-lg';
  172. break;
  173. case 'small':
  174. $size = 'btn-sm';
  175. break;
  176. case 'extra-small':
  177. $size = 'btn-xs';
  178. break;
  179. case 'default':
  180. $size = null;
  181. break;
  182. }
  183. $size = !empty($size) ? $size : null;
  184. $this->size = $size;
  185. }
  186. /**
  187. * Freeze the element so that only its value is returned
  188. *
  189. * @access public
  190. * @return void
  191. */
  192. public function freeze()
  193. {
  194. return false;
  195. }
  196. /**
  197. * @param string $layout
  198. *
  199. * @return string
  200. */
  201. public function getTemplate($layout)
  202. {
  203. $size = $this->getColumnsSize();
  204. if (empty($size)) {
  205. $size = array(2, 8, 2);
  206. } else {
  207. if (is_array($size)) {
  208. if (count($size) == 1) {
  209. $size = array(2, intval($size[0]), 2);
  210. } elseif (count($size) != 3) {
  211. $size = array(2, 8, 2);
  212. }
  213. // else just keep the $size array as received
  214. } else {
  215. $size = array(2, intval($size), 2);
  216. }
  217. }
  218. switch ($layout) {
  219. case FormValidator::LAYOUT_INLINE:
  220. return '
  221. {element}
  222. ';
  223. break;
  224. case FormValidator::LAYOUT_HORIZONTAL:
  225. return '
  226. <div class="form-group {error_class}">
  227. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  228. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  229. {label}
  230. </label>
  231. <div class="col-sm-'.$size[1].'">
  232. {icon}
  233. {element}
  234. <!-- BEGIN label_2 -->
  235. <p class="help-block">{label_2}</p>
  236. <!-- END label_2 -->
  237. <!-- BEGIN error -->
  238. <span class="help-inline">{error}</span>
  239. <!-- END error -->
  240. </div>
  241. <div class="col-sm-'.$size[2].'">
  242. <!-- BEGIN label_3 -->
  243. {label_3}
  244. <!-- END label_3 -->
  245. </div>
  246. </div>';
  247. break;
  248. case FormValidator::LAYOUT_BOX:
  249. case FormValidator::LAYOUT_BOX_NO_LABEL:
  250. return '
  251. {element}
  252. ';
  253. break;
  254. }
  255. }
  256. }