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.36 2005/07/22 14:19:58 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.         $this->_createElementsIfNotExist();
  154.         foreach (array_keys($this->_elementsas $key{
  155.             if (!$this->_appendName{
  156.                 $v $this->_elements[$key]->_findValue($value);
  157.                 if (null !== $v{
  158.                     $this->_elements[$key]->onQuickFormEvent('setGroupValue'$v$this);
  159.                 }
  160.  
  161.             else {
  162.                 $elementName $this->_elements[$key]->getName();
  163.                 $index       strlen($elementName$elementName $key;
  164.                 if (is_array($value)) {
  165.                     if (isset($value[$index])) {
  166.                         $this->_elements[$key]->onQuickFormEvent('setGroupValue'$value[$index]$this);
  167.                     }
  168.                 elseif (isset($value)) {
  169.                     $this->_elements[$key]->onQuickFormEvent('setGroupValue'$value$this);
  170.                 }
  171.             }
  172.         }
  173.     //end func setValue
  174.     
  175.     // }}}
  176.     // {{{ getValue()
  177.  
  178.     /**
  179.      * Returns the value of the group
  180.      *
  181.      * @since     1.0
  182.      * @access    public
  183.      * @return    mixed 
  184.      */
  185.     function getValue()
  186.     {
  187.         $value = null;
  188.         foreach (array_keys($this->_elementsas $key{
  189.             $element =$this->_elements[$key];
  190.             switch ($element->getType()) {
  191.                 case 'radio'
  192.                     $v $element->getChecked()$element->getValue(): null;
  193.                     break;
  194.                 case 'checkbox'
  195.                     $v $element->getChecked()? true: null;
  196.                     break;
  197.                 default:
  198.                     $v $element->getValue();
  199.             }
  200.             if (null !== $v{
  201.                 $elementName $element->getName();
  202.                 if (is_null($elementName)) {
  203.                     $value $v;
  204.                 else {
  205.                     if (!is_array($value)) {
  206.                         $value is_null($value)? array(): array($value);
  207.                     }
  208.                     if ('' === $elementName{
  209.                         $value[$v;
  210.                     else {
  211.                         $value[$elementName$v;
  212.                     }
  213.                 }
  214.             }
  215.         }
  216.         return $value;
  217.     // end func getValue
  218.  
  219.     // }}}
  220.     // {{{ setElements()
  221.  
  222.     /**
  223.      * Sets the grouped elements
  224.      *
  225.      * @param     array     $elements   Array of elements
  226.      * @since     1.1
  227.      * @access    public
  228.      * @return    void 
  229.      */
  230.     function setElements($elements)
  231.     {
  232.         $this->_elements array_values($elements);
  233.         if ($this->_flagFrozen{
  234.             $this->freeze();
  235.         }
  236.     // end func setElements
  237.  
  238.     // }}}
  239.     // {{{ getElements()
  240.  
  241.     /**
  242.      * Gets the grouped elements
  243.      *
  244.      * @since     2.4
  245.      * @access    public
  246.      * @return    array 
  247.      */
  248.     function &getElements()
  249.     {
  250.         $this->_createElementsIfNotExist();
  251.         return $this->_elements;
  252.     // end func getElements
  253.  
  254.     // }}}
  255.     // {{{ getGroupType()
  256.  
  257.     /**
  258.      * Gets the group type based on its elements
  259.      * Will return 'mixed' if elements contained in the group
  260.      * are of different types.
  261.      *
  262.      * @access    public
  263.      * @return    string    group elements type
  264.      */
  265.     function getGroupType()
  266.     {
  267.         $this->_createElementsIfNotExist();
  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.         $this->_createElementsIfNotExist();
  312.         $elementName = false;
  313.         if (is_int($index&& isset($this->_elements[$index])) {
  314.             $elementName $this->_elements[$index]->getName();
  315.             if (isset($elementName&& $elementName == ''{
  316.                 $elementName $index;
  317.             }
  318.             if ($this->_appendName{
  319.                 if (is_null($elementName)) {
  320.                     $elementName $this->getName();
  321.                 else {
  322.                     $elementName $this->getName().'['.$elementName.']';
  323.                 }
  324.             }
  325.  
  326.         elseif (is_string($index)) {
  327.             foreach (array_keys($this->_elementsas $key{
  328.                 $elementName $this->_elements[$key]->getName();
  329.                 if ($index == $elementName{
  330.                     if ($this->_appendName{
  331.                         $elementName $this->getName().'['.$elementName.']';
  332.                     }
  333.                     break;
  334.                 elseif ($this->_appendName && $this->getName().'['.$elementName.']' == $index{
  335.                     break;
  336.                 }
  337.             }
  338.         }
  339.         return $elementName;
  340.     //end func getElementName
  341.  
  342.     // }}}
  343.     // {{{ getFrozenHtml()
  344.  
  345.     /**
  346.      * Returns the value of field without HTML tags
  347.      * 
  348.      * @since     1.3
  349.      * @access    public
  350.      * @return    string 
  351.      */
  352.     function getFrozenHtml()
  353.     {
  354.         $flags = array();
  355.         $this->_createElementsIfNotExist();
  356.         foreach (array_keys($this->_elementsas $key{
  357.             if (false === ($flags[$key$this->_elements[$key]->isFrozen())) {
  358.                 $this->_elements[$key]->freeze();
  359.             }
  360.         }
  361.         $html $this->toHtml();
  362.         foreach (array_keys($this->_elementsas $key{
  363.             if (!$flags[$key]{
  364.                 $this->_elements[$key]->unfreeze();
  365.             }
  366.         }
  367.         return $html;
  368.     //end func getFrozenHtml
  369.  
  370.     // }}}
  371.     // {{{ onQuickFormEvent()
  372.  
  373.     /**
  374.      * Called by HTML_QuickForm whenever form event is made on this element
  375.      *
  376.      * @param     string    $event  Name of event
  377.      * @param     mixed     $arg    event arguments
  378.      * @param     object    $caller calling object
  379.      * @since     1.0
  380.      * @access    public
  381.      * @return    void 
  382.      */
  383.     function onQuickFormEvent($event$arg&$caller)
  384.     {
  385.         switch ($event{
  386.             case 'updateValue':
  387.                 $this->_createElementsIfNotExist();
  388.                 foreach (array_keys($this->_elementsas $key{
  389.                     if ($this->_appendName{
  390.                         $elementName $this->_elements[$key]->getName();
  391.                         if (is_null($elementName)) {
  392.                             $this->_elements[$key]->setName($this->getName());
  393.                         elseif ('' === $elementName{
  394.                             $this->_elements[$key]->setName($this->getName('[' $key ']');
  395.                         else {
  396.                             $this->_elements[$key]->setName($this->getName('[' $elementName ']');
  397.                         }
  398.                     }
  399.                     $this->_elements[$key]->onQuickFormEvent('updateValue'$arg$caller);
  400.                     if ($this->_appendName{
  401.                         $this->_elements[$key]->setName($elementName);
  402.                     }
  403.                 }
  404.                 break;
  405.  
  406.             default:
  407.                 parent::onQuickFormEvent($event$arg$caller);
  408.         }
  409.         return true;
  410.     // end func onQuickFormEvent
  411.  
  412.     // }}}
  413.     // {{{ accept()
  414.  
  415.    /**
  416.     * Accepts a renderer
  417.     *
  418.     * @param object     An HTML_QuickForm_Renderer object
  419.     * @param bool       Whether a group is required
  420.     * @param string     An error message associated with a group
  421.     * @access public
  422.     * @return void 
  423.     */
  424.     function accept(&$renderer$required = false$error = null)
  425.     {
  426.         $this->_createElementsIfNotExist();
  427.         $renderer->startGroup($this$required$error);
  428.         $name $this->getName();
  429.         foreach (array_keys($this->_elementsas $key{
  430.             $element =$this->_elements[$key];
  431.             
  432.             if ($this->_appendName{
  433.                 $elementName $element->getName();
  434.                 if (isset($elementName)) {
  435.                     $element->setName($name '['(strlen($elementName)$elementName$key.']');
  436.                 else {
  437.                     $element->setName($name);
  438.                 }
  439.             }
  440.  
  441.             $required !$element->isFrozen(&& 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.     // {{{ _createElementsIfNotExist()
  523.  
  524.    /**
  525.     * A wrapper around _createElements()
  526.     *
  527.     * This method calls _createElements() if the group's _elements array
  528.     * is empty. It also performs some updates, e.g. freezes the created
  529.     * elements if the group is already frozen.
  530.     *
  531.     * @access private
  532.     */
  533.     function _createElementsIfNotExist()
  534.     {
  535.         if (empty($this->_elements)) {
  536.             $this->_createElements();
  537.             if ($this->_flagFrozen{
  538.                 $this->freeze();
  539.             }
  540.         }
  541.     }
  542.  
  543.     // }}}
  544.     // {{{ freeze()
  545.  
  546.     function freeze()
  547.     {
  548.         parent::freeze();
  549.         foreach (array_keys($this->_elementsas $key{
  550.             $this->_elements[$key]->freeze();
  551.         }
  552.     }
  553.  
  554.     // }}}
  555.     // {{{ unfreeze()
  556.  
  557.     function unfreeze()
  558.     {
  559.         parent::unfreeze();
  560.         foreach (array_keys($this->_elementsas $key{
  561.             $this->_elements[$key]->unfreeze();
  562.         }
  563.     }
  564.  
  565.     // }}}
  566.     // {{{ setPersistantFreeze()
  567.  
  568.     function setPersistantFreeze($persistant = false)
  569.     {
  570.         parent::setPersistantFreeze($persistant);
  571.         foreach (array_keys($this->_elementsas $key{
  572.             $this->_elements[$key]->setPersistantFreeze($persistant);
  573.         }
  574.     }
  575.  
  576.     // }}}
  577. //end class HTML_QuickForm_group
  578. ?>

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