Source for file ITStatic.php
Documentation is available at ITStatic.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Bertrand Mansion <bmansion@mamasam.com> |
// +----------------------------------------------------------------------+
// $Id: ITStatic.php,v 1.7 2004/06/28 14:20:22 avb Exp $
require_once('HTML/QuickForm/Renderer.php');
* A static renderer for HTML_QuickForm compatible
* with HTML_Template_IT and HTML_Template_Sigma.
* As opposed to the dynamic renderer, this renderer needs
* every elements and labels in the form to be specified by
* placeholders at the position you want them to be displayed.
* @author Bertrand Mansion <bmansion@mamasam.com>
* An HTML_Template_IT or some other API compatible Template instance
* The errors that were not shown near concrete fields go here
* Show the block with required note?
var $_showRequired = false;
* Which group are we currently parsing ?
* Index of the element in its group
* If elements have been added with the same name
var $_duplicateElements = array ();
* How to handle the required tag for required fields
var $_required = '{label}<font size="1" color="red">*</font>';
* How to handle error messages in form validation
var $_error = '<font color="red">{error}</font><br />{html}';
* Collected HTML for hidden elements, if needed
* @param object An HTML_Template_IT or other compatible Template object to use
$this->HTML_QuickForm_Renderer ();
* Called when visiting a form, before processing any form elements
* @param object An HTML_QuickForm object being visited
$this->_formName = $form->getAttribute ('id');
if (count($form->_duplicateIndex ) > 0 ) {
// Take care of duplicate elements
foreach ($form->_duplicateIndex as $elementName => $indexes) {
$this->_duplicateElements[$elementName] = 0;
* Called when visiting a form, after processing all form elements
* @param object An HTML_QuickForm object being visited
// display errors above form
if (!empty ($this->_errors) && $this->_tpl->blockExists ($this->_formName. '_error_loop')) {
foreach ($this->_errors as $error) {
$this->_tpl->setVariable ($this->_formName. '_error', $error);
$this->_tpl->parse ($this->_formName. '_error_loop');
if ($this->_showRequired) {
$this->_tpl->setVariable ($this->_formName. '_required_note', $form->getRequiredNote ());
// add hidden elements, if collected
if (!empty ($this->_hidden)) {
$this->_tpl->setVariable ($this->_formName . '_hidden', $this->_hidden);
// assign form attributes
$this->_tpl->setVariable ($this->_formName. '_attributes', $form->getAttributes (true ));
// assign javascript validation rules
$this->_tpl->setVariable ($this->_formName. '_javascript', $form->getValidationScript ());
* Called when visiting a header element
* @param object An HTML_QuickForm_header element being visited
$name = $header->getName ();
$varName = $this->_formName. '_header';
if (!empty ($name) && $this->_tpl->placeHolderExists ($this->_formName. '_header_'. $name)) {
$varName = $this->_formName. '_header_'. $name;
$this->_tpl->setVariable ($varName, $header->toHtml ());
} // end func renderHeader
* Called when visiting an element
* @param object An HTML_QuickForm_element object being visited
* @param bool Whether an element is required
* @param string An error message associated with an element
$name = $element->getName ();
// are we inside a group?
if (!empty ($this->_inGroup)) {
$varName = $this->_formName. '_'. str_replace(array ('[', ']'), '_', $name);
if (substr($varName, -2 ) == '__') {
// element name is of type : group[]
$varName = $this->_inGroup. '_'. $this->_elementIndex. '_';
if ($varName != $this->_inGroup) {
$varName .= '_' == substr($varName, -1 )? '': '_';
// element name is of type : group[name]
$label = $element->getLabel ();
$html = $element->toHtml ();
if ($required && !$element->isFrozen ()) {
$this->_renderRequired ($label, $html);
$this->_showRequired = true;
foreach ($label as $key => $value) {
$this->_tpl->setVariable ($varName. 'label_'. $key, $value);
$this->_tpl->setVariable ($varName. 'label', $label);
$this->_tpl->setVariable ($varName. 'html', $html);
$name = str_replace(array ('[', ']'), array ('_', ''), $name);
if (isset ($this->_duplicateElements[$name])) {
// Element is a duplicate
$varName = $this->_formName. '_'. $name. '_'. $this->_duplicateElements[$name];
$this->_duplicateElements[$name]++;
$varName = $this->_formName. '_'. $name;
$label = $element->getLabel ();
$html = $element->toHtml ();
$this->_showRequired = true;
$this->_renderRequired ($label, $html);
$this->_renderError ($label, $html, $error);
foreach ($label as $key => $value) {
$this->_tpl->setVariable ($varName. '_label_'. $key, $value);
$this->_tpl->setVariable ($varName. '_label', $label);
$this->_tpl->setVariable ($varName. '_html', $html);
} // end func renderElement
* Called when visiting a hidden element
* @param object An HTML_QuickForm_hidden object being visited
if ($this->_tpl->placeholderExists ($this->_formName . '_hidden')) {
$this->_hidden .= $element->toHtml ();
$name = $element->getName ();
$name = str_replace(array ('[', ']'), array ('_', ''), $name);
$this->_tpl->setVariable ($this->_formName. '_'. $name. '_html', $element->toHtml ());
} // end func renderHidden
* Called when visiting a group, before processing any group elements
* @param object An HTML_QuickForm_group object being visited
* @param bool Whether a group is required
* @param string An error message associated with a group
$name = $group->getName ();
$varName = $this->_formName. '_'. $name;
$this->_elementIndex = 0;
$html = $this->_tpl->placeholderExists ($varName. '_html') ? $group->toHtml () : '';
$label = $group->getLabel ();
$this->_renderRequired ($label, $html);
$this->_renderError ($label, $html, $error);
$this->_tpl->setVariable ($varName. '_html', $html);
// Uses error blocks to set the special groups layout error
// <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error -->
if ($this->_tpl->placeholderExists ($varName. '_error')) {
if ($this->_tpl->blockExists ($this->_formName . '_error_block')) {
$this->_tpl->setVariable ($this->_formName . '_error', $error);
$error = $this->_getTplBlock ($this->_formName . '_error_block');
} elseif (strpos($this->_error, '{html}') !== false || strpos($this->_error, '{label}') !== false ) {
$error = str_replace('{error}', $error, $this->_error);
$this->_tpl->setVariable ($varName . '_error', $error);
foreach ($label as $key => $value) {
$this->_tpl->setVariable ($varName. '_label_'. $key, $value);
$this->_tpl->setVariable ($varName. '_label', $label);
$this->_inGroup = $varName;
* Called when visiting a group, after processing all group elements
* @param object An HTML_QuickForm_group object being visited
} // end func finishGroup
* Sets the way required elements are rendered
* You can use {label} or {html} placeholders to let the renderer know where
* where the element label or the element html are positionned according to the
* required tag. They will be replaced accordingly with the right value.
* <font color="red">*</font>{label}
* will put a red star in front of the label if the element is required.
* @param string The required element template
$this->_required = $template;
} // end func setRequiredTemplate
* Sets the way elements with validation errors are rendered
* You can use {label} or {html} placeholders to let the renderer know where
* where the element label or the element html are positionned according to the
* error message. They will be replaced accordingly with the right value.
* The error message will replace the {error} place holder.
* <font color="red">{error}</font><br />{html}
* will put the error message in red on top of the element html.
* If you want all error messages to be output in the main error block, do not specify
* Groups can have special layouts. With this kind of groups, the renderer will need
* to know where to place the error message. In this case, use error blocks like:
* <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error -->
* where you want the error message to appear in the form.
* @param string The element error template
$this->_error = $template;
} // end func setErrorTemplate
* Called when an element is required
* This method will add the required tag to the element label and/or the element html
* such as defined with the method setRequiredTemplate
* @param string The element label
* @param string The element html rendering
* @see setRequiredTemplate()
function _renderRequired (&$label, &$html)
if ($this->_tpl->blockExists ($tplBlock = $this->_formName . '_required_block')) {
if (!empty ($label) && $this->_tpl->placeholderExists ($this->_formName . '_label', $tplBlock)) {
$this->_tpl->setVariable ($this->_formName . '_label', is_array($label)? $label[0 ]: $label);
$label[0 ] = $this->_getTplBlock ($tplBlock);
$label = $this->_getTplBlock ($tplBlock);
if (!empty ($html) && $this->_tpl->placeholderExists ($this->_formName . '_html', $tplBlock)) {
$this->_tpl->setVariable ($this->_formName . '_html', $html);
$html = $this->_getTplBlock ($tplBlock);
if (!empty ($label) && strpos($this->_required, '{label}') !== false ) {
$label[0 ] = str_replace('{label}', $label[0 ], $this->_required);
$label = str_replace('{label}', $label, $this->_required);
if (!empty ($html) && strpos($this->_required, '{html}') !== false ) {
$html = str_replace('{html}', $html, $this->_required);
} // end func _renderRequired
* Called when an element has a validation error
* This method will add the error message to the element label or the element html
* such as defined with the method setErrorTemplate. If the error placeholder is not found
* in the template, the error will be displayed in the form error block.
* @param string The element label
* @param string The element html rendering
* @param string The element error
* @see setErrorTemplate()
function _renderError (&$label, &$html, $error)
if ($this->_tpl->blockExists ($tplBlock = $this->_formName . '_error_block')) {
$this->_tpl->setVariable ($this->_formName . '_error', $error);
if (!empty ($label) && $this->_tpl->placeholderExists ($this->_formName . '_label', $tplBlock)) {
$this->_tpl->setVariable ($this->_formName . '_label', is_array($label)? $label[0 ]: $label);
$label[0 ] = $this->_getTplBlock ($tplBlock);
$label = $this->_getTplBlock ($tplBlock);
} elseif (!empty ($html) && $this->_tpl->placeholderExists ($this->_formName . '_html', $tplBlock)) {
$this->_tpl->setVariable ($this->_formName . '_html', $html);
$html = $this->_getTplBlock ($tplBlock);
// clean up after ourselves
$this->_tpl->setVariable ($this->_formName . '_error', null );
} elseif (!empty ($label) && strpos($this->_error, '{label}') !== false ) {
$label[0 ] = str_replace(array ('{label}', '{error}'), array ($label[0 ], $error), $this->_error);
$label = str_replace(array ('{label}', '{error}'), array ($label, $error), $this->_error);
} elseif (!empty ($html) && strpos($this->_error, '{html}') !== false ) {
$html = str_replace(array ('{html}', '{error}'), array ($html, $error), $this->_error);
$this->_errors[] = $error;
}// end func _renderError
* Returns the block's contents
* The method is needed because ITX and Sigma implement clearing
* the block contents on get() a bit differently
* @param string Block name
* @return string Block contents
function _getTplBlock ($block)
$this->_tpl->parse ($block);
if (is_a($this->_tpl, 'html_template_sigma')) {
$ret = $this->_tpl->get ($block, true );
$oldClear = $this->_tpl->clearCache;
$this->_tpl->clearCache = true;
$ret = $this->_tpl->get ($block);
$this->_tpl->clearCache = $oldClear;
} // end class HTML_QuickForm_Renderer_ITStatic
Documentation generated on Mon, 11 Mar 2019 14:16:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|