text.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. /**
  35. * Class constructor
  36. *
  37. * @param string $elementName (optional)Input field name attribute
  38. * @param string $elementLabel (optional)Input field label
  39. * @param mixed $attributes (optional)Either a typical HTML attribute string
  40. * or an associative array
  41. * @since 1.0
  42. * @access public
  43. * @return void
  44. */
  45. public function __construct(
  46. $elementName = null,
  47. $elementLabel = null,
  48. $attributes = []
  49. ) {
  50. if (is_string($attributes) && empty($attributes)) {
  51. $attributes = [];
  52. }
  53. if (is_array($attributes) || empty($attributes)) {
  54. $classFromAttributes = isset($attributes['class']) ? $attributes['class'] : '';
  55. $attributes['class'] = $classFromAttributes.' 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->calculateSize();
  95. $attributes = $this->getAttributes();
  96. switch ($layout) {
  97. case FormValidator::LAYOUT_INLINE:
  98. return '
  99. <div class="form-group {error_class}">
  100. <label {label-for} >
  101. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  102. {label}
  103. </label>
  104. {element}
  105. </div>';
  106. break;
  107. case FormValidator::LAYOUT_HORIZONTAL:
  108. return '
  109. <div class="form-group {error_class}">
  110. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  111. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  112. {label}
  113. </label>
  114. <div class="col-sm-'.$size[1].'">
  115. {icon}
  116. {element}
  117. <!-- BEGIN label_2 -->
  118. <p class="help-block">{label_2}</p>
  119. <!-- END label_2 -->
  120. <!-- BEGIN error -->
  121. <span class="help-inline help-block">{error}</span>
  122. <!-- END error -->
  123. </div>
  124. <div class="col-sm-'.$size[2].'">
  125. <!-- BEGIN label_3 -->
  126. {label_3}
  127. <!-- END label_3 -->
  128. </div>
  129. </div>';
  130. break;
  131. case FormValidator::LAYOUT_BOX_NO_LABEL:
  132. if (isset($attributes['custom']) && $attributes['custom'] == true) {
  133. $template = '
  134. <label {label-for}>{label}</label>
  135. <div class="input-group">
  136. {icon}
  137. {element}
  138. <div class="input-group-btn">
  139. <button class="btn btn-default" type="submit">
  140. <em class="fa fa-search"></em>
  141. </button>
  142. </div>
  143. </div>
  144. ';
  145. } else {
  146. $template = '
  147. <label {label-for}>{label}</label>
  148. <div class="input-group">
  149. {icon}
  150. {element}
  151. </div>';
  152. }
  153. return $template;
  154. break;
  155. }
  156. }
  157. /**
  158. * Sets size of text field
  159. *
  160. * @param string $size Size of text field
  161. * @since 1.3
  162. * @access public
  163. * @return void
  164. */
  165. public function setSize($size)
  166. {
  167. $this->updateAttributes(array('size' => $size));
  168. }
  169. /**
  170. * Sets maxlength of text field
  171. *
  172. * @param string $maxlength Maximum length of text field
  173. * @since 1.3
  174. * @access public
  175. * @return void
  176. */
  177. public function setMaxlength($maxlength)
  178. {
  179. $this->updateAttributes(array('maxlength' => $maxlength));
  180. }
  181. /**
  182. * @return string
  183. */
  184. public function toHtml()
  185. {
  186. if ($this->isFrozen()) {
  187. return $this->getFrozenHtml();
  188. } else {
  189. return '<input '.$this->_getAttrString($this->_attributes).' />';
  190. }
  191. }
  192. }