text.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * HTML class for a text field
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: text.php,v 1.7 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * HTML class for a text field
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  28. * @author Bertrand Mansion <bmansion@mamasam.com>
  29. * @version Release: 3.2.11
  30. * @since 1.0
  31. */
  32. class HTML_QuickForm_text extends HTML_QuickForm_input
  33. {
  34. private $inputSize;
  35. /**
  36. * Class constructor
  37. *
  38. * @param string $elementName (optional)Input field name attribute
  39. * @param string $elementLabel (optional)Input field label
  40. * @param mixed $attributes (optional)Either a typical HTML attribute string
  41. * or an associative array
  42. * @since 1.0
  43. * @access public
  44. * @return void
  45. */
  46. public function __construct(
  47. $elementName = null,
  48. $elementLabel = null,
  49. $attributes = []
  50. ) {
  51. if (is_string($attributes) && empty($attributes)) {
  52. $attributes = [];
  53. }
  54. if (is_array($attributes) || empty($attributes)) {
  55. $attributes['class'] = 'form-control';
  56. }
  57. $inputSize = isset($attributes['input-size']) ? $attributes['input-size'] : null;
  58. $this->setInputSize($inputSize);
  59. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  60. $this->setColumnsSize($columnsSize);
  61. $icon = isset($attributes['icon']) ? $attributes['icon'] : null;
  62. $this->setIcon($icon);
  63. if (!empty($inputSize)) {
  64. unset($attributes['input-size']);
  65. }
  66. if (!empty($icon)) {
  67. unset($attributes['icon']);
  68. }
  69. parent::__construct($elementName, $elementLabel, $attributes);
  70. $this->_persistantFreeze = true;
  71. $this->setType('text');
  72. }
  73. /**
  74. * Show an icon at the left side of an input
  75. * @return string
  76. */
  77. public function getIconToHtml()
  78. {
  79. $icon = $this->getIcon();
  80. if (empty($icon)) {
  81. return '';
  82. }
  83. return '<div class="input-group-addon">
  84. <em class="fa fa-'.$icon.'"></em>
  85. </div>';
  86. }
  87. /**
  88. * @param string $layout
  89. *
  90. * @return string
  91. */
  92. public function getTemplate($layout)
  93. {
  94. $size = $this->getColumnsSize();
  95. if (empty($size)) {
  96. $sizeTemp = $this->getInputSize();
  97. if (empty($size)) {
  98. $sizeTemp = 8;
  99. }
  100. $size = array(2, $sizeTemp, 2);
  101. } else {
  102. if (is_array($size)) {
  103. if (count($size) != 3) {
  104. $sizeTemp = $this->getInputSize();
  105. if (empty($size)) {
  106. $sizeTemp = 8;
  107. }
  108. $size = array(2, $sizeTemp, 2);
  109. }
  110. // else just keep the $size array as received
  111. } else {
  112. $size = array(2, intval($size), 2);
  113. }
  114. }
  115. switch ($layout) {
  116. case FormValidator::LAYOUT_INLINE:
  117. return '
  118. <div class="form-group {error_class}">
  119. <label {label-for} >
  120. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  121. {label}
  122. </label>
  123. {element}
  124. </div>';
  125. break;
  126. case FormValidator::LAYOUT_HORIZONTAL:
  127. return '
  128. <div class="form-group {error_class}">
  129. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  130. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  131. {label}
  132. </label>
  133. <div class="col-sm-'.$size[1].'">
  134. {icon}
  135. {element}
  136. <!-- BEGIN label_2 -->
  137. <p class="help-block">{label_2}</p>
  138. <!-- END label_2 -->
  139. <!-- BEGIN error -->
  140. <span class="help-inline">{error}</span>
  141. <!-- END error -->
  142. </div>
  143. <div class="col-sm-'.$size[2].'">
  144. <!-- BEGIN label_3 -->
  145. {label_3}
  146. <!-- END label_3 -->
  147. </div>
  148. </div>';
  149. break;
  150. case FormValidator::LAYOUT_BOX_NO_LABEL:
  151. return '
  152. <label {label-for}>{label}</label>
  153. <div class="input-group">
  154. {icon}
  155. {element}
  156. </div>';
  157. break;
  158. }
  159. }
  160. /**
  161. * @return null
  162. */
  163. public function getInputSize()
  164. {
  165. return $this->inputSize;
  166. }
  167. /**
  168. * @param null $inputSize
  169. */
  170. public function setInputSize($inputSize)
  171. {
  172. $this->inputSize = $inputSize;
  173. }
  174. /**
  175. * Sets size of text field
  176. *
  177. * @param string $size Size of text field
  178. * @since 1.3
  179. * @access public
  180. * @return void
  181. */
  182. public function setSize($size)
  183. {
  184. $this->updateAttributes(array('size' => $size));
  185. }
  186. /**
  187. * Sets maxlength of text field
  188. *
  189. * @param string $maxlength Maximum length of text field
  190. * @since 1.3
  191. * @access public
  192. * @return void
  193. */
  194. public function setMaxlength($maxlength)
  195. {
  196. $this->updateAttributes(array('maxlength' => $maxlength));
  197. }
  198. /**
  199. * @return string
  200. */
  201. public function toHtml()
  202. {
  203. if ($this->isFrozen()) {
  204. return $this->getFrozenHtml();
  205. } else {
  206. return '<input ' . $this->_getAttrString($this->_attributes) . ' />';
  207. }
  208. }
  209. }