adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / Renderer / Tableless.php
blobfb5196ffec6c2d976c7b111520fc88474f42857d
1 <?php
2 /**
3 * A renderer for HTML_QuickForm that only uses XHTML and CSS but no
4 * table tags
6 * PHP versions 4 and 5
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.
14 * @category HTML
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
22 * @version CVS: $Id$
23 * @link http://pear.php.net/package/HTML_QuickForm_Renderer_Tableless
26 require_once 'HTML/QuickForm/Renderer/Default.php';
28 /**
29 * A renderer for HTML_QuickForm that only uses XHTML and CSS but no
30 * table tags
32 * You need to specify a stylesheet like the one that you find in
33 * data/stylesheet.css to make this work.
35 * @category HTML
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
47 /**
48 * Header Template string
49 * @var string
50 * @access private
52 var $_headerTemplate =
53 "\n\t\t<legend>{header}</legend>";
55 /**
56 * Element template string
57 * @var string
58 * @access private
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 />";
63 /**
64 * Form template string
65 * @var string
66 * @access private
68 var $_formTemplate =
69 "\n<form{attributes}>\n\t<div style=\"display: none;\">{hidden}</div>\n{content}\n</form>";
71 /**
72 * Template used when opening a fieldset
73 * @var string
74 * @access private
76 var $_openFieldsetTemplate = "\n\t<fieldset{id}>";
78 /**
79 * Template used when opening a hidden fieldset
80 * (i.e. a fieldset that is opened when there is no header element)
81 * @var string
82 * @access private
84 var $_openHiddenFieldsetTemplate = "\n\t<fieldset class=\"hidden\">";
86 /**
87 * Template used when closing a fieldset
88 * @var string
89 * @access private
91 var $_closeFieldsetTemplate = "\n\t</fieldset>";
93 /**
94 * Required Note template string
95 * @var string
96 * @access private
98 var $_requiredNoteTemplate =
99 "\n\t\t<div class=\"qfreqnote\">{requiredNote}</div>";
102 * How many fieldsets are open
103 * @var integer
104 * @access private
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)
111 * @var array
112 * @access private
114 var $_stopFieldsetElements = array();
117 * Constructor
119 * @access public
121 function HTML_QuickForm_Renderer_Tableless()
123 $this->HTML_QuickForm_Renderer_Default();
124 } // end constructor
127 * Called when visiting a header element
129 * @param object An HTML_QuickForm_header element being visited
130 * @access public
131 * @return void
133 function renderHeader(&$header)
135 $name = $header->getName();
136 $id = empty($name) ? '' : ' id="' . $name . '"';
137 if (is_null($header->_text)) {
138 $header_html = '';
140 elseif (!empty($name) && isset($this->_templates[$name])) {
141 $header_html = str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
142 } else {
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
161 * @access public
162 * @return void
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
174 // XHTML validity
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');
186 } else {
187 $id = $element->getName();
189 if (!empty($id)) {
190 $html = str_replace('<label', '<label for="' . $id . '"', $html);
191 $element_html = preg_replace('#name="' . $id . '#',
192 'id="' . $id . '" name="' . $id . '',
193 $element_html,
196 $this->_html .= str_replace('{element}', $element_html, $html);
197 } elseif (!empty($this->_groupElementTemplate)) {
198 $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
199 if ($required) {
200 $html = str_replace('<!-- BEGIN required -->', '', $html);
201 $html = str_replace('<!-- END required -->', '', $html);
202 } else {
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);
207 } else {
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
216 * @access public
217 * @return void
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
230 * @access public
231 * @return void
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);
253 } else {
254 $this->_html .= $this->_hiddenHtml;
256 $this->_hiddenHtml = '';
257 $this->_html = str_replace('{content}', $this->_html, $html);
258 $this->_html = str_replace('></label>', '>&nbsp;</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
269 * @access public
270 * @return void
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
282 * @access public
283 * @return void
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
294 * @access public
295 * @return void
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)
307 * @access public
308 * @return void
310 function addStopFieldsetElements($element)
312 if (is_array($element)) {
313 $this->_stopFieldsetElements = array_merge($this->_stopFieldsetElements,
314 $element);
315 } else {
316 $this->_stopFieldsetElements[] = $element;
318 } // end func addStopFieldsetElements
320 } // end class HTML_QuickForm_Renderer_Default