home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / HTML / QuickForm / checkbox.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.6 KB  |  278 lines

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