1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4.0 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | [email protected] so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Ron McClain <[email protected]> |
// +----------------------------------------------------------------------+
//
// $Id: Object.php,v 1.2 2003/11/03 12:53:01 avb Exp $
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
*
* @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
* @public
*/
function HTML_QuickForm_Renderer_Object($collecthidden = false)
{
$this->HTML_QuickForm_Renderer();
$this->_collectHidden = $collecthidden;
$this->_obj = new QuickformForm;
}
/**
* Return the rendered Object
* @public
*/
function toObject()
{
return $this->_obj;
}
/**
* Set the class of the form elements. Defaults to StdClass.
* @param type string Name of element class
* @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->_view->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
*
* @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
*
* @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
/**
* @abstract Long Description
* This class represents the object passed to outputObject()
*
* Eg.
* {form.outputJavaScript():h}
* {form.outputHeader():h}
* <table>
* <tr>
* <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
* </tr>
* </table>
* </form>
*
* @public
*/
class QuickformForm {
/**
* Whether the form has been frozen
* @var boolean $frozen
*/
var $frozen;
/**
* Javascript for client-side validation
* @var string $javascript
*/
var $javascript;
/**
* Attributes for form tag
* @var string $attributes
*/
var $attributes;
/**
* Note about required elements
* @var string $requirednote
*/
var $requirednote;
/**
* Collected html of all hidden variables
* @var string $hidden
*/
var $hidden;
/**
* Set if there were validation errors.
* StdClass object with element names for keys and their
* error messages as values
* @var object $errors
*/
var $errors;
/**
* Array of QuickformElementObject elements. If there are headers in the form
* this will be empty and the elements will be in the
* separate sections
* @var array $elements
*/
var $elements;
/**
* Array of sections contained in the document
* @var array $sections
*/
var $sections;
/**
* Output <form> header
* {form.outputHeader():h}
* @return string <form attributes>
*/
function outputHeader() {
$hdr = "<form " . $this->attributes . ">\n";
return $hdr;
}
/**
* Output form javascript
* {form.outputJavaScript():h}
* @return string Javascript
*/
function outputJavaScript() {
return $this->javascript;
}
} // end class QuickformForm
/**
* Convenience class describing a form element.
* The properties defined here will be available from
* your flexy templates by referencing
* {form.zip.label:h}, {form.zip.html:h}, etc.
*/
class QuickformElement {
/**
* Element name
* @var string $name
*/
var $name;
/**
* Element value
* @var mixed $value
*/
var $value;
/**
* Type of element
* @var string $type
*/
var $type;
/**
* Whether the element is frozen
* @var boolean $frozen
*/
var $frozen;
/**
* Label for the element
* @var string $label
*/
var $label;
/**
* Whether element is required
* @var boolean $required
*/
var $required;
/**
* Error associated with the element
* @var string $error
*/
var $error;
/**
* Some information about element style
* @var string $style
*/
var $style;
/**
* HTML for the element
* @var string $html
*/
var $html;
/**
* If element is a group, the group separator
* @var mixed $separator
*/
var $separator;
/**
* If element is a group, an array of subelements
* @var array $elements
*/
var $elements;
function isType($type)
{
if($this->type == $type) {
return true;
} else {
return false;
}
}
function notFrozen()
{
if(!$this->frozen) {
return true;
} else {
return false;
}
}
function isButton()
{
if($this->type == "submit" || $this->type == "reset") {
return true;
} else {
return false;
}
}
function outputStyle()
{
$filename = "styles/".$this->style.".html";
ob_start();
HTML_Template_Flexy::staticQuickTemplate($filename, $this);
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}
} // end class QuickformElement
?>
|