advcheckbox.php 8.2 KB

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