Subversion Repositories Applications.papyrus

Rev

Rev 320 | 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: Alexey Borzov <borz_off@cs.msu.su>                           |
16
// | Author: Alexey Borzov <borz_off@cs.msu.su>                           |
17
// +----------------------------------------------------------------------+
17
// +----------------------------------------------------------------------+
18
//
18
//
19
// $Id: ITDynamic.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
19
// $Id: ITDynamic.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
20
 
20
 
21
require_once 'HTML/QuickForm/Renderer.php';
21
require_once 'HTML/QuickForm/Renderer.php';
22
 
22
 
23
/**
23
/**
24
 * A concrete renderer for HTML_QuickForm, using Integrated Templates.
24
 * A concrete renderer for HTML_QuickForm, using Integrated Templates.
25
 * 
25
 * 
26
 * This is a "dynamic" renderer, which means that concrete form look 
26
 * This is a "dynamic" renderer, which means that concrete form look 
27
 * is defined at runtime. This also means that you can define 
27
 * is defined at runtime. This also means that you can define 
28
 * <b>one</b> template file for <b>all</b> your forms. That template
28
 * <b>one</b> template file for <b>all</b> your forms. That template
29
 * should contain a block for every element 'look' appearing in your 
29
 * should contain a block for every element 'look' appearing in your 
30
 * forms and also some special blocks (consult the examples). If a
30
 * forms and also some special blocks (consult the examples). If a
31
 * special block is not set for an element, the renderer falls back to
31
 * special block is not set for an element, the renderer falls back to
32
 * a default one.
32
 * a default one.
33
 * 
33
 * 
34
 * @author Alexey Borzov <borz_off@cs.msu.su>
34
 * @author Alexey Borzov <borz_off@cs.msu.su>
35
 * @access public
35
 * @access public
36
 */
36
 */
37
class HTML_QuickForm_Renderer_ITDynamic extends HTML_QuickForm_Renderer
37
class HTML_QuickForm_Renderer_ITDynamic extends HTML_QuickForm_Renderer
38
{
38
{
39
   /**
39
   /**
40
    * A template class (HTML_Template_ITX or HTML_Template_Sigma) instance
40
    * A template class (HTML_Template_ITX or HTML_Template_Sigma) instance
41
    * @var object
41
    * @var object
42
    */
42
    */
43
    var $_tpl = null;
43
    var $_tpl = null;
44
 
44
 
45
   /**
45
   /**
46
    * The errors that were not shown near concrete fields go here
46
    * The errors that were not shown near concrete fields go here
47
    * @var array
47
    * @var array
48
    */
48
    */
49
    var $_errors = array();
49
    var $_errors = array();
50
 
50
 
51
   /**
51
   /**
52
    * Show the block with required note?
52
    * Show the block with required note?
53
    * @var bool
53
    * @var bool
54
    */
54
    */
55
    var $_showRequired = false;
55
    var $_showRequired = false;
56
 
56
 
57
   /**
57
   /**
58
    * A separator for group elements
58
    * A separator for group elements
59
    * @var mixed
59
    * @var mixed
60
    */
60
    */
61
    var $_groupSeparator = null;
61
    var $_groupSeparator = null;
62
 
62
 
63
   /**
63
   /**
64
    * The current element index inside a group
64
    * The current element index inside a group
65
    * @var integer
65
    * @var integer
66
    */
66
    */
67
    var $_groupElementIdx = 0;
67
    var $_groupElementIdx = 0;
68
 
68
 
69
   /**
69
   /**
70
    * Blocks to use for different elements  
70
    * Blocks to use for different elements  
71
    * @var array
71
    * @var array
72
    */
72
    */
73
    var $_elementBlocks = array();
73
    var $_elementBlocks = array();
74
 
74
 
75
   /**
75
   /**
76
    * Block to use for headers
76
    * Block to use for headers
77
    * @var string
77
    * @var string
78
    */
78
    */
79
    var $_headerBlock = null;
79
    var $_headerBlock = null;
80
 
80
 
81
 
81
 
82
   /**
82
   /**
83
    * Constructor
83
    * Constructor
84
    *
84
    *
85
    * @param object     An HTML_Template_ITX/HTML_Template_Sigma object to use
85
    * @param object     An HTML_Template_ITX/HTML_Template_Sigma object to use
86
    */
86
    */
87
    function HTML_QuickForm_Renderer_ITDynamic(&$tpl)
87
    function HTML_QuickForm_Renderer_ITDynamic(&$tpl)
88
    {
88
    {
89
        $this->HTML_QuickForm_Renderer();
89
        $this->HTML_QuickForm_Renderer();
90
        $this->_tpl =& $tpl;
90
        $this->_tpl =& $tpl;
91
        $this->_tpl->setCurrentBlock('qf_main_loop');
91
        $this->_tpl->setCurrentBlock('qf_main_loop');
92
    }
92
    }
93
 
93
 
94
 
94
 
95
    function finishForm(&$form)
95
    function finishForm(&$form)
96
    {
96
    {
97
        // display errors above form
97
        // display errors above form
98
        if (!empty($this->_errors) && $this->_tpl->blockExists('qf_error_loop')) {
98
        if (!empty($this->_errors) && $this->_tpl->blockExists('qf_error_loop')) {
99
            foreach ($this->_errors as $error) {
99
            foreach ($this->_errors as $error) {
100
                $this->_tpl->setVariable('qf_error', $error);
100
                $this->_tpl->setVariable('qf_error', $error);
101
                $this->_tpl->parse('qf_error_loop');
101
                $this->_tpl->parse('qf_error_loop');
102
            }
102
            }
103
        }
103
        }
104
        // show required note
104
        // show required note
105
        if ($this->_showRequired) {
105
        if ($this->_showRequired) {
106
            $this->_tpl->setVariable('qf_required_note', $form->getRequiredNote());
106
            $this->_tpl->setVariable('qf_required_note', $form->getRequiredNote());
107
        }
107
        }
108
        // assign form attributes
108
        // assign form attributes
109
        $this->_tpl->setVariable('qf_attributes', $form->getAttributes(true));
109
        $this->_tpl->setVariable('qf_attributes', $form->getAttributes(true));
110
        // assign javascript validation rules
110
        // assign javascript validation rules
111
        $this->_tpl->setVariable('qf_javascript', $form->getValidationScript());
111
        $this->_tpl->setVariable('qf_javascript', $form->getValidationScript());
112
    }
112
    }
113
      
113
      
114
 
114
 
115
    function renderHeader(&$header)
115
    function renderHeader(&$header)
116
    {
116
    {
117
        $blockName = $this->_matchBlock($header);
117
        $blockName = $this->_matchBlock($header);
118
        if ('qf_header' == $blockName && isset($this->_headerBlock)) {
118
        if ('qf_header' == $blockName && isset($this->_headerBlock)) {
119
            $blockName = $this->_headerBlock;
119
            $blockName = $this->_headerBlock;
120
        }
120
        }
121
        $this->_tpl->setVariable('qf_header', $header->toHtml());
121
        $this->_tpl->setVariable('qf_header', $header->toHtml());
122
        $this->_tpl->parse($blockName);
122
        $this->_tpl->parse($blockName);
123
        $this->_tpl->parse('qf_main_loop');
123
        $this->_tpl->parse('qf_main_loop');
124
    }
124
    }
125
 
125
 
126
 
126
 
127
    function renderElement(&$element, $required, $error)
127
    function renderElement(&$element, $required, $error)
128
    {
128
    {
129
        $blockName = $this->_matchBlock($element);
129
        $blockName = $this->_matchBlock($element);
130
        // are we inside a group?
130
        // are we inside a group?
131
        if ('qf_main_loop' != $this->_tpl->currentBlock) {
131
        if ('qf_main_loop' != $this->_tpl->currentBlock) {
132
            if (0 != $this->_groupElementIdx && $this->_tpl->placeholderExists('qf_separator', $blockName)) {
132
            if (0 != $this->_groupElementIdx && $this->_tpl->placeholderExists('qf_separator', $blockName)) {
133
                if (is_array($this->_groupSeparator)) {
133
                if (is_array($this->_groupSeparator)) {
134
                    $this->_tpl->setVariable('qf_separator', $this->_groupSeparator[($this->_groupElementIdx - 1) % count($this->_groupSeparator)]);
134
                    $this->_tpl->setVariable('qf_separator', $this->_groupSeparator[($this->_groupElementIdx - 1) % count($this->_groupSeparator)]);
135
                } else {
135
                } else {
136
                    $this->_tpl->setVariable('qf_separator', (string)$this->_groupSeparator);
136
                    $this->_tpl->setVariable('qf_separator', (string)$this->_groupSeparator);
137
                }
137
                }
138
            }
138
            }
139
            $this->_groupElementIdx++;
139
            $this->_groupElementIdx++;
140
 
140
 
141
        } elseif(!empty($error)) {
141
        } elseif(!empty($error)) {
142
            // show the error message or keep it for later use
142
            // show the error message or keep it for later use
143
            if ($this->_tpl->blockExists($blockName . '_error')) {
143
            if ($this->_tpl->blockExists($blockName . '_error')) {
144
                $this->_tpl->setVariable('qf_error', $error);
144
                $this->_tpl->setVariable('qf_error', $error);
145
            } else {
145
            } else {
146
                $this->_errors[] = $error;
146
                $this->_errors[] = $error;
147
            }
147
            }
148
        }
148
        }
149
        // show an '*' near the required element
149
        // show an '*' near the required element
150
        if ($required) {
150
        if ($required) {
151
            $this->_showRequired = true;
151
            $this->_showRequired = true;
152
            if ($this->_tpl->blockExists($blockName . '_required')) {
152
            if ($this->_tpl->blockExists($blockName . '_required')) {
153
                $this->_tpl->touchBlock($blockName . '_required');
153
                $this->_tpl->touchBlock($blockName . '_required');
154
            }
154
            }
155
        }
155
        }
156
        // Prepare multiple labels
156
        // Prepare multiple labels
157
        $labels = $element->getLabel();
157
        $labels = $element->getLabel();
158
        if (is_array($labels)) {
158
        if (is_array($labels)) {
159
            $mainLabel = array_shift($labels);
159
            $mainLabel = array_shift($labels);
160
        } else {
160
        } else {
161
            $mainLabel = $labels;
161
            $mainLabel = $labels;
162
        }
162
        }
163
        // render the element itself with its main label
163
        // render the element itself with its main label
164
        $this->_tpl->setVariable('qf_element', $element->toHtml());
164
        $this->_tpl->setVariable('qf_element', $element->toHtml());
165
        if ($this->_tpl->placeholderExists('qf_label', $blockName)) {
165
        if ($this->_tpl->placeholderExists('qf_label', $blockName)) {
166
            $this->_tpl->setVariable('qf_label', $mainLabel);
166
            $this->_tpl->setVariable('qf_label', $mainLabel);
167
        }
167
        }
168
        // render extra labels, if any
168
        // render extra labels, if any
169
        if (is_array($labels)) {
169
        if (is_array($labels)) {
170
            foreach($labels as $key => $label) {
170
            foreach($labels as $key => $label) {
171
                $key = is_int($key)? $key + 2: $key;
171
                $key = is_int($key)? $key + 2: $key;
172
                if ($this->_tpl->blockExists($blockName . '_label_' . $key)) {
172
                if ($this->_tpl->blockExists($blockName . '_label_' . $key)) {
173
                    $this->_tpl->setVariable('qf_label_' . $key, $label);
173
                    $this->_tpl->setVariable('qf_label_' . $key, $label);
174
                }
174
                }
175
            }
175
            }
176
        }
176
        }
177
        $this->_tpl->parse($blockName);
177
        $this->_tpl->parse($blockName);
178
        $this->_tpl->parseCurrentBlock();
178
        $this->_tpl->parseCurrentBlock();
179
    }
179
    }
180
   
180
   
181
 
181
 
182
    function renderHidden(&$element)
182
    function renderHidden(&$element)
183
    {
183
    {
184
        $this->_tpl->setVariable('qf_hidden', $element->toHtml());
184
        $this->_tpl->setVariable('qf_hidden', $element->toHtml());
185
        $this->_tpl->parse('qf_hidden_loop');
185
        $this->_tpl->parse('qf_hidden_loop');
186
    }
186
    }
187
 
187
 
188
 
188
 
189
    function startGroup(&$group, $required, $error)
189
    function startGroup(&$group, $required, $error)
190
    {
190
    {
191
        $blockName = $this->_matchBlock($group);
191
        $blockName = $this->_matchBlock($group);
192
        $this->_tpl->setCurrentBlock($blockName . '_loop');
192
        $this->_tpl->setCurrentBlock($blockName . '_loop');
193
        $this->_groupElementIdx = 0;
193
        $this->_groupElementIdx = 0;
194
        $this->_groupSeparator  = is_null($group->_separator)? '&nbsp;': $group->_separator;
194
        $this->_groupSeparator  = is_null($group->_separator)? '&nbsp;': $group->_separator;
195
        // show an '*' near the required element
195
        // show an '*' near the required element
196
        if ($required) {
196
        if ($required) {
197
            $this->_showRequired = true;
197
            $this->_showRequired = true;
198
            if ($this->_tpl->blockExists($blockName . '_required')) {
198
            if ($this->_tpl->blockExists($blockName . '_required')) {
199
                $this->_tpl->touchBlock($blockName . '_required');
199
                $this->_tpl->touchBlock($blockName . '_required');
200
            }
200
            }
201
        }
201
        }
202
        // show the error message or keep it for later use
202
        // show the error message or keep it for later use
203
        if (!empty($error)) {
203
        if (!empty($error)) {
204
            if ($this->_tpl->blockExists($blockName . '_error')) {
204
            if ($this->_tpl->blockExists($blockName . '_error')) {
205
                $this->_tpl->setVariable('qf_error', $error);
205
                $this->_tpl->setVariable('qf_error', $error);
206
            } else {
206
            } else {
207
                $this->_errors[] = $error;
207
                $this->_errors[] = $error;
208
            }
208
            }
209
        }
209
        }
210
        $this->_tpl->setVariable('qf_group_label', $group->getLabel());
210
        $this->_tpl->setVariable('qf_group_label', $group->getLabel());
211
    }
211
    }
212
 
212
 
213
 
213
 
214
    function finishGroup(&$group)
214
    function finishGroup(&$group)
215
    {
215
    {
216
        $this->_tpl->parse($this->_matchBlock($group));
216
        $this->_tpl->parse($this->_matchBlock($group));
217
        $this->_tpl->setCurrentBlock('qf_main_loop');
217
        $this->_tpl->setCurrentBlock('qf_main_loop');
218
        $this->_tpl->parseCurrentBlock();
218
        $this->_tpl->parseCurrentBlock();
219
    }
219
    }
220
 
220
 
221
 
221
 
222
   /**
222
   /**
223
    * Returns the name of a block to use for element rendering
223
    * Returns the name of a block to use for element rendering
224
    * 
224
    * 
225
    * If a name was not explicitly set via setElementBlock(), it tries
225
    * If a name was not explicitly set via setElementBlock(), it tries
226
    * the names '{prefix}_{element type}' and '{prefix}_{element}', where
226
    * the names '{prefix}_{element type}' and '{prefix}_{element}', where
227
    * prefix is either 'qf' or the name of the current group's block
227
    * prefix is either 'qf' or the name of the current group's block
228
    * 
228
    * 
229
    * @param object     An HTML_QuickForm_element object
229
    * @param object     An HTML_QuickForm_element object
230
    * @access private
230
    * @access private
231
    * @return string    block name
231
    * @return string    block name
232
    */
232
    */
233
    function _matchBlock(&$element)
233
    function _matchBlock(&$element)
234
    {
234
    {
235
        $name = $element->getName();
235
        $name = $element->getName();
236
        $type = $element->getType();
236
        $type = $element->getType();
237
        if (isset($this->_elementBlocks[$name]) && $this->_tpl->blockExists($this->_elementBlocks[$name])) {
237
        if (isset($this->_elementBlocks[$name]) && $this->_tpl->blockExists($this->_elementBlocks[$name])) {
238
            if (('group' == $type) || ($this->_elementBlocks[$name] . '_loop' != $this->_tpl->currentBlock)) {
238
            if (('group' == $type) || ($this->_elementBlocks[$name] . '_loop' != $this->_tpl->currentBlock)) {
239
                return $this->_elementBlocks[$name];
239
                return $this->_elementBlocks[$name];
240
            }
240
            }
241
        }
241
        }
242
        if ('group' != $type && 'qf_main_loop' != $this->_tpl->currentBlock) {
242
        if ('group' != $type && 'qf_main_loop' != $this->_tpl->currentBlock) {
243
            $prefix = substr($this->_tpl->currentBlock, 0, -5); // omit '_loop' postfix
243
            $prefix = substr($this->_tpl->currentBlock, 0, -5); // omit '_loop' postfix
244
        } else {
244
        } else {
245
            $prefix = 'qf';
245
            $prefix = 'qf';
246
        }
246
        }
247
        if ($this->_tpl->blockExists($prefix . '_' . $type)) {
247
        if ($this->_tpl->blockExists($prefix . '_' . $type)) {
248
            return $prefix . '_' . $type;
248
            return $prefix . '_' . $type;
249
        } elseif ($this->_tpl->blockExists($prefix . '_' . $name)) {
249
        } elseif ($this->_tpl->blockExists($prefix . '_' . $name)) {
250
            return $prefix . '_' . $name;
250
            return $prefix . '_' . $name;
251
        } else {
251
        } else {
252
            return $prefix . '_element';
252
            return $prefix . '_element';
253
        }
253
        }
254
    }
254
    }
255
 
255
 
256
 
256
 
257
   /**
257
   /**
258
    * Sets the block to use for element rendering
258
    * Sets the block to use for element rendering
259
    * 
259
    * 
260
    * @param mixed      element name or array ('element name' => 'block name')
260
    * @param mixed      element name or array ('element name' => 'block name')
261
    * @param string     block name if $elementName is not an array
261
    * @param string     block name if $elementName is not an array
262
    * @access public
262
    * @access public
263
    * @return void
263
    * @return void
264
    */
264
    */
265
    function setElementBlock($elementName, $blockName = null)
265
    function setElementBlock($elementName, $blockName = null)
266
    {
266
    {
267
        if (is_array($elementName)) {
267
        if (is_array($elementName)) {
268
            $this->_elementBlocks = array_merge($this->_elementBlocks, $elementName);
268
            $this->_elementBlocks = array_merge($this->_elementBlocks, $elementName);
269
        } else {
269
        } else {
270
            $this->_elementBlocks[$elementName] = $blockName;
270
            $this->_elementBlocks[$elementName] = $blockName;
271
        }
271
        }
272
    }
272
    }
273
 
273
 
274
 
274
 
275
   /**
275
   /**
276
    * Sets the name of a block to use for header rendering
276
    * Sets the name of a block to use for header rendering
277
    *
277
    *
278
    * @param string     block name
278
    * @param string     block name
279
    * @access public
279
    * @access public
280
    * @return void
280
    * @return void
281
    */
281
    */
282
    function setHeaderBlock($blockName)
282
    function setHeaderBlock($blockName)
283
    {
283
    {
284
        $this->_headerBlock = $blockName;
284
        $this->_headerBlock = $blockName;
285
    }
285
    }
286
}
286
}
287
?>
287
?>