| // +----------------------------------------------------------------------+ // // $Id$ require_once('HTML/QuickForm/Renderer.php'); /** * A concrete renderer for HTML_QuickForm, makes an object from form contents * * Based on HTML_Quickform_Renderer_Array code * * @access public */ class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer { /** * The object being generated * @var object $_obj */ var $_obj= null; /** * Number of sections in the form (i.e. number of headers in it) * @var integer $_sectionCount */ var $_sectionCount; /** * Current section number * @var integer $_currentSection */ var $_currentSection; /** * Object representing current group * @var object $_currentGroup */ var $_currentGroup = null; /** * Class of Element Objects * @var object $_elementType */ var $_elementType = 'QuickFormElement'; /** * Additional style information for different elements * @var array $_elementStyles */ var $_elementStyles = array(); /** * true: collect all hidden elements into string; false: process them as usual form elements * @var bool $_collectHidden */ var $_collectHidden = false; /** * Constructor * * @param collecthidden bool true: collect all hidden elements * @access public */ function HTML_QuickForm_Renderer_Object($collecthidden = false) { $this->HTML_QuickForm_Renderer(); $this->_collectHidden = $collecthidden; $this->_obj = new QuickformForm; } /** * Return the rendered Object * @access public */ function toObject() { return $this->_obj; } /** * Set the class of the form elements. Defaults to QuickformElement. * @param type string Name of element class * @access public */ function setElementType($type) { $this->_elementType = $type; } function startForm(&$form) { $this->_obj->frozen = $form->isFrozen(); $this->_obj->javascript = $form->getValidationScript(); $this->_obj->attributes = $form->getAttributes(true); $this->_obj->requirednote = $form->getRequiredNote(); $this->_obj->errors = new StdClass; if($this->_collectHidden) { $this->_obj->hidden = ''; } $this->_elementIdx = 1; $this->_currentSection = null; $this->_sectionCount = 0; } // end func startForm function renderHeader(&$header) { $hobj = new StdClass; $hobj->header = $header->toHtml(); $this->_obj->sections[$this->_sectionCount] = $hobj; $this->_currentSection = $this->_sectionCount++; } function renderElement(&$element, $required, $error) { $elObj = $this->_elementToObject($element, $required, $error); if(!empty($error)) { $name = $elObj->name; $this->_obj->errors->$name = $error; } $this->_storeObject($elObj); } // end func renderElement function renderHidden(&$element) { if($this->_collectHidden) { $this->_obj->hidden .= $element->toHtml() . "\n"; } else { $this->renderElement($element, false, null); } } //end func renderHidden function startGroup(&$group, $required, $error) { $this->_currentGroup = $this->_elementToObject($group, $required, $error); if(!empty($error)) { $name = $this->_currentGroup->name; $this->_obj->errors->$name = $error; } } // end func startGroup function finishGroup(&$group) { $this->_storeObject($this->_currentGroup); $this->_currentGroup = null; } // end func finishGroup /** * Creates an object representing an element * * @access private * @param element object An HTML_QuickForm_element object * @param required bool Whether an element is required * @param error string Error associated with the element * @return object */ function _elementToObject(&$element, $required, $error) { if($this->_elementType) { $ret = new $this->_elementType; } $ret->name = $element->getName(); $ret->value = $element->getValue(); $ret->type = $element->getType(); $ret->frozen = $element->isFrozen(); $labels = $element->getLabel(); if (is_array($labels)) { $ret->label = array_shift($labels); foreach ($labels as $key => $label) { $key = is_int($key)? $key + 2: $key; $ret->{'label_' . $key} = $label; } } else { $ret->label = $labels; } $ret->required = $required; $ret->error = $error; if(isset($this->_elementStyles[$ret->name])) { $ret->style = $this->_elementStyles[$ret->name]; $ret->styleTemplate = "styles/". $ret->style .".html"; } if($ret->type == 'group') { $ret->separator = $element->_separator; $ret->elements = array(); } else { $ret->html = $element->toHtml(); } return $ret; } /** * Stores an object representation of an element in the form array * * @access private * @param elObj object Object representation of an element * @return void */ function _storeObject($elObj) { $name = $elObj->name; if(is_object($this->_currentGroup) && $elObj->type != 'group') { $this->_currentGroup->elements[] = $elObj; } elseif (isset($this->_currentSection)) { $this->_obj->sections[$this->_currentSection]->elements[] = $elObj; } else { $this->_obj->elements[] = $elObj; } } function setElementStyle($elementName, $styleName = null) { if(is_array($elementName)) { $this->_elementStyles = array_merge($this->_elementStyles, $elementName); } else { $this->_elementStyles[$elementName] = $styleName; } } } // end class HTML_QuickForm_Renderer_Object /** * Convenience class for the form object passed to outputObject() * * Eg. * {form.outputJavaScript():h} * {form.outputHeader():h} *
{form.name.label:h} | {form.name.html:h} | *