radio.php 6.8 KB

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