[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/pear/HTML/QuickForm/ -> radio.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4: */
   3  // +----------------------------------------------------------------------+
   4  // | PHP version 4.0                                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available at through the world-wide-web at                           |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | [email protected] so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Authors: Adam Daniel <[email protected]>                        |
  17  // |          Bertrand Mansion <[email protected]>                     |
  18  // +----------------------------------------------------------------------+
  19  //
  20  // $Id$
  21  
  22  require_once('HTML/QuickForm/input.php');
  23  
  24  /**
  25   * HTML class for a radio type element
  26   * 
  27   * @author       Adam Daniel <[email protected]>
  28   * @author       Bertrand Mansion <[email protected]>
  29   * @version      1.1
  30   * @since        PHP4.04pl1
  31   * @access       public
  32   */
  33  class HTML_QuickForm_radio extends HTML_QuickForm_input
  34  {
  35      // {{{ properties
  36  
  37      /**
  38       * Radio display text
  39       * @var       string
  40       * @since     1.1
  41       * @access    private
  42       */
  43      var $_text = '';
  44  
  45      // }}}
  46      // {{{ constructor
  47  
  48      /**
  49       * Class constructor
  50       * 
  51       * @param     string    Input field name attribute
  52       * @param     mixed     Label(s) for a field
  53       * @param     string    Text to display near the radio
  54       * @param     string    Input field value
  55       * @param     mixed     Either a typical HTML attribute string or an associative array
  56       * @since     1.0
  57       * @access    public
  58       * @return    void
  59       */
  60      function HTML_QuickForm_radio($elementName=null, $elementLabel=null, $text=null, $value=null, $attributes=null)
  61      {
  62          $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
  63          if (isset($value)) {
  64              $this->setValue($value);
  65          }
  66          $this->_persistantFreeze = true;
  67          $this->setType('radio');
  68          $this->_text = $text;
  69      } //end constructor
  70  
  71      // }}}
  72  
  73      function _generateId() {
  74          // Override the standard implementation, since you can have multiple
  75          // check-boxes with the same name on a form. Therefore, add the
  76          // (cleaned up) value to the id.
  77  
  78          if ($this->getAttribute('id')) {
  79              return;
  80          }
  81  
  82          parent::_generateId();
  83          $id = $this->getAttribute('id') . '_' . clean_param($this->getValue(), PARAM_ALPHANUMEXT);
  84          $this->updateAttributes(array('id' => $id));
  85      }
  86  
  87      // {{{ setChecked()
  88  
  89      /**
  90       * Sets whether radio button is checked
  91       * 
  92       * @param     bool    $checked  Whether the field is checked or not
  93       * @since     1.0
  94       * @access    public
  95       * @return    void
  96       */
  97      function setChecked($checked)
  98      {
  99          if (!$checked) {
 100              $this->removeAttribute('checked');
 101          } else {
 102              $this->updateAttributes(array('checked'=>'checked'));
 103          }
 104      } //end func setChecked
 105  
 106      // }}}
 107      // {{{ getChecked()
 108  
 109      /**
 110       * Returns whether radio button is checked
 111       * 
 112       * @since     1.0
 113       * @access    public
 114       * @return    string
 115       */
 116      function getChecked()
 117      {
 118          return $this->getAttribute('checked');
 119      } //end func getChecked
 120          
 121      // }}}
 122      // {{{ toHtml()
 123  
 124      /**
 125       * Returns the radio element in HTML
 126       * 
 127       * @since     1.0
 128       * @access    public
 129       * @return    string
 130       */
 131      function toHtml()
 132      {
 133          if (0 == strlen($this->_text)) {
 134              $label = '';
 135          } elseif ($this->_flagFrozen) {
 136              $label = $this->_text;
 137          } else {
 138              $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
 139          }
 140          return HTML_QuickForm_input::toHtml() . $label;
 141      } //end func toHtml
 142      
 143      // }}}
 144      // {{{ getFrozenHtml()
 145  
 146      /**
 147       * Returns the value of field without HTML tags
 148       * 
 149       * @since     1.0
 150       * @access    public
 151       * @return    string
 152       */
 153      function getFrozenHtml()
 154      {
 155          if ($this->getChecked()) {
 156              return '<tt>(x)</tt>' .
 157                     $this->_getPersistantData();
 158          } else {
 159              return '<tt>( )</tt>';
 160          }
 161      } //end func getFrozenHtml
 162  
 163      // }}}
 164      // {{{ setText()
 165  
 166      /**
 167       * Sets the radio text
 168       * 
 169       * @param     string    $text  Text to display near the radio button
 170       * @since     1.1
 171       * @access    public
 172       * @return    void
 173       */
 174      function setText($text)
 175      {
 176          $this->_text = $text;
 177      } //end func setText
 178  
 179      // }}}
 180      // {{{ getText()
 181  
 182      /**
 183       * Returns the radio text 
 184       * 
 185       * @since     1.1
 186       * @access    public
 187       * @return    string
 188       */
 189      function getText()
 190      {
 191          return $this->_text;
 192      } //end func getText
 193  
 194      // }}}
 195      // {{{ onQuickFormEvent()
 196  
 197      /**
 198       * Called by HTML_QuickForm whenever form event is made on this element
 199       *
 200       * @param     string    $event  Name of event
 201       * @param     mixed     $arg    event arguments
 202       * @param     object    $caller calling object
 203       * @since     1.0
 204       * @access    public
 205       * @return    void
 206       */
 207      function onQuickFormEvent($event, $arg, &$caller)
 208      {
 209          switch ($event) {
 210              case 'updateValue':
 211                  // constant values override both default and submitted ones
 212                  // default values are overriden by submitted
 213                  $value = $this->_findValue($caller->_constantValues);
 214                  if (null === $value) {
 215                      $value = $this->_findValue($caller->_submitValues);
 216                      if (null === $value) {
 217                          $value = $this->_findValue($caller->_defaultValues);
 218                      }
 219                  }
 220                  if ($value == $this->getValue()) {
 221                      $this->setChecked(true);
 222                  } else {
 223                      $this->setChecked(false);
 224                  }
 225                  break;
 226              case 'setGroupValue':
 227                  if ($arg == $this->getValue()) {
 228                      $this->setChecked(true);
 229                  } else {
 230                      $this->setChecked(false);
 231                  }
 232                  break;
 233              default:
 234                  parent::onQuickFormEvent($event, $arg, $caller);
 235          }
 236          return true;
 237      } // end func onQuickFormLoad
 238  
 239      // }}}
 240      // {{{ exportValue()
 241  
 242     /**
 243      * Returns the value attribute if the radio is checked, null if it is not
 244      */
 245      function exportValue(&$submitValues, $assoc = false)
 246      {
 247          $value = $this->_findValue($submitValues);
 248          if (null === $value) {
 249              $value = $this->getChecked()? $this->getValue(): null;
 250          } elseif ($value != $this->getValue()) {
 251              $value = null;
 252          }
 253          return $this->_prepareValue($value, $assoc);
 254      }
 255      
 256      // }}}
 257  } //end class HTML_QuickForm_radio
 258  ?>


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1