commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / packages / HTML / QuickForm / Renderer / Default.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * A concrete renderer for HTML_QuickForm, based on QuickForm 2.x built-in one
6 *
7 * PHP versions 4 and 5
8 *
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.
14 *
15 * @category HTML
16 * @package HTML_QuickForm
17 * @author Alexey Borzov <avb@php.net>
18 * @author Adam Daniel <adaniel1@eesus.jnj.com>
19 * @author Bertrand Mansion <bmansion@mamasam.com>
20 * @copyright 2001-2009 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
24 */
25
26 /**
27 * An abstract base class for QuickForm renderers
28 */
29 require_once 'HTML/QuickForm/Renderer.php';
30
31 /**
32 * A concrete renderer for HTML_QuickForm, based on QuickForm 2.x built-in one
33 *
34 * @category HTML
35 * @package HTML_QuickForm
36 * @author Alexey Borzov <avb@php.net>
37 * @author Adam Daniel <adaniel1@eesus.jnj.com>
38 * @author Bertrand Mansion <bmansion@mamasam.com>
39 * @version Release: 3.2.11
40 * @since 3.0
41 */
42 class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
43 {
44 /**
45 * The HTML of the form
46 * @var string
47 * @access private
48 */
49 var $_html;
50
51 /**
52 * Header Template string
53 * @var string
54 * @access private
55 */
56 var $_headerTemplate =
57 "\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>";
58
59 /**
60 * Element template string
61 * @var string
62 * @access private
63 */
64 var $_elementTemplate =
65 "\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>";
66
67 /**
68 * Form template string
69 * @var string
70 * @access private
71 */
72 var $_formTemplate =
73 "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>";
74
75 /**
76 * Required Note template string
77 * @var string
78 * @access private
79 */
80 var $_requiredNoteTemplate =
81 "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>";
82
83 /**
84 * Array containing the templates for customised elements
85 * @var array
86 * @access private
87 */
88 var $_templates = array();
89
90 /**
91 * Array containing the templates for group wraps.
92 *
93 * These templates are wrapped around group elements and groups' own
94 * templates wrap around them. This is set by setGroupTemplate().
95 *
96 * @var array
97 * @access private
98 */
99 var $_groupWraps = array();
100
101 /**
102 * Array containing the templates for elements within groups
103 * @var array
104 * @access private
105 */
106 var $_groupTemplates = array();
107
108 /**
109 * True if we are inside a group
110 * @var bool
111 * @access private
112 */
113 var $_inGroup = false;
114
115 /**
116 * Array with HTML generated for group elements
117 * @var array
118 * @access private
119 */
120 var $_groupElements = array();
121
122 /**
123 * Template for an element inside a group
124 * @var string
125 * @access private
126 */
127 var $_groupElementTemplate = '';
128
129 /**
130 * HTML that wraps around the group elements
131 * @var string
132 * @access private
133 */
134 var $_groupWrap = '';
135
136 /**
137 * HTML for the current group
138 * @var string
139 * @access private
140 */
141 var $_groupTemplate = '';
142
143 /**
144 * Collected HTML of the hidden fields
145 * @var string
146 * @access private
147 */
148 var $_hiddenHtml = '';
149
150 /**
151 * Constructor
152 *
153 * @access public
154 */
155 function HTML_QuickForm_Renderer_Default()
156 {
157 $this->HTML_QuickForm_Renderer();
158 } // end constructor
159
160 /**
161 * returns the HTML generated for the form
162 *
163 * @access public
164 * @return string
165 */
166 function toHtml()
167 {
168 // _hiddenHtml is cleared in finishForm(), so this only matters when
169 // finishForm() was not called (e.g. group::toHtml(), bug #3511)
170 return $this->_hiddenHtml . $this->_html;
171 } // end func toHtml
172
173 /**
174 * Called when visiting a form, before processing any form elements
175 *
176 * @param HTML_QuickForm form object being visited
177 * @access public
178 * @return void
179 */
180 function startForm(&$form)
181 {
182 $this->_html = '';
183 $this->_hiddenHtml = '';
184 } // end func startForm
185
186 /**
187 * Called when visiting a form, after processing all form elements
188 * Adds required note, form attributes, validation javascript and form content.
189 *
190 * @param HTML_QuickForm form object being visited
191 * @access public
192 * @return void
193 */
194 function finishForm(&$form)
195 {
196 // add a required note, if one is needed
197 if (!empty($form->_required) && !$form->_freezeAll) {
198 $this->_html .= str_replace('{requiredNote}', $form->getRequiredNote(), $this->_requiredNoteTemplate);
199 }
200 // add form attributes and content
201 $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
202 if (strpos($this->_formTemplate, '{hidden}')) {
203 $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
204 } else {
205 $this->_html .= $this->_hiddenHtml;
206 }
207 $this->_hiddenHtml = '';
208 $this->_html = str_replace('{content}', $this->_html, $html);
209 // add a validation script
210 if ('' != ($script = $form->getValidationScript())) {
211 $this->_html = $script . "\n" . $this->_html;
212 }
213 } // end func finishForm
214
215 /**
216 * Called when visiting a header element
217 *
218 * @param HTML_QuickForm_header header element being visited
219 * @access public
220 * @return void
221 */
222 function renderHeader(&$header)
223 {
224 $name = $header->getName();
225 if (!empty($name) && isset($this->_templates[$name])) {
226 $this->_html .= str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
227 } else {
228 $this->_html .= str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
229 }
230 } // end func renderHeader
231
232 /**
233 * Helper method for renderElement
234 *
235 * @param string Element name
236 * @param mixed Element label (if using an array of labels, you should set the appropriate template)
237 * @param bool Whether an element is required
238 * @param string Error message associated with the element
239 * @access private
240 * @see renderElement()
241 * @return string Html for element
242 */
243 function _prepareTemplate($name, $label, $required, $error)
244 {
245 if (is_array($label)) {
246 $nameLabel = array_shift($label);
247 } else {
248 $nameLabel = $label;
249 }
250 if (isset($this->_templates[$name])) {
251 $html = str_replace('{label}', $nameLabel, $this->_templates[$name]);
252 } else {
253 $html = str_replace('{label}', $nameLabel, $this->_elementTemplate);
254 }
255 if ($required) {
256 $html = str_replace('<!-- BEGIN required -->', '', $html);
257 $html = str_replace('<!-- END required -->', '', $html);
258 } else {
259 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->.*<!-- END required -->([ \t\n\r]*)?/isU", '', $html);
260 }
261 if (isset($error)) {
262 $html = str_replace('{error}', $error, $html);
263 $html = str_replace('<!-- BEGIN error -->', '', $html);
264 $html = str_replace('<!-- END error -->', '', $html);
265 } else {
266 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->.*<!-- END error -->([ \t\n\r]*)?/isU", '', $html);
267 }
268 if (is_array($label)) {
269 foreach($label as $key => $text) {
270 $key = is_int($key)? $key + 2: $key;
271 $html = str_replace("{label_{$key}}", $text, $html);
272 $html = str_replace("<!-- BEGIN label_{$key} -->", '', $html);
273 $html = str_replace("<!-- END label_{$key} -->", '', $html);
274 }
275 }
276 if (strpos($html, '{label_')) {
277 $html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/is', '', $html);
278 }
279 return $html;
280 } // end func _prepareTemplate
281
282 /**
283 * Renders an element Html
284 * Called when visiting an element
285 *
286 * @param HTML_QuickForm_element form element being visited
287 * @param bool Whether an element is required
288 * @param string An error message associated with an element
289 * @access public
290 * @return void
291 */
292 function renderElement(&$element, $required, $error)
293 {
294 // make sure that all elements are id'ed even in a group!
295
296 CRM_Core_Form_Renderer::updateAttributes( $element, $required, $error );
297
298 if (!$this->_inGroup) {
299 $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
300 $this->_html .= str_replace('{element}', $element->toHtml(), $html);
301
302 } elseif (!empty($this->_groupElementTemplate)) {
303 $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
304 if ($required) {
305 $html = str_replace('<!-- BEGIN required -->', '', $html);
306 $html = str_replace('<!-- END required -->', '', $html);
307 } else {
308 $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->.*<!-- END required -->([ \t\n\r]*)?/isU", '', $html);
309 }
310 $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
311
312 } else {
313 $this->_groupElements[] = $element->toHtml();
314 }
315 } // end func renderElement
316
317 /**
318 * Renders an hidden element
319 * Called when visiting a hidden element
320 *
321 * @param HTML_QuickForm_element form element being visited
322 * @access public
323 * @return void
324 */
325 function renderHidden(&$element)
326 {
327 $this->_hiddenHtml .= $element->toHtml() . "\n";
328 } // end func renderHidden
329
330 /**
331 * Called when visiting a raw HTML/text pseudo-element
332 *
333 * @param HTML_QuickForm_html element being visited
334 * @access public
335 * @return void
336 */
337 function renderHtml(&$data)
338 {
339 $this->_html .= $data->toHtml();
340 } // end func renderHtml
341
342 /**
343 * Called when visiting a group, before processing any group elements
344 *
345 * @param HTML_QuickForm_group group being visited
346 * @param bool Whether a group is required
347 * @param string An error message associated with a group
348 * @access public
349 * @return void
350 */
351 function startGroup(&$group, $required, $error)
352 {
353 $name = $group->getName();
354 $this->_groupTemplate = $this->_prepareTemplate($name, $group->getLabel(), $required, $error);
355 $this->_groupElementTemplate = empty($this->_groupTemplates[$name])? '': $this->_groupTemplates[$name];
356 $this->_groupWrap = empty($this->_groupWraps[$name])? '': $this->_groupWraps[$name];
357 $this->_groupElements = array();
358 $this->_inGroup = true;
359 } // end func startGroup
360
361 /**
362 * Called when visiting a group, after processing all group elements
363 *
364 * @param HTML_QuickForm_group group being visited
365 * @access public
366 * @return void
367 */
368 function finishGroup(&$group)
369 {
370 $separator = $group->_separator;
371 if (is_array($separator)) {
372 $count = count($separator);
373 $html = '';
374 for ($i = 0; $i < count($this->_groupElements); $i++) {
375 $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i];
376 }
377 } else {
378 if (is_null($separator)) {
379 $separator = '&nbsp;';
380 }
381 $html = implode((string)$separator, $this->_groupElements);
382 }
383 if (!empty($this->_groupWrap)) {
384 $html = str_replace('{content}', $html, $this->_groupWrap);
385 }
386 $this->_html .= str_replace('{element}', $html, $this->_groupTemplate);
387 $this->_inGroup = false;
388 } // end func finishGroup
389
390 /**
391 * Sets element template
392 *
393 * @param string The HTML surrounding an element
394 * @param string (optional) Name of the element to apply template for
395 * @access public
396 * @return void
397 */
398 function setElementTemplate($html, $element = null)
399 {
400 if (is_null($element)) {
401 $this->_elementTemplate = $html;
402 } else {
403 $this->_templates[$element] = $html;
404 }
405 } // end func setElementTemplate
406
407
408 /**
409 * Sets template for a group wrapper
410 *
411 * This template is contained within a group-as-element template
412 * set via setTemplate() and contains group's element templates, set
413 * via setGroupElementTemplate()
414 *
415 * @param string The HTML surrounding group elements
416 * @param string Name of the group to apply template for
417 * @access public
418 * @return void
419 */
420 function setGroupTemplate($html, $group)
421 {
422 $this->_groupWraps[$group] = $html;
423 } // end func setGroupTemplate
424
425 /**
426 * Sets element template for elements within a group
427 *
428 * @param string The HTML surrounding an element
429 * @param string Name of the group to apply template for
430 * @access public
431 * @return void
432 */
433 function setGroupElementTemplate($html, $group)
434 {
435 $this->_groupTemplates[$group] = $html;
436 } // end func setGroupElementTemplate
437
438 /**
439 * Sets header template
440 *
441 * @param string The HTML surrounding the header
442 * @access public
443 * @return void
444 */
445 function setHeaderTemplate($html)
446 {
447 $this->_headerTemplate = $html;
448 } // end func setHeaderTemplate
449
450 /**
451 * Sets form template
452 *
453 * @param string The HTML surrounding the form tags
454 * @access public
455 * @return void
456 */
457 function setFormTemplate($html)
458 {
459 $this->_formTemplate = $html;
460 } // end func setFormTemplate
461
462 /**
463 * Sets the note indicating required fields template
464 *
465 * @param string The HTML surrounding the required note
466 * @access public
467 * @return void
468 */
469 function setRequiredNoteTemplate($html)
470 {
471 $this->_requiredNoteTemplate = $html;
472 } // end func setRequiredNoteTemplate
473
474 /**
475 * Clears all the HTML out of the templates that surround notes, elements, etc.
476 * Useful when you want to use addData() to create a completely custom form look
477 *
478 * @access public
479 * @return void
480 */
481 function clearAllTemplates()
482 {
483 $this->setElementTemplate('{element}');
484 $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
485 $this->setRequiredNoteTemplate('');
486 $this->_templates = array();
487 } // end func clearAllTemplates
488 } // end class HTML_QuickForm_Renderer_Default
489 ?>