HTML_QuickForm
[ class tree: HTML_QuickForm ] [ index: HTML_QuickForm ] [ all elements ]

Source for file advcheckbox.php

Documentation is available at advcheckbox.php

  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: advcheckbox.php,v 1.16 2006/06/20 16:33:06 avb Exp $
  21.  
  22. require_once('HTML/QuickForm/checkbox.php');
  23.  
  24. /**
  25.  * HTML class for an advanced checkbox type field
  26.  *
  27.  * Basically this fixes a problem that HTML has had
  28.  * where checkboxes can only pass a single value (the
  29.  * value of the checkbox when checked).  A value for when
  30.  * the checkbox is not checked cannot be passed, and
  31.  * furthermore the checkbox variable doesn't even exist if
  32.  * the checkbox was submitted unchecked.
  33.  *
  34.  * It works by prepending a hidden field with the same name and
  35.  * another "unchecked" value to the checbox. If the checkbox is
  36.  * checked, PHP overwrites the value of the hidden field with
  37.  * its value.
  38.  * 
  39.  * @author       Jason Rust <jrust@php.net>
  40.  * @since        2.0
  41.  * @access       public
  42.  */
  43. {
  44.     // {{{ properties
  45.  
  46.     /**
  47.      * The values passed by the hidden elment
  48.      *
  49.      * @var array 
  50.      * @access private
  51.      */
  52.     var $_values = null;
  53.  
  54.     /**
  55.      * The default value
  56.      *
  57.      * @var boolean 
  58.      * @access private
  59.      */
  60.     var $_currentValue = null;
  61.  
  62.     // }}}
  63.     // {{{ constructor
  64.  
  65.     /**
  66.      * Class constructor
  67.      * 
  68.      * @param     string    $elementName    (optional)Input field name attribute
  69.      * @param     string    $elementLabel   (optional)Input field label
  70.      * @param     string    $text           (optional)Text to put after the checkbox
  71.      * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  72.      *                                       or an associative array
  73.      * @param     mixed     $values         (optional)Values to pass if checked or not checked
  74.      *
  75.      * @since     1.0
  76.      * @access    public
  77.      * @return    void 
  78.      */
  79.     function HTML_QuickForm_advcheckbox($elementName=null$elementLabel=null$text=null$attributes=null$values=null)
  80.     {
  81.         $this->HTML_QuickForm_checkbox($elementName$elementLabel$text$attributes);
  82.         $this->setValues($values);
  83.     //end constructor
  84.     
  85.     // }}}
  86.     // {{{ getPrivateName()
  87.  
  88.     /**
  89.      * Gets the private name for the element
  90.      *
  91.      * @param   string  $elementName The element name to make private
  92.      *
  93.      * @access public
  94.      * @return string 
  95.      *
  96.      * @deprecated          Deprecated since 3.2.6, both generated elements have the same name
  97.      */
  98.     function getPrivateName($elementName)
  99.     {
  100.         return '__'.$elementName;
  101.     }
  102.  
  103.     // }}}
  104.     // {{{ getOnclickJs()
  105.  
  106.     /**
  107.      * Create the javascript for the onclick event which will
  108.      * set the value of the hidden field
  109.      *
  110.      * @param     string    $elementName    The element name
  111.      *
  112.      * @access public
  113.      * @return string 
  114.      *
  115.      * @deprecated          Deprecated since 3.2.6, this element no longer uses any javascript
  116.      */
  117.     function getOnclickJs($elementName)
  118.     {
  119.         $onclickJs 'if (this.checked) { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[1]'\'').'\'; }';
  120.         $onclickJs .= 'else { this.form[\''.$elementName.'\'].value=\''.addcslashes($this->_values[0]'\'').'\'; }';
  121.         return $onclickJs;
  122.     }
  123.  
  124.     // }}}
  125.     // {{{ setValues()
  126.  
  127.     /**
  128.      * Sets the values used by the hidden element
  129.      *
  130.      * @param   mixed   $values The values, either a string or an array
  131.      *
  132.      * @access public
  133.      * @return void 
  134.      */
  135.     function setValues($values)
  136.     {
  137.         if (empty($values)) {
  138.             // give it default checkbox behavior
  139.             $this->_values = array(''1);
  140.         elseif (is_scalar($values)) {
  141.             // if it's string, then assume the value to 
  142.             // be passed is for when the element is checked
  143.             $this->_values = array(''$values);
  144.         else {
  145.             $this->_values $values;
  146.         }
  147.         $this->updateAttributes(array('value' => $this->_values[1]));
  148.         $this->setChecked($this->_currentValue == $this->_values[1]);
  149.     }
  150.  
  151.     // }}}
  152.     // {{{ setValue()
  153.  
  154.    /**
  155.     * Sets the element's value
  156.     * 
  157.     * @param    mixed   Element's value
  158.     * @access   public
  159.     */
  160.     function setValue($value)
  161.     {
  162.         $this->setChecked(isset($this->_values[1]&& $value == $this->_values[1]);
  163.         $this->_currentValue $value;
  164.     }
  165.  
  166.     // }}}
  167.     // {{{ getValue()
  168.  
  169.    /**
  170.     * Returns the element's value
  171.     *
  172.     * @access   public
  173.     * @return   mixed 
  174.     */
  175.     function getValue()
  176.     {
  177.         if (is_array($this->_values)) {
  178.             return $this->_values[$this->getChecked()? 1: 0];
  179.         else {
  180.             return null;
  181.         }
  182.     }
  183.  
  184.     // }}}
  185.     // {{{ toHtml()
  186.  
  187.     /**
  188.      * Returns the checkbox element in HTML
  189.      * and the additional hidden element in HTML
  190.      * 
  191.      * @access    public
  192.      * @return    string 
  193.      */
  194.     function toHtml()
  195.     {
  196.         if ($this->_flagFrozen{
  197.             return parent::toHtml();
  198.         else {
  199.             return '<input' $this->_getAttrString(array(
  200.                         'type'  => 'hidden'
  201.                         'name'  => $this->getName()
  202.                         'value' => $this->_values[0]
  203.                    )) ' />' . parent::toHtml();
  204.             
  205.         }
  206.     //end func toHtml
  207.     
  208.     // }}}
  209.     // {{{ getFrozenHtml()
  210.  
  211.    /**
  212.     * Unlike checkbox, this has to append a hidden input in both
  213.     * checked and non-checked states
  214.     */
  215.     function getFrozenHtml()
  216.     {
  217.         return ($this->getChecked()'<tt>[x]</tt>''<tt>[ ]</tt>'.
  218.                $this->_getPersistantData();
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ onQuickFormEvent()
  223.  
  224.     /**
  225.      * Called by HTML_QuickForm whenever form event is made on this element
  226.      *
  227.      * @param     string    $event  Name of event
  228.      * @param     mixed     $arg    event arguments
  229.      * @param     object    $caller calling object
  230.      * @since     1.0
  231.      * @access    public
  232.      * @return    void 
  233.      */
  234.     function onQuickFormEvent($event$arg&$caller)
  235.     {
  236.         switch ($event{
  237.             case 'updateValue':
  238.                 // constant values override both default and submitted ones
  239.                 // default values are overriden by submitted
  240.                 $value $this->_findValue($caller->_constantValues);
  241.                 if (null === $value{
  242.                     $value $this->_findValue($caller->_submitValues);
  243.                     if (null === $value{
  244.                         $value $this->_findValue($caller->_defaultValues);
  245.                     }
  246.                 }
  247.                 if (null !== $value{
  248.                     $this->setValue($value);
  249.                 }
  250.                 break;
  251.             default:
  252.                 parent::onQuickFormEvent($event$arg$caller);
  253.         }
  254.         return true;
  255.     // end func onQuickFormLoad
  256.  
  257.     // }}}
  258.     // {{{ exportValue()
  259.  
  260.    /**
  261.     * This element has a value even if it is not checked, thus we override
  262.     * checkbox's behaviour here
  263.     */
  264.     function exportValue(&$submitValues$assoc)
  265.     {
  266.         $value $this->_findValue($submitValues);
  267.         if (null === $value{
  268.             $value $this->getValue();
  269.         elseif (is_array($this->_values&& ($value != $this->_values[0]&& ($value != $this->_values[1])) {
  270.             $value = null;
  271.         }
  272.         return $this->_prepareValue($value$assoc);
  273.     }
  274.     // }}}
  275. //end class HTML_QuickForm_advcheckbox
  276. ?>

Documentation generated on Mon, 11 Mar 2019 14:41:19 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.