button.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /**
  40. * @param string $name input name example 'submit'
  41. * @param string $text button text to show
  42. * @param string $icon icons based in font-awesome
  43. * @param string $style i.e default|primary|success|info|warning|danger|link
  44. * @param string $size large|default|small|extra-small
  45. * @param string $class
  46. * @param array $attributes
  47. */
  48. public function __construct(
  49. $name,
  50. $text,
  51. $icon = 'check',
  52. $style = 'default',
  53. $size = 'default',
  54. $class = null,
  55. $attributes = array()
  56. ) {
  57. $this->setIcon($icon);
  58. $this->setStyle($style);
  59. $this->setSize($size);
  60. $this->setClass($class);
  61. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  62. $this->setColumnsSize($columnsSize);
  63. parent::__construct(
  64. $name,
  65. null,
  66. $attributes
  67. );
  68. $this->_persistantFreeze = false;
  69. $this->setValue($text);
  70. $this->setType('submit');
  71. }
  72. /**
  73. * @return string
  74. */
  75. public function toHtml()
  76. {
  77. if ($this->_flagFrozen) {
  78. return $this->getFrozenHtml();
  79. } else {
  80. $value = null;
  81. if (isset($this->_attributes['value'])) {
  82. $value = $this->_attributes['value'];
  83. unset($this->_attributes['value']);
  84. }
  85. unset($this->_attributes['class']);
  86. $icon = $this->getIcon();
  87. if (!empty($icon)) {
  88. $icon = '<em class="' . $this->getIcon() . '"></em> ';
  89. }
  90. $class = $this->getClass().' '.$this->getStyle().' '.$this->getSize();
  91. return
  92. $this->_getTabs() . '
  93. <button class="'.$class.'" ' . $this->_getAttrString($this->_attributes) . '>'.
  94. $icon.
  95. $value.
  96. '</button>';
  97. }
  98. }
  99. /**
  100. * @return mixed
  101. */
  102. public function getClass()
  103. {
  104. return $this->class;
  105. }
  106. /**
  107. * @param mixed $class
  108. */
  109. public function setClass($class)
  110. {
  111. $this->class = $class;
  112. }
  113. /**
  114. * @return mixed
  115. */
  116. public function getIcon()
  117. {
  118. return $this->icon;
  119. }
  120. /**
  121. * @param mixed $icon
  122. */
  123. public function setIcon($icon)
  124. {
  125. $this->icon = !empty($icon) ? 'fa fa-'.$icon : null;
  126. }
  127. /**
  128. * @return mixed
  129. */
  130. public function getStyle()
  131. {
  132. return $this->style;
  133. }
  134. /**
  135. * @param mixed $style
  136. */
  137. public function setStyle($style)
  138. {
  139. $style = !empty($style) ? 'btn btn-'.$style : null;
  140. $this->style = $style;
  141. }
  142. /**
  143. * @return mixed
  144. */
  145. public function getSize()
  146. {
  147. return $this->size;
  148. }
  149. /**
  150. * @param mixed $size
  151. */
  152. public function setSize($size)
  153. {
  154. switch ($size) {
  155. case 'large':
  156. $size = 'btn-lg';
  157. break;
  158. case 'small':
  159. $size = 'btn-sm';
  160. break;
  161. case 'extra-small':
  162. $size = 'btn-xs';
  163. break;
  164. case 'default':
  165. $size = null;
  166. break;
  167. }
  168. $size = !empty($size) ? $size : null;
  169. $this->size = $size;
  170. }
  171. /**
  172. * Freeze the element so that only its value is returned
  173. *
  174. * @access public
  175. * @return void
  176. */
  177. public function freeze()
  178. {
  179. return false;
  180. }
  181. /**
  182. * @param string $layout
  183. *
  184. * @return string
  185. */
  186. public function getTemplate($layout)
  187. {
  188. $size = $this->getColumnsSize();
  189. if (empty($size)) {
  190. $size = array(2, 8, 2);
  191. } else {
  192. if (is_array($size)) {
  193. if (count($size) == 1) {
  194. $size = array(2, intval($size[0]), 2);
  195. } elseif (count($size) != 3) {
  196. $size = array(2, 8, 2);
  197. }
  198. // else just keep the $size array as received
  199. } else {
  200. $size = array(2, intval($size), 2);
  201. }
  202. }
  203. switch ($layout) {
  204. case FormValidator::LAYOUT_INLINE:
  205. return '
  206. {element}
  207. ';
  208. break;
  209. case FormValidator::LAYOUT_HORIZONTAL:
  210. return '
  211. <div class="form-group {error_class}">
  212. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  213. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  214. {label}
  215. </label>
  216. <div class="col-sm-'.$size[1].'">
  217. {icon}
  218. {element}
  219. <!-- BEGIN label_2 -->
  220. <p class="help-block">{label_2}</p>
  221. <!-- END label_2 -->
  222. <!-- BEGIN error -->
  223. <span class="help-inline">{error}</span>
  224. <!-- END error -->
  225. </div>
  226. <div class="col-sm-'.$size[2].'">
  227. <!-- BEGIN label_3 -->
  228. {label_3}
  229. <!-- END label_3 -->
  230. </div>
  231. </div>';
  232. break;
  233. case FormValidator::LAYOUT_BOX:
  234. case FormValidator::LAYOUT_BOX_NO_LABEL:
  235. return '
  236. {element}
  237. ';
  238. break;
  239. }
  240. }
  241. }