PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/www/lib/HTML/QuickForm/checkbox.php

https://gitlab.com/florianocomercial/centreon
PHP | 277 lines | 96 code | 27 blank | 154 comment | 14 complexity | cbf33785d7fcd61b1a6dbd7beb0f5498 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * HTML class for a checkbox 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. * @author Alexey Borzov <avb@php.net>
  19. * @copyright 2001-2011 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id$
  22. * @link http://pear.php.net/package/HTML_QuickForm
  23. */
  24. /**
  25. * Base class for <input /> form elements
  26. */
  27. require_once 'HTML/QuickForm/input.php';
  28. /**
  29. * HTML class for a checkbox type field
  30. *
  31. * @category HTML
  32. * @package HTML_QuickForm
  33. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  34. * @author Bertrand Mansion <bmansion@mamasam.com>
  35. * @author Alexey Borzov <avb@php.net>
  36. * @version Release: 3.2.14
  37. * @since 1.0
  38. */
  39. class HTML_QuickForm_checkbox extends HTML_QuickForm_input
  40. {
  41. // {{{ properties
  42. /**
  43. * Checkbox display text
  44. * @var string
  45. * @since 1.1
  46. * @access private
  47. */
  48. var $_text = '';
  49. // }}}
  50. // {{{ constructor
  51. /**
  52. * Class constructor
  53. *
  54. * @param string $elementName (optional)Input field name attribute
  55. * @param string $elementLabel (optional)Input field value
  56. * @param string $text (optional)Checkbox display text
  57. * @param mixed $attributes (optional)Either a typical HTML attribute string
  58. * or an associative array
  59. * @since 1.0
  60. * @access public
  61. * @return void
  62. */
  63. function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
  64. {
  65. HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  66. $this->_persistantFreeze = true;
  67. $this->_text = $text;
  68. $this->setType('checkbox');
  69. $this->updateAttributes(array('value'=>1));
  70. $this->_generateId();
  71. } //end constructor
  72. // }}}
  73. // {{{ setChecked()
  74. /**
  75. * Sets whether a checkbox is checked
  76. *
  77. * @param bool $checked Whether the field is checked or not
  78. * @since 1.0
  79. * @access public
  80. * @return void
  81. */
  82. function setChecked($checked)
  83. {
  84. if (!$checked) {
  85. $this->removeAttribute('checked');
  86. } else {
  87. $this->updateAttributes(array('checked'=>'checked'));
  88. }
  89. } //end func setChecked
  90. // }}}
  91. // {{{ getChecked()
  92. /**
  93. * Returns whether a checkbox is checked
  94. *
  95. * @since 1.0
  96. * @access public
  97. * @return bool
  98. */
  99. function getChecked()
  100. {
  101. return (bool)$this->getAttribute('checked');
  102. } //end func getChecked
  103. // }}}
  104. // {{{ toHtml()
  105. /**
  106. * Returns the checkbox element in HTML
  107. *
  108. * @since 1.0
  109. * @access public
  110. * @return string
  111. */
  112. function toHtml()
  113. {
  114. if (0 == strlen((string)$this->_text)) {
  115. $label = '';
  116. } elseif ($this->_flagFrozen) {
  117. $label = $this->_text;
  118. } else {
  119. $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
  120. }
  121. return HTML_QuickForm_input::toHtml() . $label;
  122. } //end func toHtml
  123. // }}}
  124. // {{{ getFrozenHtml()
  125. /**
  126. * Returns the value of field without HTML tags
  127. *
  128. * @since 1.0
  129. * @access public
  130. * @return string
  131. */
  132. function getFrozenHtml()
  133. {
  134. if ($this->getChecked()) {
  135. return '<tt>[x]</tt>' .
  136. $this->_getPersistantData();
  137. } else {
  138. return '<tt>[ ]</tt>';
  139. }
  140. } //end func getFrozenHtml
  141. // }}}
  142. // {{{ setText()
  143. /**
  144. * Sets the checkbox text
  145. *
  146. * @param string $text
  147. * @since 1.1
  148. * @access public
  149. * @return void
  150. */
  151. function setText($text)
  152. {
  153. $this->_text = $text;
  154. } //end func setText
  155. // }}}
  156. // {{{ getText()
  157. /**
  158. * Returns the checkbox text
  159. *
  160. * @since 1.1
  161. * @access public
  162. * @return string
  163. */
  164. function getText()
  165. {
  166. return $this->_text;
  167. } //end func getText
  168. // }}}
  169. // {{{ setValue()
  170. /**
  171. * Sets the value of the form element
  172. *
  173. * @param string $value Default value of the form element
  174. * @since 1.0
  175. * @access public
  176. * @return void
  177. */
  178. function setValue($value)
  179. {
  180. return $this->setChecked($value);
  181. } // end func setValue
  182. // }}}
  183. // {{{ getValue()
  184. /**
  185. * Returns the value of the form element
  186. *
  187. * @since 1.0
  188. * @access public
  189. * @return bool
  190. */
  191. function getValue()
  192. {
  193. return $this->getChecked();
  194. } // end func getValue
  195. // }}}
  196. // {{{ onQuickFormEvent()
  197. /**
  198. * Called by HTML_QuickForm whenever form event is made on this element
  199. *
  200. * @param string $event Name of event
  201. * @param mixed $arg event arguments
  202. * @param object &$caller calling object
  203. * @since 1.0
  204. * @access public
  205. * @return void
  206. */
  207. function onQuickFormEvent($event, $arg, &$caller)
  208. {
  209. switch ($event) {
  210. case 'updateValue':
  211. // constant values override both default and submitted ones
  212. // default values are overriden by submitted
  213. $value = $this->_findValue($caller->_constantValues);
  214. if (null === $value) {
  215. // if no boxes were checked, then there is no value in the array
  216. // yet we don't want to display default value in this case
  217. if ($caller->isSubmitted()) {
  218. $value = $this->_findValue($caller->_submitValues);
  219. } else {
  220. $value = $this->_findValue($caller->_defaultValues);
  221. }
  222. }
  223. if (null !== $value || $caller->isSubmitted()) {
  224. $this->setChecked($value);
  225. }
  226. break;
  227. case 'setGroupValue':
  228. $this->setChecked($arg);
  229. break;
  230. default:
  231. parent::onQuickFormEvent($event, $arg, $caller);
  232. }
  233. return true;
  234. } // end func onQuickFormEvent
  235. // }}}
  236. // {{{ exportValue()
  237. /**
  238. * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
  239. */
  240. function exportValue(&$submitValues, $assoc = false)
  241. {
  242. $value = $this->_findValue($submitValues);
  243. if (null === $value) {
  244. $value = $this->getChecked()? true: null;
  245. }
  246. return $this->_prepareValue($value, $assoc);
  247. }
  248. // }}}
  249. } //end class HTML_QuickForm_checkbox
  250. ?>