advcheckbox.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * HTML class for an advanced 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 Jason Rust <jrust@php.net>
  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: advcheckbox.php,v 1.18 2009/04/04 21:34:02 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * HTML class for an advanced checkbox type field
  25. *
  26. * Basically this fixes a problem that HTML has had
  27. * where checkboxes can only pass a single value (the
  28. * value of the checkbox when checked). A value for when
  29. * the checkbox is not checked cannot be passed, and
  30. * furthermore the checkbox variable doesn't even exist if
  31. * the checkbox was submitted unchecked.
  32. *
  33. * It works by prepending a hidden field with the same name and
  34. * another "unchecked" value to the checbox. If the checkbox is
  35. * checked, PHP overwrites the value of the hidden field with
  36. * its value.
  37. *
  38. * @category HTML
  39. * @package HTML_QuickForm
  40. * @author Jason Rust <jrust@php.net>
  41. * @author Alexey Borzov <avb@php.net>
  42. * @version Release: 3.2.11
  43. * @since 2.0
  44. */
  45. class HTML_QuickForm_advcheckbox extends HTML_QuickForm_checkbox
  46. {
  47. // {{{ properties
  48. /**
  49. * The values passed by the hidden elment
  50. *
  51. * @var array
  52. * @access private
  53. */
  54. var $_values = null;
  55. /**
  56. * The default value
  57. *
  58. * @var boolean
  59. * @access private
  60. */
  61. var $_currentValue = null;
  62. // }}}
  63. // {{{ constructor
  64. /**
  65. * Class constructor
  66. *
  67. * @param string $elementName (optional)Input field name attribute
  68. * @param string $elementLabel (optional)Input field label
  69. * @param string $text (optional)Text to put after the checkbox
  70. * @param mixed $attributes (optional)Either a typical HTML attribute string
  71. * or an associative array
  72. * @param mixed $values (optional)Values to pass if checked or not checked
  73. *
  74. * @since 1.0
  75. * @access public
  76. * @return void
  77. */
  78. public function __construct($elementName=null, $elementLabel=null, $text=null, $attributes=null, $values=null)
  79. {
  80. parent::__construct($elementName, $elementLabel, $text, $attributes);
  81. $this->setValues($values);
  82. } //end constructor
  83. // }}}
  84. // {{{ getPrivateName()
  85. /**
  86. * Gets the private name for the element
  87. *
  88. * @param string $elementName The element name to make private
  89. *
  90. * @access public
  91. * @return string
  92. *
  93. * @deprecated Deprecated since 3.2.6, both generated elements have the same name
  94. */
  95. function getPrivateName($elementName)
  96. {
  97. return '__'.$elementName;
  98. }
  99. // }}}
  100. // {{{ getOnclickJs()
  101. /**
  102. * Create the javascript for the onclick event which will
  103. * set the value of the hidden field
  104. *
  105. * @param string $elementName The element name
  106. *
  107. * @access public
  108. * @return string
  109. *
  110. * @deprecated Deprecated since 3.2.6, this element no longer uses any javascript
  111. */
  112. function getOnclickJs($elementName)
  113. {
  114. $onclickJs = 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1], '\'').'\'; }';
  115. $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0], '\'').'\'; }';
  116. return $onclickJs;
  117. }
  118. // }}}
  119. // {{{ setValues()
  120. /**
  121. * Sets the values used by the hidden element
  122. *
  123. * @param mixed $values The values, either a string or an array
  124. *
  125. * @access public
  126. * @return void
  127. */
  128. function setValues($values)
  129. {
  130. if (empty($values)) {
  131. // give it default checkbox behavior
  132. $this->_values = array('', 1);
  133. } elseif (is_scalar($values)) {
  134. // if it's string, then assume the value to
  135. // be passed is for when the element is checked
  136. $this->_values = array('', $values);
  137. } else {
  138. $this->_values = $values;
  139. }
  140. $this->updateAttributes(array('value' => $this->_values[1]));
  141. $this->setChecked($this->_currentValue == $this->_values[1]);
  142. }
  143. // }}}
  144. // {{{ setValue()
  145. /**
  146. * Sets the element's value
  147. *
  148. * @param mixed Element's value
  149. * @access public
  150. */
  151. function setValue($value)
  152. {
  153. $this->setChecked(isset($this->_values[1]) && $value == $this->_values[1]);
  154. $this->_currentValue = $value;
  155. }
  156. // }}}
  157. // {{{ getValue()
  158. /**
  159. * Returns the element's value
  160. *
  161. * @access public
  162. * @return mixed
  163. */
  164. function getValue()
  165. {
  166. if (is_array($this->_values)) {
  167. return $this->_values[$this->getChecked()? 1: 0];
  168. } else {
  169. return null;
  170. }
  171. }
  172. // }}}
  173. // {{{ toHtml()
  174. /**
  175. * Returns the checkbox element in HTML
  176. * and the additional hidden element in HTML
  177. *
  178. * @access public
  179. * @return string
  180. */
  181. function toHtml()
  182. {
  183. if ($this->_flagFrozen) {
  184. return parent::toHtml();
  185. } else {
  186. return '<input' . $this->_getAttrString(array(
  187. 'type' => 'hidden',
  188. 'name' => $this->getName(),
  189. 'value' => $this->_values[0]
  190. )) . ' />' . parent::toHtml();
  191. }
  192. } //end func toHtml
  193. // }}}
  194. // {{{ getFrozenHtml()
  195. /**
  196. * Unlike checkbox, this has to append a hidden input in both
  197. * checked and non-checked states
  198. */
  199. function getFrozenHtml()
  200. {
  201. return ($this->getChecked()? '<tt>[x]</tt>': '<tt>[ ]</tt>') .
  202. $this->_getPersistantData();
  203. }
  204. // }}}
  205. // {{{ onQuickFormEvent()
  206. /**
  207. * Called by HTML_QuickForm whenever form event is made on this element
  208. *
  209. * @param string $event Name of event
  210. * @param mixed $arg event arguments
  211. * @param object &$caller calling object
  212. * @since 1.0
  213. * @access public
  214. * @return void
  215. */
  216. function onQuickFormEvent($event, $arg, &$caller)
  217. {
  218. switch ($event) {
  219. case 'updateValue':
  220. // constant values override both default and submitted ones
  221. // default values are overriden by submitted
  222. $value = $this->_findValue($caller->_constantValues);
  223. if (null === $value) {
  224. $value = $this->_findValue($caller->_submitValues);
  225. if (null === $value) {
  226. $value = $this->_findValue($caller->_defaultValues);
  227. }
  228. }
  229. if (null !== $value) {
  230. $this->setValue($value);
  231. }
  232. break;
  233. default:
  234. parent::onQuickFormEvent($event, $arg, $caller);
  235. }
  236. return true;
  237. } // end func onQuickFormLoad
  238. // }}}
  239. // {{{ exportValue()
  240. /**
  241. * This element has a value even if it is not checked, thus we override
  242. * checkbox's behaviour here
  243. */
  244. function exportValue(&$submitValues, $assoc)
  245. {
  246. $value = $this->_findValue($submitValues);
  247. if (null === $value) {
  248. $value = $this->getValue();
  249. } elseif (is_array($this->_values) && ($value != $this->_values[0]) && ($value != $this->_values[1])) {
  250. $value = null;
  251. }
  252. return $this->_prepareValue($value, $assoc);
  253. }
  254. // }}}
  255. } //end class HTML_QuickForm_advcheckbox
  256. ?>