2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
23 require_once('HTML/QuickForm/Renderer.php');
26 * A concrete renderer for HTML_QuickForm,
27 * based on QuickForm 2.x built-in one
31 class HTML_QuickForm_Renderer_Default
extends HTML_QuickForm_Renderer
34 * The HTML of the form
41 * Header Template string
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>";
49 * Element template string
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>";
57 * Form template string
62 "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>";
65 * Required Note template string
69 var $_requiredNoteTemplate =
70 "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>";
73 * Array containing the templates for customised elements
77 var $_templates = array();
80 * Array containing the templates for group wraps.
82 * These templates are wrapped around group elements and groups' own
83 * templates wrap around them. This is set by setGroupTemplate().
88 var $_groupWraps = array();
91 * Array containing the templates for elements within groups
95 var $_groupTemplates = array();
98 * True if we are inside a group
102 var $_inGroup = false;
105 * Array with HTML generated for group elements
109 var $_groupElements = array();
112 * Template for an element inside a group
116 var $_groupElementTemplate = '';
119 * HTML that wraps around the group elements
123 var $_groupWrap = '';
126 * HTML for the current group
130 var $_groupTemplate = '';
133 * Collected HTML of the hidden fields
137 var $_hiddenHtml = '';
144 function HTML_QuickForm_Renderer_Default()
146 $this->HTML_QuickForm_Renderer();
150 * returns the HTML generated for the form
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
;
163 * Called when visiting a form, before processing any form elements
165 * @param object An HTML_QuickForm object being visited
169 function startForm(&$form)
172 $this->_hiddenHtml
= '';
173 } // end func startForm
176 * Called when visiting a form, after processing all form elements
177 * Adds required note, form attributes, validation javascript and form content.
179 * @param object An HTML_QuickForm object being visited
183 function finishForm(&$form)
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
);
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);
194 $this->_html
.= $this->_hiddenHtml
;
196 $this->_hiddenHtml
= '';
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
;
202 } // end func finishForm
205 * Called when visiting a header element
207 * @param object An HTML_QuickForm_header element being visited
211 function renderHeader(&$header)
213 $name = $header->getName();
214 if (!empty($name) && isset($this->_templates
[$name])) {
215 $this->_html
.= str_replace('{header}', $header->toHtml(), $this->_templates
[$name]);
217 $this->_html
.= str_replace('{header}', $header->toHtml(), $this->_headerTemplate
);
219 } // end func renderHeader
222 * Helper method for renderElement
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
229 * @see renderElement()
230 * @return string Html for element
232 function _prepareTemplate($name, $label, $required, $error)
234 if (is_array($label)) {
235 $nameLabel = array_shift($label);
239 if (isset($this->_templates
[$name])) {
240 $html = str_replace('{label}', $nameLabel, $this->_templates
[$name]);
242 $html = str_replace('{label}', $nameLabel, $this->_elementTemplate
);
245 $html = str_replace('<!-- BEGIN required -->', '', $html);
246 $html = str_replace('<!-- END required -->', '', $html);
248 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/iU", '', $html);
251 $html = str_replace('{error}', $error, $html);
252 $html = str_replace('<!-- BEGIN error -->', '', $html);
253 $html = str_replace('<!-- END error -->', '', $html);
255 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/iU", '', $html);
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);
265 if (strpos($html, '{label_')) {
266 $html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $html);
269 } // end func _prepareTemplate
272 * Renders an element Html
273 * Called when visiting an element
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
281 function renderElement(&$element, $required, $error)
283 if (!$this->_inGroup
) {
284 $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
285 $this->_html
.= str_replace('{element}', $element->toHtml(), $html);
287 } elseif (!empty($this->_groupElementTemplate
)) {
288 $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate
);
290 $html = str_replace('<!-- BEGIN required -->', '', $html);
291 $html = str_replace('<!-- END required -->', '', $html);
293 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/iU", '', $html);
295 $this->_groupElements
[] = str_replace('{element}', $element->toHtml(), $html);
298 $this->_groupElements
[] = $element->toHtml();
300 } // end func renderElement
303 * Renders an hidden element
304 * Called when visiting a hidden element
306 * @param object An HTML_QuickForm_hidden object being visited
310 function renderHidden(&$element)
312 $this->_hiddenHtml
.= $element->toHtml() . "\n";
313 } // end func renderHidden
316 * Called when visiting a raw HTML/text pseudo-element
318 * @param object An HTML_QuickForm_html element being visited
322 function renderHtml(&$data)
324 $this->_html
.= $data->toHtml();
325 } // end func renderHtml
328 * Called when visiting a group, before processing any group elements
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
336 function startGroup(&$group, $required, $error)
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
347 * Called when visiting a group, after processing all group elements
349 * @param object An HTML_QuickForm_group object being visited
353 function finishGroup(&$group)
355 $separator = $group->_separator
;
356 if (is_array($separator)) {
357 $count = count($separator);
359 for ($i = 0; $i < count($this->_groupElements
); $i++
) {
360 $html .= (0 == $i?
'': $separator[($i - 1) %
$count]) . $this->_groupElements
[$i];
363 if (is_null($separator)) {
364 $separator = ' ';
366 $html = implode((string)$separator, $this->_groupElements
);
368 if (!empty($this->_groupWrap
)) {
369 $html = str_replace('{content}', $html, $this->_groupWrap
);
371 $this->_html
.= str_replace('{element}', $html, $this->_groupTemplate
);
372 $this->_inGroup
= false;
373 } // end func finishGroup
376 * Sets element template
378 * @param string The HTML surrounding an element
379 * @param string (optional) Name of the element to apply template for
383 function setElementTemplate($html, $element = null)
385 if (is_null($element)) {
386 $this->_elementTemplate
= $html;
388 $this->_templates
[$element] = $html;
390 } // end func setElementTemplate
394 * Sets template for a group wrapper
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()
400 * @param string The HTML surrounding group elements
401 * @param string Name of the group to apply template for
405 function setGroupTemplate($html, $group)
407 $this->_groupWraps
[$group] = $html;
408 } // end func setGroupTemplate
411 * Sets element template for elements within a group
413 * @param string The HTML surrounding an element
414 * @param string Name of the group to apply template for
418 function setGroupElementTemplate($html, $group)
420 $this->_groupTemplates
[$group] = $html;
421 } // end func setGroupElementTemplate
424 * Sets header template
426 * @param string The HTML surrounding the header
430 function setHeaderTemplate($html)
432 $this->_headerTemplate
= $html;
433 } // end func setHeaderTemplate
438 * @param string The HTML surrounding the form tags
442 function setFormTemplate($html)
444 $this->_formTemplate
= $html;
445 } // end func setFormTemplate
448 * Sets the note indicating required fields template
450 * @param string The HTML surrounding the required note
454 function setRequiredNoteTemplate($html)
456 $this->_requiredNoteTemplate
= $html;
457 } // end func setRequiredNoteTemplate
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
466 function clearAllTemplates()
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