text.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. $attributes['class'] = 'form-control';
  55. }
  56. $inputSize = isset($attributes['input-size']) ? $attributes['input-size'] : null;
  57. $this->setInputSize($inputSize);
  58. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  59. $this->setColumnsSize($columnsSize);
  60. $icon = isset($attributes['icon']) ? $attributes['icon'] : null;
  61. $this->setIcon($icon);
  62. if (!empty($inputSize)) {
  63. unset($attributes['input-size']);
  64. }
  65. if (!empty($icon)) {
  66. unset($attributes['icon']);
  67. }
  68. parent::__construct($elementName, $elementLabel, $attributes);
  69. $this->_persistantFreeze = true;
  70. $this->setType('text');
  71. }
  72. /**
  73. * Show an icon at the left side of an input
  74. * @return string
  75. */
  76. public function getIconToHtml()
  77. {
  78. $icon = $this->getIcon();
  79. if (empty($icon)) {
  80. return '';
  81. }
  82. return '<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->calculateSize();
  94. $attributes = $this->getAttributes();
  95. switch ($layout) {
  96. case FormValidator::LAYOUT_INLINE:
  97. return '
  98. <div class="form-group {error_class}">
  99. <label {label-for} >
  100. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  101. {label}
  102. </label>
  103. {element}
  104. </div>';
  105. break;
  106. case FormValidator::LAYOUT_HORIZONTAL:
  107. return '
  108. <div class="form-group {error_class}">
  109. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  110. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  111. {label}
  112. </label>
  113. <div class="col-sm-'.$size[1].'">
  114. {icon}
  115. {element}
  116. <!-- BEGIN label_2 -->
  117. <p class="help-block">{label_2}</p>
  118. <!-- END label_2 -->
  119. <!-- BEGIN error -->
  120. <span class="help-inline help-block">{error}</span>
  121. <!-- END error -->
  122. </div>
  123. <div class="col-sm-'.$size[2].'">
  124. <!-- BEGIN label_3 -->
  125. {label_3}
  126. <!-- END label_3 -->
  127. </div>
  128. </div>';
  129. break;
  130. case FormValidator::LAYOUT_BOX_NO_LABEL:
  131. if (isset($attributes['custom']) && $attributes['custom'] == true) {
  132. $template = '
  133. <label {label-for}>{label}</label>
  134. <div class="input-group">
  135. {icon}
  136. {element}
  137. <div class="input-group-btn">
  138. <button class="btn btn-default" type="submit">
  139. <em class="fa fa-search"></em>
  140. </button>
  141. </div>
  142. </div>
  143. ';
  144. } else {
  145. $template = '
  146. <label {label-for}>{label}</label>
  147. <div class="input-group">
  148. {icon}
  149. {element}
  150. </div>';
  151. }
  152. return $template;
  153. break;
  154. }
  155. }
  156. /**
  157. * Sets size of text field
  158. *
  159. * @param string $size Size of text field
  160. * @since 1.3
  161. * @access public
  162. * @return void
  163. */
  164. public function setSize($size)
  165. {
  166. $this->updateAttributes(array('size' => $size));
  167. }
  168. /**
  169. * Sets maxlength of text field
  170. *
  171. * @param string $maxlength Maximum length of text field
  172. * @since 1.3
  173. * @access public
  174. * @return void
  175. */
  176. public function setMaxlength($maxlength)
  177. {
  178. $this->updateAttributes(array('maxlength' => $maxlength));
  179. }
  180. /**
  181. * @return string
  182. */
  183. public function toHtml()
  184. {
  185. if ($this->isFrozen()) {
  186. return $this->getFrozenHtml();
  187. } else {
  188. return '<input '.$this->_getAttrString($this->_attributes).' />';
  189. }
  190. }
  191. }