text.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. private $columnsSize;
  36. /**
  37. * Class constructor
  38. *
  39. * @param string $elementName (optional)Input field name attribute
  40. * @param string $elementLabel (optional)Input field label
  41. * @param mixed $attributes (optional)Either a typical HTML attribute string
  42. * or an associative array
  43. * @since 1.0
  44. * @access public
  45. * @return void
  46. */
  47. public function __construct(
  48. $elementName = null,
  49. $elementLabel = null,
  50. $attributes = array()
  51. ) {
  52. if (is_array($attributes) || empty($attributes)) {
  53. $attributes['class'] = 'form-control';
  54. }
  55. $inputSize = isset($attributes['input-size']) ? $attributes['input-size'] : null;
  56. $this->setInputSize($inputSize);
  57. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  58. $this->setColumnsSize($columnsSize);
  59. $icon = isset($attributes['icon']) ? $attributes['icon'] : null;
  60. $this->setIcon($icon);
  61. if (!empty($inputSize)) {
  62. unset($attributes['input-size']);
  63. }
  64. if (!empty($icon)) {
  65. unset($attributes['icon']);
  66. }
  67. parent::__construct($elementName, $elementLabel, $attributes);
  68. $this->_persistantFreeze = true;
  69. $this->setType('text');
  70. }
  71. /**
  72. * Show an icon at the left side of an input
  73. * @return string
  74. */
  75. public function getIconToHtml()
  76. {
  77. $icon = $this->getIcon();
  78. if (empty($icon)) {
  79. return '';
  80. }
  81. return '
  82. <div class="input-group-addon">
  83. <i class="fa fa-'.$icon.'"></i>
  84. </div>';
  85. }
  86. /**
  87. * @param string $layout
  88. *
  89. * @return string
  90. */
  91. public function getTemplate($layout)
  92. {
  93. $size = $this->getColumnsSize();
  94. if (empty($size)) {
  95. $sizeTemp = $this->getInputSize();
  96. if (empty($size)) {
  97. $sizeTemp = 8;
  98. }
  99. $size = array(2, $sizeTemp, 2);
  100. } else {
  101. if (is_array($size)) {
  102. if (count($size) != 3) {
  103. $sizeTemp = $this->getInputSize();
  104. if (empty($size)) {
  105. $sizeTemp = 8;
  106. }
  107. $size = array(2, $sizeTemp, 2);
  108. }
  109. // else just keep the $size array as received
  110. } else {
  111. $size = array(2, intval($size), 2);
  112. }
  113. }
  114. switch ($layout) {
  115. case FormValidator::LAYOUT_INLINE:
  116. return '
  117. <div class="form-group {error_class}">
  118. <label {label-for} >
  119. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  120. {label}
  121. </label>
  122. {element}
  123. </div>';
  124. break;
  125. case FormValidator::LAYOUT_HORIZONTAL:
  126. return '
  127. <div class="form-group {error_class}">
  128. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  129. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  130. {label}
  131. </label>
  132. <div class="col-sm-'.$size[1].'">
  133. {icon}
  134. {element}
  135. <!-- BEGIN label_2 -->
  136. <p class="help-block">{label_2}</p>
  137. <!-- END label_2 -->
  138. <!-- BEGIN error -->
  139. <span class="help-inline">{error}</span>
  140. <!-- END error -->
  141. </div>
  142. <div class="col-sm-'.$size[2].'">
  143. <!-- BEGIN label_3 -->
  144. {label_3}
  145. <!-- END label_3 -->
  146. </div>
  147. </div>';
  148. break;
  149. case FormValidator::LAYOUT_BOX_NO_LABEL:
  150. return '
  151. <div class="input-group">
  152. {icon}
  153. {element}
  154. </div>';
  155. break;
  156. }
  157. }
  158. /**
  159. * @return null
  160. */
  161. public function getInputSize()
  162. {
  163. return $this->inputSize;
  164. }
  165. /**
  166. * @param null $inputSize
  167. */
  168. public function setInputSize($inputSize)
  169. {
  170. $this->inputSize = $inputSize;
  171. }
  172. /**
  173. * @return null
  174. */
  175. public function getColumnsSize()
  176. {
  177. return $this->columnsSize;
  178. }
  179. /**
  180. * @param null $columnsSize
  181. */
  182. public function setColumnsSize($columnsSize)
  183. {
  184. $this->columnsSize = $columnsSize;
  185. }
  186. /**
  187. * Sets size of text field
  188. *
  189. * @param string $size Size of text field
  190. * @since 1.3
  191. * @access public
  192. * @return void
  193. */
  194. public function setSize($size)
  195. {
  196. $this->updateAttributes(array('size' => $size));
  197. }
  198. /**
  199. * Sets maxlength of text field
  200. *
  201. * @param string $maxlength Maximum length of text field
  202. * @since 1.3
  203. * @access public
  204. * @return void
  205. */
  206. public function setMaxlength($maxlength)
  207. {
  208. $this->updateAttributes(array('maxlength' => $maxlength));
  209. }
  210. /**
  211. * @return string
  212. */
  213. public function toHtml()
  214. {
  215. if ($this->_flagFrozen) {
  216. return $this->getFrozenHtml();
  217. } else {
  218. return '<input' . $this->_getAttrString($this->_attributes) . ' />';
  219. }
  220. }
  221. }