PageRenderTime 31ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/include/HTML/QuickForm/checkbox.php

https://github.com/radicaldesigns/amp
PHP | 268 lines | 96 code | 26 blank | 146 comment | 14 complexity | 0cf6ba042a6fa1ed28491c37021773af MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, BSD-3-Clause, LGPL-2.0, CC-BY-SA-3.0, AGPL-1.0
  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: checkbox.php,v 1.19 2004/02/28 22:10:16 avb Exp $
  21. require_once("HTML/QuickForm/input.php");
  22. /**
  23. * HTML class for a checkbox type field
  24. *
  25. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  26. * @author Bertrand Mansion <bmansion@mamasam.com>
  27. * @version 1.1
  28. * @since PHP4.04pl1
  29. * @access public
  30. */
  31. class HTML_QuickForm_checkbox extends HTML_QuickForm_input
  32. {
  33. // {{{ properties
  34. /**
  35. * Checkbox display text
  36. * @var string
  37. * @since 1.1
  38. * @access private
  39. */
  40. var $_text = '';
  41. // }}}
  42. // {{{ constructor
  43. /**
  44. * Class constructor
  45. *
  46. * @param string $elementName (optional)Input field name attribute
  47. * @param string $elementLabel (optional)Input field value
  48. * @param string $text (optional)Checkbox display text
  49. * @param mixed $attributes (optional)Either a typical HTML attribute string
  50. * or an associative array
  51. * @since 1.0
  52. * @access public
  53. * @return void
  54. */
  55. function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
  56. {
  57. HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  58. $this->_persistantFreeze = true;
  59. $this->_text = $text;
  60. $this->setType('checkbox');
  61. $this->updateAttributes(array('value'=>1));
  62. $this->_generateId();
  63. } //end constructor
  64. // }}}
  65. // {{{ setChecked()
  66. /**
  67. * Sets whether a checkbox is checked
  68. *
  69. * @param bool $checked Whether the field is checked or not
  70. * @since 1.0
  71. * @access public
  72. * @return void
  73. */
  74. function setChecked($checked)
  75. {
  76. if (!$checked) {
  77. $this->removeAttribute('checked');
  78. } else {
  79. $this->updateAttributes(array('checked'=>'checked'));
  80. }
  81. } //end func setChecked
  82. // }}}
  83. // {{{ getChecked()
  84. /**
  85. * Returns whether a checkbox is checked
  86. *
  87. * @since 1.0
  88. * @access public
  89. * @return bool
  90. */
  91. function getChecked()
  92. {
  93. return (bool)$this->getAttribute('checked');
  94. } //end func getChecked
  95. // }}}
  96. // {{{ toHtml()
  97. /**
  98. * Returns the checkbox element in HTML
  99. *
  100. * @since 1.0
  101. * @access public
  102. * @return string
  103. */
  104. function toHtml()
  105. {
  106. if (0 == strlen($this->_text)) {
  107. $label = '';
  108. } elseif ($this->_flagFrozen) {
  109. $label = $this->_text;
  110. } else {
  111. $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
  112. }
  113. return HTML_QuickForm_input::toHtml() . $label;
  114. } //end func toHtml
  115. // }}}
  116. // {{{ getFrozenHtml()
  117. /**
  118. * Returns the value of field without HTML tags
  119. *
  120. * @since 1.0
  121. * @access public
  122. * @return string
  123. */
  124. function getFrozenHtml()
  125. {
  126. if ($this->getChecked()) {
  127. return '<tt>[x]</tt>' .
  128. $this->_getPersistantData();
  129. } else {
  130. return '<tt>[ ]</tt>';
  131. }
  132. } //end func getFrozenHtml
  133. // }}}
  134. // {{{ setText()
  135. /**
  136. * Sets the checkbox text
  137. *
  138. * @param string $text
  139. * @since 1.1
  140. * @access public
  141. * @return void
  142. */
  143. function setText($text)
  144. {
  145. $this->_text = $text;
  146. } //end func setText
  147. // }}}
  148. // {{{ getText()
  149. /**
  150. * Returns the checkbox text
  151. *
  152. * @since 1.1
  153. * @access public
  154. * @return string
  155. */
  156. function getText()
  157. {
  158. return $this->_text;
  159. } //end func getText
  160. // }}}
  161. // {{{ setValue()
  162. /**
  163. * Sets the value of the form element
  164. *
  165. * @param string $value Default value of the form element
  166. * @since 1.0
  167. * @access public
  168. * @return void
  169. */
  170. function setValue($value)
  171. {
  172. return $this->setChecked($value);
  173. } // end func setValue
  174. // }}}
  175. // {{{ getValue()
  176. /**
  177. * Returns the value of the form element
  178. *
  179. * @since 1.0
  180. * @access public
  181. * @return bool
  182. */
  183. function getValue()
  184. {
  185. return $this->getChecked();
  186. } // end func getValue
  187. // }}}
  188. // {{{ onQuickFormEvent()
  189. /**
  190. * Called by HTML_QuickForm whenever form event is made on this element
  191. *
  192. * @param string $event Name of event
  193. * @param mixed $arg event arguments
  194. * @param object $caller calling object
  195. * @since 1.0
  196. * @access public
  197. * @return void
  198. */
  199. function onQuickFormEvent($event, $arg, &$caller)
  200. {
  201. switch ($event) {
  202. case 'updateValue':
  203. // constant values override both default and submitted ones
  204. // default values are overriden by submitted
  205. $value = $this->_findValue($caller->_constantValues);
  206. if (null === $value) {
  207. // if no boxes were checked, then there is no value in the array
  208. // yet we don't want to display default value in this case
  209. if (isset($caller->_submitValues) && 0 < count($caller->_submitValues)) {
  210. $value = $this->_findValue($caller->_submitValues);
  211. } else {
  212. $value = $this->_findValue($caller->_defaultValues);
  213. }
  214. }
  215. if (null !== $value) {
  216. $this->setChecked($value);
  217. }
  218. break;
  219. case 'setGroupValue':
  220. $this->setChecked($arg);
  221. break;
  222. default:
  223. parent::onQuickFormEvent($event, $arg, $caller);
  224. }
  225. return true;
  226. } // end func onQuickFormEvent
  227. // }}}
  228. // {{{ exportValue()
  229. /**
  230. * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
  231. */
  232. function exportValue(&$submitValues, $assoc = false)
  233. {
  234. $value = $this->_findValue($submitValues);
  235. if (null === $value) {
  236. $value = $this->getChecked()? true: null;
  237. }
  238. return $this->_prepareValue($value, $assoc);
  239. }
  240. // }}}
  241. } //end class HTML_QuickForm_checkbox
  242. ?>