textarea.php 7.4 KB

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