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.34 2004/10/14 20:00:49 avb 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.         if ($this->_flagFrozen{
  236.             $this->freeze();
  237.         }
  238.     // end func setElements
  239.  
  240.     // }}}
  241.     // {{{ getElements()
  242.  
  243.     /**
  244.      * Gets the grouped elements
  245.      *
  246.      * @since     2.4
  247.      * @access    public
  248.      * @return    array 
  249.      */
  250.     function &getElements()
  251.     {
  252.         return $this->_elements;
  253.     // end func getElements
  254.  
  255.     // }}}
  256.     // {{{ getGroupType()
  257.  
  258.     /**
  259.      * Gets the group type based on its elements
  260.      * Will return 'mixed' if elements contained in the group
  261.      * are of different types.
  262.      *
  263.      * @access    public
  264.      * @return    string    group elements type
  265.      */
  266.     function getGroupType()
  267.     {
  268.         if (empty($this->_elements)) {
  269.             $this->_createElements();
  270.         }
  271.         $prevType '';
  272.         foreach (array_keys($this->_elementsas $key{
  273.             $type $this->_elements[$key]->getType();
  274.             if ($type != $prevType && $prevType != ''{
  275.                 return 'mixed';
  276.             }
  277.             $prevType $type;
  278.         }
  279.         return $type;
  280.     // end func getGroupType
  281.  
  282.     // }}}
  283.     // {{{ toHtml()
  284.  
  285.     /**
  286.      * Returns Html for the group
  287.      * 
  288.      * @since       1.0
  289.      * @access      public
  290.      * @return      string 
  291.      */
  292.     function toHtml()
  293.     {
  294.         include_once('HTML/QuickForm/Renderer/Default.php');
  295.         $renderer =new HTML_QuickForm_Renderer_Default();
  296.         $renderer->setElementTemplate('{element}');
  297.         $this->accept($renderer);
  298.         return $renderer->toHtml();
  299.     //end func toHtml
  300.     
  301.     // }}}
  302.     // {{{ getElementName()
  303.  
  304.     /**
  305.      * Returns the element name inside the group such as found in the html form
  306.      * 
  307.      * @param     mixed     $index  Element name or element index in the group
  308.      * @since     3.0
  309.      * @access    public
  310.      * @return    mixed     string with element name, false if not found
  311.      */
  312.     function getElementName($index)
  313.     {
  314.         if (empty($this->_elements)) {
  315.             $this->_createElements();
  316.         }
  317.         $elementName = false;
  318.         if (is_int($index&& isset($this->_elements[$index])) {
  319.             $elementName $this->_elements[$index]->getName();
  320.             if (isset($elementName&& $elementName == ''{
  321.                 $elementName $index;
  322.             }
  323.             if ($this->_appendName{
  324.                 if (is_null($elementName)) {
  325.                     $elementName $this->getName();
  326.                 else {
  327.                     $elementName $this->getName().'['.$elementName.']';
  328.                 }
  329.             }
  330.  
  331.         elseif (is_string($index)) {
  332.             foreach (array_keys($this->_elementsas $key{
  333.                 $elementName $this->_elements[$key]->getName();
  334.                 if ($index == $elementName{
  335.                     if ($this->_appendName{
  336.                         $elementName $this->getName().'['.$elementName.']';
  337.                     }
  338.                     break;
  339.                 elseif ($this->_appendName && $this->getName().'['.$elementName.']' == $index{
  340.                     break;
  341.                 }
  342.             }
  343.         }
  344.         return $elementName;
  345.     //end func getElementName
  346.  
  347.     // }}}
  348.     // {{{ getFrozenHtml()
  349.  
  350.     /**
  351.      * Returns the value of field without HTML tags
  352.      * 
  353.      * @since     1.3
  354.      * @access    public
  355.      * @return    string 
  356.      */
  357.     function getFrozenHtml()
  358.     {
  359.         $flags = array();
  360.         if (empty($this->_elements)) {
  361.             $this->_createElements();
  362.         }
  363.         foreach (array_keys($this->_elementsas $key{
  364.             if (false === ($flags[$key$this->_elements[$key]->isFrozen())) {
  365.                 $this->_elements[$key]->freeze();
  366.             }
  367.         }
  368.         $html $this->toHtml();
  369.         foreach (array_keys($this->_elementsas $key{
  370.             if (!$flags[$key]{
  371.                 $this->_elements[$key]->unfreeze();
  372.             }
  373.         }
  374.         return $html;
  375.     //end func getFrozenHtml
  376.  
  377.     // }}}
  378.     // {{{ onQuickFormEvent()
  379.  
  380.     /**
  381.      * Called by HTML_QuickForm whenever form event is made on this element
  382.      *
  383.      * @param     string    $event  Name of event
  384.      * @param     mixed     $arg    event arguments
  385.      * @param     object    $caller calling object
  386.      * @since     1.0
  387.      * @access    public
  388.      * @return    void 
  389.      */
  390.     function onQuickFormEvent($event$arg&$caller)
  391.     {
  392.         switch ($event{
  393.             case 'updateValue':
  394.                 if (empty($this->_elements)) {
  395.                     $this->_createElements();
  396.                 }
  397.                 foreach (array_keys($this->_elementsas $key{
  398.                     if ($this->_appendName{
  399.                         $elementName $this->_elements[$key]->getName();
  400.                         if (is_null($elementName)) {
  401.                             $this->_elements[$key]->setName($this->getName());
  402.                         elseif ('' == $elementName{
  403.                             $this->_elements[$key]->setName($this->getName('[' $key ']');
  404.                         else {
  405.                             $this->_elements[$key]->setName($this->getName('[' $elementName ']');
  406.                         }
  407.                     }
  408.                     $this->_elements[$key]->onQuickFormEvent('updateValue'$arg$caller);
  409.                     if ($this->_appendName{
  410.                         $this->_elements[$key]->setName($elementName);
  411.                     }
  412.                 }
  413.                 break;
  414.  
  415.             default:
  416.                 parent::onQuickFormEvent($event$arg$caller);
  417.         }
  418.         return true;
  419.     // end func onQuickFormEvent
  420.  
  421.     // }}}
  422.     // {{{ accept()
  423.  
  424.    /**
  425.     * Accepts a renderer
  426.     *
  427.     * @param object     An HTML_QuickForm_Renderer object
  428.     * @param bool       Whether a group is required
  429.     * @param string     An error message associated with a group
  430.     * @access public
  431.     * @return void 
  432.     */
  433.     function accept(&$renderer$required = false$error = null)
  434.     {
  435.         if (empty($this->_elements)) {
  436.             $this->_createElements();
  437.         }
  438.         $renderer->startGroup($this$required$error);
  439.         $name $this->getName();
  440.         foreach (array_keys($this->_elementsas $key{
  441.             $element =$this->_elements[$key];
  442.             
  443.             if ($this->_appendName{
  444.                 $elementName $element->getName();
  445.                 if (isset($elementName)) {
  446.                     $element->setName($name '['(strlen($elementName)$elementName$key.']');
  447.                 else {
  448.                     $element->setName($name);
  449.                 }
  450.             }
  451.  
  452.             $required !$element->isFrozen(&& in_array($element->getName()$this->_required);
  453.  
  454.             $element->accept($renderer$required);
  455.  
  456.             // restore the element's name
  457.             if ($this->_appendName{
  458.                 $element->setName($elementName);
  459.             }
  460.         }
  461.         $renderer->finishGroup($this);
  462.     // end func accept
  463.  
  464.     // }}}
  465.     // {{{ exportValue()
  466.  
  467.    /**
  468.     * As usual, to get the group's value we access its elements and call
  469.     * their exportValue() methods
  470.     */
  471.     function exportValue(&$submitValues$assoc = false)
  472.     {
  473.         $value = null;
  474.         foreach (array_keys($this->_elementsas $key{
  475.             $elementName $this->_elements[$key]->getName();
  476.             if ($this->_appendName{
  477.                 if (is_null($elementName)) {
  478.                     $this->_elements[$key]->setName($this->getName());
  479.                 elseif ('' == $elementName{
  480.                     $this->_elements[$key]->setName($this->getName('[' $key ']');
  481.                 else {
  482.                     $this->_elements[$key]->setName($this->getName('[' $elementName ']');
  483.                 }
  484.             }
  485.             $v $this->_elements[$key]->exportValue($submitValues$assoc);
  486.             if ($this->_appendName{
  487.                 $this->_elements[$key]->setName($elementName);
  488.             }
  489.             if (null !== $v{
  490.                 // Make $value an array, we will use it like one
  491.                 if (null === $value{
  492.                     $value = array();
  493.                 }
  494.                 if ($assoc{
  495.                     // just like HTML_QuickForm::exportValues()
  496.                     $value HTML_QuickForm::arrayMerge($value$v);
  497.                 else {
  498.                     // just like getValue(), but should work OK every time here
  499.                     if (is_null($elementName)) {
  500.                         $value $v;
  501.                     elseif ('' == $elementName{
  502.                         $value[$v;
  503.                     else {
  504.                         $value[$elementName$v;
  505.                     }
  506.                 }
  507.             }
  508.         }
  509.         // do not pass the value through _prepareValue, we took care of this already
  510.         return $value;
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ _createElements()
  515.  
  516.    /**
  517.     * Creates the group's elements.
  518.     * 
  519.     * This should be overriden by child classes that need to create their
  520.     * elements. The method will be called automatically when needed, calling
  521.     * it from the constructor is discouraged as the constructor is usually
  522.     * called _twice_ on element creation, first time with _no_ parameters.
  523.     * 
  524.     * @access private
  525.     * @abstract
  526.     */
  527.     function _createElements()
  528.     {
  529.         // abstract
  530.     }
  531.  
  532.     // }}}
  533.     // {{{ freeze()
  534.  
  535.     function freeze()
  536.     {
  537.         parent::freeze();
  538.         foreach (array_keys($this->_elementsas $key{
  539.             $this->_elements[$key]->freeze();
  540.         }
  541.     }
  542.  
  543.     // }}}
  544.     // {{{ unfreeze()
  545.  
  546.     function unfreeze()
  547.     {
  548.         parent::unfreeze();
  549.         foreach (array_keys($this->_elementsas $key{
  550.             $this->_elements[$key]->unfreeze();
  551.         }
  552.     }
  553.  
  554.     // }}}
  555.     // {{{ setPersistantFreeze()
  556.  
  557.     function setPersistantFreeze($persistant = false)
  558.     {
  559.         parent::setPersistantFreeze($persistant);
  560.         foreach (array_keys($this->_elementsas $key{
  561.             $this->_elements[$key]->setPersistantFreeze($persistant);
  562.         }
  563.     }
  564.  
  565.     // }}}
  566. //end class HTML_QuickForm_group
  567. ?>

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