2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
20 // $Id: hierselect.php,v 1.12 2004/10/20 10:03:49 avb Exp $
22 require_once('HTML/QuickForm/group.php');
23 require_once('HTML/QuickForm/select.php');
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]...
31 * @author Herim Vasquez <vasquezh@iro.umontreal.ca>
32 * @author Bertrand Mansion <bmansion@mamasam.com>
37 class HTML_QuickForm_hierselect
extends HTML_QuickForm_group
42 * Options for all the select elements
44 * Format is a bit more complex as we need to know which options
45 * are related to the ones in the previous select:
49 * $select1[0] = 'Pop';
50 * $select1[1] = 'Classical';
51 * $select1[2] = 'Funeral doom';
54 * $select2[0][0] = 'Red Hot Chil Peppers';
55 * $select2[0][1] = 'The Pixies';
56 * $select2[1][0] = 'Wagner';
57 * $select2[1][1] = 'Strauss';
58 * $select2[2][0] = 'Pantheist';
59 * $select2[2][1] = 'Skepticism';
61 * // If only need two selects
62 * // - and using the depracated functions
63 * $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
64 * $sel->setMainOptions($select1);
65 * $sel->setSecOptions($select2);
67 * // - and using the new setOptions function
68 * $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
69 * $sel->setOptions(array($select1, $select2));
71 * // If you have a third select with prices for the cds
72 * $select3[0][0][0] = '15.00$';
73 * $select3[0][0][1] = '17.00$';
77 * $sel =& $form->addElement('hierselect', 'cds', 'Choose CD:');
78 * $sel->setOptions(array($select1, $select2, $select3));
83 var $_options = array();
86 * Number of select elements on this group
94 * The javascript used to set and change the options
102 * The javascript array name
104 var $_jsArrayName = '';
112 * @param string $elementName (optional)Input field name attribute
113 * @param string $elementLabel (optional)Input field label in form
114 * @param mixed $attributes (optional)Either a typical HTML attribute string
115 * or an associative array. Date format is passed along the attributes.
116 * @param mixed $separator (optional)Use a string for one separator,
117 * use an array to alternate the separators.
121 function HTML_QuickForm_hierselect($elementName=null, $elementLabel=null, $attributes=null, $separator=null)
123 $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
124 $this->_persistantFreeze
= true;
125 if (isset($separator)) {
126 $this->_separator
= $separator;
128 $this->_type
= 'hierselect';
129 $this->_appendName
= true;
136 * Initialize the array structure containing the options for each select element.
137 * Call the functions that actually do the magic.
139 * @param array $options Array of options defining each element
144 function setOptions($options)
146 $this->_options
= $options;
148 if (empty($this->_elements
)) {
149 $this->_nbElements
= count($this->_options
);
150 $this->_createElements();
152 // setDefaults has probably been called before this function
153 // check if all elements have been created
154 $totalNbElements = count($this->_options
);
155 for ($i = $this->_nbElements
; $i < $totalNbElements; $i ++
) {
156 $this->_elements
[] = new HTML_QuickForm_select($i, null, array(), $this->getAttributes());
157 $this->_nbElements++
;
161 $this->_setOptions();
163 } // end func setMainOptions
166 // {{{ setMainOptions()
169 * Sets the options for the first select element. Deprecated. setOptions() should be used.
171 * @param array $array Options for the first select element
176 function setMainOptions($array)
178 $this->_options
[0] = $array;
180 if (empty($this->_elements
)) {
181 $this->_nbElements
= 2;
182 $this->_createElements();
184 } // end func setMainOptions
187 // {{{ setSecOptions()
190 * Sets the options for the second select element. Deprecated. setOptions() should be used.
191 * The main _options array is initialized and the _setOptions function is called.
193 * @param array $array Options for the second select element
198 function setSecOptions($array)
200 $this->_options
[1] = $array;
202 if (empty($this->_elements
)) {
203 $this->_nbElements
= 2;
204 $this->_createElements();
206 // setDefaults has probably been called before this function
207 // check if all elements have been created
208 $totalNbElements = 2;
209 for ($i = $this->_nbElements
; $i < $totalNbElements; $i ++
) {
210 $this->_elements
[] = new HTML_QuickForm_select($i, null, array(), $this->getAttributes());
211 $this->_nbElements++
;
215 $this->_setOptions();
217 } // end func setSecOptions
223 * Sets the options for each select element
228 function _setOptions()
231 foreach (array_keys($this->_elements
) AS $key) {
232 if (eval("return isset(\$this->_options[{$key}]{$toLoad});") ) {
233 $array = eval("return \$this->_options[{$key}]{$toLoad};");
234 if (is_array($array)) {
235 $select =& $this->_elements
[$key];
236 $select->_options
= array();
237 $select->loadArray($array);
239 $value = is_array($v = $select->getValue()) ?
$v[0] : key($array);
240 $toLoad .= '[\''.$value.'\']';
244 } // end func _setOptions
250 * Sets values for group's elements
252 * @param array $value An array of 2 or more values, for the first,
253 * the second, the third etc. select
258 function setValue($value)
260 $this->_nbElements
= count($value);
261 parent
::setValue($value);
262 $this->_setOptions();
263 } // end func setValue
266 // {{{ _createElements()
269 * Creates all the elements for the group
274 function _createElements()
276 //hack to add id attribute for hier select
277 $attributes = $this->getAttributes();
279 if ( isset( $attributes['id'] ) ) {
280 $id = "{$attributes['id']}";
283 for ($i = 0; $i < $this->_nbElements
; $i++
) {
284 if ( isset( $id ) ) {
285 $attributes['id'] = "{$id}_{$i}";
288 $this->_elements
[] = new HTML_QuickForm_select($i, null, array(), $attributes);
290 } // end func _createElements
296 * Set the JavaScript for each select element (excluding de main one).
303 static $jsArrayName = null;
305 $this->_js
= $js = '';
306 if ( ! $jsArrayName ) {
307 $this->_jsArrayName
= 'hs_' . preg_replace('/\[|\]/', '_', $this->getName());
308 for ($i = 1; $i < $this->_nbElements
; $i++
) {
309 $this->_setJSArray($this->_jsArrayName
, $this->_options
[$i], $js);
311 $jsArrayName = $this->_jsArrayName
;
313 $this->_jsArrayName
= $jsArrayName;
321 * Recursively builds the JavaScript array defining the options that a select
324 * @param string $grpName Group Name attribute
325 * @param array $options Select element options
326 * @param string $js JavaScript definition is build using this variable
327 * @param string $optValue The value for the current JavaScript option
332 function _setJSArray($grpName, $options, &$js, $optValue = '')
334 static $jsNameCache = array( );
335 if (is_array($options)) {
337 // For a hierselect containing 3 elements:
338 // if option 1 has been selected for the 1st element
339 // and option 3 has been selected for the 2nd element,
340 // then the javascript array containing the values to load
341 // on the 3rd element will have the following name: grpName_1_3
342 $name = ($optValue === '') ?
$grpName : $grpName.'_'.$optValue;
343 foreach($options AS $k => $v) {
344 $this->_setJSArray($name, $v, $js, $k);
347 // if $js !== '' add it to the JavaScript
350 // check if we have already this js in cache, if so reuse it
351 $cacheKey = md5( $js );
352 if ( array_key_exists( $cacheKey, $jsNameCache ) ) {
353 $this->_js
.= "$name = {$jsNameCache[$cacheKey]}\n";
355 $this->_js
.= $name." = {\n".$js."\n}\n";
356 $jsNameCache[$cacheKey] = $name;
361 // $js empty means that we are adding the first element to the JavaScript.
365 $js .= '"'.$optValue.'":"'.addcslashes($options,'"').'"';
373 * Returns Html for the group
380 if ($this->_flagFrozen
) {
383 // set the onchange attribute for each element
384 $keys = array_keys($this->_elements
);
385 $nbElements = count($keys);
386 $nbElementsUsingFnc = $nbElements - 1; // last element doesn't need it
387 for ($i = 0; $i < $nbElementsUsingFnc; $i++
) {
388 $select =& $this->_elements
[$keys[$i]];
389 $select->updateAttributes(
390 array('onChange' => 'swapOptions(this.form, \''.$this->getName().'\', '.$keys[$i].', '.$nbElements.', \''.$this->_jsArrayName
.'\');')
394 // create the js function to call
395 if (!defined('HTML_QUICKFORM_HIERSELECT_EXISTS')) {
396 $this->_js
.= "function swapOptions(frm, grpName, eleIndex, nbElements, arName)\n"
400 ." for (var i = 0; i < nbElements; i++) {\n"
401 ." ctl = frm[grpName+'['+i+']'];\n"
403 ." ctl = frm[grpName+'['+i+'][]'];\n"
405 ." if (i <= eleIndex) {\n"
406 ." n += \"_\"+ctl.value;\n"
408 ." ctl.length = 0;\n"
411 ." var t = eval(\"typeof(\"+arName + n +\")\");\n"
412 ." if (t != 'undefined') {\n"
413 ." var the_array = eval(arName+n);\n"
415 ." n = eleIndex + 1;\n"
416 ." ctl = frm[grpName+'['+ n +']'];\n"
418 ." ctl = frm[grpName+'['+ n +'][]'];\n"
420 ." ctl.style.display = 'inline';\n"
421 ." for (var i in the_array) {\n"
422 ." opt = new Option(the_array[i], i, false, false);\n"
423 ." ctl.options[j++] = opt;\n"
426 ." n = eleIndex + 1;\n"
427 ." ctl = frm[grpName+'['+n+']'];\n"
429 ." ctl = frm[grpName+'['+ n +'][]'];\n"
432 ." ctl.style.display = 'none';\n"
435 ." if (eleIndex+1 < nbElements) {\n"
436 ." swapOptions(frm, grpName, eleIndex+1, nbElements, arName);\n"
439 define('HTML_QUICKFORM_HIERSELECT_EXISTS', true);
442 include_once('HTML/QuickForm/Renderer/Default.php');
443 $renderer = new HTML_QuickForm_Renderer_Default();
444 $renderer->setElementTemplate('{element}');
445 parent
::accept($renderer);
447 if ( ! empty( $this->_js
) ) {
448 $result .= "<script type=\"text/javascript\">\n//<![CDATA[\n" . $this->_js
. "//]]>\n</script>";
460 * @param object An HTML_QuickForm_Renderer object
461 * @param bool Whether a group is required
462 * @param string An error message associated with a group
466 function accept(&$renderer, $required = false, $error = null)
468 $renderer->renderElement($this, $required, $error);
472 // {{{ onQuickFormEvent()
474 function onQuickFormEvent($event, $arg, &$caller)
476 if ('updateValue' == $event) {
477 // we need to call setValue() so that the secondary option
478 // matches the main option
479 return HTML_QuickForm_element
::onQuickFormEvent($event, $arg, $caller);
481 return parent
::onQuickFormEvent($event, $arg, $caller);
483 } // end func onQuickFormEvent
486 } // end class HTML_QuickForm_hierselect