checkbox.php 7.5 KB

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