PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/include/HTML/QuickForm/radio.php

https://github.com/radicaldesigns/amp
PHP | 244 lines | 97 code | 22 blank | 125 comment | 19 complexity | 881f0fbc46ecf5956fd7e050637dd2fe 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: radio.php,v 1.17 2004/02/28 22:10:16 avb Exp $
  21. require_once('HTML/QuickForm/input.php');
  22. /**
  23. * HTML class for a radio type element
  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_radio extends HTML_QuickForm_input
  32. {
  33. // {{{ properties
  34. /**
  35. * Radio 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 Input field name attribute
  47. * @param mixed Label(s) for a field
  48. * @param string Text to display near the radio
  49. * @param string Input field value
  50. * @param mixed Either a typical HTML attribute string or an associative array
  51. * @since 1.0
  52. * @access public
  53. * @return void
  54. */
  55. function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
  56. {
  57. $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  58. if (isset($value)) {
  59. $this->setValue($value);
  60. }
  61. $this->_persistantFreeze = true;
  62. $this->setType('radio');
  63. $this->_text = $text;
  64. $this->_generateId();
  65. } //end constructor
  66. // }}}
  67. // {{{ setChecked()
  68. /**
  69. * Sets whether radio button is checked
  70. *
  71. * @param bool $checked Whether the field is checked or not
  72. * @since 1.0
  73. * @access public
  74. * @return void
  75. */
  76. function setChecked($checked)
  77. {
  78. if (!$checked) {
  79. $this->removeAttribute('checked');
  80. } else {
  81. $this->updateAttributes(array('checked'=>'checked'));
  82. }
  83. } //end func setChecked
  84. // }}}
  85. // {{{ getChecked()
  86. /**
  87. * Returns whether radio button is checked
  88. *
  89. * @since 1.0
  90. * @access public
  91. * @return string
  92. */
  93. function getChecked()
  94. {
  95. return $this->getAttribute('checked');
  96. } //end func getChecked
  97. // }}}
  98. // {{{ toHtml()
  99. /**
  100. * Returns the radio element in HTML
  101. *
  102. * @since 1.0
  103. * @access public
  104. * @return string
  105. */
  106. function toHtml()
  107. {
  108. if (0 == strlen($this->_text)) {
  109. $label = '';
  110. } elseif ($this->_flagFrozen) {
  111. $label = $this->_text;
  112. } else {
  113. $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
  114. }
  115. return HTML_QuickForm_input::toHtml() . $label;
  116. } //end func toHtml
  117. // }}}
  118. // {{{ getFrozenHtml()
  119. /**
  120. * Returns the value of field without HTML tags
  121. *
  122. * @since 1.0
  123. * @access public
  124. * @return string
  125. */
  126. function getFrozenHtml()
  127. {
  128. if ($this->getChecked()) {
  129. return '<tt>(x)</tt>' .
  130. $this->_getPersistantData();
  131. } else {
  132. return '<tt>( )</tt>';
  133. }
  134. } //end func getFrozenHtml
  135. // }}}
  136. // {{{ setText()
  137. /**
  138. * Sets the radio text
  139. *
  140. * @param string $text Text to display near the radio button
  141. * @since 1.1
  142. * @access public
  143. * @return void
  144. */
  145. function setText($text)
  146. {
  147. $this->_text = $text;
  148. } //end func setText
  149. // }}}
  150. // {{{ getText()
  151. /**
  152. * Returns the radio text
  153. *
  154. * @since 1.1
  155. * @access public
  156. * @return string
  157. */
  158. function getText()
  159. {
  160. return $this->_text;
  161. } //end func getText
  162. // }}}
  163. // {{{ onQuickFormEvent()
  164. /**
  165. * Called by HTML_QuickForm whenever form event is made on this element
  166. *
  167. * @param string $event Name of event
  168. * @param mixed $arg event arguments
  169. * @param object $caller calling object
  170. * @since 1.0
  171. * @access public
  172. * @return void
  173. */
  174. function onQuickFormEvent($event, $arg, &$caller)
  175. {
  176. switch ($event) {
  177. case 'updateValue':
  178. // constant values override both default and submitted ones
  179. // default values are overriden by submitted
  180. $value = $this->_findValue($caller->_constantValues);
  181. if (null === $value) {
  182. $value = $this->_findValue($caller->_submitValues);
  183. if (null === $value) {
  184. $value = $this->_findValue($caller->_defaultValues);
  185. }
  186. }
  187. if ($value == $this->getValue()) {
  188. $this->setChecked(true);
  189. } else {
  190. $this->setChecked(false);
  191. }
  192. break;
  193. case 'setGroupValue':
  194. if ($arg == $this->getValue()) {
  195. $this->setChecked(true);
  196. } else {
  197. $this->setChecked(false);
  198. }
  199. break;
  200. default:
  201. parent::onQuickFormEvent($event, $arg, $caller);
  202. }
  203. return true;
  204. } // end func onQuickFormLoad
  205. // }}}
  206. // {{{ exportValue()
  207. /**
  208. * Returns the value attribute if the radio is checked, null if it is not
  209. */
  210. function exportValue(&$submitValues, $assoc = false)
  211. {
  212. $value = $this->_findValue($submitValues);
  213. if (null === $value) {
  214. $value = $this->getChecked()? $this->getValue(): null;
  215. } elseif ($value != $this->getValue()) {
  216. $value = null;
  217. }
  218. return $this->_prepareValue($value, $assoc);
  219. }
  220. // }}}
  221. } //end class HTML_QuickForm_radio
  222. ?>