textarea.php 8.4 KB

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