textarea.php 7.3 KB

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