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

Source for file checkbox.php

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

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