Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 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
// +----------------------------------------------------------------------+
18
//
19
// $Id: Object.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
20
 
21
require_once('HTML/QuickForm/Renderer.php');
22
 
23
/**
24
 * A concrete renderer for HTML_QuickForm, makes an object from form contents
25
 *
26
 * Based on HTML_Quickform_Renderer_Array code
27
 *
28
 * @public
29
 */
30
class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer {
31
    /**
32
     * The object being generated
33
     * @var object $_obj
34
     */
35
    var $_obj= null;
36
 
37
    /**
38
     * Number of sections in the form (i.e. number of headers in it)
39
     * @var integer $_sectionCount
40
     */
41
    var $_sectionCount;
42
 
43
    /**
44
    * Current section number
45
    * @var integer $_currentSection
46
    */
47
    var $_currentSection;
48
 
49
    /**
50
    * Object representing current group
51
    * @var object $_currentGroup
52
    */
53
    var $_currentGroup = null;
54
 
55
    /**
56
     * Class of Element Objects
57
     * @var object $_elementType
58
     */
59
    var $_elementType = 'QuickFormElement';
60
 
61
    /**
62
    * Additional style information for different elements
63
    * @var array $_elementStyles
64
    */
65
    var $_elementStyles = array();
66
 
67
    /**
68
    * true: collect all hidden elements into string; false: process them as usual form elements
69
    * @var bool $_collectHidden
70
    */
71
    var $_collectHidden = false;
72
 
73
 
74
    /**
75
     * Constructor
76
     *
77
     * @param collecthidden bool    true: collect all hidden elements
78
     * @public
79
     */
80
    function HTML_QuickForm_Renderer_Object($collecthidden = false)
81
    {
82
        $this->HTML_QuickForm_Renderer();
83
        $this->_collectHidden = $collecthidden;
84
        $this->_obj = new QuickformForm;
85
    }
86
 
87
    /**
88
     * Return the rendered Object
89
     * @public
90
     */
91
    function toObject()
92
    {
93
        return $this->_obj;
94
    }
95
 
96
    /**
97
     * Set the class of the form elements.  Defaults to StdClass.
98
     * @param type string   Name of element class
99
     * @public
100
     */
101
    function setElementType($type) {
102
        $this->_elementType = $type;
103
    }
104
 
105
    function startForm(&$form)
106
    {
107
        $this->_obj->frozen = $form->isFrozen();
108
        $this->_obj->javascript = $form->getValidationScript();
109
        $this->_obj->attributes = $form->getAttributes(true);
110
        $this->_obj->requirednote = $form->getRequiredNote();
111
        $this->_obj->errors = new StdClass;
112
 
113
        if($this->_collectHidden) {
114
            $this->_obj->hidden = '';
115
        }
116
        $this->_elementIdx = 1;
117
        $this->_currentSection = null;
118
        $this->_sectionCount = 0;
119
    } // end func startForm
120
 
121
    function renderHeader(&$header)
122
    {
123
        $hobj = new StdClass;
124
        $hobj->header = $header->toHtml();
125
        $this->_obj->sections[$this->_sectionCount] = $hobj;
126
        $this->_currentSection = $this->_sectionCount++;
127
    }
128
    function renderElement(&$element, $required, $error)
129
    {
130
        $elObj = $this->_elementToObject($element, $required, $error);
131
        if(!empty($error)) {
132
            $name = $elObj->name;
133
            $this->_obj->errors->$name = $error;
134
        }
135
        $this->_storeObject($elObj);
136
    } // end func renderElement
137
 
138
    function renderHidden(&$element) {
139
        if($this->_collectHidden) {
140
            $this->_obj->hidden .= $element->toHtml() . "\n";
141
        } else {
142
            $this->renderElement($element, false, null);
143
        }
144
    } //end func renderHidden
145
 
146
    function startGroup(&$group, $required, $error)
147
    {
148
        $this->_currentGroup = $this->_elementToObject($group, $required, $error);
149
        if(!empty($error)) {
150
            $name = $this->_currentGroup->name;
151
            $this->_view->errors->$name = $error;
152
        }
153
    } // end func startGroup
154
 
155
    function finishGroup(&$group)
156
    {
157
        $this->_storeObject($this->_currentGroup);
158
        $this->_currentGroup = null;
159
    } // end func finishGroup
160
 
161
    /**
162
     * Creates an object representing an element
163
     *
164
     * @private
165
     * @param element object    An HTML_QuickForm_element object
166
     * @param required bool         Whether an element is required
167
     * @param error string    Error associated with the element
168
     * @return object
169
     */
170
    function _elementToObject(&$element, $required, $error)
171
    {
172
        if($this->_elementType) {
173
            $ret = new $this->_elementType;
174
        }
175
        $ret->name = $element->getName();
176
        $ret->value = $element->getValue();
177
        $ret->type = $element->getType();
178
        $ret->frozen = $element->isFrozen();
179
        $labels = $element->getLabel();
180
        if (is_array($labels)) {
181
            $ret->label = array_shift($labels);
182
            foreach ($labels as $key => $label) {
183
                $key = is_int($key)? $key + 2: $key;
184
                $ret->{'label_' . $key} = $label;
185
            }
186
        } else {
187
            $ret->label = $labels;
188
        }
189
        $ret->required = $required;
190
        $ret->error = $error;
191
 
192
        if(isset($this->_elementStyles[$ret->name])) {
193
            $ret->style = $this->_elementStyles[$ret->name];
194
            $ret->styleTemplate = "styles/". $ret->style .".html";
195
        }
196
        if($ret->type == 'group') {
197
            $ret->separator = $element->_separator;
198
            $ret->elements = array();
199
        } else {
200
            $ret->html = $element->toHtml();
201
        }
202
        return $ret;
203
    }
204
 
205
    /**
206
     * Stores an object representation of an element in the form array
207
     *
208
     * @private
209
     * @param elObj object     Object representation of an element
210
     * @return void
211
     */
212
    function _storeObject($elObj)
213
    {
214
        $name = $elObj->name;
215
        if(is_object($this->_currentGroup) && $elObj->type != 'group') {
216
            $this->_currentGroup->elements[] = $elObj;
217
        } elseif (isset($this->_currentSection)) {
218
            $this->_obj->sections[$this->_currentSection]->elements[] = $elObj;
219
        } else {
220
            $this->_obj->elements[] = $elObj;
221
        }
222
    }
223
 
224
    function setElementStyle($elementName, $styleName = null)
225
    {
226
        if(is_array($elementName)) {
227
            $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
228
        } else {
229
            $this->_elementStyles[$elementName] = $styleName;
230
        }
231
    }
232
 
233
} // end class HTML_QuickForm_Renderer_Object
234
 
235
 
236
 
237
/**
238
 * @abstract Long Description
239
 * This class represents the object passed to outputObject()
240
 *
241
 * Eg.
242
 * {form.outputJavaScript():h}
243
 * {form.outputHeader():h}
244
 *   <table>
245
 *     <tr>
246
 *       <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
247
 *     </tr>
248
 *   </table>
249
 * </form>
250
 *
251
 * @public
252
 */
253
class QuickformForm {
254
    /**
255
     * Whether the form has been frozen
256
     * @var boolean $frozen
257
     */
258
    var $frozen;
259
 
260
    /**
261
     * Javascript for client-side validation
262
     * @var string $javascript
263
     */
264
     var $javascript;
265
 
266
     /**
267
      * Attributes for form tag
268
      * @var string $attributes
269
      */
270
     var $attributes;
271
 
272
     /**
273
      * Note about required elements
274
      * @var string $requirednote
275
      */
276
     var $requirednote;
277
 
278
     /**
279
      * Collected html of all hidden variables
280
      * @var string $hidden
281
      */
282
     var $hidden;
283
 
284
     /**
285
      * Set if there were validation errors.
286
      * StdClass object with element names for keys and their
287
      * error messages as values
288
      * @var object $errors
289
      */
290
     var $errors;
291
 
292
     /**
293
      * Array of QuickformElementObject elements.  If there are headers in the form
294
      * this will be empty and the elements will be in the
295
      * separate sections
296
      * @var array $elements
297
      */
298
     var $elements;
299
 
300
     /**
301
      * Array of sections contained in the document
302
      * @var array $sections
303
      */
304
     var $sections;
305
 
306
     /**
307
      * Output &lt;form&gt; header
308
      * {form.outputHeader():h}
309
      * @return string    &lt;form attributes&gt;
310
      */
311
     function outputHeader() {
312
        $hdr = "<form " . $this->attributes . ">\n";
313
        return $hdr;
314
       }
315
     /**
316
      * Output form javascript
317
      * {form.outputJavaScript():h}
318
      * @return string    Javascript
319
      */
320
     function outputJavaScript() {
321
        return $this->javascript;
322
     }
323
} // end class QuickformForm
324
 
325
 
326
/**
327
 * Convenience class describing a form element.
328
 * The properties defined here will be available from
329
 * your flexy templates by referencing
330
 * {form.zip.label:h}, {form.zip.html:h}, etc.
331
 */
332
class QuickformElement {
333
 
334
    /**
335
     * Element name
336
     * @var string $name
337
     */
338
    var $name;
339
 
340
    /**
341
     * Element value
342
     * @var mixed $value
343
     */
344
    var $value;
345
 
346
    /**
347
     * Type of element
348
     * @var string $type
349
     */
350
    var $type;
351
 
352
    /**
353
     * Whether the element is frozen
354
     * @var boolean $frozen
355
     */
356
    var $frozen;
357
 
358
    /**
359
     * Label for the element
360
     * @var string $label
361
     */
362
    var $label;
363
 
364
    /**
365
     * Whether element is required
366
     * @var boolean $required
367
     */
368
    var $required;
369
 
370
    /**
371
     * Error associated with the element
372
     * @var string $error
373
     */
374
    var $error;
375
 
376
    /**
377
     * Some information about element style
378
     * @var string $style
379
     */
380
    var $style;
381
 
382
    /**
383
     * HTML for the element
384
     * @var string $html
385
     */
386
    var $html;
387
 
388
    /**
389
     * If element is a group, the group separator
390
     * @var mixed $separator
391
     */
392
    var $separator;
393
 
394
    /**
395
     * If element is a group, an array of subelements
396
     * @var array $elements
397
     */
398
    var $elements;
399
 
400
    function isType($type)
401
    {
402
        if($this->type == $type) {
403
            return true;
404
        } else {
405
            return false;
406
        }
407
    }
408
 
409
    function notFrozen()
410
    {
411
        if(!$this->frozen) {
412
            return true;
413
        } else {
414
            return false;
415
        }
416
    }
417
 
418
    function isButton()
419
    {
420
        if($this->type == "submit" || $this->type == "reset") {
421
            return true;
422
        } else {
423
            return false;
424
        }
425
    }
426
 
427
    function outputStyle()
428
    {
429
        $filename = "styles/".$this->style.".html";
430
        ob_start();
431
        HTML_Template_Flexy::staticQuickTemplate($filename, $this);
432
        $ret = ob_get_contents();
433
        ob_end_clean();
434
        return $ret;
435
    }
436
 
437
} // end class QuickformElement
438
?>