adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / Renderer / Object.php
blob9ed495c7c643de22d0e70bc7c528f3eb64b5fe40
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
4 // | PHP version 4.0 |
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 // +----------------------------------------------------------------------+
19 // $Id$
21 require_once('HTML/QuickForm/Renderer.php');
23 /**
24 * A concrete renderer for HTML_QuickForm, makes an object from form contents
26 * Based on HTML_Quickform_Renderer_Array code
28 * @access public
30 class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer
32 /**
33 * The object being generated
34 * @var object $_obj
36 var $_obj= null;
38 /**
39 * Number of sections in the form (i.e. number of headers in it)
40 * @var integer $_sectionCount
42 var $_sectionCount;
44 /**
45 * Current section number
46 * @var integer $_currentSection
48 var $_currentSection;
50 /**
51 * Object representing current group
52 * @var object $_currentGroup
54 var $_currentGroup = null;
56 /**
57 * Class of Element Objects
58 * @var object $_elementType
60 var $_elementType = 'QuickFormElement';
62 /**
63 * Additional style information for different elements
64 * @var array $_elementStyles
66 var $_elementStyles = array();
68 /**
69 * true: collect all hidden elements into string; false: process them as usual form elements
70 * @var bool $_collectHidden
72 var $_collectHidden = false;
75 /**
76 * Constructor
78 * @param collecthidden bool true: collect all hidden elements
79 * @access public
81 function HTML_QuickForm_Renderer_Object($collecthidden = false)
83 $this->HTML_QuickForm_Renderer();
84 $this->_collectHidden = $collecthidden;
85 $this->_obj = new QuickformForm;
88 /**
89 * Return the rendered Object
90 * @access public
92 function toObject()
94 return $this->_obj;
97 /**
98 * Set the class of the form elements. Defaults to QuickformElement.
99 * @param type string Name of element class
100 * @access public
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);
134 if(!empty($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";
145 } else {
146 $this->renderElement($element, false, null);
148 } //end func renderHidden
150 function startGroup(&$group, $required, $error)
152 $this->_currentGroup = $this->_elementToObject($group, $required, $error);
153 if(!empty($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
168 * @access private
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
172 * @return object
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;
190 } else {
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();
203 } else {
204 $ret->html = $element->toHtml();
206 return $ret;
209 /**
210 * Stores an object representation of an element in the form array
212 * @access private
213 * @param elObj object Object representation of an element
214 * @return void
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;
223 } else {
224 $this->_obj->elements[] = $elObj;
228 function setElementStyle($elementName, $styleName = null)
230 if(is_array($elementName)) {
231 $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
232 } else {
233 $this->_elementStyles[$elementName] = $styleName;
237 } // end class HTML_QuickForm_Renderer_Object
242 * Convenience class for the form object passed to outputObject()
244 * Eg.
245 * {form.outputJavaScript():h}
246 * {form.outputHeader():h}
247 * <table>
248 * <tr>
249 * <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
250 * </tr>
251 * </table>
252 * </form>
254 class QuickformForm
257 * Whether the form has been frozen
258 * @var boolean $frozen
260 var $frozen;
263 * Javascript for client-side validation
264 * @var string $javascript
266 var $javascript;
269 * Attributes for form tag
270 * @var string $attributes
272 var $attributes;
275 * Note about required elements
276 * @var string $requirednote
278 var $requirednote;
281 * Collected html of all hidden variables
282 * @var string $hidden
284 var $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
292 var $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
297 * separate sections
298 * @var array $elements
300 var $elements;
303 * Array of sections contained in the document
304 * @var array $sections
306 var $sections;
309 * Output &lt;form&gt; header
310 * {form.outputHeader():h}
311 * @return string &lt;form attributes&gt;
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
339 * Element name
340 * @var string $name
342 var $name;
345 * Element value
346 * @var mixed $value
348 var $value;
351 * Type of element
352 * @var string $type
354 var $type;
357 * Whether the element is frozen
358 * @var boolean $frozen
360 var $frozen;
363 * Label for the element
364 * @var string $label
366 var $label;
369 * Whether element is required
370 * @var boolean $required
372 var $required;
375 * Error associated with the element
376 * @var string $error
378 var $error;
381 * Some information about element style
382 * @var string $style
384 var $style;
387 * HTML for the element
388 * @var string $html
390 var $html;
393 * If element is a group, the group separator
394 * @var mixed $separator
396 var $separator;
399 * If element is a group, an array of subelements
400 * @var array $elements
402 var $elements;
404 function isType($type)
406 return ($this->type == $type);
409 function notFrozen()
411 return !$this->frozen;
414 function isButton()
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()
425 ob_start();
426 HTML_Template_Flexy::staticQuickTemplate('styles/' . $this->style . '.html', $this);
427 $ret = ob_get_contents();
428 ob_end_clean();
429 return $ret;
431 } // end class QuickformElement