text.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. $isButton = $this->getButton();
  81. if (empty($icon)) {
  82. return '';
  83. }
  84. $element = '<span class="input-group-text"><em class="fa fa-' . $icon . '"></em></span>';
  85. if ($isButton) {
  86. $element = '<button class="btn btn-outline-secondary" type="submit">
  87. <em class="fa fa-' . $icon . '"></em>
  88. </button>';
  89. }
  90. return '<div class="input-group-append">
  91. ' . $element . '
  92. </div>';
  93. }
  94. /**
  95. * @param string $layout
  96. *
  97. * @return string
  98. */
  99. public function getTemplate($layout)
  100. {
  101. $size = $this->calculateSize();
  102. $attributes = $this->getAttributes();
  103. switch ($layout) {
  104. case FormValidator::LAYOUT_INLINE:
  105. return '
  106. <label class="sr-only" {label-for} >
  107. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  108. {label}
  109. </label>
  110. {element}
  111. ';
  112. break;
  113. case FormValidator::LAYOUT_HORIZONTAL:
  114. return '
  115. <div class="form-group row {error_class}">
  116. <label {label-for} class="col-sm-'.$size[0].' col-form-label" >
  117. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  118. {label}
  119. </label>
  120. <div class="col-sm-'.$size[1].'">
  121. {icon}
  122. {element}
  123. <!-- BEGIN label_2 -->
  124. <p class="help-block">{label_2}</p>
  125. <!-- END label_2 -->
  126. <!-- BEGIN error -->
  127. <span class="help-inline help-block">{error}</span>
  128. <!-- END error -->
  129. </div>
  130. <div class="col-sm-'.$size[2].'">
  131. <!-- BEGIN label_3 -->
  132. {label_3}
  133. <!-- END label_3 -->
  134. </div>
  135. </div>';
  136. break;
  137. case FormValidator::LAYOUT_BOX_NO_LABEL:
  138. return '
  139. <label {label-for}>{label}</label>
  140. <div class="input-group mb-3">
  141. {icon}
  142. {element}
  143. </div>';
  144. break;
  145. case FormValidator::LAYOUT_BOX_SEARCH:
  146. return '
  147. <div class="form-group row">
  148. <label class="col-sm-2 col-form-label" {label-for}>{label}</label>
  149. <div class="col-sm-8">
  150. <div class="input-group mb-3">
  151. {element}
  152. {icon}
  153. </div>
  154. </div>
  155. <div class="col-sm-2"></div>
  156. </div>';
  157. break;
  158. }
  159. }
  160. /**
  161. * Sets size of text field
  162. *
  163. * @param string $size Size of text field
  164. * @since 1.3
  165. * @access public
  166. * @return void
  167. */
  168. public function setSize($size)
  169. {
  170. $this->updateAttributes(array('size' => $size));
  171. }
  172. /**
  173. * Sets maxlength of text field
  174. *
  175. * @param string $maxlength Maximum length of text field
  176. * @since 1.3
  177. * @access public
  178. * @return void
  179. */
  180. public function setMaxlength($maxlength)
  181. {
  182. $this->updateAttributes(array('maxlength' => $maxlength));
  183. }
  184. /**
  185. * @return string
  186. */
  187. public function toHtml()
  188. {
  189. if ($this->isFrozen()) {
  190. return $this->getFrozenHtml();
  191. } else {
  192. return '<input '.$this->_getAttrString($this->_attributes).' />';
  193. }
  194. }
  195. }