radio.php 6.2 KB

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