[ Index ]
 

Code source de phpMyVisites 2.3

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/libs/Html/QuickForm/ -> checkbox.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  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
  17  // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18  // +----------------------------------------------------------------------+
  19  //
  20  // $Id: checkbox.php,v 1.1 2005/12/06 01:50:39 matthieu_ Exp $
  21  
  22  require_once("Html/QuickForm/input.php");
  23  
  24  /**
  25   * HTML class for a checkbox type field
  26   * 
  27   * @author       Adam Daniel <adaniel1@eesus.jnj.com>
  28   * @author       Bertrand Mansion <bmansion@mamasam.com>
  29   * @version      1.1
  30   * @since        PHP4.04pl1
  31   * @access       public
  32   */
  33  class HTML_QuickForm_checkbox extends HTML_QuickForm_input
  34  {
  35      // {{{ properties
  36  
  37      /**
  38       * Checkbox 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    $elementName    (optional)Input field name attribute
  52       * @param     string    $elementLabel   (optional)Input field value
  53       * @param     string    $text           (optional)Checkbox display text
  54       * @param     mixed     $attributes     (optional)Either a typical HTML attribute string 
  55       *                                      or an associative array
  56       * @since     1.0
  57       * @access    public
  58       * @return    void
  59       */
  60      function HTML_QuickForm_checkbox($elementName=null, $elementLabel=null, $text='', $attributes=null)
  61      {
  62          HTML_QuickForm_input::HTML_QuickForm_input($elementName, $elementLabel, $attributes);
  63          $this->_persistantFreeze = true;
  64          $this->_text = $text;
  65          $this->setType('checkbox');
  66          $this->updateAttributes(array('value'=>1));
  67          $this->_generateId();
  68      } //end constructor
  69      
  70      // }}}
  71      // {{{ setChecked()
  72  
  73      /**
  74       * Sets whether a checkbox is checked
  75       * 
  76       * @param     bool    $checked  Whether the field is checked or not
  77       * @since     1.0
  78       * @access    public
  79       * @return    void
  80       */
  81      function setChecked($checked)
  82      {
  83          if (!$checked) {
  84              $this->removeAttribute('checked');
  85          } else {
  86              $this->updateAttributes(array('checked'=>'checked'));
  87          }
  88      } //end func setChecked
  89  
  90      // }}}
  91      // {{{ getChecked()
  92  
  93      /**
  94       * Returns whether a checkbox is checked
  95       * 
  96       * @since     1.0
  97       * @access    public
  98       * @return    bool
  99       */
 100      function getChecked()
 101      {
 102          return (bool)$this->getAttribute('checked');
 103      } //end func getChecked
 104      
 105      // }}}
 106      // {{{ toHtml()
 107  
 108      /**
 109       * Returns the checkbox element in HTML
 110       * 
 111       * @since     1.0
 112       * @access    public
 113       * @return    string
 114       */
 115      function toHtml()
 116      {
 117          if (0 == strlen($this->_text)) {
 118              $label = '';
 119          } elseif ($this->_flagFrozen) {
 120              $label = $this->_text;
 121          } else {
 122              $label = '<label for="' . $this->getAttribute('id') . '">' . $this->_text . '</label>';
 123          }
 124          return HTML_QuickForm_input::toHtml() . $label;
 125      } //end func toHtml
 126      
 127      // }}}
 128      // {{{ getFrozenHtml()
 129  
 130      /**
 131       * Returns the value of field without HTML tags
 132       * 
 133       * @since     1.0
 134       * @access    public
 135       * @return    string
 136       */
 137      function getFrozenHtml()
 138      {
 139          if ($this->getChecked()) {
 140              return '<tt>[x]</tt>' .
 141                     $this->_getPersistantData();
 142          } else {
 143              return '<tt>[ ]</tt>';
 144          }
 145      } //end func getFrozenHtml
 146  
 147      // }}}
 148      // {{{ setText()
 149  
 150      /**
 151       * Sets the checkbox text
 152       * 
 153       * @param     string    $text  
 154       * @since     1.1
 155       * @access    public
 156       * @return    void
 157       */
 158      function setText($text)
 159      {
 160          $this->_text = $text;
 161      } //end func setText
 162  
 163      // }}}
 164      // {{{ getText()
 165  
 166      /**
 167       * Returns the checkbox text 
 168       * 
 169       * @since     1.1
 170       * @access    public
 171       * @return    string
 172       */
 173      function getText()
 174      {
 175          return $this->_text;
 176      } //end func getText
 177  
 178      // }}}
 179      // {{{ setValue()
 180  
 181      /**
 182       * Sets the value of the form element
 183       *
 184       * @param     string    $value      Default value of the form element
 185       * @since     1.0
 186       * @access    public
 187       * @return    void
 188       */
 189      function setValue($value)
 190      {
 191          return $this->setChecked($value);
 192      } // end func setValue
 193  
 194      // }}}
 195      // {{{ getValue()
 196  
 197      /**
 198       * Returns the value of the form element
 199       *
 200       * @since     1.0
 201       * @access    public
 202       * @return    bool
 203       */
 204      function getValue()
 205      {
 206          return $this->getChecked();
 207      } // end func getValue
 208  
 209      // }}}
 210      // {{{ onQuickFormEvent()
 211  
 212      /**
 213       * Called by HTML_QuickForm whenever form event is made on this element
 214       *
 215       * @param     string    $event  Name of event
 216       * @param     mixed     $arg    event arguments
 217       * @param     object    $caller calling object
 218       * @since     1.0
 219       * @access    public
 220       * @return    void
 221       */
 222      function onQuickFormEvent($event, $arg, &$caller)
 223      {
 224          switch ($event) {
 225              case 'updateValue':
 226                  // constant values override both default and submitted ones
 227                  // default values are overriden by submitted
 228                  $value = $this->_findValue($caller->_constantValues);
 229                  if (null === $value) {
 230                      // if no boxes were checked, then there is no value in the array
 231                      // yet we don't want to display default value in this case
 232                      if ($caller->isSubmitted()) {
 233                          $value = $this->_findValue($caller->_submitValues);
 234                      } else {
 235                          $value = $this->_findValue($caller->_defaultValues);
 236                      }
 237                  }
 238                  if (null !== $value) {
 239                      $this->setChecked($value);
 240                  }
 241                  break;
 242              case 'setGroupValue':
 243                  $this->setChecked($arg);
 244                  break;
 245              default:
 246                  parent::onQuickFormEvent($event, $arg, $caller);
 247          }
 248          return true;
 249      } // end func onQuickFormEvent
 250  
 251      // }}}
 252      // {{{ exportValue()
 253  
 254     /**
 255      * Return true if the checkbox is checked, null if it is not checked (getValue() returns false)
 256      */
 257      function exportValue(&$submitValues, $assoc = false)
 258      {
 259          $value = $this->_findValue($submitValues);
 260          if (null === $value) {
 261              $value = $this->getChecked()? true: null;
 262          }
 263          return $this->_prepareValue($value, $assoc);
 264      }
 265      
 266      // }}}
 267  } //end class HTML_QuickForm_checkbox
 268  ?>


Généré le : Mon Nov 26 14:10:01 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics