radio.php 6.7 KB

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