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

Source for file hierselect.php

Documentation is available at hierselect.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 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: Herim Vasquez <vasquezh@iro.umontreal.ca>                   |
  17. // |          Bertrand Mansion <bmansion@mamasam.com>                     |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: hierselect.php,v 1.10 2004/06/15 10:51:42 mansion Exp $
  21.  
  22. require_once('HTML/QuickForm/group.php');
  23. require_once('HTML/QuickForm/select.php');
  24.  
  25. /**
  26.  * Class to dynamically create two or more HTML Select elements
  27.  * The first select changes the content of the second select and so on.
  28.  * This element is considered as a group. Selects will be named
  29.  * groupName[0], groupName[1], groupName[2]...
  30.  *
  31.  * @author       Herim Vasquez <vasquezh@iro.umontreal.ca>
  32.  * @author       Bertrand Mansion <bmansion@mamasam.com>
  33.  * @version      1.0
  34.  * @since        PHP4.04pl1
  35.  * @access       public
  36.  */
  37. {   
  38.     // {{{ properties
  39.  
  40.     /**
  41.      * Options for all the select elements
  42.      *
  43.      * Format is a bit more complex as we need to know which options
  44.      * are related to the ones in the previous select:
  45.      *
  46.      * Ex:
  47.      * // first select
  48.      * $select1[0] = 'Pop';
  49.      * $select1[1] = 'Classical';
  50.      * $select1[2] = 'Funeral doom';
  51.      *
  52.      * // second select
  53.      * $select2[0][0] = 'Red Hot Chil Peppers';
  54.      * $select2[0][1] = 'The Pixies';
  55.      * $select2[1][0] = 'Wagner';
  56.      * $select2[1][1] = 'Strauss';
  57.      * $select2[2][0] = 'Pantheist';
  58.      * $select2[2][1] = 'Skepticism';
  59.      *
  60.      * // If only need two selects
  61.      * //     - and using the depracated functions
  62.      * $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
  63.      * $sel->setMainOptions($select1);
  64.      * $sel->setSecOptions($select2);
  65.      *
  66.      * //     - and using the new setOptions function
  67.      * $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
  68.      * $sel->setOptions(array($select1, $select2));
  69.      *
  70.      * // If you have a third select with prices for the cds
  71.      * $select3[0][0][0] = '15.00$';
  72.      * $select3[0][0][1] = '17.00$';
  73.      * etc
  74.      *
  75.      * // You can now use
  76.      * $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
  77.      * $sel->setOptions(array($select1, $select2, $select3));
  78.      * 
  79.      * @var       array 
  80.      * @access    private
  81.      */
  82.     var $_options = array();
  83.     
  84.     /**
  85.      * Number of select elements on this group
  86.      *
  87.      * @var       int 
  88.      * @access    private
  89.      */
  90.     var $_nbElements = 0;
  91.  
  92.     /**
  93.      * The javascript used to set and change the options
  94.      *
  95.      * @var       string 
  96.      * @access    private
  97.      */
  98.     var $_js "<script type=\"text/javascript\">\n//<![CDATA[\n";
  99.     
  100.     /**
  101.     * The javascript array name
  102.     */
  103.     var $_jsArrayName '';
  104.  
  105.     // }}}
  106.     // {{{ constructor
  107.  
  108.     /**
  109.      * Class constructor
  110.      * 
  111.      * @param     string    $elementName    (optional)Input field name attribute
  112.      * @param     string    $elementLabel   (optional)Input field label in form
  113.      * @param     mixed     $attributes     (optional)Either a typical HTML attribute string
  114.      *                                       or an associative array. Date format is passed along the attributes.
  115.      * @param     mixed     $separator      (optional)Use a string for one separator,
  116.      *                                       use an array to alternate the separators.
  117.      * @access    public
  118.      * @return    void 
  119.      */
  120.     function HTML_QuickForm_hierselect($elementName=null$elementLabel=null$attributes=null$separator=null)
  121.     {
  122.         $this->HTML_QuickForm_element($elementName$elementLabel$attributes);
  123.         $this->_persistantFreeze = true;
  124.         if (isset($separator)) {
  125.             $this->_separator $separator;
  126.         }
  127.         $this->_type 'hierselect';
  128.         $this->_appendName = true;
  129.     //end constructor
  130.  
  131.     // }}}
  132.     // {{{ setOptions()
  133.  
  134.     /**
  135.      * Initialize the array structure containing the options for each select element.
  136.      * Call the functions that actually do the magic.
  137.      *
  138.      * @param     array    $options    Array of options defining each element
  139.      *
  140.      * @access    public
  141.      * @return    void 
  142.      */
  143.     function setOptions($options)
  144.     {
  145.         $this->_options $options;
  146.  
  147.         if (empty($this->_elements)) {
  148.             $this->_nbElements count($this->_options);
  149.             $this->_createElements();
  150.         else {
  151.             // setDefaults has probably been called before this function
  152.             // check if all elements have been created
  153.             $totalNbElements count($this->_options);
  154.             for ($i $this->_nbElements$i $totalNbElements$i ++{
  155.                 $this->_elements[=new HTML_QuickForm_select($inullarray()$this->getAttributes());
  156.                 $this->_nbElements++;
  157.             }
  158.         }
  159.         
  160.         $this->_setOptions();
  161.         $this->_setJS();
  162.     // end func setMainOptions
  163.  
  164.     // }}}
  165.     // {{{ setMainOptions()
  166.     
  167.     /**
  168.      * Sets the options for the first select element. Deprecated. setOptions() should be used.
  169.      *
  170.      * @param     array     $array    Options for the first select element
  171.      *
  172.      * @access    public
  173.      * @return    void 
  174.      */
  175.     function setMainOptions($array)
  176.     {
  177.         $this->_options[0$array;
  178.  
  179.         if (empty($this->_elements)) {
  180.             $this->_nbElements = 2;
  181.             $this->_createElements();
  182.         }
  183.     // end func setMainOptions
  184.     
  185.     // }}}
  186.     // {{{ setSecOptions()
  187.     
  188.     /**
  189.      * Sets the options for the second select element. Deprecated. setOptions() should be used.
  190.      * The main _options array is initialized and the _setOptions function is called.
  191.      *
  192.      * @param     array     $array    Options for the second select element
  193.      *
  194.      * @access    public
  195.      * @return    void 
  196.      */
  197.     function setSecOptions($array)
  198.     {
  199.         $this->_options[1$array;
  200.  
  201.         if (empty($this->_elements)) {
  202.             $this->_nbElements = 2;
  203.             $this->_createElements();
  204.         else {
  205.             // setDefaults has probably been called before this function
  206.             // check if all elements have been created
  207.             $totalNbElements = 2;
  208.             for ($i $this->_nbElements$i $totalNbElements$i ++{
  209.                 $this->_elements[=new HTML_QuickForm_select($inullarray()$this->getAttributes());
  210.                 $this->_nbElements++;
  211.             }
  212.         }
  213.         
  214.         $this->_setOptions();
  215.         $this->_setJS();
  216.     // end func setSecOptions
  217.     
  218.     // }}}
  219.     // {{{ _setOptions()
  220.     
  221.     /**
  222.      * Sets the options for each select element
  223.      *
  224.      * @access    private
  225.      * @return    void 
  226.      */
  227.     function _setOptions()
  228.     {
  229.         $toLoad '';
  230.         foreach (array_keys($this->_elementsAS $key{
  231.             if (eval("return isset(\$this->_options[{$key}]{$toLoad});") ) {
  232.                 $array = eval("return \$this->_options[{$key}]{$toLoad};");
  233.                 if (is_array($array)) {
  234.                     $select =$this->_elements[$key];
  235.                     $select->_options = array();
  236.                     $select->loadArray($array);
  237.  
  238.                     $value  is_array($v $select->getValue()) $v[0key($array);
  239.                     $toLoad .= '[\''.$value.'\']';
  240.                 }
  241.             }
  242.         }
  243.     // end func _setOptions
  244.     
  245.     // }}}
  246.     // {{{ setValue()
  247.  
  248.     /**
  249.      * Sets values for group's elements
  250.      * 
  251.      * @param     array     $value    An array of 2 or more values, for the first,
  252.      *                                 the second, the third etc. select
  253.      *
  254.      * @access    public
  255.      * @return    void 
  256.      */
  257.     function setValue($value)
  258.     {
  259.         $this->_nbElements count($value);
  260.         parent::setValue($value);
  261.         $this->_setOptions();
  262.     // end func setValue
  263.     
  264.     // }}}
  265.     // {{{ _createElements()
  266.  
  267.     /**
  268.      * Creates all the elements for the group
  269.      * 
  270.      * @access    private
  271.      * @return    void 
  272.      */
  273.     function _createElements()
  274.     {
  275.         for ($i = 0; $i $this->_nbElements$i++{
  276.             $this->_elements[=new HTML_QuickForm_select($inullarray()$this->getAttributes());
  277.         }
  278.     // end func _createElements
  279.  
  280.     // }}}
  281.     // {{{ _setJS()
  282.     
  283.     /**
  284.      * Set the JavaScript for each select element (excluding de main one).
  285.      *
  286.      * @access    private
  287.      * @return    void 
  288.      */
  289.     function _setJS()
  290.     {
  291.         $js      '';
  292.         $this->_jsArrayName 'hs_' $this->getName();
  293.         for ($i = 1; $i $this->_nbElements$i++{
  294.             $this->_setJSArray($this->_jsArrayName$this->_options[$i]$js);
  295.         }
  296.     // end func _setJS
  297.     
  298.     // }}}
  299.     // {{{ _setJSArray()
  300.     
  301.     /**
  302.      * Recursively builds the JavaScript array defining the options that a select
  303.      * element can have.
  304.      *
  305.      * @param       string      $grpName    Group Name attribute
  306.      * @param       array       $options    Select element options
  307.      * @param       string      $js         JavaScript definition is build using this variable
  308.      * @param       string      $optValue   The value for the current JavaScript option
  309.      *
  310.      * @access      private
  311.      * @return      void 
  312.      */
  313.     function _setJSArray($grpName$options&$js$optValue '')
  314.     {
  315.         if (is_array($options)) {
  316.             $js '';
  317.             // For a hierselect containing 3 elements:
  318.             //      if option 1 has been selected for the 1st element
  319.             //      and option 3 has been selected for the 2nd element,
  320.             //      then the javascript array containing the values to load 
  321.             //      on the 3rd element will have the following name:   grpName_1_3
  322.             $name  ($optValue === ''$grpName $grpName.'_'.$optValue;
  323.             foreach($options AS $k => $v{
  324.                 $this->_setJSArray($name$v$js$k);
  325.             }
  326.             
  327.             // if $js !== '' add it to the JavaScript
  328.             $this->_js .= ($js !== ''$name." = {\n".$js."\n}\n" '';
  329.             $js '';
  330.         else {
  331.             // $js empty means that we are adding the first element to the JavaScript.
  332.             if ($js != ''{
  333.                 $js .= ",\n";
  334.             }
  335.             $js .= '"'.$optValue.'":"'.$options.'"';
  336.         }
  337.     }
  338.  
  339.     // }}}
  340.     // {{{ toHtml()
  341.  
  342.     /**
  343.      * Returns Html for the group
  344.      * 
  345.      * @access      public
  346.      * @return      string 
  347.      */
  348.     function toHtml()
  349.     {
  350.         if ($this->_flagFrozen{
  351.             $this->_js '';
  352.         else {
  353.             // set the onchange attribute for each element
  354.             $keys               array_keys($this->_elements);
  355.             $nbElements         count($keys);
  356.             $nbElementsUsingFnc $nbElements - 1; // last element doesn't need it
  357.             for ($i = 0; $i $nbElementsUsingFnc$i++{
  358.                 $select =$this->_elements[$keys[$i]];
  359.                 $select->updateAttributes(
  360.                     array('onChange' => 'swapOptions(this, \''.$this->getName().'\', '.$keys[$i].', '.$nbElements.', \''.$this->_jsArrayName.'\');')
  361.                 );
  362.             }
  363.             
  364.             // create the js function to call
  365.             if (!defined('HTML_QUICKFORM_HIERSELECT_EXISTS')) {
  366.                 $this->_js .= "function swapOptions(frm, grpName, eleIndex, nbElements, arName)\n"
  367.                              ."{\n"
  368.                              ."    var n = \"\";\n"
  369.                              ."    var ctl;\n\n"
  370.                              ."    for (var i = 0; i < nbElements; i++) {\n"
  371.                              ."        ctl = frm.form[grpName+'['+i+']'];\n"
  372.                              ."        if (!ctl) {\n"
  373.                              ."            ctl = frm.form[grpName+'['+i+'][]'];\n"
  374.                              ."        }\n"
  375.                              ."        if (i <= eleIndex) {\n"
  376.                              ."            n += \"_\"+ctl.value;\n"
  377.                              ."        } else {\n"
  378.                              ."            ctl.length = 0;\n"
  379.                              ."        }\n"
  380.                              ."    }\n\n"
  381.                              ."    var t = eval(\"typeof(\"+arName + n +\")\");\n"
  382.                              ."    if (t != 'undefined') {\n"
  383.                              ."        var the_array = eval(arName+n);\n"
  384.                              ."        var j = 0;\n"
  385.                              ."        n = eleIndex + 1;\n"
  386.                              ."        ctl = frm.form[grpName+'['+ n +']'];\n"
  387.                              ."        if (!ctl) {\n"
  388.                              ."            ctl = frm.form[grpName+'['+ n +'][]'];\n"
  389.                              ."        }\n"
  390.                              ."        for (var i in the_array) {\n"
  391.                              ."            opt = new Option(the_array[i], i, false, false);\n"
  392.                              ."            ctl.options[j++] = opt;\n"
  393.                              ."        }\n"
  394.                              ."    }\n"
  395.                              ."}\n";
  396.                 define('HTML_QUICKFORM_HIERSELECT_EXISTS'true);
  397.             }
  398.             $this->_js .= "//]]>\n</script>\n";
  399.         }
  400.         include_once('HTML/QuickForm/Renderer/Default.php');
  401.         $renderer =new HTML_QuickForm_Renderer_Default();
  402.         $renderer->setElementTemplate('{element}');
  403.         parent::accept($renderer);
  404.         return $this->_js.$renderer->toHtml();
  405.     // end func toHtml
  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.         $renderer->renderElement($this$required$error);
  422.     // end func accept
  423.  
  424.     // }}}
  425.     // {{{ onQuickFormEvent()
  426.  
  427.     function onQuickFormEvent($event$arg&$caller)
  428.     {
  429.         if ('updateValue' == $event{
  430.             // we need to call setValue() so that the secondary option
  431.             // matches the main option
  432.             return HTML_QuickForm_element::onQuickFormEvent($event$arg$caller);
  433.         else {
  434.             return parent::onQuickFormEvent($event$arg$caller);
  435.         }
  436.     // end func onQuickFormEvent
  437.  
  438.     // }}}    
  439. // end class HTML_QuickForm_hierselect
  440. ?>

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