radio.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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->isFrozen()) {
  121. $label = $this->_text;
  122. if ($this->freezeSeeOnlySelected) {
  123. $invisible = $this->getChecked() ? '' : ' style="display:none"';
  124. return "<div $invisible>".HTML_QuickForm_input::toHtml().$this->_text."</div>";
  125. }
  126. } else {
  127. $labelClass = $this->labelClass;
  128. $radioClass = $this->radioClass;
  129. $label = '<div class="'.$radioClass.'">
  130. <label class="'.$labelClass.'">' .
  131. HTML_QuickForm_input::toHtml().
  132. ''.
  133. $this->_text .
  134. '</label>&nbsp;&nbsp;
  135. </div>';
  136. return $label;
  137. }
  138. return HTML_QuickForm_input::toHtml() . $label;
  139. }
  140. /**
  141. * Returns the value of field without HTML tags
  142. *
  143. * @since 1.0
  144. * @access public
  145. * @return string
  146. */
  147. public function getFrozenHtml()
  148. {
  149. if ($this->getChecked()) {
  150. return '<code>(x)</code>' .
  151. $this->_getPersistantData();
  152. } else {
  153. return '<code>( )</code>';
  154. }
  155. }
  156. /**
  157. * Sets the radio text
  158. *
  159. * @param string $text Text to display near the radio button
  160. * @since 1.1
  161. * @access public
  162. * @return void
  163. */
  164. public function setText($text)
  165. {
  166. $this->_text = $text;
  167. }
  168. /**
  169. * Returns the radio text
  170. *
  171. * @since 1.1
  172. * @access public
  173. * @return string
  174. */
  175. public function getText()
  176. {
  177. return $this->_text;
  178. }
  179. /**
  180. * Called by HTML_QuickForm whenever form event is made on this element
  181. *
  182. * @param string $event Name of event
  183. * @param mixed $arg event arguments
  184. * @param object &$caller calling object
  185. * @since 1.0
  186. * @access public
  187. * @return void
  188. */
  189. public function onQuickFormEvent($event, $arg, &$caller)
  190. {
  191. switch ($event) {
  192. case 'updateValue':
  193. // constant values override both default and submitted ones
  194. // default values are overriden by submitted
  195. $value = $this->_findValue($caller->_constantValues);
  196. if (null === $value) {
  197. $value = $this->_findValue($caller->_submitValues);
  198. if (null === $value) {
  199. $value = $this->_findValue($caller->_defaultValues);
  200. }
  201. }
  202. if (!is_null($value) && $value == $this->getValue()) {
  203. $this->setChecked(true);
  204. } else {
  205. $this->setChecked(false);
  206. }
  207. break;
  208. case 'setGroupValue':
  209. if ($arg == $this->getValue()) {
  210. $this->setChecked(true);
  211. } else {
  212. $this->setChecked(false);
  213. }
  214. break;
  215. default:
  216. parent::onQuickFormEvent($event, $arg, $caller);
  217. }
  218. return true;
  219. }
  220. /**
  221. * Returns the value attribute if the radio is checked, null if it is not
  222. */
  223. public function exportValue(&$submitValues, $assoc = false)
  224. {
  225. $value = $this->_findValue($submitValues);
  226. if (null === $value) {
  227. $value = $this->getChecked()? $this->getValue(): null;
  228. } elseif ($value != $this->getValue()) {
  229. $value = null;
  230. }
  231. return $this->_prepareValue($value, $assoc);
  232. }
  233. /**
  234. * @return null
  235. */
  236. public function getColumnsSize()
  237. {
  238. return $this->columnsSize;
  239. }
  240. }