textarea.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 = null;
  42. /**
  43. * Class constructor
  44. *
  45. * @param string Input field name attribute
  46. * @param mixed Label(s) for a field
  47. * @param mixed Either a typical HTML attribute string or an associative array
  48. * @since 1.0
  49. * @access public
  50. */
  51. public function __construct(
  52. $elementName = null,
  53. $elementLabel = null,
  54. $attributes = null
  55. ) {
  56. $attributes['class'] = isset($attributes['class']) ? $attributes['class'] : 'form-control';
  57. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  58. $this->setColumnsSize($columnsSize);
  59. parent::__construct($elementName, $elementLabel, $attributes);
  60. $this->_persistantFreeze = true;
  61. $this->_type = 'textarea';
  62. }
  63. /**
  64. * Sets the input field name
  65. *
  66. * @param string $name Input field name attribute
  67. * @since 1.0
  68. * @access public
  69. * @return void
  70. */
  71. function setName($name)
  72. {
  73. $this->updateAttributes(array('name'=>$name));
  74. } //end func setName
  75. // }}}
  76. // {{{ getName()
  77. /**
  78. * Returns the element name
  79. *
  80. * @since 1.0
  81. * @access public
  82. * @return string
  83. */
  84. function getName()
  85. {
  86. return $this->getAttribute('name');
  87. } //end func getName
  88. // }}}
  89. // {{{ setValue()
  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. function setValue($value)
  99. {
  100. $this->_value = $value;
  101. } //end func setValue
  102. // }}}
  103. // {{{ getValue()
  104. /**
  105. * Returns the value of the form element
  106. *
  107. * @since 1.0
  108. * @access public
  109. * @return string
  110. */
  111. function getValue()
  112. {
  113. return $this->_value;
  114. } // end func getValue
  115. // }}}
  116. // {{{ setWrap()
  117. /**
  118. * Sets wrap type for textarea element
  119. *
  120. * @param string $wrap Wrap type
  121. * @since 1.0
  122. * @access public
  123. * @return void
  124. */
  125. function setWrap($wrap)
  126. {
  127. $this->updateAttributes(array('wrap' => $wrap));
  128. } //end func setWrap
  129. // }}}
  130. // {{{ setRows()
  131. /**
  132. * Sets height in rows for textarea element
  133. *
  134. * @param string $rows Height expressed in rows
  135. * @since 1.0
  136. * @access public
  137. * @return void
  138. */
  139. function setRows($rows)
  140. {
  141. $this->updateAttributes(array('rows' => $rows));
  142. } //end func setRows
  143. // }}}
  144. // {{{ setCols()
  145. /**
  146. * Sets width in cols for textarea element
  147. *
  148. * @param string $cols Width expressed in cols
  149. * @since 1.0
  150. * @access public
  151. * @return void
  152. */
  153. function setCols($cols)
  154. {
  155. $this->updateAttributes(array('cols' => $cols));
  156. } //end func setCols
  157. // }}}
  158. // {{{ toHtml()
  159. /**
  160. * Returns the textarea element in HTML
  161. *
  162. * @since 1.0
  163. * @access public
  164. * @return string
  165. */
  166. public function toHtml()
  167. {
  168. if ($this->_flagFrozen) {
  169. return $this->getFrozenHtml();
  170. } else {
  171. return $this->_getTabs() .
  172. '<textarea' . $this->_getAttrString($this->_attributes) . '>' .
  173. // because we wrap the form later we don't want the text indented
  174. // Modified by Ivan Tcholakov, 16-MAR-2010.
  175. //preg_replace("/(\r\n|\n|\r)/", '&#010;', htmlspecialchars($this->_value)) .
  176. preg_replace("/(\r\n|\n|\r)/", '&#010;', @htmlspecialchars($this->_value, ENT_COMPAT, HTML_Common::charset())) .
  177. //
  178. '</textarea>';
  179. }
  180. }
  181. /**
  182. * Returns the value of field without HTML tags (in this case, value is changed to a mask)
  183. *
  184. * @since 1.0
  185. * @access public
  186. * @return string
  187. */
  188. public function getFrozenHtml()
  189. {
  190. // Modified by Ivan Tcholakov, 16-MAR-2010.
  191. //$value = htmlspecialchars($this->getValue());
  192. $value = @htmlspecialchars($this->getValue(), ENT_COMPAT, HTML_Common::charset());
  193. //
  194. if ($this->getAttribute('wrap') == 'off') {
  195. $html = $this->_getTabs() . '<pre>' . $value."</pre>\n";
  196. } else {
  197. $html = nl2br($value)."\n";
  198. }
  199. return $html . $this->_getPersistantData();
  200. }
  201. /**
  202. * @param string $layout
  203. *
  204. * @return string
  205. */
  206. public function getTemplate($layout)
  207. {
  208. $size = $this->getColumnsSize();
  209. $this->removeAttribute('cols-size');
  210. if (empty($size)) {
  211. $size = [2, 8, 2];
  212. }
  213. switch ($layout) {
  214. case FormValidator::LAYOUT_INLINE:
  215. return '
  216. <div class="form-group {error_class}">
  217. <label {label-for} >
  218. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  219. {label}
  220. </label>
  221. {element}
  222. </div>';
  223. break;
  224. case FormValidator::LAYOUT_HORIZONTAL:
  225. return '
  226. <div class="form-group {error_class}">
  227. <label {label-for} class="col-sm-'.$size[0].' control-label" >
  228. <!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
  229. {label}
  230. </label>
  231. <div class="col-sm-'.$size[1].'">
  232. {icon}
  233. {element}
  234. <!-- BEGIN label_2 -->
  235. <p class="help-block">{label_2}</p>
  236. <!-- END label_2 -->
  237. <!-- BEGIN error -->
  238. <span class="help-inline">{error}</span>
  239. <!-- END error -->
  240. </div>
  241. <div class="col-sm-'.$size[2].'">
  242. <!-- BEGIN label_3 -->
  243. {label_3}
  244. <!-- END label_3 -->
  245. </div>
  246. </div>';
  247. break;
  248. case FormValidator::LAYOUT_BOX_NO_LABEL:
  249. return '
  250. <label {label-for}>{label}</label>
  251. <div class="input-group">
  252. {icon}
  253. {element}
  254. </div>';
  255. break;
  256. }
  257. }
  258. }