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

Documentation generated on Mon, 11 Mar 2019 13:52:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.