Subversion Repositories Applications.papyrus

Rev

Rev 320 | Details | Compare with Previous | 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
//
443 ddelon 19
// $Id: Object.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
320 jpm 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
 *
443 ddelon 28
 * @access public
320 jpm 29
 */
443 ddelon 30
class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer
31
{
320 jpm 32
    /**
33
     * The object being generated
34
     * @var object $_obj
35
     */
36
    var $_obj= null;
37
 
38
    /**
39
     * Number of sections in the form (i.e. number of headers in it)
40
     * @var integer $_sectionCount
41
     */
42
    var $_sectionCount;
43
 
44
    /**
45
    * Current section number
46
    * @var integer $_currentSection
47
    */
48
    var $_currentSection;
49
 
50
    /**
51
    * Object representing current group
52
    * @var object $_currentGroup
53
    */
54
    var $_currentGroup = null;
55
 
56
    /**
57
     * Class of Element Objects
58
     * @var object $_elementType
59
     */
60
    var $_elementType = 'QuickFormElement';
61
 
62
    /**
63
    * Additional style information for different elements
64
    * @var array $_elementStyles
65
    */
66
    var $_elementStyles = array();
67
 
68
    /**
69
    * true: collect all hidden elements into string; false: process them as usual form elements
70
    * @var bool $_collectHidden
71
    */
72
    var $_collectHidden = false;
73
 
74
 
75
    /**
76
     * Constructor
77
     *
78
     * @param collecthidden bool    true: collect all hidden elements
443 ddelon 79
     * @access public
320 jpm 80
     */
81
    function HTML_QuickForm_Renderer_Object($collecthidden = false)
82
    {
83
        $this->HTML_QuickForm_Renderer();
84
        $this->_collectHidden = $collecthidden;
85
        $this->_obj = new QuickformForm;
86
    }
87
 
88
    /**
89
     * Return the rendered Object
443 ddelon 90
     * @access public
320 jpm 91
     */
92
    function toObject()
93
    {
94
        return $this->_obj;
95
    }
96
 
97
    /**
443 ddelon 98
     * Set the class of the form elements.  Defaults to QuickformElement.
320 jpm 99
     * @param type string   Name of element class
443 ddelon 100
     * @access public
320 jpm 101
     */
443 ddelon 102
    function setElementType($type)
103
    {
320 jpm 104
        $this->_elementType = $type;
105
    }
106
 
107
    function startForm(&$form)
108
    {
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;
114
 
115
        if($this->_collectHidden) {
116
            $this->_obj->hidden = '';
117
        }
118
        $this->_elementIdx = 1;
119
        $this->_currentSection = null;
120
        $this->_sectionCount = 0;
121
    } // end func startForm
122
 
123
    function renderHeader(&$header)
124
    {
125
        $hobj = new StdClass;
126
        $hobj->header = $header->toHtml();
127
        $this->_obj->sections[$this->_sectionCount] = $hobj;
128
        $this->_currentSection = $this->_sectionCount++;
129
    }
443 ddelon 130
 
320 jpm 131
    function renderElement(&$element, $required, $error)
132
    {
133
        $elObj = $this->_elementToObject($element, $required, $error);
134
        if(!empty($error)) {
135
            $name = $elObj->name;
136
            $this->_obj->errors->$name = $error;
137
        }
138
        $this->_storeObject($elObj);
139
    } // end func renderElement
140
 
443 ddelon 141
    function renderHidden(&$element)
142
    {
320 jpm 143
        if($this->_collectHidden) {
144
            $this->_obj->hidden .= $element->toHtml() . "\n";
145
        } else {
146
            $this->renderElement($element, false, null);
147
        }
148
    } //end func renderHidden
149
 
150
    function startGroup(&$group, $required, $error)
151
    {
152
        $this->_currentGroup = $this->_elementToObject($group, $required, $error);
153
        if(!empty($error)) {
154
            $name = $this->_currentGroup->name;
443 ddelon 155
            $this->_obj->errors->$name = $error;
320 jpm 156
        }
157
    } // end func startGroup
158
 
159
    function finishGroup(&$group)
160
    {
161
        $this->_storeObject($this->_currentGroup);
162
        $this->_currentGroup = null;
163
    } // end func finishGroup
164
 
165
    /**
166
     * Creates an object representing an element
167
     *
443 ddelon 168
     * @access private
320 jpm 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
173
     */
174
    function _elementToObject(&$element, $required, $error)
175
    {
176
        if($this->_elementType) {
177
            $ret = new $this->_elementType;
178
        }
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;
189
            }
190
        } else {
191
            $ret->label = $labels;
192
        }
193
        $ret->required = $required;
194
        $ret->error = $error;
195
 
196
        if(isset($this->_elementStyles[$ret->name])) {
197
            $ret->style = $this->_elementStyles[$ret->name];
198
            $ret->styleTemplate = "styles/". $ret->style .".html";
199
        }
200
        if($ret->type == 'group') {
201
            $ret->separator = $element->_separator;
202
            $ret->elements = array();
203
        } else {
204
            $ret->html = $element->toHtml();
205
        }
206
        return $ret;
207
    }
208
 
209
    /**
210
     * Stores an object representation of an element in the form array
211
     *
443 ddelon 212
     * @access private
320 jpm 213
     * @param elObj object     Object representation of an element
214
     * @return void
215
     */
216
    function _storeObject($elObj)
217
    {
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;
225
        }
226
    }
227
 
228
    function setElementStyle($elementName, $styleName = null)
229
    {
230
        if(is_array($elementName)) {
231
            $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
232
        } else {
233
            $this->_elementStyles[$elementName] = $styleName;
234
        }
235
    }
236
 
237
} // end class HTML_QuickForm_Renderer_Object
238
 
239
 
240
 
241
/**
443 ddelon 242
 * Convenience class for the form object passed to outputObject()
320 jpm 243
 *
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>
253
 */
443 ddelon 254
class QuickformForm
255
{
256
   /**
257
    * Whether the form has been frozen
258
    * @var boolean $frozen
259
    */
260
    var $frozen;
320 jpm 261
 
443 ddelon 262
   /**
263
    * Javascript for client-side validation
264
    * @var string $javascript
265
    */
266
    var $javascript;
320 jpm 267
 
443 ddelon 268
   /**
269
    * Attributes for form tag
270
    * @var string $attributes
271
    */
272
    var $attributes;
320 jpm 273
 
443 ddelon 274
   /**
275
    * Note about required elements
276
    * @var string $requirednote
277
    */
278
    var $requirednote;
320 jpm 279
 
443 ddelon 280
   /**
281
    * Collected html of all hidden variables
282
    * @var string $hidden
283
    */
284
    var $hidden;
320 jpm 285
 
443 ddelon 286
   /**
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
291
    */
292
    var $errors;
320 jpm 293
 
443 ddelon 294
   /**
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
299
    */
300
    var $elements;
320 jpm 301
 
443 ddelon 302
   /**
303
    * Array of sections contained in the document
304
    * @var array $sections
305
    */
306
    var $sections;
307
 
308
   /**
309
    * Output &lt;form&gt; header
310
    * {form.outputHeader():h}
311
    * @return string    &lt;form attributes&gt;
312
    */
313
    function outputHeader()
314
    {
315
        return "<form " . $this->attributes . ">\n";
316
    }
317
 
318
   /**
319
    * Output form javascript
320
    * {form.outputJavaScript():h}
321
    * @return string    Javascript
322
    */
323
    function outputJavaScript()
324
    {
320 jpm 325
        return $this->javascript;
443 ddelon 326
    }
320 jpm 327
} // end class QuickformForm
328
 
329
 
330
/**
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.
335
 */
443 ddelon 336
class QuickformElement
337
{
320 jpm 338
    /**
339
     * Element name
340
     * @var string $name
341
     */
342
    var $name;
343
 
344
    /**
345
     * Element value
346
     * @var mixed $value
347
     */
348
    var $value;
349
 
350
    /**
351
     * Type of element
352
     * @var string $type
353
     */
354
    var $type;
355
 
356
    /**
357
     * Whether the element is frozen
358
     * @var boolean $frozen
359
     */
360
    var $frozen;
361
 
362
    /**
363
     * Label for the element
364
     * @var string $label
365
     */
366
    var $label;
367
 
368
    /**
369
     * Whether element is required
370
     * @var boolean $required
371
     */
372
    var $required;
373
 
374
    /**
375
     * Error associated with the element
376
     * @var string $error
377
     */
378
    var $error;
379
 
380
    /**
381
     * Some information about element style
382
     * @var string $style
383
     */
384
    var $style;
385
 
386
    /**
387
     * HTML for the element
388
     * @var string $html
389
     */
390
    var $html;
391
 
392
    /**
393
     * If element is a group, the group separator
394
     * @var mixed $separator
395
     */
396
    var $separator;
397
 
398
    /**
399
     * If element is a group, an array of subelements
400
     * @var array $elements
401
     */
402
    var $elements;
403
 
404
    function isType($type)
405
    {
443 ddelon 406
        return ($this->type == $type);
320 jpm 407
    }
443 ddelon 408
 
320 jpm 409
    function notFrozen()
410
    {
443 ddelon 411
        return !$this->frozen;
320 jpm 412
    }
413
 
414
    function isButton()
415
    {
443 ddelon 416
        return ($this->type == "submit" || $this->type == "reset");
320 jpm 417
    }
418
 
443 ddelon 419
 
420
   /**
421
    * XXX: why does it use Flexy when all other stuff here does not depend on it?
422
    */
320 jpm 423
    function outputStyle()
424
    {
425
        ob_start();
443 ddelon 426
        HTML_Template_Flexy::staticQuickTemplate('styles/' . $this->style . '.html', $this);
320 jpm 427
        $ret = ob_get_contents();
428
        ob_end_clean();
429
        return $ret;
430
    }
431
} // end class QuickformElement
443 ddelon 432
?>