2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * A concrete renderer for HTML_QuickForm, using Integrated Templates.
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package HTML_QuickForm
17 * @author Alexey Borzov <avb@php.net>
18 * @copyright 2001-2011 The PHP Group
19 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @link http://pear.php.net/package/HTML_QuickForm
25 * An abstract base class for QuickForm renderers
27 require_once 'HTML/QuickForm/Renderer.php';
30 * A concrete renderer for HTML_QuickForm, using Integrated Templates.
32 * This is a "dynamic" renderer, which means that concrete form look
33 * is defined at runtime. This also means that you can define
34 * <b>one</b> template file for <b>all</b> your forms. That template
35 * should contain a block for every element 'look' appearing in your
36 * forms and also some special blocks (consult the examples). If a
37 * special block is not set for an element, the renderer falls back to
41 * @package HTML_QuickForm
42 * @author Alexey Borzov <avb@php.net>
43 * @version Release: 3.2.16
46 class HTML_QuickForm_Renderer_ITDynamic extends HTML_QuickForm_Renderer
52 * A template class (HTML_Template_ITX or HTML_Template_Sigma) instance
53 * @var HTML_Template_ITX|HTML_Template_Sigma
58 * The errors that were not shown near concrete fields go here
61 var $_errors = array();
64 * Show the block with required note?
67 var $_showRequired = false;
70 * A separator for group elements
73 var $_groupSeparator = null;
76 * The current element index inside a group
79 var $_groupElementIdx = 0;
82 * Blocks to use for different elements
85 var $_elementBlocks = array();
88 * Block to use for headers
91 var $_headerBlock = null;
98 * @param HTML_Template_ITX|HTML_Template_Sigma Template object to use
100 function HTML_QuickForm_Renderer_ITDynamic(&$tpl)
102 $this->HTML_QuickForm_Renderer();
104 $this->_tpl->setCurrentBlock('qf_main_loop');
108 function finishForm(&$form)
110 // display errors above form
111 if (!empty($this->_errors) && $this->_tpl->blockExists('qf_error_loop')) {
112 foreach ($this->_errors as $error) {
113 $this->_tpl->setVariable('qf_error', $error);
114 $this->_tpl->parse('qf_error_loop');
117 // show required note
118 if ($this->_showRequired) {
119 $this->_tpl->setVariable('qf_required_note', $form->getRequiredNote());
121 // assign form attributes
122 $this->_tpl->setVariable('qf_attributes', $form->getAttributes(true));
123 // assign javascript validation rules
124 $this->_tpl->setVariable('qf_javascript', $form->getValidationScript());
128 function renderHeader(&$header)
130 $blockName = $this->_matchBlock($header);
131 if ('qf_header' == $blockName && isset($this->_headerBlock)) {
132 $blockName = $this->_headerBlock;
134 $this->_tpl->setVariable('qf_header', $header->toHtml());
135 $this->_tpl->parse($blockName);
136 $this->_tpl->parse('qf_main_loop');
140 function renderElement(&$element, $required, $error)
142 $blockName = $this->_matchBlock($element);
143 // are we inside a group?
144 if ('qf_main_loop' != $this->_tpl->currentBlock) {
145 if (0 != $this->_groupElementIdx && $this->_tpl->placeholderExists('qf_separator', $blockName)) {
146 if (is_array($this->_groupSeparator)) {
147 $this->_tpl->setVariable('qf_separator', $this->_groupSeparator[($this->_groupElementIdx - 1) % count($this->_groupSeparator)]);
149 $this->_tpl->setVariable('qf_separator', (string)$this->_groupSeparator);
152 $this->_groupElementIdx++;
154 } elseif(!empty($error)) {
155 // show the error message or keep it for later use
156 if ($this->_tpl->blockExists($blockName . '_error')) {
157 $this->_tpl->setVariable('qf_error', $error);
159 $this->_errors[] = $error;
162 // show an '*' near the required element
164 $this->_showRequired = true;
165 if ($this->_tpl->blockExists($blockName . '_required')) {
166 $this->_tpl->touchBlock($blockName . '_required');
169 // Prepare multiple labels
170 $labels = $element->getLabel();
171 if (is_array($labels)) {
172 $mainLabel = array_shift($labels);
174 $mainLabel = $labels;
176 // render the element itself with its main label
177 $this->_tpl->setVariable('qf_element', $element->toHtml());
178 if ($this->_tpl->placeholderExists('qf_label', $blockName)) {
179 $this->_tpl->setVariable('qf_label', $mainLabel);
181 // render extra labels, if any
182 if (is_array($labels)) {
183 foreach($labels as $key => $label) {
184 $key = is_int($key)? $key + 2: $key;
185 if ($this->_tpl->blockExists($blockName . '_label_' . $key)) {
186 $this->_tpl->setVariable('qf_label_' . $key, $label);
190 $this->_tpl->parse($blockName);
191 $this->_tpl->parseCurrentBlock();
195 function renderHidden(&$element)
197 $this->_tpl->setVariable('qf_hidden', $element->toHtml());
198 $this->_tpl->parse('qf_hidden_loop');
202 function startGroup(&$group, $required, $error)
204 $blockName = $this->_matchBlock($group);
205 $this->_tpl->setCurrentBlock($blockName . '_loop');
206 $this->_groupElementIdx = 0;
207 $this->_groupSeparator = is_null($group->_separator)? ' ': $group->_separator;
208 // show an '*' near the required element
210 $this->_showRequired = true;
211 if ($this->_tpl->blockExists($blockName . '_required')) {
212 $this->_tpl->touchBlock($blockName . '_required');
215 // show the error message or keep it for later use
216 if (!empty($error)) {
217 if ($this->_tpl->blockExists($blockName . '_error')) {
218 $this->_tpl->setVariable('qf_error', $error);
220 $this->_errors[] = $error;
223 $this->_tpl->setVariable('qf_group_label', $group->getLabel());
227 function finishGroup(&$group)
229 $this->_tpl->parse($this->_matchBlock($group));
230 $this->_tpl->setCurrentBlock('qf_main_loop');
231 $this->_tpl->parseCurrentBlock();
236 * Returns the name of a block to use for element rendering
238 * If a name was not explicitly set via setElementBlock(), it tries
239 * the names '{prefix}_{element type}' and '{prefix}_{element}', where
240 * prefix is either 'qf' or the name of the current group's block
242 * @param HTML_QuickForm_element form element being rendered
244 * @return string block name
246 function _matchBlock(&$element)
248 $name = $element->getName();
249 $type = $element->getType();
250 if (isset($this->_elementBlocks[$name]) && $this->_tpl->blockExists($this->_elementBlocks[$name])) {
251 if (('group' == $type) || ($this->_elementBlocks[$name] . '_loop' != $this->_tpl->currentBlock)) {
252 return $this->_elementBlocks[$name];
255 if ('group' != $type && 'qf_main_loop' != $this->_tpl->currentBlock) {
256 $prefix = substr($this->_tpl->currentBlock, 0, -5); // omit '_loop' postfix
260 if ($this->_tpl->blockExists($prefix . '_' . $type)) {
261 return $prefix . '_' . $type;
262 } elseif ($this->_tpl->blockExists($prefix . '_' . $name)) {
263 return $prefix . '_' . $name;
265 return $prefix . '_element';
271 * Sets the block to use for element rendering
273 * @param mixed element name or array ('element name' => 'block name')
274 * @param string block name if $elementName is not an array
278 function setElementBlock($elementName, $blockName = null)
280 if (is_array($elementName)) {
281 $this->_elementBlocks = array_merge($this->_elementBlocks, $elementName);
283 $this->_elementBlocks[$elementName] = $blockName;
289 * Sets the name of a block to use for header rendering
291 * @param string block name
295 function setHeaderBlock($blockName)
297 $this->_headerBlock = $blockName;