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
// | Authors: Alexey Borzov <borz_off@cs.msu.su>                          |
17
// |          Adam Daniel <adaniel1@eesus.jnj.com>                        |
18
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
19
// +----------------------------------------------------------------------+
20
//
443 ddelon 21
// $Id: Default.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
320 jpm 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
    {
443 ddelon 157
        // _hiddenHtml is cleared in finishForm(), so this only matters when
158
        // finishForm() was not called (e.g. group::toHtml(), bug #3511)
159
        return $this->_hiddenHtml . $this->_html;
320 jpm 160
    } // end func toHtml
161
 
162
   /**
163
    * Called when visiting a form, before processing any form elements
164
    *
165
    * @param    object      An HTML_QuickForm object being visited
166
    * @access   public
167
    * @return   void
168
    */
169
    function startForm(&$form)
170
    {
171
        $this->_html = '';
172
        $this->_hiddenHtml = '';
173
    } // end func startForm
174
 
175
   /**
176
    * Called when visiting a form, after processing all form elements
177
    * Adds required note, form attributes, validation javascript and form content.
178
    *
179
    * @param    object      An HTML_QuickForm object being visited
180
    * @access   public
181
    * @return   void
182
    */
183
    function finishForm(&$form)
184
    {
185
        // add a required note, if one is needed
186
        if (!empty($form->_required) && !$form->_freezeAll) {
187
            $this->_html .= str_replace('{requiredNote}', $form->getRequiredNote(), $this->_requiredNoteTemplate);
188
        }
189
        // add form attributes and content
190
        $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
191
        if (strpos($this->_formTemplate, '{hidden}')) {
192
            $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
193
        } else {
194
            $this->_html .= $this->_hiddenHtml;
195
        }
443 ddelon 196
        $this->_hiddenHtml = '';
320 jpm 197
        $this->_html = str_replace('{content}', $this->_html, $html);
198
        // add a validation script
199
        if ('' != ($script = $form->getValidationScript())) {
200
            $this->_html = $script . "\n" . $this->_html;
201
        }
202
    } // end func finishForm
203
 
204
   /**
205
    * Called when visiting a header element
206
    *
207
    * @param    object     An HTML_QuickForm_header element being visited
208
    * @access   public
209
    * @return   void
210
    */
211
    function renderHeader(&$header)
212
    {
213
        $name = $header->getName();
214
        if (!empty($name) && isset($this->_templates[$name])) {
215
            $this->_html .= str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
216
        } else {
217
            $this->_html .= str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
218
        }
219
    } // end func renderHeader
220
 
221
   /**
222
    * Helper method for renderElement
223
    *
224
    * @param    string      Element name
225
    * @param    mixed       Element label (if using an array of labels, you should set the appropriate template)
226
    * @param    bool        Whether an element is required
227
    * @param    string      Error message associated with the element
228
    * @access   private
229
    * @see      renderElement()
230
    * @return   string      Html for element
231
    */
232
    function _prepareTemplate($name, $label, $required, $error)
233
    {
234
        if (is_array($label)) {
235
            $nameLabel = array_shift($label);
236
        } else {
237
            $nameLabel = $label;
238
        }
239
        if (isset($this->_templates[$name])) {
240
            $html = str_replace('{label}', $nameLabel, $this->_templates[$name]);
241
        } else {
242
            $html = str_replace('{label}', $nameLabel, $this->_elementTemplate);
243
        }
244
        if ($required) {
245
            $html = str_replace('<!-- BEGIN required -->', '', $html);
246
            $html = str_replace('<!-- END required -->', '', $html);
247
        } else {
248
            $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
249
        }
250
        if (isset($error)) {
251
            $html = str_replace('{error}', $error, $html);
252
            $html = str_replace('<!-- BEGIN error -->', '', $html);
253
            $html = str_replace('<!-- END error -->', '', $html);
254
        } else {
255
            $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i", '', $html);
256
        }
257
        if (is_array($label)) {
258
            foreach($label as $key => $text) {
259
                $key  = is_int($key)? $key + 2: $key;
260
                $html = str_replace("{label_{$key}}", $text, $html);
261
                $html = str_replace("<!-- BEGIN label_{$key} -->", '', $html);
262
                $html = str_replace("<!-- END label_{$key} -->", '', $html);
263
            }
264
        }
265
        if (strpos($html, '{label_')) {
266
            $html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $html);
267
        }
268
        return $html;
269
    } // end func _prepareTemplate
270
 
271
   /**
272
    * Renders an element Html
273
    * Called when visiting an element
274
    *
275
    * @param object     An HTML_QuickForm_element object being visited
276
    * @param bool       Whether an element is required
277
    * @param string     An error message associated with an element
278
    * @access public
279
    * @return void
280
    */
281
    function renderElement(&$element, $required, $error)
282
    {
283
        if (!$this->_inGroup) {
284
            $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
285
            $this->_html .= str_replace('{element}', $element->toHtml(), $html);
286
 
287
        } elseif (!empty($this->_groupElementTemplate)) {
288
            $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
289
            if ($required) {
290
                $html = str_replace('<!-- BEGIN required -->', '', $html);
291
                $html = str_replace('<!-- END required -->', '', $html);
292
            } else {
293
                $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
294
            }
295
            $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
296
 
297
        } else {
298
            $this->_groupElements[] = $element->toHtml();
299
        }
300
    } // end func renderElement
301
 
302
   /**
303
    * Renders an hidden element
304
    * Called when visiting a hidden element
305
    *
306
    * @param object     An HTML_QuickForm_hidden object being visited
307
    * @access public
308
    * @return void
309
    */
310
    function renderHidden(&$element)
311
    {
312
        $this->_hiddenHtml .= $element->toHtml() . "\n";
313
    } // end func renderHidden
314
 
315
   /**
316
    * Called when visiting a raw HTML/text pseudo-element
317
    *
318
    * @param  object     An HTML_QuickForm_html element being visited
319
    * @access public
320
    * @return void
321
    */
322
    function renderHtml(&$data)
323
    {
324
        $this->_html .= $data->toHtml();
325
    } // end func renderHtml
326
 
327
   /**
328
    * Called when visiting a group, before processing any group elements
329
    *
330
    * @param object     An HTML_QuickForm_group object being visited
331
    * @param bool       Whether a group is required
332
    * @param string     An error message associated with a group
333
    * @access public
334
    * @return void
335
    */
336
    function startGroup(&$group, $required, $error)
337
    {
338
        $name = $group->getName();
339
        $this->_groupTemplate        = $this->_prepareTemplate($name, $group->getLabel(), $required, $error);
340
        $this->_groupElementTemplate = empty($this->_groupTemplates[$name])? '': $this->_groupTemplates[$name];
341
        $this->_groupWrap            = empty($this->_groupWraps[$name])? '': $this->_groupWraps[$name];
342
        $this->_groupElements        = array();
343
        $this->_inGroup              = true;
344
    } // end func startGroup
345
 
346
   /**
347
    * Called when visiting a group, after processing all group elements
348
    *
349
    * @param    object      An HTML_QuickForm_group object being visited
350
    * @access   public
351
    * @return   void
352
    */
353
    function finishGroup(&$group)
354
    {
355
        $separator = $group->_separator;
356
        if (is_array($separator)) {
357
            $count = count($separator);
358
            $html  = '';
359
            for ($i = 0; $i < count($this->_groupElements); $i++) {
360
                $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i];
361
            }
362
        } else {
363
            if (is_null($separator)) {
364
                $separator = '&nbsp;';
365
            }
366
            $html = implode((string)$separator, $this->_groupElements);
367
        }
368
        if (!empty($this->_groupWrap)) {
369
            $html = str_replace('{content}', $html, $this->_groupWrap);
370
        }
371
        $this->_html   .= str_replace('{element}', $html, $this->_groupTemplate);
372
        $this->_inGroup = false;
373
    } // end func finishGroup
374
 
375
    /**
376
     * Sets element template
377
     *
378
     * @param       string      The HTML surrounding an element
379
     * @param       string      (optional) Name of the element to apply template for
380
     * @access      public
381
     * @return      void
382
     */
383
    function setElementTemplate($html, $element = null)
384
    {
385
        if (is_null($element)) {
386
            $this->_elementTemplate = $html;
387
        } else {
388
            $this->_templates[$element] = $html;
389
        }
390
    } // end func setElementTemplate
391
 
392
 
393
    /**
394
     * Sets template for a group wrapper
395
     *
396
     * This template is contained within a group-as-element template
397
     * set via setTemplate() and contains group's element templates, set
398
     * via setGroupElementTemplate()
399
     *
400
     * @param       string      The HTML surrounding group elements
401
     * @param       string      Name of the group to apply template for
402
     * @access      public
403
     * @return      void
404
     */
405
    function setGroupTemplate($html, $group)
406
    {
407
        $this->_groupWraps[$group] = $html;
408
    } // end func setGroupTemplate
409
 
410
    /**
411
     * Sets element template for elements within a group
412
     *
413
     * @param       string      The HTML surrounding an element
414
     * @param       string      Name of the group to apply template for
415
     * @access      public
416
     * @return      void
417
     */
418
    function setGroupElementTemplate($html, $group)
419
    {
420
        $this->_groupTemplates[$group] = $html;
421
    } // end func setGroupElementTemplate
422
 
423
    /**
424
     * Sets header template
425
     *
426
     * @param       string      The HTML surrounding the header
427
     * @access      public
428
     * @return      void
429
     */
430
    function setHeaderTemplate($html)
431
    {
432
        $this->_headerTemplate = $html;
433
    } // end func setHeaderTemplate
434
 
435
    /**
436
     * Sets form template
437
     *
438
     * @param     string    The HTML surrounding the form tags
439
     * @access    public
440
     * @return    void
441
     */
442
    function setFormTemplate($html)
443
    {
444
        $this->_formTemplate = $html;
445
    } // end func setFormTemplate
446
 
447
    /**
448
     * Sets the note indicating required fields template
449
     *
450
     * @param       string      The HTML surrounding the required note
451
     * @access      public
452
     * @return      void
453
     */
454
    function setRequiredNoteTemplate($html)
455
    {
456
        $this->_requiredNoteTemplate = $html;
457
    } // end func setRequiredNoteTemplate
458
 
459
    /**
460
     * Clears all the HTML out of the templates that surround notes, elements, etc.
461
     * Useful when you want to use addData() to create a completely custom form look
462
     *
463
     * @access  public
464
     * @return  void
465
     */
466
    function clearAllTemplates()
467
    {
468
        $this->setElementTemplate('{element}');
469
        $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
470
        $this->setRequiredNoteTemplate('');
471
        $this->_templates = array();
472
    } // end func clearAllTemplates
473
} // end class HTML_QuickForm_Renderer_Default
474
?>