3 * A renderer for HTML_QuickForm that only uses XHTML and CSS but no
8 * LICENSE: This source file is subject to version 3.01 of the PHP license
9 * that is available through the world-wide-web at the following URI:
10 * http://www.php.net/license/3_01.txt. If you did not receive a copy of
11 * the PHP License and are unable to obtain it through the web, please
12 * send a note to license@php.net so we can mail you a copy immediately.
15 * @package HTML_QuickForm_Renderer_Tableless
16 * @author Alexey Borzov <borz_off@cs.msu.su>
17 * @author Adam Daniel <adaniel1@eesus.jnj.com>
18 * @author Bertrand Mansion <bmansion@mamasam.com>
19 * @author Mark Wiesemann <wiesemann@php.net>
20 * @copyright 2005-2006 The PHP Group
21 * @license http://www.php.net/license/3_01.txt PHP License 3.01
23 * @link http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
26 require_once 'HTML/QuickForm/Renderer/Default.php';
29 * A renderer for HTML_QuickForm that only uses XHTML and CSS but no
32 * You need to specify a stylesheet like the one that you find in
33 * data/stylesheet.css to make this work.
36 * @package HTML_QuickForm_Renderer_Tableless
37 * @author Alexey Borzov <borz_off@cs.msu.su>
38 * @author Adam Daniel <adaniel1@eesus.jnj.com>
39 * @author Bertrand Mansion <bmansion@mamasam.com>
40 * @author Mark Wiesemann <wiesemann@php.net>
41 * @license http://www.php.net/license/3_01.txt PHP License 3.01
42 * @version Release: 0.3.4
43 * @link http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
45 class HTML_QuickForm_Renderer_Tableless
extends HTML_QuickForm_Renderer_Default
48 * Header Template string
52 var $_headerTemplate =
53 "\n\t\t<legend>{header}</legend>";
56 * Element template string
60 var $_elementTemplate =
61 "\n\t\t<div class=\"qfrow\"><label class=\"qflabel\"><!-- BEGIN required --><span class=\"required\">*</span><!-- END required -->{label}</label><div class=\"qfelement<!-- BEGIN error --> error<!-- END error -->\"><!-- BEGIN error --><span class=\"error\">{error}</span><br /><!-- END error -->{element}</div></div><br />";
64 * Form template string
69 "\n<form{attributes}>\n\t<div style=\"display: none;\">{hidden}</div>\n{content}\n</form>";
72 * Template used when opening a fieldset
76 var $_openFieldsetTemplate = "\n\t<fieldset{id}>";
79 * Template used when opening a hidden fieldset
80 * (i.e. a fieldset that is opened when there is no header element)
84 var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">";
87 * Template used when closing a fieldset
91 var $_closeFieldsetTemplate = "\n\t</fieldset>";
94 * Required Note template string
98 var $_requiredNoteTemplate =
99 "\n\t\t<div class=\"qfreqnote\">{requiredNote}</div>";
102 * How many fieldsets are open
106 var $_fieldsetsOpen = 0;
109 * Array of element names that indicate the end of a fieldset
110 * (a new one will be opened when a the next header element occurs)
114 var $_stopFieldsetElements = array();
121 function HTML_QuickForm_Renderer_Tableless()
123 $this->HTML_QuickForm_Renderer_Default();
127 * Called when visiting a header element
129 * @param object An HTML_QuickForm_header element being visited
133 function renderHeader(&$header)
135 $name = $header->getName();
136 $id = empty($name) ?
'' : ' id="' . $name . '"';
137 if (is_null($header->_text
)) {
140 elseif (!empty($name) && isset($this->_templates
[$name])) {
141 $header_html = str_replace('{header}', $header->toHtml(), $this->_templates
[$name]);
143 $header_html = str_replace('{header}', $header->toHtml(), $this->_headerTemplate
);
145 if ($this->_fieldsetsOpen
> 0) {
146 $this->_html
.= $this->_closeFieldsetTemplate
;
147 $this->_fieldsetsOpen
--;
149 $openFieldsetTemplate = str_replace('{id}', $id, $this->_openFieldsetTemplate
);
150 $this->_html
.= $openFieldsetTemplate . $header_html;
151 $this->_fieldsetsOpen++
;
152 } // end func renderHeader
155 * Renders an element Html
156 * Called when visiting an element
158 * @param object An HTML_QuickForm_element object being visited
159 * @param bool Whether an element is required
160 * @param string An error message associated with an element
164 function renderElement(&$element, $required, $error)
166 // if the element name indicates the end of a fieldset, close the fieldset
167 if ( in_array($element->getName(), $this->_stopFieldsetElements
)
168 && $this->_fieldsetsOpen
> 0
170 $this->_html
.= $this->_closeFieldsetTemplate
;
171 $this->_fieldsetsOpen
--;
173 // if no fieldset was opened, we need to open a hidden one here to get
175 if ($this->_fieldsetsOpen
=== 0) {
176 $this->_html
.= $this->_openHiddenFieldsetTemplate
;
177 $this->_fieldsetsOpen++
;
179 if (!$this->_inGroup
) {
180 $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
181 // the following lines (until the "elseif") were changed / added
182 // compared to the default renderer
183 $element_html = $element->toHtml();
184 if (!is_null($element->getAttribute('id'))) {
185 $id = $element->getAttribute('id');
187 $id = $element->getName();
190 $html = str_replace('<label', '<label for="' . $id . '"', $html);
191 $element_html = preg_replace('#name="' . $id . '#',
192 'id="' . $id . '" name="' . $id . '',
196 $this->_html
.= str_replace('{element}', $element_html, $html);
197 } elseif (!empty($this->_groupElementTemplate
)) {
198 $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate
);
200 $html = str_replace('<!-- BEGIN required -->', '', $html);
201 $html = str_replace('<!-- END required -->', '', $html);
203 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html);
205 $this->_groupElements
[] = str_replace('{element}', $element->toHtml(), $html);
208 $this->_groupElements
[] = $element->toHtml();
210 } // end func renderElement
213 * Called when visiting a form, before processing any form elements
215 * @param object An HTML_QuickForm object being visited
219 function startForm(&$form)
221 $this->_fieldsetsOpen
= 0;
222 parent
::startForm($form);
223 } // end func startForm
226 * Called when visiting a form, after processing all form elements
227 * Adds required note, form attributes, validation javascript and form content.
229 * @param object An HTML_QuickForm object being visited
233 function finishForm(&$form)
235 // add a required note, if one is needed
236 if (!empty($form->_required
) && !$form->_freezeAll
) {
237 $requiredNote = $form->getRequiredNote();
238 // replace default required note by DOM/XHTML optimized note
239 if ($requiredNote == '<span style="font-size:80%; color:#ff0000;">*</span><span style="font-size:80%;"> denotes required field</span>') {
240 $requiredNote = '<span class="required">*</span> denotes required field';
242 $this->_html
.= str_replace('{requiredNote}', $requiredNote, $this->_requiredNoteTemplate
);
244 // close the open fieldset
245 if ($this->_fieldsetsOpen
> 0) {
246 $this->_html
.= $this->_closeFieldsetTemplate
;
247 $this->_fieldsetsOpen
--;
249 // add form attributes and content
250 $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate
);
251 if (strpos($this->_formTemplate
, '{hidden}')) {
252 $html = str_replace('{hidden}', $this->_hiddenHtml
, $html);
254 $this->_html
.= $this->_hiddenHtml
;
256 $this->_hiddenHtml
= '';
257 $this->_html
= str_replace('{content}', $this->_html
, $html);
258 $this->_html
= str_replace('></label>', '> </label>', $this->_html
);
259 // add a validation script
260 if ('' != ($script = $form->getValidationScript())) {
261 $this->_html
= $script . "\n" . $this->_html
;
263 } // end func finishForm
266 * Sets the template used when opening a fieldset
268 * @param string The HTML used when opening a fieldset
272 function setOpenFieldsetTemplate($html)
274 $this->_openFieldsetTemplate
= $html;
275 } // end func setOpenFieldsetTemplate
278 * Sets the template used when opening a hidden fieldset
279 * (i.e. a fieldset that is opened when there is no header element)
281 * @param string The HTML used when opening a hidden fieldset
285 function setOpenHiddenFieldsetTemplate($html)
287 $this->_openHiddenFieldsetTemplate
= $html;
288 } // end func setOpenHiddenFieldsetTemplate
291 * Sets the template used when closing a fieldset
293 * @param string The HTML used when closing a fieldset
297 function setCloseFieldsetTemplate($html)
299 $this->_closeFieldsetTemplate
= $html;
300 } // end func setCloseFieldsetTemplate
303 * Adds one or more element names that indicate the end of a fieldset
304 * (a new one will be opened when a the next header element occurs)
306 * @param mixed Element name(s) (as array or string)
310 function addStopFieldsetElements($element)
312 if (is_array($element)) {
313 $this->_stopFieldsetElements
= array_merge($this->_stopFieldsetElements
,
316 $this->_stopFieldsetElements
[] = $element;
318 } // end func addStopFieldsetElements
320 } // end class HTML_QuickForm_Renderer_Default