text.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. <em class="fa fa-'.$icon.'"></em>
  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. <label {label-for}>{label}</label>
  152. <div class="input-group">
  153. {icon}
  154. {element}
  155. </div>';
  156. break;
  157. }
  158. }
  159. /**
  160. * @return null
  161. */
  162. public function getInputSize()
  163. {
  164. return $this->inputSize;
  165. }
  166. /**
  167. * @param null $inputSize
  168. */
  169. public function setInputSize($inputSize)
  170. {
  171. $this->inputSize = $inputSize;
  172. }
  173. /**
  174. * @return null
  175. */
  176. public function getColumnsSize()
  177. {
  178. return $this->columnsSize;
  179. }
  180. /**
  181. * @param null $columnsSize
  182. */
  183. public function setColumnsSize($columnsSize)
  184. {
  185. $this->columnsSize = $columnsSize;
  186. }
  187. /**
  188. * Sets size of text field
  189. *
  190. * @param string $size Size of text field
  191. * @since 1.3
  192. * @access public
  193. * @return void
  194. */
  195. public function setSize($size)
  196. {
  197. $this->updateAttributes(array('size' => $size));
  198. }
  199. /**
  200. * Sets maxlength of text field
  201. *
  202. * @param string $maxlength Maximum length of text field
  203. * @since 1.3
  204. * @access public
  205. * @return void
  206. */
  207. public function setMaxlength($maxlength)
  208. {
  209. $this->updateAttributes(array('maxlength' => $maxlength));
  210. }
  211. /**
  212. * @return string
  213. */
  214. public function toHtml()
  215. {
  216. if ($this->isFrozen()) {
  217. return $this->getFrozenHtml();
  218. } else {
  219. return '<input ' . $this->_getAttrString($this->_attributes) . ' />';
  220. }
  221. }
  222. }