2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
22 require_once("HTML/QuickForm/element.php");
25 * HTML class for a form element group
27 * @author Adam Daniel <adaniel1@eesus.jnj.com>
28 * @author Bertrand Mansion <bmansion@mamasam.com>
33 class HTML_QuickForm_group
extends HTML_QuickForm_element
46 * Array of grouped elements
51 var $_elements = array();
54 * String to separate elements
59 var $_separator = null;
62 * Required elements in this group
67 var $_required = array();
70 * Whether to change elements' names to $groupName[$elementName] or leave them as is
75 var $_appendName = true;
83 * @param string $elementName (optional)Group name
84 * @param array $elementLabel (optional)Group label
85 * @param array $elements (optional)Group elements
86 * @param mixed $separator (optional)Use a string for one separator,
87 * use an array to alternate the separators.
88 * @param bool $appendName (optional)whether to change elements' names to
89 * the form $groupName[$elementName] or leave
95 function HTML_QuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true)
97 $this->HTML_QuickForm_element($elementName, $elementLabel);
98 $this->_type
= 'group';
99 if (isset($elements) && is_array($elements)) {
100 $this->setElements($elements);
102 if (isset($separator)) {
103 $this->_separator
= $separator;
105 if (isset($appendName)) {
106 $this->_appendName
= $appendName;
114 * Sets the group name
116 * @param string $name Group name
121 function setName($name)
123 $this->_name
= $name;
130 * Returns the group name
145 * Sets values for group's elements
147 * @param mixed Values for group's elements
152 function setValue($value)
154 $this->_createElementsIfNotExist();
155 foreach (array_keys($this->_elements
) as $key) {
156 if (!$this->_appendName
) {
157 $v = $this->_elements
[$key]->_findValue($value);
159 $this->_elements
[$key]->onQuickFormEvent('setGroupValue', $v, $this);
163 $elementName = $this->_elements
[$key]->getName();
164 $index = strlen($elementName) ?
$elementName : $key;
165 if (is_array($value)) {
166 if (isset($value[$index])) {
167 $this->_elements
[$key]->onQuickFormEvent('setGroupValue', $value[$index], $this);
169 } elseif (isset($value)) {
170 $this->_elements
[$key]->onQuickFormEvent('setGroupValue', $value, $this);
174 } //end func setValue
180 * Returns the value of the group
189 foreach (array_keys($this->_elements
) as $key) {
190 $element =& $this->_elements
[$key];
191 switch ($element->getType()) {
193 $v = $element->getChecked()?
$element->getValue(): null;
196 $v = $element->getChecked()?
true: null;
199 $v = $element->getValue();
202 $elementName = $element->getName();
203 if (is_null($elementName)) {
206 if (!is_array($value)) {
207 $value = is_null($value)?
array(): array($value);
209 if ('' === $elementName) {
212 $value[$elementName] = $v;
218 } // end func getValue
224 * Sets the grouped elements
226 * @param array $elements Array of elements
231 function setElements($elements)
233 $this->_elements
= array_values($elements);
234 if ($this->_flagFrozen
) {
237 } // end func setElements
243 * Gets the grouped elements
249 function &getElements()
251 $this->_createElementsIfNotExist();
252 return $this->_elements
;
253 } // end func getElements
256 // {{{ getGroupType()
259 * Gets the group type based on its elements
260 * Will return 'mixed' if elements contained in the group
261 * are of different types.
264 * @return string group elements type
266 function getGroupType()
268 $this->_createElementsIfNotExist();
270 foreach (array_keys($this->_elements
) as $key) {
271 $type = $this->_elements
[$key]->getType();
272 if ($type != $prevType && $prevType != '') {
278 } // end func getGroupType
284 * Returns Html for the group
292 include_once('HTML/QuickForm/Renderer/Default.php');
293 $renderer =& new HTML_QuickForm_Renderer_Default();
294 $renderer->setElementTemplate('{element}');
295 $this->accept($renderer);
296 return $renderer->toHtml();
300 // {{{ getElementName()
303 * Returns the element name inside the group such as found in the html form
305 * @param mixed $index Element name or element index in the group
308 * @return mixed string with element name, false if not found
310 function getElementName($index)
312 $this->_createElementsIfNotExist();
313 $elementName = false;
314 if (is_int($index) && isset($this->_elements
[$index])) {
315 $elementName = $this->_elements
[$index]->getName();
316 if (isset($elementName) && $elementName == '') {
317 $elementName = $index;
319 if ($this->_appendName
) {
320 if (is_null($elementName)) {
321 $elementName = $this->getName();
323 $elementName = $this->getName().'['.$elementName.']';
327 } elseif (is_string($index)) {
328 foreach (array_keys($this->_elements
) as $key) {
329 $elementName = $this->_elements
[$key]->getName();
330 if ($index == $elementName) {
331 if ($this->_appendName
) {
332 $elementName = $this->getName().'['.$elementName.']';
335 } elseif ($this->_appendName
&& $this->getName().'['.$elementName.']' == $index) {
341 } //end func getElementName
344 // {{{ getFrozenHtml()
347 * Returns the value of field without HTML tags
353 function getFrozenHtml()
356 $this->_createElementsIfNotExist();
357 foreach (array_keys($this->_elements
) as $key) {
358 if (false === ($flags[$key] = $this->_elements
[$key]->isFrozen())) {
359 $this->_elements
[$key]->freeze();
362 $html = $this->toHtml();
363 foreach (array_keys($this->_elements
) as $key) {
365 $this->_elements
[$key]->unfreeze();
369 } //end func getFrozenHtml
372 // {{{ onQuickFormEvent()
375 * Called by HTML_QuickForm whenever form event is made on this element
377 * @param string $event Name of event
378 * @param mixed $arg event arguments
379 * @param object $caller calling object
384 function onQuickFormEvent($event, $arg, &$caller)
388 $this->_createElementsIfNotExist();
389 foreach (array_keys($this->_elements
) as $key) {
390 if ($this->_appendName
) {
391 $elementName = $this->_elements
[$key]->getName();
392 if (is_null($elementName)) {
393 $this->_elements
[$key]->setName($this->getName());
394 } elseif ('' === $elementName) {
395 $this->_elements
[$key]->setName($this->getName() . '[' . $key . ']');
397 $this->_elements
[$key]->setName($this->getName() . '[' . $elementName . ']');
400 $this->_elements
[$key]->onQuickFormEvent('updateValue', $arg, $caller);
401 if ($this->_appendName
) {
402 $this->_elements
[$key]->setName($elementName);
408 parent
::onQuickFormEvent($event, $arg, $caller);
411 } // end func onQuickFormEvent
419 * @param object An HTML_QuickForm_Renderer object
420 * @param bool Whether a group is required
421 * @param string An error message associated with a group
425 function accept(&$renderer, $required = false, $error = null)
427 $this->_createElementsIfNotExist();
428 $renderer->startGroup($this, $required, $error);
429 $name = $this->getName();
430 foreach (array_keys($this->_elements
) as $key) {
431 $element =& $this->_elements
[$key];
433 if ($this->_appendName
) {
434 $elementName = $element->getName();
435 if (isset($elementName)) {
436 $element->setName($name . '['. (strlen($elementName)?
$elementName: $key) .']');
438 $element->setName($name);
442 $required = !$element->isFrozen() && in_array($element->getName(), $this->_required
);
444 $element->accept($renderer, $required);
446 // restore the element's name
447 if ($this->_appendName
) {
448 $element->setName($elementName);
451 $renderer->finishGroup($this);
458 * As usual, to get the group's value we access its elements and call
459 * their exportValue() methods
461 function exportValue(&$submitValues, $assoc = false)
464 foreach (array_keys($this->_elements
) as $key) {
465 $elementName = $this->_elements
[$key]->getName();
466 if ($this->_appendName
) {
467 if (is_null($elementName)) {
468 $this->_elements
[$key]->setName($this->getName());
469 } elseif ('' === $elementName) {
470 $this->_elements
[$key]->setName($this->getName() . '[' . $key . ']');
472 $this->_elements
[$key]->setName($this->getName() . '[' . $elementName . ']');
475 $v = $this->_elements
[$key]->exportValue($submitValues, $assoc);
476 if ($this->_appendName
) {
477 $this->_elements
[$key]->setName($elementName);
480 // Make $value an array, we will use it like one
481 if (null === $value) {
485 // just like HTML_QuickForm::exportValues()
486 $value = HTML_QuickForm
::arrayMerge($value, $v);
488 // just like getValue(), but should work OK every time here
489 if (is_null($elementName)) {
491 } elseif ('' === $elementName) {
494 $value[$elementName] = $v;
499 // do not pass the value through _prepareValue, we took care of this already
504 // {{{ _createElements()
507 * Creates the group's elements.
509 * This should be overriden by child classes that need to create their
510 * elements. The method will be called automatically when needed, calling
511 * it from the constructor is discouraged as the constructor is usually
512 * called _twice_ on element creation, first time with _no_ parameters.
517 function _createElements()
523 // {{{ _createElementsIfNotExist()
526 * A wrapper around _createElements()
528 * This method calls _createElements() if the group's _elements array
529 * is empty. It also performs some updates, e.g. freezes the created
530 * elements if the group is already frozen.
534 function _createElementsIfNotExist()
536 if (empty($this->_elements
)) {
537 $this->_createElements();
538 if ($this->_flagFrozen
) {
550 foreach (array_keys($this->_elements
) as $key) {
551 $this->_elements
[$key]->freeze();
561 foreach (array_keys($this->_elements
) as $key) {
562 $this->_elements
[$key]->unfreeze();
567 // {{{ setPersistantFreeze()
569 function setPersistantFreeze($persistant = false)
571 parent
::setPersistantFreeze($persistant);
572 foreach (array_keys($this->_elements
) as $key) {
573 $this->_elements
[$key]->setPersistantFreeze($persistant);
578 } //end class HTML_QuickForm_group