Subversion Repositories Applications.papyrus

Rev

Rev 320 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 320 Rev 443
1
<?php
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
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        |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
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   |
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          |
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.               |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
15
// +----------------------------------------------------------------------+
16
// | Author: Ron McClain <ron@humaniq.com>                                |
16
// | Author: Ron McClain <ron@humaniq.com>                                |
17
// +----------------------------------------------------------------------+
17
// +----------------------------------------------------------------------+
18
//
18
//
19
// $Id: ObjectFlexy.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
19
// $Id: ObjectFlexy.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
20
 
20
 
21
require_once("HTML/QuickForm/Renderer/Object.php");
21
require_once("HTML/QuickForm/Renderer/Object.php");
22
 
22
 
23
/**
23
/**
24
 * @abstract Long Description
24
 * QuickForm renderer for Flexy template engine, static version.
-
 
25
 * 
25
 * A static renderer for HTML_Quickform.  Makes a QuickFormFlexyObject
26
 * A static renderer for HTML_Quickform.  Makes a QuickFormFlexyObject
26
 * from the form content suitable for use with a Flexy template
27
 * from the form content suitable for use with a Flexy template
27
 *
28
 *
28
 * Usage:
29
 * Usage:
29
 * $form =& new HTML_QuickForm('form', 'POST');
30
 * $form =& new HTML_QuickForm('form', 'POST');
30
 * $template =& new HTML_Template_Flexy();
31
 * $template =& new HTML_Template_Flexy();
31
 * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
32
 * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
32
 * $renderer->setHtmlTemplate("html.html");
33
 * $renderer->setHtmlTemplate("html.html");
33
 * $renderer->setLabelTemplate("label.html");
34
 * $renderer->setLabelTemplate("label.html");
34
 * $form->accept($renderer);
35
 * $form->accept($renderer);
35
 * $view = new StdClass;
36
 * $view = new StdClass;
36
 * $view->form = $renderer->toObject();
37
 * $view->form = $renderer->toObject();
37
 * $template->compile("mytemplate.html");
38
 * $template->compile("mytemplate.html");
38
 *
39
 *
39
 * @see QuickFormFlexyObject
-
 
40
 *
-
 
41
 * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
40
 * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
42
 *
41
 *
-
 
42
 * @see QuickFormFlexyObject
43
 * @public
43
 * @access public
44
 */
44
 */
45
class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object {
45
class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object
-
 
46
{
46
    /**
47
    /**
47
     * HTML_Template_Flexy instance
48
     * HTML_Template_Flexy instance
48
     * @var object $_flexy
49
     * @var object $_flexy
49
     */
50
     */
50
    var $_flexy;
51
    var $_flexy;
51
 
52
 
52
    /**
53
    /**
53
     * Current element index
54
     * Current element index
54
     * @var integer $_elementIdx
55
     * @var integer $_elementIdx
55
     */
56
     */
56
    var $_elementIdx;
57
    var $_elementIdx;
57
 
58
 
58
    /**
59
    /**
59
     * The current element index inside a group
60
     * The current element index inside a group
60
     * @var integer $_groupElementIdx
61
     * @var integer $_groupElementIdx
61
     */
62
     */
62
     var $_groupElementIdx = 0;
63
    var $_groupElementIdx = 0;
63
 
64
 
64
    /**
65
    /**
65
     * Name of template file for form html
66
     * Name of template file for form html
66
     * @var string $_html
67
     * @var string $_html
67
     * @see     setRequiredTemplate()
68
     * @see     setRequiredTemplate()
68
     */
69
     */
69
    var $_html = '';
70
    var $_html = '';
70
 
71
 
71
    /**
72
    /**
72
     * Name of template file for form labels
73
     * Name of template file for form labels
73
     * @var string $label
74
     * @var string $label
74
     * @see        setErrorTemplate()
75
     * @see        setErrorTemplate()
75
     */
76
     */
76
    var $label = '';
77
    var $label = '';
77
 
78
 
78
    /**
79
    /**
79
     * Class of the element objects, so you can add your own
80
     * Class of the element objects, so you can add your own
80
     * element methods
81
     * element methods
81
     * @var string $_elementType
82
     * @var string $_elementType
82
     */
83
     */
83
    var $_elementType = 'QuickformFlexyElement';
84
    var $_elementType = 'QuickformFlexyElement';
84
 
85
 
85
    /**
86
    /**
86
     * Constructor
87
     * Constructor
87
     *
88
     *
88
     * @param $flexy object   HTML_Template_Flexy instance
89
     * @param $flexy object   HTML_Template_Flexy instance
89
     * @public
90
     * @public
90
     */
91
     */
91
    function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
92
    function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
92
    {
93
    {
93
        $this->HTML_QuickForm_Renderer_Object(true);
94
        $this->HTML_QuickForm_Renderer_Object(true);
94
        $this->_obj = new QuickformFlexyForm();
95
        $this->_obj = new QuickformFlexyForm();
95
        $this->_flexy =& $flexy;
96
        $this->_flexy =& $flexy;
96
    } // end constructor
97
    } // end constructor
97
 
98
 
98
    function renderHeader(&$header)
99
    function renderHeader(&$header)
99
    {
100
    {
100
        if($name = $header->getName()) {
101
        if($name = $header->getName()) {
101
            $this->_obj->header->$name = $header->toHtml();
102
            $this->_obj->header->$name = $header->toHtml();
102
        } else {
103
        } else {
103
            $this->_obj->header[$this->_sectionCount] = $header->toHtml();
104
            $this->_obj->header[$this->_sectionCount] = $header->toHtml();
104
        }
105
        }
105
        $this->_currentSection = $this->_sectionCount++;
106
        $this->_currentSection = $this->_sectionCount++;
106
    } // end func renderHeader
107
    } // end func renderHeader
107
 
108
 
108
    function startGroup(&$group, $required, $error)
109
    function startGroup(&$group, $required, $error)
109
    {
110
    {
110
        parent::startGroup($group, $required, $error);
111
        parent::startGroup($group, $required, $error);
111
        $this->_groupElementIdx = 1;
112
        $this->_groupElementIdx = 1;
112
    } //end func startGroup
113
    } //end func startGroup
113
 
114
 
114
    /**
115
    /**
115
     * Creates an object representing an element containing
116
     * Creates an object representing an element containing
116
     * the key for storing this
117
     * the key for storing this
117
     *
118
     *
118
     * @private
119
     * @access private
119
     * @param element object     An HTML_QuickForm_element object
120
     * @param element object     An HTML_QuickForm_element object
120
     * @param required bool        Whether an element is required
121
     * @param required bool        Whether an element is required
121
     * @param error string    Error associated with the element
122
     * @param error string    Error associated with the element
122
     * @return object
123
     * @return object
123
     */
124
     */
124
     function _elementToObject(&$element, $required, $error)
125
    function _elementToObject(&$element, $required, $error)
125
     {
126
    {
126
        $ret = parent::_elementToObject($element, $required, $error);
127
        $ret = parent::_elementToObject($element, $required, $error);
127
        if($ret->type == 'group') {
128
        if($ret->type == 'group') {
128
            $ret->html = $element->toHtml();
129
            $ret->html = $element->toHtml();
129
            unset($ret->elements);
130
            unset($ret->elements);
130
        }
131
        }
131
        if(!empty($this->_label)) {
132
        if(!empty($this->_label)) {
132
            $this->_renderLabel($ret);
133
            $this->_renderLabel($ret);
133
        }
134
        }
134
 
135
 
135
        if(!empty($this->_html)) {
136
        if(!empty($this->_html)) {
136
            $this->_renderHtml($ret);
137
            $this->_renderHtml($ret);
137
            $ret->error = $error;
138
            $ret->error = $error;
138
        }
139
        }
139
 
140
 
140
        // Create an element key from the name
141
        // Create an element key from the name
141
        if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
142
        if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
142
            if (!$pos) {
143
            if (!$pos) {
143
                $keys = '->{\'' . $ret->name . '\'}';
144
                $keys = '->{\'' . $ret->name . '\'}';
144
            } else {
145
            } else {
145
                $keys = '->{\'' . str_replace(array('[', ']'), array('\'}->{\'', ''), $ret->name) . '\'}';
146
                $keys = '->{\'' . str_replace(array('[', ']'), array('\'}->{\'', ''), $ret->name) . '\'}';
146
            }
147
            }
147
            // special handling for elements in native groups
148
            // special handling for elements in native groups
148
            if (is_object($this->_currentGroup)) {
149
            if (is_object($this->_currentGroup)) {
149
                // skip unnamed group items unless radios: no name -> no static access
150
                // skip unnamed group items unless radios: no name -> no static access
150
                // identification: have the same key string as the parent group
151
                // identification: have the same key string as the parent group
151
                if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
152
                if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
152
                    return false;
153
                    return false;
153
                }
154
                }
154
                // reduce string of keys by remove leading group keys
155
                // reduce string of keys by remove leading group keys
155
                if (0 === strpos($keys, $this->_currentGroup->keys)) {
156
                if (0 === strpos($keys, $this->_currentGroup->keys)) {
156
                    $keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys));
157
                    $keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys));
157
                }
158
                }
158
            }
159
            }
159
        } elseif (0 == strlen($ret->name)) {
160
        } elseif (0 == strlen($ret->name)) {
160
            $keys = '->{\'element_' . $this->_elementIdx . '\'}';
161
            $keys = '->{\'element_' . $this->_elementIdx . '\'}';
161
        } else {
162
        } else {
162
            $keys = '->{\'' . $ret->name . '\'}';
163
            $keys = '->{\'' . $ret->name . '\'}';
163
        }
164
        }
164
        // for radios: add extra key from value
165
        // for radios: add extra key from value
165
        if ('radio' == $ret->type && '[]' != substr($keys, -2)) {
166
        if ('radio' == $ret->type && '[]' != substr($keys, -2)) {
166
            $keys .= '->{\'' . $ret->value . '\'}';
167
            $keys .= '->{\'' . $ret->value . '\'}';
167
        }
168
        }
168
        $ret->keys = $keys;
169
        $ret->keys = $keys;
169
        $this->_elementIdx++;
170
        $this->_elementIdx++;
170
        return $ret;
171
        return $ret;
171
    }
172
    }
172
 
173
 
173
    /**
174
    /**
174
     * Stores an object representation of an element in the 
175
     * Stores an object representation of an element in the 
175
     * QuickformFormObject instance
176
     * QuickformFormObject instance
176
     *
177
     *
177
     * @private
178
     * @access private
178
     * @param elObj object  Object representation of an element
179
     * @param elObj object  Object representation of an element
179
     * @return void
180
     * @return void
180
     */
181
     */
181
    function _storeObject($elObj) 
182
    function _storeObject($elObj) 
182
    {
183
    {
183
        if ($elObj) {
184
        if ($elObj) {
184
            $keys = $elObj->keys;
185
            $keys = $elObj->keys;
185
            unset($elObj->keys);
186
            unset($elObj->keys);
186
            if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
187
            if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
187
                $code = '$this->_currentGroup' . $keys . ' = $elObj;';
188
                $code = '$this->_currentGroup' . $keys . ' = $elObj;';
188
            } else {
189
            } else {
189
                $code = '$this->_obj' . $keys . ' = $elObj;';
190
                $code = '$this->_obj' . $keys . ' = $elObj;';
190
            }
191
            }
191
            eval($code);
192
            eval($code);
192
        }
193
        }
193
    }
194
    }
194
 
195
 
195
    /**
196
    /**
196
     * Set the filename of the template to render html elements.
197
     * Set the filename of the template to render html elements.
197
     * In your template, {html} is replaced by the unmodified html.
198
     * In your template, {html} is replaced by the unmodified html.
198
     * If the element is required, {required} will be true.
199
     * If the element is required, {required} will be true.
199
     * Eg.
200
     * Eg.
200
     * {if:error}
201
     * {if:error}
201
     *   <font color="red" size="1">{error:h}</font><br />
202
     *   <font color="red" size="1">{error:h}</font><br />
202
     * {end:}
203
     * {end:}
203
     * {html:h}
204
     * {html:h}
204
     *
205
     *
205
     * @public
206
     * @access public
206
     * @param template string   Filename of template
207
     * @param template string   Filename of template
207
     * @return void
208
     * @return void
208
     */
209
     */
209
    function setHtmlTemplate($template)
210
    function setHtmlTemplate($template)
210
    {
211
    {
211
        $this->_html = $template;
212
        $this->_html = $template;
212
    } 
213
    } 
213
 
214
 
214
    /**
215
    /**
215
     * Set the filename of the template to render form labels
216
     * Set the filename of the template to render form labels
216
     * In your template, {label} is replaced by the unmodified label.
217
     * In your template, {label} is replaced by the unmodified label.
217
     * {error} will be set to the error, if any.  {required} will
218
     * {error} will be set to the error, if any.  {required} will
218
     * be true if this is a required field
219
     * be true if this is a required field
219
     * Eg.
220
     * Eg.
220
     * {if:required}
221
     * {if:required}
221
     * <font color="orange" size="1">*</font>
222
     * <font color="orange" size="1">*</font>
222
     * {end:}
223
     * {end:}
223
     * {label:h}
224
     * {label:h}
224
     *
225
     *
225
     * @public
226
     * @access public
226
     * @param template string   Filename of template
227
     * @param template string   Filename of template
227
     * @return void
228
     * @return void
228
     */
229
     */
229
    function setLabelTemplate($template) 
230
    function setLabelTemplate($template) 
230
    {
231
    {
231
        $this->_label = $template;
232
        $this->_label = $template;
232
    }
233
    }
233
 
234
 
234
    function _renderLabel(&$ret)
235
    function _renderLabel(&$ret)
235
    {
236
    {
236
        $this->_flexy->compile($this->_label);
237
        $this->_flexy->compile($this->_label);
237
        $ret->label = $this->_flexy->bufferedOutputObject($ret);
238
        $ret->label = $this->_flexy->bufferedOutputObject($ret);
238
    }
239
    }
239
 
240
 
240
    function _renderHtml(&$ret)
241
    function _renderHtml(&$ret)
241
    {
242
    {
242
        $this->_flexy->compile($this->_html);
243
        $this->_flexy->compile($this->_html);
243
        $ret->html = $this->_flexy->bufferedOutputObject($ret);
244
        $ret->html = $this->_flexy->bufferedOutputObject($ret);
244
    }
245
    }
245
 
-
 
246
} // end class HTML_QuickForm_Renderer_ObjectFlexy
246
} // end class HTML_QuickForm_Renderer_ObjectFlexy
247
 
247
 
248
/**
248
/**
249
 * @abstract Long Description
-
 
250
 * This class represents the object passed to outputObject()
249
 * Adds nothing to QuickformForm, left for backwards compatibility
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
 */
250
 */
264
class QuickformFlexyForm {
251
class QuickformFlexyForm extends QuickformForm
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
 
252
{
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
 
253
}
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
 
254
 
339
/**
-
 
340
 * Convenience class describing a form element.
255
/**
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.
256
 * Adds nothing to QuickformElement, left for backwards compatibility
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
?>
257
 */
-
 
258
class QuickformFlexyElement extends QuickformElement
-
 
259
{
-
 
260
}
-
 
261
?>