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.15 2005/06/24 17:58:29 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' .
  200.                     $this->_getAttrString(array(
  201.                         'type'  => 'hidden'
  202.                         'name'  => $oldName
  203.                         'value' => $this->getValue()
  204.                     )) ' />';
  205.             // revert the name and JS, in case this method will be called once more
  206.             $this->updateAttributes(array(
  207.                 'name'    => $oldName
  208.                 'onclick' => $oldJs
  209.             ));
  210.             return $html;
  211.         }
  212.     //end func toHtml
  213.     
  214.     // }}}
  215.     // {{{ getFrozenHtml()
  216.  
  217.    /**
  218.     * Unlike checkbox, this has to append a hidden input in both
  219.     * checked and non-checked states
  220.     */
  221.     function getFrozenHtml()
  222.     {
  223.         return ($this->getChecked()'<tt>[x]</tt>''<tt>[ ]</tt>'.
  224.                $this->_getPersistantData();
  225.     }
  226.  
  227.     // }}}
  228.     // {{{ onQuickFormEvent()
  229.  
  230.     /**
  231.      * Called by HTML_QuickForm whenever form event is made on this element
  232.      *
  233.      * @param     string    $event  Name of event
  234.      * @param     mixed     $arg    event arguments
  235.      * @param     object    $caller calling object
  236.      * @since     1.0
  237.      * @access    public
  238.      * @return    void 
  239.      */
  240.     function onQuickFormEvent($event$arg&$caller)
  241.     {
  242.         switch ($event{
  243.             case 'updateValue':
  244.                 // constant values override both default and submitted ones
  245.                 // default values are overriden by submitted
  246.                 $value $this->_findValue($caller->_constantValues);
  247.                 if (null === $value{
  248.                     $value $this->_findValue($caller->_submitValues);
  249.                     if (null === $value{
  250.                         $value $this->_findValue($caller->_defaultValues);
  251.                     }
  252.                 }
  253.                 if (null !== $value{
  254.                     $this->setValue($value);
  255.                 }
  256.                 break;
  257.             default:
  258.                 parent::onQuickFormEvent($event$arg$caller);
  259.         }
  260.         return true;
  261.     // end func onQuickFormLoad
  262.  
  263.     // }}}
  264.     // {{{ exportValue()
  265.  
  266.    /**
  267.     * This element has a value even if it is not checked, thus we override
  268.     * checkbox's behaviour here
  269.     */
  270.     function exportValue(&$submitValues$assoc)
  271.     {
  272.         $value $this->_findValue($submitValues);
  273.         if (null === $value{
  274.             $value $this->getValue();
  275.         elseif (is_array($this->_values&& ($value != $this->_values[0]&& ($value != $this->_values[1])) {
  276.             $value = null;
  277.         }
  278.         return $this->_prepareValue($value$assoc);
  279.     }
  280.     // }}}
  281. //end class HTML_QuickForm_advcheckbox
  282. ?>

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