button.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. // Try and sanitize $icon in case it's an array (take the first element and consider it's a string)
  126. if (is_array($icon)) {
  127. $icon = @strval($icon[0]);
  128. }
  129. $this->icon = !empty($icon) ? 'fa fa-'.$icon : null;
  130. }
  131. /**
  132. * @return mixed
  133. */
  134. public function getStyle()
  135. {
  136. return $this->style;
  137. }
  138. /**
  139. * @param mixed $style
  140. */
  141. public function setStyle($style)
  142. {
  143. $style = !empty($style) ? 'btn btn-'.$style : null;
  144. $this->style = $style;
  145. }
  146. /**
  147. * @return mixed
  148. */
  149. public function getSize()
  150. {
  151. return $this->size;
  152. }
  153. /**
  154. * @param mixed $size
  155. */
  156. public function setSize($size)
  157. {
  158. switch ($size) {
  159. case 'large':
  160. $size = 'btn-lg';
  161. break;
  162. case 'small':
  163. $size = 'btn-sm';
  164. break;
  165. case 'extra-small':
  166. $size = 'btn-xs';
  167. break;
  168. case 'default':
  169. $size = null;
  170. break;
  171. }
  172. $size = !empty($size) ? $size : null;
  173. $this->size = $size;
  174. }
  175. /**
  176. * Freeze the element so that only its value is returned
  177. *
  178. * @access public
  179. * @return void
  180. */
  181. public function freeze()
  182. {
  183. return false;
  184. }
  185. /**
  186. * @param string $layout
  187. *
  188. * @return string
  189. */
  190. public function getTemplate($layout)
  191. {
  192. $size = $this->calculateSize();
  193. $attributes = $this->getAttributes();
  194. switch ($layout) {
  195. case FormValidator::LAYOUT_INLINE:
  196. return '
  197. {element}
  198. ';
  199. break;
  200. case FormValidator::LAYOUT_HORIZONTAL:
  201. if (isset($attributes['custom']) && $attributes['custom'] == true) {
  202. $template = '
  203. {icon}
  204. {element}
  205. ';
  206. } else {
  207. if(isset($attributes['data-block']) && $attributes['data-block'] == true){
  208. $template = '
  209. <div class="form-group text-center">
  210. {icon}
  211. {element}
  212. </div>
  213. ';
  214. } else {
  215. $template = '
  216. <div class="form-group {error_class}">
  217. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  218. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  219. {label}
  220. </label>
  221. <div class="col-sm-'.$size[1].'">
  222. {icon}
  223. {element}
  224. <!-- BEGIN label_2 -->
  225. <p class="help-block">{label_2}</p>
  226. <!-- END label_2 -->
  227. <!-- BEGIN error -->
  228. <span class="help-inline help-block">{error}</span>
  229. <!-- END error -->
  230. </div>
  231. <div class="col-sm-'.$size[2].'">
  232. <!-- BEGIN label_3 -->
  233. {label_3}
  234. <!-- END label_3 -->
  235. </div>
  236. </div>';
  237. }
  238. }
  239. return $template;
  240. break;
  241. case FormValidator::LAYOUT_BOX:
  242. return '{element}';
  243. break;
  244. case FormValidator::LAYOUT_BOX_NO_LABEL:
  245. return '<div class="input-group mt-3">
  246. {element}
  247. </div>
  248. ';
  249. break;
  250. }
  251. }
  252. }