radio.php 7.1 KB

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