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: ObjectFlexy.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
20
 
21
require_once("HTML/QuickForm/Renderer/Object.php");
22
 
23
/**
24
 * @abstract Long Description
25
 * A static renderer for HTML_Quickform.  Makes a QuickFormFlexyObject
26
 * from the form content suitable for use with a Flexy template
27
 *
28
 * Usage:
29
 * $form =& new HTML_QuickForm('form', 'POST');
30
 * $template =& new HTML_Template_Flexy();
31
 * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
32
 * $renderer->setHtmlTemplate("html.html");
33
 * $renderer->setLabelTemplate("label.html");
34
 * $form->accept($renderer);
35
 * $view = new StdClass;
36
 * $view->form = $renderer->toObject();
37
 * $template->compile("mytemplate.html");
38
 *
39
 * @see QuickFormFlexyObject
40
 *
41
 * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
42
 *
43
 * @public
44
 */
45
class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object {
46
    /**
47
     * HTML_Template_Flexy instance
48
     * @var object $_flexy
49
     */
50
    var $_flexy;
51
 
52
    /**
53
     * Current element index
54
     * @var integer $_elementIdx
55
     */
56
    var $_elementIdx;
57
 
58
    /**
59
     * The current element index inside a group
60
     * @var integer $_groupElementIdx
61
     */
62
     var $_groupElementIdx = 0;
63
 
64
    /**
65
     * Name of template file for form html
66
     * @var string $_html
67
     * @see     setRequiredTemplate()
68
     */
69
    var $_html = '';
70
 
71
    /**
72
     * Name of template file for form labels
73
     * @var string $label
74
     * @see        setErrorTemplate()
75
     */
76
    var $label = '';
77
 
78
    /**
79
     * Class of the element objects, so you can add your own
80
     * element methods
81
     * @var string $_elementType
82
     */
83
    var $_elementType = 'QuickformFlexyElement';
84
 
85
    /**
86
     * Constructor
87
     *
88
     * @param $flexy object   HTML_Template_Flexy instance
89
     * @public
90
     */
91
    function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
92
    {
93
        $this->HTML_QuickForm_Renderer_Object(true);
94
        $this->_obj = new QuickformFlexyForm();
95
        $this->_flexy =& $flexy;
96
    } // end constructor
97
 
98
    function renderHeader(&$header)
99
    {
100
        if($name = $header->getName()) {
101
            $this->_obj->header->$name = $header->toHtml();
102
        } else {
103
            $this->_obj->header[$this->_sectionCount] = $header->toHtml();
104
        }
105
        $this->_currentSection = $this->_sectionCount++;
106
    } // end func renderHeader
107
 
108
    function startGroup(&$group, $required, $error)
109
    {
110
        parent::startGroup($group, $required, $error);
111
        $this->_groupElementIdx = 1;
112
    } //end func startGroup
113
 
114
    /**
115
     * Creates an object representing an element containing
116
     * the key for storing this
117
     *
118
     * @private
119
     * @param element object     An HTML_QuickForm_element object
120
     * @param required bool        Whether an element is required
121
     * @param error string    Error associated with the element
122
     * @return object
123
     */
124
     function _elementToObject(&$element, $required, $error)
125
     {
126
        $ret = parent::_elementToObject($element, $required, $error);
127
        if($ret->type == 'group') {
128
            $ret->html = $element->toHtml();
129
            unset($ret->elements);
130
        }
131
        if(!empty($this->_label)) {
132
            $this->_renderLabel($ret);
133
        }
134
 
135
        if(!empty($this->_html)) {
136
            $this->_renderHtml($ret);
137
            $ret->error = $error;
138
        }
139
 
140
        // Create an element key from the name
141
        if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
142
            if (!$pos) {
143
                $keys = '->{\'' . $ret->name . '\'}';
144
            } else {
145
                $keys = '->{\'' . str_replace(array('[', ']'), array('\'}->{\'', ''), $ret->name) . '\'}';
146
            }
147
            // special handling for elements in native groups
148
            if (is_object($this->_currentGroup)) {
149
                // skip unnamed group items unless radios: no name -> no static access
150
                // identification: have the same key string as the parent group
151
                if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
152
                    return false;
153
                }
154
                // reduce string of keys by remove leading group keys
155
                if (0 === strpos($keys, $this->_currentGroup->keys)) {
156
                    $keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys));
157
                }
158
            }
159
        } elseif (0 == strlen($ret->name)) {
160
            $keys = '->{\'element_' . $this->_elementIdx . '\'}';
161
        } else {
162
            $keys = '->{\'' . $ret->name . '\'}';
163
        }
164
        // for radios: add extra key from value
165
        if ('radio' == $ret->type && '[]' != substr($keys, -2)) {
166
            $keys .= '->{\'' . $ret->value . '\'}';
167
        }
168
        $ret->keys = $keys;
169
        $this->_elementIdx++;
170
        return $ret;
171
    }
172
 
173
    /**
174
     * Stores an object representation of an element in the
175
     * QuickformFormObject instance
176
     *
177
     * @private
178
     * @param elObj object  Object representation of an element
179
     * @return void
180
     */
181
    function _storeObject($elObj)
182
    {
183
        if ($elObj) {
184
            $keys = $elObj->keys;
185
            unset($elObj->keys);
186
            if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
187
                $code = '$this->_currentGroup' . $keys . ' = $elObj;';
188
            } else {
189
                $code = '$this->_obj' . $keys . ' = $elObj;';
190
            }
191
            eval($code);
192
        }
193
    }
194
 
195
    /**
196
     * Set the filename of the template to render html elements.
197
     * In your template, {html} is replaced by the unmodified html.
198
     * If the element is required, {required} will be true.
199
     * Eg.
200
     * {if:error}
201
     *   <font color="red" size="1">{error:h}</font><br />
202
     * {end:}
203
     * {html:h}
204
     *
205
     * @public
206
     * @param template string   Filename of template
207
     * @return void
208
     */
209
    function setHtmlTemplate($template)
210
    {
211
        $this->_html = $template;
212
    }
213
 
214
    /**
215
     * Set the filename of the template to render form labels
216
     * In your template, {label} is replaced by the unmodified label.
217
     * {error} will be set to the error, if any.  {required} will
218
     * be true if this is a required field
219
     * Eg.
220
     * {if:required}
221
     * <font color="orange" size="1">*</font>
222
     * {end:}
223
     * {label:h}
224
     *
225
     * @public
226
     * @param template string   Filename of template
227
     * @return void
228
     */
229
    function setLabelTemplate($template)
230
    {
231
        $this->_label = $template;
232
    }
233
 
234
    function _renderLabel(&$ret)
235
    {
236
        $this->_flexy->compile($this->_label);
237
        $ret->label = $this->_flexy->bufferedOutputObject($ret);
238
    }
239
 
240
    function _renderHtml(&$ret)
241
    {
242
        $this->_flexy->compile($this->_html);
243
        $ret->html = $this->_flexy->bufferedOutputObject($ret);
244
    }
245
 
246
} // end class HTML_QuickForm_Renderer_ObjectFlexy
247
 
248
/**
249
 * @abstract Long Description
250
 * This class represents the object passed to outputObject()
251
 *
252
 * Eg.
253
 * {form.outputJavaScript():h}
254
 * {form.outputHeader():h}
255
 *   <table>
256
 *     <tr>
257
 *       <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
258
 *     </tr>
259
 *   </table>
260
 * </form>
261
 *
262
 * @public
263
 */
264
class QuickformFlexyForm {
265
    /**
266
     * Whether the form has been frozen
267
     * @var boolean $frozen
268
     */
269
    var $frozen;
270
 
271
    /**
272
     * Javascript for client-side validation
273
     * @var string $javascript
274
     */
275
     var $javascript;
276
 
277
     /**
278
      * Attributes for form tag
279
      * @var string $attributes
280
      */
281
     var $attributes;
282
 
283
     /**
284
      * Note about required elements
285
      * @var string $requirednote
286
      */
287
     var $requirednote;
288
 
289
     /**
290
      * Collected html of all hidden variables
291
      * @var string $hidden
292
      */
293
     var $hidden;
294
 
295
     /**
296
      * Set if there were validation errors.
297
      * StdClass object with element names for keys and their
298
      * error messages as values
299
      * @var object $errors
300
      */
301
     var $errors;
302
 
303
     /**
304
      * Array of QuickformElementObject elements.  If there are headers in the form
305
      * this will be empty and the elements will be in the
306
      * separate sections
307
      * @var array $elements
308
      */
309
     var $elements;
310
 
311
     /**
312
      * Array of sections contained in the document
313
      * @var array $sections
314
      */
315
     var $sections;
316
 
317
     /**
318
      * Output &lt;form&gt; header
319
      * {form.outputHeader():h}
320
      * @return string    &lt;form attributes&gt;
321
      */
322
     function outputHeader()
323
     {
324
        $hdr = "<form " . $this->attributes . ">\n";
325
        return $hdr;
326
     }
327
 
328
     /**
329
      * Output form javascript
330
      * {form.outputJavaScript():h}
331
      * @return string    Javascript
332
      */
333
     function outputJavaScript()
334
     {
335
        return $this->javascript;
336
     }
337
} // end class QuickformFlexyForm
338
 
339
/**
340
 * Convenience class describing a form element.
341
 * The properties defined here will be available from
342
 * your flexy templates by referencing
343
 * {form.zip.label:h}, {form.zip.html:h}, etc.
344
 */
345
class QuickformFlexyElement {
346
 
347
    /**
348
     * Element name
349
     * @var string $name
350
     */
351
    var $name;
352
 
353
    /**
354
     * Element value
355
     * @var mixed $value
356
     */
357
    var $value;
358
 
359
    /**
360
     * Type of element
361
     * @var string $type
362
     */
363
    var $type;
364
 
365
    /**
366
     * Whether the element is frozen
367
     * @var boolean $frozen
368
     */
369
    var $frozen;
370
 
371
    /**
372
     * Label for the element
373
     * @var string $label
374
     */
375
    var $label;
376
 
377
    /**
378
     * Whether element is required
379
     * @var boolean $required
380
     */
381
    var $required;
382
 
383
    /**
384
     * Error associated with the element
385
     * @var string $error
386
     */
387
    var $error;
388
 
389
    /**
390
     * Some information about element style
391
     * @var string $style
392
     */
393
    var $style;
394
 
395
    /**
396
     * HTML for the element
397
     * @var string $html
398
     */
399
    var $html;
400
 
401
    /**
402
     * If element is a group, the group separator
403
     * @var mixed $separator
404
     */
405
    var $separator;
406
 
407
    /**
408
     * If element is a group, an array of subelements
409
     * @var array $elements
410
     */
411
    var $elements;
412
} // end class QuickformFlexyElement
413
?>