2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 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 // | Author: Ron McClain <ron@humaniq.com> |
17 // +----------------------------------------------------------------------+
21 require_once('HTML/QuickForm/Renderer.php');
24 * A concrete renderer for HTML_QuickForm, makes an object from form contents
26 * Based on HTML_Quickform_Renderer_Array code
30 class HTML_QuickForm_Renderer_Object
extends HTML_QuickForm_Renderer
33 * The object being generated
39 * Number of sections in the form (i.e. number of headers in it)
40 * @var integer $_sectionCount
45 * Current section number
46 * @var integer $_currentSection
51 * Object representing current group
52 * @var object $_currentGroup
54 var $_currentGroup = null;
57 * Class of Element Objects
58 * @var object $_elementType
60 var $_elementType = 'QuickFormElement';
63 * Additional style information for different elements
64 * @var array $_elementStyles
66 var $_elementStyles = array();
69 * true: collect all hidden elements into string; false: process them as usual form elements
70 * @var bool $_collectHidden
72 var $_collectHidden = false;
78 * @param collecthidden bool true: collect all hidden elements
81 function HTML_QuickForm_Renderer_Object($collecthidden = false)
83 $this->HTML_QuickForm_Renderer();
84 $this->_collectHidden
= $collecthidden;
85 $this->_obj
= new QuickformForm
;
89 * Return the rendered Object
98 * Set the class of the form elements. Defaults to QuickformElement.
99 * @param type string Name of element class
102 function setElementType($type)
104 $this->_elementType
= $type;
107 function startForm(&$form)
109 $this->_obj
->frozen
= $form->isFrozen();
110 $this->_obj
->javascript
= $form->getValidationScript();
111 $this->_obj
->attributes
= $form->getAttributes(true);
112 $this->_obj
->requirednote
= $form->getRequiredNote();
113 $this->_obj
->errors
= new StdClass
;
115 if($this->_collectHidden
) {
116 $this->_obj
->hidden
= '';
118 $this->_elementIdx
= 1;
119 $this->_currentSection
= null;
120 $this->_sectionCount
= 0;
121 } // end func startForm
123 function renderHeader(&$header)
125 $hobj = new StdClass
;
126 $hobj->header
= $header->toHtml();
127 $this->_obj
->sections
[$this->_sectionCount
] = $hobj;
128 $this->_currentSection
= $this->_sectionCount++
;
131 function renderElement(&$element, $required, $error)
133 $elObj = $this->_elementToObject($element, $required, $error);
135 $name = $elObj->name
;
136 $this->_obj
->errors
->$name = $error;
138 $this->_storeObject($elObj);
139 } // end func renderElement
141 function renderHidden(&$element)
143 if($this->_collectHidden
) {
144 $this->_obj
->hidden
.= $element->toHtml() . "\n";
146 $this->renderElement($element, false, null);
148 } //end func renderHidden
150 function startGroup(&$group, $required, $error)
152 $this->_currentGroup
= $this->_elementToObject($group, $required, $error);
154 $name = $this->_currentGroup
->name
;
155 $this->_obj
->errors
->$name = $error;
157 } // end func startGroup
159 function finishGroup(&$group)
161 $this->_storeObject($this->_currentGroup
);
162 $this->_currentGroup
= null;
163 } // end func finishGroup
166 * Creates an object representing an element
169 * @param element object An HTML_QuickForm_element object
170 * @param required bool Whether an element is required
171 * @param error string Error associated with the element
174 function _elementToObject(&$element, $required, $error)
176 if($this->_elementType
) {
177 $ret = new $this->_elementType
;
179 $ret->name
= $element->getName();
180 $ret->value
= $element->getValue();
181 $ret->type
= $element->getType();
182 $ret->frozen
= $element->isFrozen();
183 $labels = $element->getLabel();
184 if (is_array($labels)) {
185 $ret->label
= array_shift($labels);
186 foreach ($labels as $key => $label) {
187 $key = is_int($key)?
$key +
2: $key;
188 $ret->{'label_' . $key} = $label;
191 $ret->label
= $labels;
193 $ret->required
= $required;
194 $ret->error
= $error;
196 if(isset($this->_elementStyles
[$ret->name
])) {
197 $ret->style
= $this->_elementStyles
[$ret->name
];
198 $ret->styleTemplate
= "styles/". $ret->style
.".html";
200 if($ret->type
== 'group') {
201 $ret->separator
= $element->_separator
;
202 $ret->elements
= array();
204 $ret->html
= $element->toHtml();
210 * Stores an object representation of an element in the form array
213 * @param elObj object Object representation of an element
216 function _storeObject($elObj)
218 $name = $elObj->name
;
219 if(is_object($this->_currentGroup
) && $elObj->type
!= 'group') {
220 $this->_currentGroup
->elements
[] = $elObj;
221 } elseif (isset($this->_currentSection
)) {
222 $this->_obj
->sections
[$this->_currentSection
]->elements
[] = $elObj;
224 $this->_obj
->elements
[] = $elObj;
228 function setElementStyle($elementName, $styleName = null)
230 if(is_array($elementName)) {
231 $this->_elementStyles
= array_merge($this->_elementStyles
, $elementName);
233 $this->_elementStyles
[$elementName] = $styleName;
237 } // end class HTML_QuickForm_Renderer_Object
242 * Convenience class for the form object passed to outputObject()
245 * {form.outputJavaScript():h}
246 * {form.outputHeader():h}
249 * <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
257 * Whether the form has been frozen
258 * @var boolean $frozen
263 * Javascript for client-side validation
264 * @var string $javascript
269 * Attributes for form tag
270 * @var string $attributes
275 * Note about required elements
276 * @var string $requirednote
281 * Collected html of all hidden variables
282 * @var string $hidden
287 * Set if there were validation errors.
288 * StdClass object with element names for keys and their
289 * error messages as values
290 * @var object $errors
295 * Array of QuickformElementObject elements. If there are headers in the form
296 * this will be empty and the elements will be in the
298 * @var array $elements
303 * Array of sections contained in the document
304 * @var array $sections
309 * Output <form> header
310 * {form.outputHeader():h}
311 * @return string <form attributes>
313 function outputHeader()
315 return "<form " . $this->attributes
. ">\n";
319 * Output form javascript
320 * {form.outputJavaScript():h}
321 * @return string Javascript
323 function outputJavaScript()
325 return $this->javascript
;
327 } // end class QuickformForm
331 * Convenience class describing a form element.
332 * The properties defined here will be available from
333 * your flexy templates by referencing
334 * {form.zip.label:h}, {form.zip.html:h}, etc.
336 class QuickformElement
357 * Whether the element is frozen
358 * @var boolean $frozen
363 * Label for the element
369 * Whether element is required
370 * @var boolean $required
375 * Error associated with the element
381 * Some information about element style
387 * HTML for the element
393 * If element is a group, the group separator
394 * @var mixed $separator
399 * If element is a group, an array of subelements
400 * @var array $elements
404 function isType($type)
406 return ($this->type
== $type);
411 return !$this->frozen
;
416 return ($this->type
== "submit" ||
$this->type
== "reset");
421 * XXX: why does it use Flexy when all other stuff here does not depend on it?
423 function outputStyle()
426 HTML_Template_Flexy
::staticQuickTemplate('styles/' . $this->style
. '.html', $this);
427 $ret = ob_get_contents();
431 } // end class QuickformElement