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
// | Authors: Alexey Borzov <borz_off@cs.msu.su>                          |
17
// |          Adam Daniel <adaniel1@eesus.jnj.com>                        |
18
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
19
// +----------------------------------------------------------------------+
20
//
21
// $Id: Default.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
22
 
23
require_once('HTML/QuickForm/Renderer.php');
24
 
25
/**
26
 * A concrete renderer for HTML_QuickForm,
27
 * based on QuickForm 2.x built-in one
28
 *
29
 * @access public
30
 */
31
class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
32
{
33
   /**
34
    * The HTML of the form
35
    * @var      string
36
    * @access   private
37
    */
38
    var $_html;
39
 
40
   /**
41
    * Header Template string
42
    * @var      string
43
    * @access   private
44
    */
45
    var $_headerTemplate =
46
        "\n\t<tr>\n\t\t<td style=\"white-space: nowrap; background-color: #CCCCCC;\" align=\"left\" valign=\"top\" colspan=\"2\"><b>{header}</b></td>\n\t</tr>";
47
 
48
   /**
49
    * Element template string
50
    * @var      string
51
    * @access   private
52
    */
53
    var $_elementTemplate =
54
        "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required --><b>{label}</b></td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><span style=\"color: #ff0000\">{error}</span><br /><!-- END error -->\t{element}</td>\n\t</tr>";
55
 
56
   /**
57
    * Form template string
58
    * @var      string
59
    * @access   private
60
    */
61
    var $_formTemplate =
62
        "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>";
63
 
64
   /**
65
    * Required Note template string
66
    * @var      string
67
    * @access   private
68
    */
69
    var $_requiredNoteTemplate =
70
        "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>";
71
 
72
   /**
73
    * Array containing the templates for customised elements
74
    * @var      array
75
    * @access   private
76
    */
77
    var $_templates = array();
78
 
79
   /**
80
    * Array containing the templates for group wraps.
81
    *
82
    * These templates are wrapped around group elements and groups' own
83
    * templates wrap around them. This is set by setGroupTemplate().
84
    *
85
    * @var      array
86
    * @access   private
87
    */
88
    var $_groupWraps = array();
89
 
90
   /**
91
    * Array containing the templates for elements within groups
92
    * @var      array
93
    * @access   private
94
    */
95
    var $_groupTemplates = array();
96
 
97
   /**
98
    * True if we are inside a group
99
    * @var      bool
100
    * @access   private
101
    */
102
    var $_inGroup = false;
103
 
104
   /**
105
    * Array with HTML generated for group elements
106
    * @var      array
107
    * @access   private
108
    */
109
    var $_groupElements = array();
110
 
111
   /**
112
    * Template for an element inside a group
113
    * @var      string
114
    * @access   private
115
    */
116
    var $_groupElementTemplate = '';
117
 
118
   /**
119
    * HTML that wraps around the group elements
120
    * @var      string
121
    * @access   private
122
    */
123
    var $_groupWrap = '';
124
 
125
   /**
126
    * HTML for the current group
127
    * @var      string
128
    * @access   private
129
    */
130
    var $_groupTemplate = '';
131
 
132
   /**
133
    * Collected HTML of the hidden fields
134
    * @var      string
135
    * @access   private
136
    */
137
    var $_hiddenHtml = '';
138
 
139
   /**
140
    * Constructor
141
    *
142
    * @access public
143
    */
144
    function HTML_QuickForm_Renderer_Default()
145
    {
146
        $this->HTML_QuickForm_Renderer();
147
    } // end constructor
148
 
149
   /**
150
    * returns the HTML generated for the form
151
    *
152
    * @access public
153
    * @return string
154
    */
155
    function toHtml()
156
    {
157
        return $this->_html;
158
    } // end func toHtml
159
 
160
   /**
161
    * Called when visiting a form, before processing any form elements
162
    *
163
    * @param    object      An HTML_QuickForm object being visited
164
    * @access   public
165
    * @return   void
166
    */
167
    function startForm(&$form)
168
    {
169
        $this->_html = '';
170
        $this->_hiddenHtml = '';
171
    } // end func startForm
172
 
173
   /**
174
    * Called when visiting a form, after processing all form elements
175
    * Adds required note, form attributes, validation javascript and form content.
176
    *
177
    * @param    object      An HTML_QuickForm object being visited
178
    * @access   public
179
    * @return   void
180
    */
181
    function finishForm(&$form)
182
    {
183
        // add a required note, if one is needed
184
        if (!empty($form->_required) && !$form->_freezeAll) {
185
            $this->_html .= str_replace('{requiredNote}', $form->getRequiredNote(), $this->_requiredNoteTemplate);
186
        }
187
        // add form attributes and content
188
        $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
189
        if (strpos($this->_formTemplate, '{hidden}')) {
190
            $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
191
        } else {
192
            $this->_html .= $this->_hiddenHtml;
193
        }
194
        $this->_html = str_replace('{content}', $this->_html, $html);
195
        // add a validation script
196
        if ('' != ($script = $form->getValidationScript())) {
197
            $this->_html = $script . "\n" . $this->_html;
198
        }
199
    } // end func finishForm
200
 
201
   /**
202
    * Called when visiting a header element
203
    *
204
    * @param    object     An HTML_QuickForm_header element being visited
205
    * @access   public
206
    * @return   void
207
    */
208
    function renderHeader(&$header)
209
    {
210
        $name = $header->getName();
211
        if (!empty($name) && isset($this->_templates[$name])) {
212
            $this->_html .= str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
213
        } else {
214
            $this->_html .= str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
215
        }
216
    } // end func renderHeader
217
 
218
   /**
219
    * Helper method for renderElement
220
    *
221
    * @param    string      Element name
222
    * @param    mixed       Element label (if using an array of labels, you should set the appropriate template)
223
    * @param    bool        Whether an element is required
224
    * @param    string      Error message associated with the element
225
    * @access   private
226
    * @see      renderElement()
227
    * @return   string      Html for element
228
    */
229
    function _prepareTemplate($name, $label, $required, $error)
230
    {
231
        if (is_array($label)) {
232
            $nameLabel = array_shift($label);
233
        } else {
234
            $nameLabel = $label;
235
        }
236
        if (isset($this->_templates[$name])) {
237
            $html = str_replace('{label}', $nameLabel, $this->_templates[$name]);
238
        } else {
239
            $html = str_replace('{label}', $nameLabel, $this->_elementTemplate);
240
        }
241
        if ($required) {
242
            $html = str_replace('<!-- BEGIN required -->', '', $html);
243
            $html = str_replace('<!-- END required -->', '', $html);
244
        } else {
245
            $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
246
        }
247
        if (isset($error)) {
248
            $html = str_replace('{error}', $error, $html);
249
            $html = str_replace('<!-- BEGIN error -->', '', $html);
250
            $html = str_replace('<!-- END error -->', '', $html);
251
        } else {
252
            $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i", '', $html);
253
        }
254
        if (is_array($label)) {
255
            foreach($label as $key => $text) {
256
                $key  = is_int($key)? $key + 2: $key;
257
                $html = str_replace("{label_{$key}}", $text, $html);
258
                $html = str_replace("<!-- BEGIN label_{$key} -->", '', $html);
259
                $html = str_replace("<!-- END label_{$key} -->", '', $html);
260
            }
261
        }
262
        if (strpos($html, '{label_')) {
263
            $html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $html);
264
        }
265
        return $html;
266
    } // end func _prepareTemplate
267
 
268
   /**
269
    * Renders an element Html
270
    * Called when visiting an element
271
    *
272
    * @param object     An HTML_QuickForm_element object being visited
273
    * @param bool       Whether an element is required
274
    * @param string     An error message associated with an element
275
    * @access public
276
    * @return void
277
    */
278
    function renderElement(&$element, $required, $error)
279
    {
280
        if (!$this->_inGroup) {
281
            $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
282
            $this->_html .= str_replace('{element}', $element->toHtml(), $html);
283
 
284
        } elseif (!empty($this->_groupElementTemplate)) {
285
            $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
286
            if ($required) {
287
                $html = str_replace('<!-- BEGIN required -->', '', $html);
288
                $html = str_replace('<!-- END required -->', '', $html);
289
            } else {
290
                $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
291
            }
292
            $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
293
 
294
        } else {
295
            $this->_groupElements[] = $element->toHtml();
296
        }
297
    } // end func renderElement
298
 
299
   /**
300
    * Renders an hidden element
301
    * Called when visiting a hidden element
302
    *
303
    * @param object     An HTML_QuickForm_hidden object being visited
304
    * @access public
305
    * @return void
306
    */
307
    function renderHidden(&$element)
308
    {
309
        $this->_hiddenHtml .= $element->toHtml() . "\n";
310
    } // end func renderHidden
311
 
312
   /**
313
    * Called when visiting a raw HTML/text pseudo-element
314
    *
315
    * @param  object     An HTML_QuickForm_html element being visited
316
    * @access public
317
    * @return void
318
    */
319
    function renderHtml(&$data)
320
    {
321
        $this->_html .= $data->toHtml();
322
    } // end func renderHtml
323
 
324
   /**
325
    * Called when visiting a group, before processing any group elements
326
    *
327
    * @param object     An HTML_QuickForm_group object being visited
328
    * @param bool       Whether a group is required
329
    * @param string     An error message associated with a group
330
    * @access public
331
    * @return void
332
    */
333
    function startGroup(&$group, $required, $error)
334
    {
335
        $name = $group->getName();
336
        $this->_groupTemplate        = $this->_prepareTemplate($name, $group->getLabel(), $required, $error);
337
        $this->_groupElementTemplate = empty($this->_groupTemplates[$name])? '': $this->_groupTemplates[$name];
338
        $this->_groupWrap            = empty($this->_groupWraps[$name])? '': $this->_groupWraps[$name];
339
        $this->_groupElements        = array();
340
        $this->_inGroup              = true;
341
    } // end func startGroup
342
 
343
   /**
344
    * Called when visiting a group, after processing all group elements
345
    *
346
    * @param    object      An HTML_QuickForm_group object being visited
347
    * @access   public
348
    * @return   void
349
    */
350
    function finishGroup(&$group)
351
    {
352
        $separator = $group->_separator;
353
        if (is_array($separator)) {
354
            $count = count($separator);
355
            $html  = '';
356
            for ($i = 0; $i < count($this->_groupElements); $i++) {
357
                $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i];
358
            }
359
        } else {
360
            if (is_null($separator)) {
361
                $separator = '&nbsp;';
362
            }
363
            $html = implode((string)$separator, $this->_groupElements);
364
        }
365
        if (!empty($this->_groupWrap)) {
366
            $html = str_replace('{content}', $html, $this->_groupWrap);
367
        }
368
        $this->_html   .= str_replace('{element}', $html, $this->_groupTemplate);
369
        $this->_inGroup = false;
370
    } // end func finishGroup
371
 
372
    /**
373
     * Sets element template
374
     *
375
     * @param       string      The HTML surrounding an element
376
     * @param       string      (optional) Name of the element to apply template for
377
     * @access      public
378
     * @return      void
379
     */
380
    function setElementTemplate($html, $element = null)
381
    {
382
        if (is_null($element)) {
383
            $this->_elementTemplate = $html;
384
        } else {
385
            $this->_templates[$element] = $html;
386
        }
387
    } // end func setElementTemplate
388
 
389
 
390
    /**
391
     * Sets template for a group wrapper
392
     *
393
     * This template is contained within a group-as-element template
394
     * set via setTemplate() and contains group's element templates, set
395
     * via setGroupElementTemplate()
396
     *
397
     * @param       string      The HTML surrounding group elements
398
     * @param       string      Name of the group to apply template for
399
     * @access      public
400
     * @return      void
401
     */
402
    function setGroupTemplate($html, $group)
403
    {
404
        $this->_groupWraps[$group] = $html;
405
    } // end func setGroupTemplate
406
 
407
    /**
408
     * Sets element template for elements within a group
409
     *
410
     * @param       string      The HTML surrounding an element
411
     * @param       string      Name of the group to apply template for
412
     * @access      public
413
     * @return      void
414
     */
415
    function setGroupElementTemplate($html, $group)
416
    {
417
        $this->_groupTemplates[$group] = $html;
418
    } // end func setGroupElementTemplate
419
 
420
    /**
421
     * Sets header template
422
     *
423
     * @param       string      The HTML surrounding the header
424
     * @access      public
425
     * @return      void
426
     */
427
    function setHeaderTemplate($html)
428
    {
429
        $this->_headerTemplate = $html;
430
    } // end func setHeaderTemplate
431
 
432
    /**
433
     * Sets form template
434
     *
435
     * @param     string    The HTML surrounding the form tags
436
     * @access    public
437
     * @return    void
438
     */
439
    function setFormTemplate($html)
440
    {
441
        $this->_formTemplate = $html;
442
    } // end func setFormTemplate
443
 
444
    /**
445
     * Sets the note indicating required fields template
446
     *
447
     * @param       string      The HTML surrounding the required note
448
     * @access      public
449
     * @return      void
450
     */
451
    function setRequiredNoteTemplate($html)
452
    {
453
        $this->_requiredNoteTemplate = $html;
454
    } // end func setRequiredNoteTemplate
455
 
456
    /**
457
     * Clears all the HTML out of the templates that surround notes, elements, etc.
458
     * Useful when you want to use addData() to create a completely custom form look
459
     *
460
     * @access  public
461
     * @return  void
462
     */
463
    function clearAllTemplates()
464
    {
465
        $this->setElementTemplate('{element}');
466
        $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
467
        $this->setRequiredNoteTemplate('');
468
        $this->_templates = array();
469
    } // end func clearAllTemplates
470
} // end class HTML_QuickForm_Renderer_Default
471
?>