/lib/pear/HTML/QuickForm/checkbox.php

https://github.com/StudiumDevel/moodle · PHP · 267 lines · 95 code · 26 blank · 146 comment · 13 complexity · 9a0d1ab2fb4e27dd51053b1173bf84a3 MD5 · raw file

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