checkbox.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * HTML class for a checkbox type field
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @author Alexey Borzov <avb@php.net>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: checkbox.php,v 1.23 2009/04/04 21:34:02 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * HTML class for a checkbox type field
  25. *
  26. * @category HTML
  27. * @package HTML_QuickForm
  28. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  29. * @author Bertrand Mansion <bmansion@mamasam.com>
  30. * @author Alexey Borzov <avb@php.net>
  31. * @version Release: 3.2.11
  32. * @since 1.0
  33. */
  34. class HTML_QuickForm_checkbox extends HTML_QuickForm_input
  35. {
  36. /**
  37. * Checkbox display text
  38. * @var string
  39. * @since 1.1
  40. * @access private
  41. */
  42. public $_text = '';
  43. public $labelClass;
  44. public $checkboxClass;
  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 __construct(
  60. $elementName = null,
  61. $elementLabel = null,
  62. $text = '',
  63. $attributes = null
  64. ) {
  65. $this->labelClass = isset($attributes['label-class']) ? $attributes['label-class'] : '';
  66. $this->checkboxClass = isset($attributes['checkbox-class']) ? $attributes['checkbox-class'] : 'checkbox';
  67. if (isset($attributes['label-class'])) {
  68. unset($attributes['label-class']);
  69. }
  70. if (isset($attributes['checkbox-class'])) {
  71. unset($attributes['checkbox-class']);
  72. }
  73. parent::__construct($elementName, $elementLabel, $attributes);
  74. $this->_persistantFreeze = true;
  75. $this->_text = $text;
  76. $this->setType('checkbox');
  77. if (!isset($attributes['value'])) {
  78. $this->updateAttributes(array('value' => 1));
  79. } else {
  80. $this->updateAttributes(array('value' => $attributes['value']));
  81. }
  82. $this->_generateId();
  83. }
  84. /**
  85. * Sets whether a checkbox is checked
  86. *
  87. * @param bool $checked Whether the field is checked or not
  88. * @since 1.0
  89. * @access public
  90. * @return void
  91. */
  92. function setChecked($checked)
  93. {
  94. if (!$checked) {
  95. $this->removeAttribute('checked');
  96. } else {
  97. $this->updateAttributes(array('checked'=>'checked'));
  98. }
  99. } //end func setChecked
  100. // }}}
  101. // {{{ getChecked()
  102. /**
  103. * Returns whether a checkbox is checked
  104. *
  105. * @since 1.0
  106. * @access public
  107. * @return bool
  108. */
  109. function getChecked()
  110. {
  111. return (bool)$this->getAttribute('checked');
  112. } //end func getChecked
  113. // }}}
  114. // {{{ toHtml()
  115. /**
  116. * Returns the checkbox element in HTML
  117. *
  118. * @since 1.0
  119. * @access public
  120. * @return string
  121. */
  122. public function toHtml()
  123. {
  124. if (0 == strlen($this->_text)) {
  125. $label = '';
  126. } elseif ($this->_flagFrozen) {
  127. $label = $this->_text;
  128. } else {
  129. $labelClass = $this->labelClass;
  130. $checkboxClass = $this->checkboxClass;
  131. $label ='
  132. <div class="'.$checkboxClass.'">
  133. <label class="'.$labelClass.'">' .
  134. HTML_QuickForm_input::toHtml().$this->_text.
  135. '</label>
  136. </div>
  137. ';
  138. return $label;
  139. }
  140. return HTML_QuickForm_input::toHtml() . $label;
  141. } //end func toHtml
  142. // }}}
  143. // {{{ getFrozenHtml()
  144. /**
  145. * Returns the value of field without HTML tags
  146. *
  147. * @since 1.0
  148. * @access public
  149. * @return string
  150. */
  151. function getFrozenHtml()
  152. {
  153. if ($this->getChecked()) {
  154. return '<code>[x]</code>' .
  155. $this->_getPersistantData();
  156. } else {
  157. return '<code>[ ]</code>';
  158. }
  159. } //end func getFrozenHtml
  160. // }}}
  161. // {{{ setText()
  162. /**
  163. * Sets the checkbox text
  164. *
  165. * @param string $text
  166. * @since 1.1
  167. * @access public
  168. * @return void
  169. */
  170. function setText($text)
  171. {
  172. $this->_text = $text;
  173. } //end func setText
  174. // }}}
  175. // {{{ getText()
  176. /**
  177. * Returns the checkbox text
  178. *
  179. * @since 1.1
  180. * @access public
  181. * @return string
  182. */
  183. function getText()
  184. {
  185. return $this->_text;
  186. } //end func getText
  187. // }}}
  188. // {{{ setValue()
  189. /**
  190. * Sets the value of the form element
  191. *
  192. * @param string $value Default value of the form element
  193. * @since 1.0
  194. * @access public
  195. * @return void
  196. */
  197. function setValue($value)
  198. {
  199. return $this->setChecked($value);
  200. } // end func setValue
  201. // }}}
  202. // {{{ getValue()
  203. /**
  204. * Returns the value of the form element
  205. *
  206. * @since 1.0
  207. * @access public
  208. * @return bool
  209. */
  210. function getValue()
  211. {
  212. return $this->getChecked();
  213. } // end func getValue
  214. // }}}
  215. // {{{ onQuickFormEvent()
  216. /**
  217. * Called by HTML_QuickForm whenever form event is made on this element
  218. *
  219. * @param string $event Name of event
  220. * @param mixed $arg event arguments
  221. * @param object &$caller calling object
  222. * @since 1.0
  223. * @access public
  224. * @return void
  225. */
  226. function onQuickFormEvent($event, $arg, &$caller)
  227. {
  228. switch ($event) {
  229. case 'updateValue':
  230. // constant values override both default and submitted ones
  231. // default values are overriden by submitted
  232. $value = $this->_findValue($caller->_constantValues);
  233. if (null === $value) {
  234. // if no boxes were checked, then there is no value in the array
  235. // yet we don't want to display default value in this case
  236. if ($caller->isSubmitted()) {
  237. $value = $this->_findValue($caller->_submitValues);
  238. } else {
  239. $value = $this->_findValue($caller->_defaultValues);
  240. }
  241. }
  242. if (null !== $value || $caller->isSubmitted()) {
  243. $this->setChecked($value);
  244. }
  245. break;
  246. case 'setGroupValue':
  247. $this->setChecked($arg);
  248. break;
  249. default:
  250. parent::onQuickFormEvent($event, $arg, $caller);
  251. }
  252. return true;
  253. } // end func onQuickFormEvent
  254. // }}}
  255. // {{{ exportValue()
  256. /**
  257. * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
  258. */
  259. function exportValue(&$submitValues, $assoc = false)
  260. {
  261. $value = $this->_findValue($submitValues);
  262. if (null === $value) {
  263. $value = $this->getChecked()? true: null;
  264. }
  265. return $this->_prepareValue($value, $assoc);
  266. }
  267. }