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

Source for file group.php

Documentation is available at group.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: group.php,v 1.33 2004/02/08 16:01:28 mansion Exp $
  21.  
  22. require_once("HTML/QuickForm/element.php");
  23.  
  24. /**
  25.  * HTML class for a form element group
  26.  * 
  27.  * @author       Adam Daniel <adaniel1@eesus.jnj.com>
  28.  * @author       Bertrand Mansion <bmansion@mamasam.com>
  29.  * @version      1.0
  30.  * @since        PHP4.04pl1
  31.  * @access       public
  32.  */
  33. {
  34.     // {{{ properties
  35.         
  36.     /**
  37.      * Name of the element
  38.      * @var       string 
  39.      * @since     1.0
  40.      * @access    private
  41.      */
  42.     var $_name '';
  43.  
  44.     /**
  45.      * Array of grouped elements
  46.      * @var       array 
  47.      * @since     1.0
  48.      * @access    private
  49.      */
  50.     var $_elements = array();
  51.  
  52.     /**
  53.      * String to separate elements
  54.      * @var       mixed 
  55.      * @since     2.5
  56.      * @access    private
  57.      */
  58.     var $_separator = null;
  59.  
  60.     /**
  61.      * Required elements in this group
  62.      * @var       array 
  63.      * @since     2.5
  64.      * @access    private
  65.      */
  66.     var $_required = array();
  67.  
  68.    /**
  69.     * Whether to change elements' names to $groupName[$elementName] or leave them as is
  70.     * @var      bool 
  71.     * @since    3.0
  72.     * @access   private
  73.     */
  74.     var $_appendName = true;
  75.  
  76.     // }}}
  77.     // {{{ constructor
  78.  
  79.     /**
  80.      * Class constructor
  81.      * 
  82.      * @param     string    $elementName    (optional)Group name
  83.      * @param     array     $elementLabel   (optional)Group label
  84.      * @param     array     $elements       (optional)Group elements
  85.      * @param     mixed     $separator      (optional)Use a string for one separator,
  86.      *                                       use an array to alternate the separators.
  87.      * @param     bool      $appendName     (optional)whether to change elements' names to
  88.      *                                       the form $groupName[$elementName] or leave
  89.      *                                       them as is.
  90.      * @since     1.0
  91.      * @access    public
  92.      * @return    void 
  93.      */
  94.     function HTML_QuickForm_group($elementName=null$elementLabel=null$elements=null$separator=null$appendName = true)
  95.     {
  96.         $this->HTML_QuickForm_element($elementName$elementLabel);
  97.         $this->_type 'group';
  98.         if (isset($elements&& is_array($elements)) {
  99.             $this->setElements($elements);
  100.         }
  101.         if (isset($separator)) {
  102.             $this->_separator $separator;
  103.         }
  104.         if (isset($appendName)) {
  105.             $this->_appendName $appendName;
  106.         }
  107.     //end constructor
  108.     
  109.     // }}}
  110.     // {{{ setName()
  111.  
  112.     /**
  113.      * Sets the group name
  114.      * 
  115.      * @param     string    $name   Group name
  116.      * @since     1.0
  117.      * @access    public
  118.      * @return    void 
  119.      */
  120.     function setName($name)
  121.     {
  122.         $this->_name $name;
  123.     //end func setName
  124.     
  125.     // }}}
  126.     // {{{ getName()
  127.  
  128.     /**
  129.      * Returns the group name
  130.      * 
  131.      * @since     1.0
  132.      * @access    public
  133.      * @return    string 
  134.      */
  135.     function getName()
  136.     {
  137.         return $this->_name;
  138.     //end func getName
  139.  
  140.     // }}}
  141.     // {{{ setValue()
  142.  
  143.     /**
  144.      * Sets values for group's elements
  145.      * 
  146.      * @param     mixed    Values for group's elements
  147.      * @since     1.0
  148.      * @access    public
  149.      * @return    void 
  150.      */
  151.     function setValue($value)
  152.     {
  153.         if (empty($this->_elements)) {
  154.             $this->_createElements();
  155.         }
  156.         foreach (array_keys($this->_elementsas $key{
  157.             if (!$this->_appendName{
  158.                 $v $this->_elements[$key]->_findValue($value);
  159.                 if (null !== $v{
  160.                     $this->_elements[$key]->onQuickFormEvent('setGroupValue'$v$this);
  161.                 }
  162.  
  163.             else {
  164.                 $elementName $this->_elements[$key]->getName();
  165.                 $index       (!empty($elementName)) $elementName $key;
  166.                 if (is_array($value)) {
  167.                     if (isset($value[$index])) {
  168.                         $this->_elements[$key]->onQuickFormEvent('setGroupValue'$value[$index]$this);
  169.                     }
  170.                 elseif (isset($value)) {
  171.                     $this->_elements[$key]->onQuickFormEvent('setGroupValue'$value$this);
  172.                 }
  173.             }
  174.         }
  175.     //end func setValue
  176.     
  177.     // }}}
  178.     // {{{ getValue()
  179.  
  180.     /**
  181.      * Returns the value of the group
  182.      *
  183.      * @since     1.0
  184.      * @access    public
  185.      * @return    mixed 
  186.      */
  187.     function getValue()
  188.     {
  189.         $value = null;
  190.         foreach (array_keys($this->_elementsas $key{
  191.             $element =$this->_elements[$key];
  192.             switch ($element->getType()) {
  193.                 case 'radio'
  194.                     $v $element->getChecked()$element->getValue(): null;
  195.                     break;
  196.                 case 'checkbox'
  197.                     $v $element->getChecked()? true: null;
  198.                     break;
  199.                 default:
  200.                     $v $element->getValue();
  201.             }
  202.             if (null !== $v{
  203.                 $elementName $element->getName();
  204.                 if (is_null($elementName)) {
  205.                     $value $v;
  206.                 else {
  207.                     if (!is_array($value)) {
  208.                         $value is_null($value)? array(): array($value);
  209.                     }
  210.                     if ('' == $elementName{
  211.                         $value[$v;
  212.                     else {
  213.                         $value[$elementName$v;
  214.                     }
  215.                 }
  216.             }
  217.         }
  218.         return $value;
  219.     // end func getValue
  220.  
  221.     // }}}
  222.     // {{{ setElements()
  223.  
  224.     /**
  225.      * Sets the grouped elements
  226.      *
  227.      * @param     array     $elements   Array of elements
  228.      * @since     1.1
  229.      * @access    public
  230.      * @return    void 
  231.      */
  232.     function setElements($elements)
  233.     {
  234.         $this->_elements array_values($elements);
  235.     // end func setElements
  236.  
  237.     // }}}
  238.     // {{{ getElements()
  239.  
  240.     /**
  241.      * Gets the grouped elements
  242.      *
  243.      * @since     2.4
  244.      * @access    public
  245.      * @return    array 
  246.      */
  247.     function &getElements()
  248.     {
  249.         return $this->_elements;
  250.     // end func getElements
  251.  
  252.     // }}}
  253.     // {{{ getGroupType()
  254.  
  255.     /**
  256.      * Gets the group type based on its elements
  257.      * Will return 'mixed' if elements contained in the group
  258.      * are of different types.
  259.      *
  260.      * @access    public
  261.      * @return    string    group elements type
  262.      */
  263.     function getGroupType()
  264.     {
  265.         if (empty($this->_elements)) {
  266.             $this->_createElements();
  267.         }
  268.         $prevType '';
  269.         foreach (array_keys($this->_elementsas $key{
  270.             $type $this->_elements[$key]->getType();
  271.             if ($type != $prevType && $prevType != ''{
  272.                 return 'mixed';
  273.             }
  274.             $prevType $type;
  275.         }
  276.         return $type;
  277.     // end func getGroupType
  278.  
  279.     // }}}
  280.     // {{{ toHtml()
  281.  
  282.     /**
  283.      * Returns Html for the group
  284.      * 
  285.      * @since       1.0
  286.      * @access      public
  287.      * @return      string 
  288.      */
  289.     function toHtml()
  290.     {
  291.         include_once('HTML/QuickForm/Renderer/Default.php');
  292.         $renderer =new HTML_QuickForm_Renderer_Default();
  293.         $renderer->setElementTemplate('{element}');
  294.         $this->accept($renderer);
  295.         return $renderer->toHtml();
  296.     //end func toHtml
  297.     
  298.     // }}}
  299.     // {{{ getElementName()
  300.  
  301.     /**
  302.      * Returns the element name inside the group such as found in the html form
  303.      * 
  304.      * @param     mixed     $index  Element name or element index in the group
  305.      * @since     3.0
  306.      * @access    public
  307.      * @return    mixed     string with element name, false if not found
  308.      */
  309.     function getElementName($index)
  310.     {
  311.         if (empty($this->_elements)) {
  312.             $this->_createElements();
  313.         }
  314.         $elementName = false;
  315.         if (is_int($index&& isset($this->_elements[$index])) {
  316.             $elementName $this->_elements[$index]->getName();
  317.             if (isset($elementName&& $elementName == ''{
  318.                 $elementName $index;
  319.             }
  320.             if ($this->_appendName{
  321.                 if (is_null($elementName)) {
  322.                     $elementName $this->getName();
  323.                 else {
  324.                     $elementName $this->getName().'['.$elementName.']';
  325.                 }
  326.             }
  327.  
  328.         elseif (is_string($index)) {
  329.             foreach (array_keys($this->_elementsas $key{
  330.                 $elementName $this->_elements[$key]->getName();
  331.                 if ($index == $elementName{
  332.                     if ($this->_appendName{
  333.                         $elementName $this->getName().'['.$elementName.']';
  334.                     }
  335.                     break;
  336.                 elseif ($this->_appendName && $this->getName().'['.$elementName.']' == $index{
  337.                     break;
  338.                 }
  339.             }
  340.         }
  341.         return $elementName;
  342.     //end func getElementName
  343.  
  344.     // }}}
  345.     // {{{ getFrozenHtml()
  346.  
  347.     /**
  348.      * Returns the value of field without HTML tags
  349.      * 
  350.      * @since     1.3
  351.      * @access    public
  352.      * @return    string 
  353.      */
  354.     function getFrozenHtml()
  355.     {
  356.         $tmp $this->_flagFrozen;
  357.         $this->_flagFrozen = true;
  358.         $html $this->toHtml();
  359.         $this->_flagFrozen $tmp;
  360.         return $html;
  361.     //end func getFrozenHtml
  362.  
  363.     // }}}
  364.     // {{{ onQuickFormEvent()
  365.  
  366.     /**
  367.      * Called by HTML_QuickForm whenever form event is made on this element
  368.      *
  369.      * @param     string    $event  Name of event
  370.      * @param     mixed     $arg    event arguments
  371.      * @param     object    $caller calling object
  372.      * @since     1.0
  373.      * @access    public
  374.      * @return    void 
  375.      */
  376.     function onQuickFormEvent($event$arg&$caller)
  377.     {
  378.         switch ($event{
  379.             case 'updateValue':
  380.                 if (empty($this->_elements)) {
  381.                     $this->_createElements();
  382.                 }
  383.                 foreach (array_keys($this->_elementsas $key{
  384.                     if ($this->_appendName{
  385.                         $elementName $this->_elements[$key]->getName();
  386.                         if (is_null($elementName)) {
  387.                             $this->_elements[$key]->setName($this->getName());
  388.                         elseif ('' == $elementName{
  389.                             $this->_elements[$key]->setName($this->getName('[' $key ']');
  390.                         else {
  391.                             $this->_elements[$key]->setName($this->getName('[' $elementName ']');
  392.                         }
  393.                     }
  394.                     $this->_elements[$key]->onQuickFormEvent('updateValue'$arg$caller);
  395.                     if ($this->_appendName{
  396.                         $this->_elements[$key]->setName($elementName);
  397.                     }
  398.                 }
  399.                 break;
  400.  
  401.             default:
  402.                 parent::onQuickFormEvent($event$arg$caller);
  403.         }
  404.         return true;
  405.     // end func onQuickFormEvent
  406.  
  407.     // }}}
  408.     // {{{ accept()
  409.  
  410.    /**
  411.     * Accepts a renderer
  412.     *
  413.     * @param object     An HTML_QuickForm_Renderer object
  414.     * @param bool       Whether a group is required
  415.     * @param string     An error message associated with a group
  416.     * @access public
  417.     * @return void 
  418.     */
  419.     function accept(&$renderer$required = false$error = null)
  420.     {
  421.         if (empty($this->_elements)) {
  422.             $this->_createElements();
  423.         }
  424.         $renderer->startGroup($this$required$error);
  425.         $name $this->getName();
  426.         foreach (array_keys($this->_elementsas $key{
  427.             $element =$this->_elements[$key];
  428.             
  429.             if ($this->_appendName{
  430.                 $elementName $element->getName();
  431.                 if (isset($elementName)) {
  432.                     $element->setName($name '['(strlen($elementName)$elementName$key.']');
  433.                 else {
  434.                     $element->setName($name);
  435.                 }
  436.             }
  437.  
  438.             if ($this->_flagFrozen{
  439.                 $element->freeze();
  440.             }
  441.             $required !$this->_flagFrozen && in_array($element->getName()$this->_required);
  442.  
  443.             $element->accept($renderer$required);
  444.  
  445.             // restore the element's name
  446.             if ($this->_appendName{
  447.                 $element->setName($elementName);
  448.             }
  449.         }
  450.         $renderer->finishGroup($this);
  451.     // end func accept
  452.  
  453.     // }}}
  454.     // {{{ exportValue()
  455.  
  456.    /**
  457.     * As usual, to get the group's value we access its elements and call
  458.     * their exportValue() methods
  459.     */
  460.     function exportValue(&$submitValues$assoc = false)
  461.     {
  462.         $value = null;
  463.         foreach (array_keys($this->_elementsas $key{
  464.             $elementName $this->_elements[$key]->getName();
  465.             if ($this->_appendName{
  466.                 if (is_null($elementName)) {
  467.                     $this->_elements[$key]->setName($this->getName());
  468.                 elseif ('' == $elementName{
  469.                     $this->_elements[$key]->setName($this->getName('[' $key ']');
  470.                 else {
  471.                     $this->_elements[$key]->setName($this->getName('[' $elementName ']');
  472.                 }
  473.             }
  474.             $v $this->_elements[$key]->exportValue($submitValues$assoc);
  475.             if ($this->_appendName{
  476.                 $this->_elements[$key]->setName($elementName);
  477.             }
  478.             if (null !== $v{
  479.                 // Make $value an array, we will use it like one
  480.                 if (null === $value{
  481.                     $value = array();
  482.                 }
  483.                 if ($assoc{
  484.                     // just like HTML_QuickForm::exportValues()
  485.                     $value HTML_QuickForm::arrayMerge($value$v);
  486.                 else {
  487.                     // just like getValue(), but should work OK every time here
  488.                     if (is_null($elementName)) {
  489.                         $value $v;
  490.                     elseif ('' == $elementName{
  491.                         $value[$v;
  492.                     else {
  493.                         $value[$elementName$v;
  494.                     }
  495.                 }
  496.             }
  497.         }
  498.         // do not pass the value through _prepareValue, we took care of this already
  499.         return $value;
  500.     }
  501.  
  502.     // }}}
  503.     // {{{ _createElements()
  504.  
  505.    /**
  506.     * Creates the group's elements.
  507.     * 
  508.     * This should be overriden by child classes that need to create their
  509.     * elements. The method will be called automatically when needed, calling
  510.     * it from the constructor is discouraged as the constructor is usually
  511.     * called _twice_ on element creation, first time with _no_ parameters.
  512.     * 
  513.     * @access private
  514.     * @abstract
  515.     */
  516.     function _createElements()
  517.     {
  518.         // abstract
  519.     }
  520.  
  521.     // }}}
  522. //end class HTML_QuickForm_group
  523. ?>

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