2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * A concrete renderer for HTML_QuickForm, makes an object from form contents
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package HTML_QuickForm
17 * @author Ron McClain <ron@humaniq.com>
18 * @copyright 2001-2011 The PHP Group
19 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @link http://pear.php.net/package/HTML_QuickForm
25 * An abstract base class for QuickForm renderers
27 require_once 'HTML/QuickForm/Renderer.php';
30 * A concrete renderer for HTML_QuickForm, makes an object from form contents
32 * Based on HTML_Quickform_Renderer_Array code
35 * @package HTML_QuickForm
36 * @author Ron McClain <ron@humaniq.com>
37 * @version Release: 3.2.16
40 class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer
46 * The object being generated
52 * Number of sections in the form (i.e. number of headers in it)
53 * @var integer $_sectionCount
58 * Current section number
59 * @var integer $_currentSection
64 * Object representing current group
65 * @var object $_currentGroup
67 var $_currentGroup = null;
70 * Class of Element Objects
71 * @var object $_elementType
73 var $_elementType = 'QuickFormElement';
76 * Additional style information for different elements
77 * @var array $_elementStyles
79 var $_elementStyles = array();
82 * true: collect all hidden elements into string; false: process them as usual form elements
83 * @var bool $_collectHidden
85 var $_collectHidden = false;
92 * @param bool true: collect all hidden elements
95 function HTML_QuickForm_Renderer_Object($collecthidden = false)
97 $this->HTML_QuickForm_Renderer();
98 $this->_collectHidden = $collecthidden;
99 $this->_obj = new QuickformForm;
103 * Return the rendered Object
112 * Set the class of the form elements. Defaults to QuickformElement.
113 * @param string Name of element class
116 function setElementType($type)
118 $this->_elementType = $type;
121 function startForm(&$form)
123 $this->_obj->frozen = $form->isFrozen();
124 $this->_obj->javascript = $form->getValidationScript();
125 $this->_obj->attributes = $form->getAttributes(true);
126 $this->_obj->requirednote = $form->getRequiredNote();
127 $this->_obj->errors = new StdClass;
129 if($this->_collectHidden) {
130 $this->_obj->hidden = '';
132 $this->_elementIdx = 1;
133 $this->_currentSection = null;
134 $this->_sectionCount = 0;
135 } // end func startForm
137 function renderHeader(&$header)
139 $hobj = new StdClass;
140 $hobj->header = $header->toHtml();
141 $this->_obj->sections[$this->_sectionCount] = $hobj;
142 $this->_currentSection = $this->_sectionCount++;
145 function renderElement(&$element, $required, $error)
147 $elObj = $this->_elementToObject($element, $required, $error);
149 $name = $elObj->name;
150 $this->_obj->errors->$name = $error;
152 $this->_storeObject($elObj);
153 } // end func renderElement
155 function renderHidden(&$element)
157 if($this->_collectHidden) {
158 $this->_obj->hidden .= $element->toHtml() . "\n";
160 $this->renderElement($element, false, null);
162 } //end func renderHidden
164 function startGroup(&$group, $required, $error)
166 $this->_currentGroup = $this->_elementToObject($group, $required, $error);
168 $name = $this->_currentGroup->name;
169 $this->_obj->errors->$name = $error;
171 } // end func startGroup
173 function finishGroup(&$group)
175 $this->_storeObject($this->_currentGroup);
176 $this->_currentGroup = null;
177 } // end func finishGroup
180 * Creates an object representing an element
183 * @param HTML_QuickForm_element form element being rendered
184 * @param required bool Whether an element is required
185 * @param error string Error associated with the element
188 function _elementToObject(&$element, $required, $error)
190 if($this->_elementType) {
191 $ret = new $this->_elementType;
193 $ret->name = $element->getName();
194 $ret->value = $element->getValue();
195 $ret->type = $element->getType();
196 $ret->frozen = $element->isFrozen();
197 $labels = $element->getLabel();
198 if (is_array($labels)) {
199 $ret->label = array_shift($labels);
200 foreach ($labels as $key => $label) {
201 $key = is_int($key)? $key + 2: $key;
202 $ret->{'label_' . $key} = $label;
205 $ret->label = $labels;
207 $ret->required = $required;
208 $ret->error = $error;
210 if(isset($this->_elementStyles[$ret->name])) {
211 $ret->style = $this->_elementStyles[$ret->name];
212 $ret->styleTemplate = "styles/". $ret->style .".html";
214 if($ret->type == 'group') {
215 $ret->separator = $element->_separator;
216 $ret->elements = array();
218 $ret->html = $element->toHtml();
224 * Stores an object representation of an element in the form array
227 * @param QuickformElement Object representation of an element
230 function _storeObject($elObj)
232 $name = $elObj->name;
233 if(is_object($this->_currentGroup) && $elObj->type != 'group') {
234 $this->_currentGroup->elements[] = $elObj;
235 } elseif (isset($this->_currentSection)) {
236 $this->_obj->sections[$this->_currentSection]->elements[] = $elObj;
238 $this->_obj->elements[] = $elObj;
242 function setElementStyle($elementName, $styleName = null)
244 if(is_array($elementName)) {
245 $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
247 $this->_elementStyles[$elementName] = $styleName;
251 } // end class HTML_QuickForm_Renderer_Object
256 * Convenience class for the form object passed to outputObject()
260 * {form.outputJavaScript():h}
261 * {form.outputHeader():h}
264 * <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
271 * @package HTML_QuickForm
272 * @author Ron McClain <ron@humaniq.com>
273 * @version Release: 3.2.16
279 * Whether the form has been frozen
280 * @var boolean $frozen
285 * Javascript for client-side validation
286 * @var string $javascript
291 * Attributes for form tag
292 * @var string $attributes
297 * Note about required elements
298 * @var string $requirednote
303 * Collected html of all hidden variables
304 * @var string $hidden
309 * Set if there were validation errors.
310 * StdClass object with element names for keys and their
311 * error messages as values
312 * @var object $errors
317 * Array of QuickformElementObject elements. If there are headers in the form
318 * this will be empty and the elements will be in the
320 * @var array $elements
325 * Array of sections contained in the document
326 * @var array $sections
331 * Output <form> header
332 * {form.outputHeader():h}
333 * @return string <form attributes>
335 function outputHeader()
337 return "<form " . $this->attributes . ">\n";
341 * Output form javascript
342 * {form.outputJavaScript():h}
343 * @return string Javascript
345 function outputJavaScript()
347 return $this->javascript;
349 } // end class QuickformForm
353 * Convenience class describing a form element.
355 * The properties defined here will be available from
356 * your flexy templates by referencing
357 * {form.zip.label:h}, {form.zip.html:h}, etc.
360 * @package HTML_QuickForm
361 * @author Ron McClain <ron@humaniq.com>
362 * @version Release: 3.2.16
365 class QuickformElement
386 * Whether the element is frozen
387 * @var boolean $frozen
392 * Label for the element
398 * Whether element is required
399 * @var boolean $required
404 * Error associated with the element
410 * Some information about element style
416 * HTML for the element
422 * If element is a group, the group separator
423 * @var mixed $separator
428 * If element is a group, an array of subelements
429 * @var array $elements
433 function isType($type)
435 return ($this->type == $type);
440 return !$this->frozen;
445 return ($this->type == "submit" || $this->type == "reset");
450 * XXX: why does it use Flexy when all other stuff here does not depend on it?
452 function outputStyle()
455 HTML_Template_Flexy::staticQuickTemplate('styles/' . $this->style . '.html', $this);
456 $ret = ob_get_contents();
460 } // end class QuickformElement