Source for file hierselect.php
Documentation is available at hierselect.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Herim Vasquez <vasquezh@iro.umontreal.ca> |
// | Bertrand Mansion <bmansion@mamasam.com> |
// +----------------------------------------------------------------------+
// $Id: hierselect.php,v 1.10 2004/06/15 10:51:42 mansion Exp $
require_once('HTML/QuickForm/group.php');
require_once('HTML/QuickForm/select.php');
* Class to dynamically create two or more HTML Select elements
* The first select changes the content of the second select and so on.
* This element is considered as a group. Selects will be named
* groupName[0], groupName[1], groupName[2]...
* @author Herim Vasquez <vasquezh@iro.umontreal.ca>
* @author Bertrand Mansion <bmansion@mamasam.com>
* Options for all the select elements
* Format is a bit more complex as we need to know which options
* are related to the ones in the previous select:
* $select1[1] = 'Classical';
* $select1[2] = 'Funeral doom';
* $select2[0][0] = 'Red Hot Chil Peppers';
* $select2[0][1] = 'The Pixies';
* $select2[1][0] = 'Wagner';
* $select2[1][1] = 'Strauss';
* $select2[2][0] = 'Pantheist';
* $select2[2][1] = 'Skepticism';
* // If only need two selects
* // - and using the depracated functions
* $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
* $sel->setMainOptions($select1);
* $sel->setSecOptions($select2);
* // - and using the new setOptions function
* $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
* $sel->setOptions(array($select1, $select2));
* // If you have a third select with prices for the cds
* $select3[0][0][0] = '15.00$';
* $select3[0][0][1] = '17.00$';
* $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
* $sel->setOptions(array($select1, $select2, $select3));
* Number of select elements on this group
* The javascript used to set and change the options
var $_js = "<script type=\"text/javascript\">\n//<![CDATA[\n";
* The javascript array name
* @param string $elementName (optional)Input field name attribute
* @param string $elementLabel (optional)Input field label in form
* @param mixed $attributes (optional)Either a typical HTML attribute string
* or an associative array. Date format is passed along the attributes.
* @param mixed $separator (optional)Use a string for one separator,
* use an array to alternate the separators.
$this->HTML_QuickForm_element ($elementName, $elementLabel, $attributes);
$this->_persistantFreeze = true;
$this->_separator = $separator;
$this->_type = 'hierselect';
$this->_appendName = true;
* Initialize the array structure containing the options for each select element.
* Call the functions that actually do the magic.
* @param array $options Array of options defining each element
$this->_options = $options;
if (empty ($this->_elements)) {
$this->_nbElements = count($this->_options);
$this->_createElements ();
// setDefaults has probably been called before this function
// check if all elements have been created
$totalNbElements = count($this->_options);
for ($i = $this->_nbElements; $i < $totalNbElements; $i ++ ) {
} // end func setMainOptions
* Sets the options for the first select element. Deprecated. setOptions() should be used.
* @param array $array Options for the first select element
$this->_options[0 ] = $array;
if (empty ($this->_elements)) {
$this->_createElements ();
} // end func setMainOptions
* Sets the options for the second select element. Deprecated. setOptions() should be used.
* The main _options array is initialized and the _setOptions function is called.
* @param array $array Options for the second select element
$this->_options[1 ] = $array;
if (empty ($this->_elements)) {
$this->_createElements ();
// setDefaults has probably been called before this function
// check if all elements have been created
for ($i = $this->_nbElements; $i < $totalNbElements; $i ++ ) {
} // end func setSecOptions
* Sets the options for each select element
if (eval (" return isset(\$this->_options[{$key}]{$toLoad});" ) ) {
$array = eval (" return \$this->_options[{$key}]{$toLoad};" );
$select = & $this->_elements[$key];
$select->_options = array ();
$select->loadArray ($array);
$value = is_array($v = $select->getValue ()) ? $v[0 ] : key($array);
$toLoad .= '[\''. $value. '\']';
} // end func _setOptions
* Sets values for group's elements
* @param array $value An array of 2 or more values, for the first,
* the second, the third etc. select
$this->_nbElements = count($value);
parent ::setValue ($value);
* Creates all the elements for the group
function _createElements ()
for ($i = 0; $i < $this->_nbElements; $i++ ) {
} // end func _createElements
* Set the JavaScript for each select element (excluding de main one).
$this->_jsArrayName = 'hs_' . $this->getName ();
for ($i = 1; $i < $this->_nbElements; $i++ ) {
$this->_setJSArray ($this->_jsArrayName, $this->_options[$i], $js);
* Recursively builds the JavaScript array defining the options that a select
* @param string $grpName Group Name attribute
* @param array $options Select element options
* @param string $js JavaScript definition is build using this variable
* @param string $optValue The value for the current JavaScript option
function _setJSArray ($grpName, $options, &$js, $optValue = '')
// For a hierselect containing 3 elements:
// if option 1 has been selected for the 1st element
// and option 3 has been selected for the 2nd element,
// then the javascript array containing the values to load
// on the 3rd element will have the following name: grpName_1_3
$name = ($optValue === '') ? $grpName : $grpName. '_'. $optValue;
foreach($options AS $k => $v) {
$this->_setJSArray ($name, $v, $js, $k);
// if $js !== '' add it to the JavaScript
$this->_js .= ($js !== '') ? $name. " = {\n". $js. "\n}\n" : '';
// $js empty means that we are adding the first element to the JavaScript.
$js .= '"'. $optValue. '":"'. $options. '"';
* Returns Html for the group
if ($this->_flagFrozen) {
// set the onchange attribute for each element
$nbElements = count($keys);
$nbElementsUsingFnc = $nbElements - 1; // last element doesn't need it
for ($i = 0; $i < $nbElementsUsingFnc; $i++ ) {
$select = & $this->_elements[$keys[$i]];
$select->updateAttributes (
array ('onChange' => 'swapOptions(this, \''. $this->getName (). '\', '. $keys[$i]. ', '. $nbElements. ', \''. $this->_jsArrayName. '\');')
// create the js function to call
if (!defined('HTML_QUICKFORM_HIERSELECT_EXISTS')) {
$this->_js .= "function swapOptions(frm, grpName, eleIndex, nbElements, arName)\n"
. " for (var i = 0; i < nbElements; i++) {\n"
. " ctl = frm.form[grpName+'['+i+']'];\n"
. " ctl = frm.form[grpName+'['+i+'][]'];\n"
. " if (i <= eleIndex) {\n"
. " n += \"_\"+ctl.value;\n"
. " var t = eval(\"typeof(\"+arName + n +\")\");\n"
. " if (t != 'undefined') {\n"
. " var the_array = eval(arName+n);\n"
. " ctl = frm.form[grpName+'['+ n +']'];\n"
. " ctl = frm.form[grpName+'['+ n +'][]'];\n"
. " for (var i in the_array) {\n"
. " opt = new Option(the_array[i], i, false, false);\n"
. " ctl.options[j++] = opt;\n"
define('HTML_QUICKFORM_HIERSELECT_EXISTS', true );
$this->_js .= "//]]>\n</script>\n";
include_once('HTML/QuickForm/Renderer/Default.php');
$renderer->setElementTemplate ('{element}');
parent ::accept ($renderer);
return $this->_js. $renderer->toHtml ();
* @param object An HTML_QuickForm_Renderer object
* @param bool Whether a group is required
* @param string An error message associated with a group
function accept(&$renderer, $required = false , $error = null )
$renderer->renderElement ($this, $required, $error);
// {{{ onQuickFormEvent()
if ('updateValue' == $event) {
// we need to call setValue() so that the secondary option
// matches the main option
return parent ::onQuickFormEvent ($event, $arg, $caller);
} // end func onQuickFormEvent
} // end class HTML_QuickForm_hierselect
Documentation generated on Mon, 11 Mar 2019 13:52:21 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|