textarea.php 7.1 KB

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