textarea.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * HTML class for a textarea type 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. *
  16. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. *
  21. * @version CVS: $Id: textarea.php,v 1.13 2009/04/04 21:34:04 avb Exp $
  22. *
  23. * @see http://pear.php.net/package/HTML_QuickForm
  24. */
  25. /**
  26. * HTML class for a textarea type field.
  27. *
  28. * @category HTML
  29. * @package HTML_QuickForm
  30. *
  31. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  32. * @author Bertrand Mansion <bmansion@mamasam.com>
  33. *
  34. * @version Release: 3.2.11
  35. *
  36. * @since 1.0
  37. */
  38. class HTML_QuickForm_textarea extends HTML_QuickForm_element
  39. {
  40. /**
  41. * Field value.
  42. *
  43. * @var string
  44. *
  45. * @since 1.0
  46. */
  47. public $_value;
  48. /**
  49. * Class constructor.
  50. *
  51. * @param string $elementName Input field name attribute
  52. * @param string|array $label Label(s) for a field
  53. * @param mixed $attributes Either a typical HTML attribute string or an associative array
  54. */
  55. public function __construct(
  56. $elementName = null,
  57. $label = null,
  58. $attributes = null
  59. ) {
  60. $attributes['class'] = isset($attributes['class']) ? $attributes['class'] : 'form-control';
  61. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  62. $this->setColumnsSize($columnsSize);
  63. parent::__construct($elementName, $label, $attributes);
  64. $id = $this->getAttribute('id');
  65. if (empty($id)) {
  66. $name = $this->getAttribute('name');
  67. $this->setAttribute('id', uniqid($name.'_'));
  68. }
  69. $this->_persistantFreeze = true;
  70. $this->_type = 'textarea';
  71. $this->_value = null;
  72. }
  73. /**
  74. * Sets the input field name.
  75. *
  76. * @param string $name Input field name attribute
  77. *
  78. * @since 1.0
  79. */
  80. public function setName($name)
  81. {
  82. $this->updateAttributes(['name' => $name]);
  83. }
  84. /**
  85. * Returns the element name.
  86. *
  87. * @since 1.0
  88. *
  89. * @return string
  90. */
  91. public function getName()
  92. {
  93. return $this->getAttribute('name');
  94. }
  95. /**
  96. * Sets value for textarea element.
  97. *
  98. * @param string $value Value for textarea element
  99. *
  100. * @since 1.0
  101. */
  102. public function setValue($value)
  103. {
  104. $this->_value = $value;
  105. }
  106. /**
  107. * Returns the value of the form element.
  108. *
  109. * @since 1.0
  110. *
  111. * @return string
  112. */
  113. public function getValue()
  114. {
  115. return $this->_value;
  116. }
  117. /**
  118. * Sets height in rows for textarea element.
  119. *
  120. * @param string $rows Height expressed in rows
  121. *
  122. * @since 1.0
  123. */
  124. public function setRows($rows)
  125. {
  126. $this->updateAttributes(['rows' => $rows]);
  127. }
  128. /**
  129. * Sets width in cols for textarea element.
  130. *
  131. * @param string $cols Width expressed in cols
  132. *
  133. * @since 1.0
  134. */
  135. public function setCols($cols)
  136. {
  137. $this->updateAttributes(['cols' => $cols]);
  138. }
  139. /**
  140. * Returns the textarea element in HTML.
  141. *
  142. * @since 1.0
  143. *
  144. * @return string
  145. */
  146. public function toHtml()
  147. {
  148. if ($this->_flagFrozen) {
  149. return $this->getFrozenHtml();
  150. } else {
  151. return $this->_getTabs().
  152. '<textarea'.$this->_getAttrString($this->_attributes).'>'.
  153. // because we wrap the form later we don't want the text indented
  154. // Modified by Ivan Tcholakov, 16-MAR-2010.
  155. //preg_replace("/(\r\n|\n|\r)/", '&#010;', htmlspecialchars($this->_value)) .
  156. preg_replace("/(\r\n|\n|\r)/", '&#010;', $this->getCleanValue()).
  157. '</textarea>';
  158. }
  159. }
  160. /**
  161. * Returns the value of field without HTML tags (in this case, value is changed to a mask).
  162. *
  163. * @since 1.0
  164. *
  165. * @return string
  166. */
  167. public function getFrozenHtml()
  168. {
  169. $value = $this->getCleanValue();
  170. if ($this->getAttribute('wrap') == 'off') {
  171. $html = $this->_getTabs().'<pre>'.$value."</pre>\n";
  172. } else {
  173. $html = nl2br($value)."\n";
  174. }
  175. return $html.$this->_getPersistantData();
  176. }
  177. /**
  178. * @param string $layout
  179. *
  180. * @return string
  181. */
  182. public function getTemplate($layout)
  183. {
  184. $size = $this->getColumnsSize();
  185. $custom = $this->getAttributes();
  186. $this->removeAttribute('cols-size');
  187. if (empty($size)) {
  188. $size = [2, 8, 2];
  189. }
  190. switch ($layout) {
  191. case FormValidator::LAYOUT_INLINE:
  192. return '
  193. <div class="form-group {error_class}">
  194. <label {label-for} >
  195. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  196. {label}
  197. </label>
  198. {element}
  199. </div>';
  200. break;
  201. case FormValidator::LAYOUT_HORIZONTAL:
  202. $template = '
  203. <div class="form-group row {error_class}">
  204. <label {label-for} class="col-sm-'.$size[0].' col-form-label" >
  205. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  206. {label}
  207. </label>
  208. <div class="col-sm-'.$size[1].'">
  209. {icon}
  210. {element}
  211. <!-- BEGIN label_2 -->
  212. <p class="help-block">{label_2}</p>
  213. <!-- END label_2 -->
  214. <!-- BEGIN error -->
  215. <span class="help-inline help-block">{error}</span>
  216. <!-- END error -->
  217. </div>
  218. <div class="col-sm-'.$size[2].'">
  219. <!-- BEGIN label_3 -->
  220. {label_3}
  221. <!-- END label_3 -->
  222. </div>
  223. </div>';
  224. if (isset($custom['data-block']) && $custom['data-block'] == true) {
  225. $template = '
  226. <label {label-for}>{label}</label>
  227. <div class="card-textarea">
  228. {element}
  229. </div>
  230. ';
  231. }
  232. return $template;
  233. break;
  234. case FormValidator::LAYOUT_BOX_NO_LABEL:
  235. return '
  236. <label {label-for}>{label}</label>
  237. <div class="input-group">
  238. {icon}
  239. {element}
  240. </div>';
  241. break;
  242. }
  243. }
  244. }