radio.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * HTML class for a radio type element
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: radio.php,v 1.20 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * HTML class for a radio type element
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  28. * @author Bertrand Mansion <bmansion@mamasam.com>
  29. * @version Release: 3.2.11
  30. * @since 1.0
  31. */
  32. class HTML_QuickForm_radio extends HTML_QuickForm_input
  33. {
  34. /**
  35. * Radio display text
  36. * @var string
  37. * @since 1.1
  38. * @access private
  39. */
  40. public $_text = '';
  41. public $labelClass;
  42. public $radioClass;
  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. public function __construct(
  56. $elementName = null,
  57. $elementLabel = null,
  58. $text = null,
  59. $value = null,
  60. $attributes = null
  61. ) {
  62. $this->labelClass = isset($attributes['label-class']) ? $attributes['label-class'] : '';
  63. $this->radioClass = isset($attributes['radio-class']) ? $attributes['radio-class'] : 'radio';
  64. if (isset($attributes['label-class'])) {
  65. unset($attributes['label-class']);
  66. }
  67. if (isset($attributes['radio-class'])) {
  68. unset($attributes['radio-class']);
  69. }
  70. $columnsSize = isset($attributes['cols-size']) ? $attributes['cols-size'] : null;
  71. $this->setColumnsSize($columnsSize);
  72. parent::__construct($elementName, $elementLabel, $attributes);
  73. if (isset($value)) {
  74. $this->setValue($value);
  75. }
  76. $this->_persistantFreeze = true;
  77. $this->setType('radio');
  78. $this->_text = $text;
  79. $this->_generateId();
  80. }
  81. /**
  82. * Sets whether radio button is checked
  83. *
  84. * @param bool $checked Whether the field is checked or not
  85. * @since 1.0
  86. * @access public
  87. * @return void
  88. */
  89. public function setChecked($checked)
  90. {
  91. if (!$checked) {
  92. $this->removeAttribute('checked');
  93. } else {
  94. $this->updateAttributes(array('checked'=>'checked'));
  95. }
  96. }
  97. /**
  98. * Returns whether radio button is checked
  99. *
  100. * @since 1.0
  101. * @access public
  102. * @return string
  103. */
  104. public function getChecked()
  105. {
  106. return $this->getAttribute('checked');
  107. }
  108. /**
  109. * Returns the radio element in HTML
  110. *
  111. * @since 1.0
  112. * @access public
  113. * @return string
  114. */
  115. public function toHtml()
  116. {
  117. if (0 == strlen($this->_text)) {
  118. $label = '';
  119. } elseif ($this->isFrozen()) {
  120. $label = $this->_text;
  121. if ($this->freezeSeeOnlySelected) {
  122. $invisible = $this->getChecked() ? '' : ' style="display:none"';
  123. return "<div $invisible>".HTML_QuickForm_input::toHtml().$this->_text."</div>";
  124. }
  125. } else {
  126. $labelClass = $this->labelClass;
  127. $radioClass = $this->radioClass;
  128. $label = '<div class="'.$radioClass.'">
  129. <label class="'.$labelClass.'">' .
  130. HTML_QuickForm_input::toHtml().
  131. ''.
  132. $this->_text .
  133. '</label>
  134. </div>';
  135. return $label;
  136. }
  137. return HTML_QuickForm_input::toHtml().$label;
  138. }
  139. /**
  140. * Returns the value of field without HTML tags
  141. *
  142. * @since 1.0
  143. * @access public
  144. * @return string
  145. */
  146. public function getFrozenHtml()
  147. {
  148. if ($this->getChecked()) {
  149. return '<code>(x)</code>' .
  150. $this->_getPersistantData();
  151. } else {
  152. return '<code>( )</code>';
  153. }
  154. }
  155. /**
  156. * Sets the radio text
  157. *
  158. * @param string $text Text to display near the radio button
  159. * @since 1.1
  160. * @access public
  161. * @return void
  162. */
  163. public function setText($text)
  164. {
  165. $this->_text = $text;
  166. }
  167. /**
  168. * Returns the radio text
  169. *
  170. * @since 1.1
  171. * @access public
  172. * @return string
  173. */
  174. public function getText()
  175. {
  176. return $this->_text;
  177. }
  178. /**
  179. * Called by HTML_QuickForm whenever form event is made on this element
  180. *
  181. * @param string $event Name of event
  182. * @param mixed $arg event arguments
  183. * @param object &$caller calling object
  184. * @since 1.0
  185. * @access public
  186. * @return void
  187. */
  188. public function onQuickFormEvent($event, $arg, &$caller)
  189. {
  190. switch ($event) {
  191. case 'updateValue':
  192. // constant values override both default and submitted ones
  193. // default values are overriden by submitted
  194. $value = $this->_findValue($caller->_constantValues);
  195. if (null === $value) {
  196. $value = $this->_findValue($caller->_submitValues);
  197. if (null === $value) {
  198. $value = $this->_findValue($caller->_defaultValues);
  199. }
  200. }
  201. if (!is_null($value) && $value == $this->getValue()) {
  202. $this->setChecked(true);
  203. } else {
  204. $this->setChecked(false);
  205. }
  206. break;
  207. case 'setGroupValue':
  208. if ($arg == $this->getValue()) {
  209. $this->setChecked(true);
  210. } else {
  211. $this->setChecked(false);
  212. }
  213. break;
  214. default:
  215. parent::onQuickFormEvent($event, $arg, $caller);
  216. }
  217. return true;
  218. }
  219. /**
  220. * Returns the value attribute if the radio is checked, null if it is not
  221. */
  222. public function exportValue(&$submitValues, $assoc = false)
  223. {
  224. $value = $this->_findValue($submitValues);
  225. if (null === $value) {
  226. $value = $this->getChecked()? $this->getValue(): null;
  227. } elseif ($value != $this->getValue()) {
  228. $value = null;
  229. }
  230. return $this->_prepareValue($value, $assoc);
  231. }
  232. /**
  233. * @return null
  234. */
  235. public function getColumnsSize()
  236. {
  237. return $this->columnsSize;
  238. }
  239. }