checkbox.php 8.0 KB

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