2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
 
   5  * HTML class for a radio type element
 
   9  * LICENSE: This source file is subject to version 3.01 of the PHP license
 
  10  * that is available through the world-wide-web at the following URI:
 
  11  * http://www.php.net/license/3_01.txt If you did not receive a copy of
 
  12  * the PHP License and are unable to obtain it through the web, please
 
  13  * send a note to license@php.net so we can mail you a copy immediately.
 
  16  * @package     HTML_QuickForm
 
  17  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
 
  18  * @author      Bertrand Mansion <bmansion@mamasam.com>
 
  19  * @copyright   2001-2011 The PHP Group
 
  20  * @license     http://www.php.net/license/3_01.txt PHP License 3.01
 
  22  * @link        http://pear.php.net/package/HTML_QuickForm
 
  26  * Base class for <input /> form elements
 
  28 require_once 'HTML/QuickForm/input.php';
 
  31  * HTML class for a radio type element
 
  34  * @package     HTML_QuickForm
 
  35  * @author      Adam Daniel <adaniel1@eesus.jnj.com>
 
  36  * @author      Bertrand Mansion <bmansion@mamasam.com>
 
  37  * @version     Release: 3.2.16
 
  40 class HTML_QuickForm_radio extends HTML_QuickForm_input
 
  58      * @param     string    Input field name attribute
 
  59      * @param     mixed     Label(s) for a field
 
  60      * @param     string    Text to display near the radio
 
  61      * @param     string    Input field value
 
  62      * @param     mixed     Either a typical HTML attribute string or an associative array
 
  67     function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
 
  69         $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
 
  71             $this->setValue($value);
 
  73         $this->_persistantFreeze = true;
 
  74         $this->setType('radio');
 
  83      * Sets whether radio button is checked
 
  85      * @param     bool    $checked  Whether the field is checked or not
 
  90     function setChecked($checked)
 
  93             $this->removeAttribute('checked');
 
  95             $this->updateAttributes(array('checked'=>'checked'));
 
  97     } //end func setChecked
 
 103      * Returns whether radio button is checked
 
 109     function getChecked()
 
 111         return $this->getAttribute('checked');
 
 112     } //end func getChecked
 
 118      * Returns the radio element in HTML
 
 126         if (0 == strlen($this->_text)) {
 
 128         } elseif ($this->_flagFrozen) {
 
 129             $label = $this->_text;
 
 131             $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
 
 133         return HTML_QuickForm_input::toHtml() . $label;
 
 137     // {{{ getFrozenHtml()
 
 140      * Returns the value of field without HTML tags
 
 146     function getFrozenHtml()
 
 148         if ($this->getChecked()) {
 
 149             return '<tt>(x)</tt>' .
 
 150                    $this->_getPersistantData();
 
 152             return '<tt>( )</tt>';
 
 154     } //end func getFrozenHtml
 
 160      * Sets the radio text
 
 162      * @param     string    $text  Text to display near the radio button
 
 167     function setText($text)
 
 169         $this->_text = $text;
 
 176      * Returns the radio text 
 
 188     // {{{ onQuickFormEvent()
 
 191      * Called by HTML_QuickForm whenever form event is made on this element
 
 193      * @param     string    $event  Name of event
 
 194      * @param     mixed     $arg    event arguments
 
 195      * @param     object    &$caller calling object
 
 200     function onQuickFormEvent($event, $arg, &$caller)
 
 204                 // constant values override both default and submitted ones
 
 205                 // default values are overriden by submitted
 
 206                 $value = $this->_findValue($caller->_constantValues);
 
 207                 if (null === $value) {
 
 208                     $value = $this->_findValue($caller->_submitValues);
 
 209                     if (null === $value) {
 
 210                         $value = $this->_findValue($caller->_defaultValues);
 
 213                 if (!is_null($value) && $value == $this->getValue()) {
 
 214                     $this->setChecked(true);
 
 216                     $this->setChecked(false);
 
 219             case 'setGroupValue':
 
 220                 if ($arg == $this->getValue()) {
 
 221                     $this->setChecked(true);
 
 223                     $this->setChecked(false);
 
 227                 parent::onQuickFormEvent($event, $arg, $caller);
 
 230     } // end func onQuickFormLoad
 
 236     * Returns the value attribute if the radio is checked, null if it is not
 
 238     function exportValue(&$submitValues, $assoc = false)
 
 240         $value = $this->_findValue($submitValues);
 
 241         if (null === $value) {
 
 242             $value = $this->getChecked()? $this->getValue(): null;
 
 243         } elseif ($value != $this->getValue()) {
 
 246         return $this->_prepareValue($value, $assoc);
 
 250 } //end class HTML_QuickForm_radio