advcheckbox.php 8.5 KB

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