checkbox.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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-2009 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id: checkbox.php,v 1.23 2009/04/04 21:34:02 avb Exp $
  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.11
  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($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. $label = '<label class="checkbox">' . HTML_QuickForm_input::toHtml().$this->_text . '</label>';
  121. return $label;
  122. }
  123. return HTML_QuickForm_input::toHtml() . $label;
  124. } //end func toHtml
  125. // }}}
  126. // {{{ getFrozenHtml()
  127. /**
  128. * Returns the value of field without HTML tags
  129. *
  130. * @since 1.0
  131. * @access public
  132. * @return string
  133. */
  134. function getFrozenHtml()
  135. {
  136. if ($this->getChecked()) {
  137. return '<tt>[x]</tt>' .
  138. $this->_getPersistantData();
  139. } else {
  140. return '<tt>[ ]</tt>';
  141. }
  142. } //end func getFrozenHtml
  143. // }}}
  144. // {{{ setText()
  145. /**
  146. * Sets the checkbox text
  147. *
  148. * @param string $text
  149. * @since 1.1
  150. * @access public
  151. * @return void
  152. */
  153. function setText($text)
  154. {
  155. $this->_text = $text;
  156. } //end func setText
  157. // }}}
  158. // {{{ getText()
  159. /**
  160. * Returns the checkbox text
  161. *
  162. * @since 1.1
  163. * @access public
  164. * @return string
  165. */
  166. function getText()
  167. {
  168. return $this->_text;
  169. } //end func getText
  170. // }}}
  171. // {{{ setValue()
  172. /**
  173. * Sets the value of the form element
  174. *
  175. * @param string $value Default value of the form element
  176. * @since 1.0
  177. * @access public
  178. * @return void
  179. */
  180. function setValue($value)
  181. {
  182. return $this->setChecked($value);
  183. } // end func setValue
  184. // }}}
  185. // {{{ getValue()
  186. /**
  187. * Returns the value of the form element
  188. *
  189. * @since 1.0
  190. * @access public
  191. * @return bool
  192. */
  193. function getValue()
  194. {
  195. return $this->getChecked();
  196. } // end func getValue
  197. // }}}
  198. // {{{ onQuickFormEvent()
  199. /**
  200. * Called by HTML_QuickForm whenever form event is made on this element
  201. *
  202. * @param string $event Name of event
  203. * @param mixed $arg event arguments
  204. * @param object &$caller calling object
  205. * @since 1.0
  206. * @access public
  207. * @return void
  208. */
  209. function onQuickFormEvent($event, $arg, &$caller)
  210. {
  211. switch ($event) {
  212. case 'updateValue':
  213. // constant values override both default and submitted ones
  214. // default values are overriden by submitted
  215. $value = $this->_findValue($caller->_constantValues);
  216. if (null === $value) {
  217. // if no boxes were checked, then there is no value in the array
  218. // yet we don't want to display default value in this case
  219. if ($caller->isSubmitted()) {
  220. $value = $this->_findValue($caller->_submitValues);
  221. } else {
  222. $value = $this->_findValue($caller->_defaultValues);
  223. }
  224. }
  225. if (null !== $value || $caller->isSubmitted()) {
  226. $this->setChecked($value);
  227. }
  228. break;
  229. case 'setGroupValue':
  230. $this->setChecked($arg);
  231. break;
  232. default:
  233. parent::onQuickFormEvent($event, $arg, $caller);
  234. }
  235. return true;
  236. } // end func onQuickFormEvent
  237. // }}}
  238. // {{{ exportValue()
  239. /**
  240. * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
  241. */
  242. function exportValue(&$submitValues, $assoc = false)
  243. {
  244. $value = $this->_findValue($submitValues);
  245. if (null === $value) {
  246. $value = $this->getChecked()? true: null;
  247. }
  248. return $this->_prepareValue($value, $assoc);
  249. }
  250. // }}}
  251. } //end class HTML_QuickForm_checkbox
  252. ?>