/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/Object.php |
---|
New file |
0,0 → 1,432 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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: Ron McClain <ron@humaniq.com> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: Object.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once('HTML/QuickForm/Renderer.php'); |
/** |
* A concrete renderer for HTML_QuickForm, makes an object from form contents |
* |
* Based on HTML_Quickform_Renderer_Array code |
* |
* @access public |
*/ |
class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer |
{ |
/** |
* The object being generated |
* @var object $_obj |
*/ |
var $_obj= null; |
/** |
* Number of sections in the form (i.e. number of headers in it) |
* @var integer $_sectionCount |
*/ |
var $_sectionCount; |
/** |
* Current section number |
* @var integer $_currentSection |
*/ |
var $_currentSection; |
/** |
* Object representing current group |
* @var object $_currentGroup |
*/ |
var $_currentGroup = null; |
/** |
* Class of Element Objects |
* @var object $_elementType |
*/ |
var $_elementType = 'QuickFormElement'; |
/** |
* Additional style information for different elements |
* @var array $_elementStyles |
*/ |
var $_elementStyles = array(); |
/** |
* true: collect all hidden elements into string; false: process them as usual form elements |
* @var bool $_collectHidden |
*/ |
var $_collectHidden = false; |
/** |
* Constructor |
* |
* @param collecthidden bool true: collect all hidden elements |
* @access public |
*/ |
function HTML_QuickForm_Renderer_Object($collecthidden = false) |
{ |
$this->HTML_QuickForm_Renderer(); |
$this->_collectHidden = $collecthidden; |
$this->_obj = new QuickformForm; |
} |
/** |
* Return the rendered Object |
* @access public |
*/ |
function toObject() |
{ |
return $this->_obj; |
} |
/** |
* Set the class of the form elements. Defaults to QuickformElement. |
* @param type string Name of element class |
* @access public |
*/ |
function setElementType($type) |
{ |
$this->_elementType = $type; |
} |
function startForm(&$form) |
{ |
$this->_obj->frozen = $form->isFrozen(); |
$this->_obj->javascript = $form->getValidationScript(); |
$this->_obj->attributes = $form->getAttributes(true); |
$this->_obj->requirednote = $form->getRequiredNote(); |
$this->_obj->errors = new StdClass; |
if($this->_collectHidden) { |
$this->_obj->hidden = ''; |
} |
$this->_elementIdx = 1; |
$this->_currentSection = null; |
$this->_sectionCount = 0; |
} // end func startForm |
function renderHeader(&$header) |
{ |
$hobj = new StdClass; |
$hobj->header = $header->toHtml(); |
$this->_obj->sections[$this->_sectionCount] = $hobj; |
$this->_currentSection = $this->_sectionCount++; |
} |
function renderElement(&$element, $required, $error) |
{ |
$elObj = $this->_elementToObject($element, $required, $error); |
if(!empty($error)) { |
$name = $elObj->name; |
$this->_obj->errors->$name = $error; |
} |
$this->_storeObject($elObj); |
} // end func renderElement |
function renderHidden(&$element) |
{ |
if($this->_collectHidden) { |
$this->_obj->hidden .= $element->toHtml() . "\n"; |
} else { |
$this->renderElement($element, false, null); |
} |
} //end func renderHidden |
function startGroup(&$group, $required, $error) |
{ |
$this->_currentGroup = $this->_elementToObject($group, $required, $error); |
if(!empty($error)) { |
$name = $this->_currentGroup->name; |
$this->_obj->errors->$name = $error; |
} |
} // end func startGroup |
function finishGroup(&$group) |
{ |
$this->_storeObject($this->_currentGroup); |
$this->_currentGroup = null; |
} // end func finishGroup |
/** |
* Creates an object representing an element |
* |
* @access private |
* @param element object An HTML_QuickForm_element object |
* @param required bool Whether an element is required |
* @param error string Error associated with the element |
* @return object |
*/ |
function _elementToObject(&$element, $required, $error) |
{ |
if($this->_elementType) { |
$ret = new $this->_elementType; |
} |
$ret->name = $element->getName(); |
$ret->value = $element->getValue(); |
$ret->type = $element->getType(); |
$ret->frozen = $element->isFrozen(); |
$labels = $element->getLabel(); |
if (is_array($labels)) { |
$ret->label = array_shift($labels); |
foreach ($labels as $key => $label) { |
$key = is_int($key)? $key + 2: $key; |
$ret->{'label_' . $key} = $label; |
} |
} else { |
$ret->label = $labels; |
} |
$ret->required = $required; |
$ret->error = $error; |
if(isset($this->_elementStyles[$ret->name])) { |
$ret->style = $this->_elementStyles[$ret->name]; |
$ret->styleTemplate = "styles/". $ret->style .".html"; |
} |
if($ret->type == 'group') { |
$ret->separator = $element->_separator; |
$ret->elements = array(); |
} else { |
$ret->html = $element->toHtml(); |
} |
return $ret; |
} |
/** |
* Stores an object representation of an element in the form array |
* |
* @access private |
* @param elObj object Object representation of an element |
* @return void |
*/ |
function _storeObject($elObj) |
{ |
$name = $elObj->name; |
if(is_object($this->_currentGroup) && $elObj->type != 'group') { |
$this->_currentGroup->elements[] = $elObj; |
} elseif (isset($this->_currentSection)) { |
$this->_obj->sections[$this->_currentSection]->elements[] = $elObj; |
} else { |
$this->_obj->elements[] = $elObj; |
} |
} |
function setElementStyle($elementName, $styleName = null) |
{ |
if(is_array($elementName)) { |
$this->_elementStyles = array_merge($this->_elementStyles, $elementName); |
} else { |
$this->_elementStyles[$elementName] = $styleName; |
} |
} |
} // end class HTML_QuickForm_Renderer_Object |
/** |
* Convenience class for the form object passed to outputObject() |
* |
* Eg. |
* {form.outputJavaScript():h} |
* {form.outputHeader():h} |
* <table> |
* <tr> |
* <td>{form.name.label:h}</td><td>{form.name.html:h}</td> |
* </tr> |
* </table> |
* </form> |
*/ |
class QuickformForm |
{ |
/** |
* Whether the form has been frozen |
* @var boolean $frozen |
*/ |
var $frozen; |
/** |
* Javascript for client-side validation |
* @var string $javascript |
*/ |
var $javascript; |
/** |
* Attributes for form tag |
* @var string $attributes |
*/ |
var $attributes; |
/** |
* Note about required elements |
* @var string $requirednote |
*/ |
var $requirednote; |
/** |
* Collected html of all hidden variables |
* @var string $hidden |
*/ |
var $hidden; |
/** |
* Set if there were validation errors. |
* StdClass object with element names for keys and their |
* error messages as values |
* @var object $errors |
*/ |
var $errors; |
/** |
* Array of QuickformElementObject elements. If there are headers in the form |
* this will be empty and the elements will be in the |
* separate sections |
* @var array $elements |
*/ |
var $elements; |
/** |
* Array of sections contained in the document |
* @var array $sections |
*/ |
var $sections; |
/** |
* Output <form> header |
* {form.outputHeader():h} |
* @return string <form attributes> |
*/ |
function outputHeader() |
{ |
return "<form " . $this->attributes . ">\n"; |
} |
/** |
* Output form javascript |
* {form.outputJavaScript():h} |
* @return string Javascript |
*/ |
function outputJavaScript() |
{ |
return $this->javascript; |
} |
} // end class QuickformForm |
/** |
* Convenience class describing a form element. |
* The properties defined here will be available from |
* your flexy templates by referencing |
* {form.zip.label:h}, {form.zip.html:h}, etc. |
*/ |
class QuickformElement |
{ |
/** |
* Element name |
* @var string $name |
*/ |
var $name; |
/** |
* Element value |
* @var mixed $value |
*/ |
var $value; |
/** |
* Type of element |
* @var string $type |
*/ |
var $type; |
/** |
* Whether the element is frozen |
* @var boolean $frozen |
*/ |
var $frozen; |
/** |
* Label for the element |
* @var string $label |
*/ |
var $label; |
/** |
* Whether element is required |
* @var boolean $required |
*/ |
var $required; |
/** |
* Error associated with the element |
* @var string $error |
*/ |
var $error; |
/** |
* Some information about element style |
* @var string $style |
*/ |
var $style; |
/** |
* HTML for the element |
* @var string $html |
*/ |
var $html; |
/** |
* If element is a group, the group separator |
* @var mixed $separator |
*/ |
var $separator; |
/** |
* If element is a group, an array of subelements |
* @var array $elements |
*/ |
var $elements; |
function isType($type) |
{ |
return ($this->type == $type); |
} |
function notFrozen() |
{ |
return !$this->frozen; |
} |
function isButton() |
{ |
return ($this->type == "submit" || $this->type == "reset"); |
} |
/** |
* XXX: why does it use Flexy when all other stuff here does not depend on it? |
*/ |
function outputStyle() |
{ |
ob_start(); |
HTML_Template_Flexy::staticQuickTemplate('styles/' . $this->style . '.html', $this); |
$ret = ob_get_contents(); |
ob_end_clean(); |
return $ret; |
} |
} // end class QuickformElement |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/Array.php |
---|
New file |
0,0 → 1,319 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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. | |
// +----------------------------------------------------------------------+ |
// | Authors: Alexey Borzov <borz_off@cs.msu.su> | |
// | Adam Daniel <adaniel1@eesus.jnj.com> | |
// | Bertrand Mansion <bmansion@mamasam.com> | |
// | Thomas Schulz <ths@4bconsult.de> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: Array.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once 'HTML/QuickForm/Renderer.php'; |
/** |
* A concrete renderer for HTML_QuickForm, makes an array of form contents |
* |
* Based on old toArray() code. |
* |
* The form array structure is the following: |
* array( |
* 'frozen' => 'whether the form is frozen', |
* 'javascript' => 'javascript for client-side validation', |
* 'attributes' => 'attributes for <form> tag', |
* 'requirednote => 'note about the required elements', |
* // if we set the option to collect hidden elements |
* 'hidden' => 'collected html of all hidden elements', |
* // if there were some validation errors: |
* 'errors' => array( |
* '1st element name' => 'Error for the 1st element', |
* ... |
* 'nth element name' => 'Error for the nth element' |
* ), |
* // if there are no headers in the form: |
* 'elements' => array( |
* element_1, |
* ... |
* element_N |
* ) |
* // if there are headers in the form: |
* 'sections' => array( |
* array( |
* 'header' => 'Header text for the first header', |
* 'name' => 'Header name for the first header', |
* 'elements' => array( |
* element_1, |
* ... |
* element_K1 |
* ) |
* ), |
* ... |
* array( |
* 'header' => 'Header text for the Mth header', |
* 'name' => 'Header name for the Mth header', |
* 'elements' => array( |
* element_1, |
* ... |
* element_KM |
* ) |
* ) |
* ) |
* ); |
* |
* where element_i is an array of the form: |
* array( |
* 'name' => 'element name', |
* 'value' => 'element value', |
* 'type' => 'type of the element', |
* 'frozen' => 'whether element is frozen', |
* 'label' => 'label for the element', |
* 'required' => 'whether element is required', |
* 'error' => 'error associated with the element', |
* 'style' => 'some information about element style (e.g. for Smarty)', |
* // if element is not a group |
* 'html' => 'HTML for the element' |
* // if element is a group |
* 'separator' => 'separator for group elements', |
* 'elements' => array( |
* element_1, |
* ... |
* element_N |
* ) |
* ); |
* |
* @access public |
*/ |
class HTML_QuickForm_Renderer_Array extends HTML_QuickForm_Renderer |
{ |
/** |
* An array being generated |
* @var array |
*/ |
var $_ary; |
/** |
* Number of sections in the form (i.e. number of headers in it) |
* @var integer |
*/ |
var $_sectionCount; |
/** |
* Current section number |
* @var integer |
*/ |
var $_currentSection; |
/** |
* Array representing current group |
* @var array |
*/ |
var $_currentGroup = null; |
/** |
* Additional style information for different elements |
* @var array |
*/ |
var $_elementStyles = array(); |
/** |
* true: collect all hidden elements into string; false: process them as usual form elements |
* @var bool |
*/ |
var $_collectHidden = false; |
/** |
* true: render an array of labels to many labels, $key 0 named 'label', the rest "label_$key" |
* false: leave labels as defined |
* @var bool |
*/ |
var $staticLabels = false; |
/** |
* Constructor |
* |
* @param bool true: collect all hidden elements into string; false: process them as usual form elements |
* @param bool true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key" |
* @access public |
*/ |
function HTML_QuickForm_Renderer_Array($collectHidden = false, $staticLabels = false) |
{ |
$this->HTML_QuickForm_Renderer(); |
$this->_collectHidden = $collectHidden; |
$this->_staticLabels = $staticLabels; |
} // end constructor |
/** |
* Returns the resultant array |
* |
* @access public |
* @return array |
*/ |
function toArray() |
{ |
return $this->_ary; |
} |
function startForm(&$form) |
{ |
$this->_ary = array( |
'frozen' => $form->isFrozen(), |
'javascript' => $form->getValidationScript(), |
'attributes' => $form->getAttributes(true), |
'requirednote' => $form->getRequiredNote(), |
'errors' => array() |
); |
if ($this->_collectHidden) { |
$this->_ary['hidden'] = ''; |
} |
$this->_elementIdx = 1; |
$this->_currentSection = null; |
$this->_sectionCount = 0; |
} // end func startForm |
function renderHeader(&$header) |
{ |
$this->_ary['sections'][$this->_sectionCount] = array( |
'header' => $header->toHtml(), |
'name' => $header->getName() |
); |
$this->_currentSection = $this->_sectionCount++; |
} // end func renderHeader |
function renderElement(&$element, $required, $error) |
{ |
$elAry = $this->_elementToArray($element, $required, $error); |
if (!empty($error)) { |
$this->_ary['errors'][$elAry['name']] = $error; |
} |
$this->_storeArray($elAry); |
} // end func renderElement |
function renderHidden(&$element) |
{ |
if ($this->_collectHidden) { |
$this->_ary['hidden'] .= $element->toHtml() . "\n"; |
} else { |
$this->renderElement($element, false, null); |
} |
} // end func renderHidden |
function startGroup(&$group, $required, $error) |
{ |
$this->_currentGroup = $this->_elementToArray($group, $required, $error); |
if (!empty($error)) { |
$this->_ary['errors'][$this->_currentGroup['name']] = $error; |
} |
} // end func startGroup |
function finishGroup(&$group) |
{ |
$this->_storeArray($this->_currentGroup); |
$this->_currentGroup = null; |
} // end func finishGroup |
/** |
* Creates an array representing an element |
* |
* @access private |
* @param object An HTML_QuickForm_element object |
* @param bool Whether an element is required |
* @param string Error associated with the element |
* @return array |
*/ |
function _elementToArray(&$element, $required, $error) |
{ |
$ret = array( |
'name' => $element->getName(), |
'value' => $element->getValue(), |
'type' => $element->getType(), |
'frozen' => $element->isFrozen(), |
'required' => $required, |
'error' => $error |
); |
// render label(s) |
$labels = $element->getLabel(); |
if (is_array($labels) && $this->_staticLabels) { |
foreach($labels as $key => $label) { |
$key = is_int($key)? $key + 1: $key; |
if (1 === $key) { |
$ret['label'] = $label; |
} else { |
$ret['label_' . $key] = $label; |
} |
} |
} else { |
$ret['label'] = $labels; |
} |
// set the style for the element |
if (isset($this->_elementStyles[$ret['name']])) { |
$ret['style'] = $this->_elementStyles[$ret['name']]; |
} |
if ('group' == $ret['type']) { |
$ret['separator'] = $element->_separator; |
$ret['elements'] = array(); |
} else { |
$ret['html'] = $element->toHtml(); |
} |
return $ret; |
} |
/** |
* Stores an array representation of an element in the form array |
* |
* @access private |
* @param array Array representation of an element |
* @return void |
*/ |
function _storeArray($elAry) |
{ |
// where should we put this element... |
if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) { |
$this->_currentGroup['elements'][] = $elAry; |
} elseif (isset($this->_currentSection)) { |
$this->_ary['sections'][$this->_currentSection]['elements'][] = $elAry; |
} else { |
$this->_ary['elements'][] = $elAry; |
} |
} |
/** |
* Sets a style to use for element rendering |
* |
* @param mixed element name or array ('element name' => 'style name') |
* @param string style name if $elementName is not an array |
* @access public |
* @return void |
*/ |
function setElementStyle($elementName, $styleName = null) |
{ |
if (is_array($elementName)) { |
$this->_elementStyles = array_merge($this->_elementStyles, $elementName); |
} else { |
$this->_elementStyles[$elementName] = $styleName; |
} |
} |
} |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/ArraySmarty.php |
---|
New file |
0,0 → 1,376 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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. | |
// +----------------------------------------------------------------------+ |
// | Authors: Alexey Borzov <borz_off@cs.msu.su> | |
// | Bertrand Mansion <bmansion@mamasam.com> | |
// | Thomas Schulz <ths@4bconsult.de> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: ArraySmarty.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once 'HTML/QuickForm/Renderer/Array.php'; |
/** |
* A static renderer for HTML_QuickForm, makes an array of form content |
* useful for an Smarty template |
* |
* Based on old toArray() code and ITStatic renderer. |
* |
* The form array structure is the following: |
* Array ( |
* [frozen] => whether the complete form is frozen' |
* [javascript] => javascript for client-side validation |
* [attributes] => attributes for <form> tag |
* [hidden] => html of all hidden elements |
* [requirednote] => note about the required elements |
* [errors] => Array |
* ( |
* [1st_element_name] => Error for the 1st element |
* ... |
* [nth_element_name] => Error for the nth element |
* ) |
* |
* [header] => Array |
* ( |
* [1st_header_name] => Header text for the 1st header |
* ... |
* [nth_header_name] => Header text for the nth header |
* ) |
* |
* [1st_element_name] => Array for the 1st element |
* ... |
* [nth_element_name] => Array for the nth element |
* |
* // where an element array has the form: |
* ( |
* [name] => element name |
* [value] => element value, |
* [type] => type of the element |
* [frozen] => whether element is frozen |
* [label] => label for the element |
* [required] => whether element is required |
* // if element is not a group: |
* [html] => HTML for the element |
* // if element is a group: |
* [separator] => separator for group elements |
* [1st_gitem_name] => Array for the 1st element in group |
* ... |
* [nth_gitem_name] => Array for the nth element in group |
* ) |
* ) |
* |
* @access public |
*/ |
class HTML_QuickForm_Renderer_ArraySmarty extends HTML_QuickForm_Renderer_Array |
{ |
/** |
* The Smarty template engine instance |
* @var object |
*/ |
var $_tpl = null; |
/** |
* Current element index |
* @var integer |
*/ |
var $_elementIdx = 0; |
/** |
* The current element index inside a group |
* @var integer |
*/ |
var $_groupElementIdx = 0; |
/** |
* How to handle the required tag for required fields |
* @var string |
* @see setRequiredTemplate() |
*/ |
var $_required = ''; |
/** |
* How to handle error messages in form validation |
* @var string |
* @see setErrorTemplate() |
*/ |
var $_error = ''; |
/** |
* Constructor |
* |
* @param object reference to the Smarty template engine instance |
* @param bool true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key" |
* @access public |
*/ |
function HTML_QuickForm_Renderer_ArraySmarty(&$tpl, $staticLabels = false) |
{ |
$this->HTML_QuickForm_Renderer_Array(true, $staticLabels); |
$this->_tpl =& $tpl; |
} // end constructor |
/** |
* Called when visiting a header element |
* |
* @param object An HTML_QuickForm_header element being visited |
* @access public |
* @return void |
*/ |
function renderHeader(&$header) |
{ |
if ($name = $header->getName()) { |
$this->_ary['header'][$name] = $header->toHtml(); |
} else { |
$this->_ary['header'][$this->_sectionCount] = $header->toHtml(); |
} |
$this->_currentSection = $this->_sectionCount++; |
} // end func renderHeader |
/** |
* 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 |
* @access public |
* @return void |
*/ |
function startGroup(&$group, $required, $error) |
{ |
parent::startGroup($group, $required, $error); |
$this->_groupElementIdx = 1; |
} // end func startGroup |
/** |
* Creates an array representing an element containing |
* the key for storing this |
* |
* @access private |
* @param object An HTML_QuickForm_element object |
* @param bool Whether an element is required |
* @param string Error associated with the element |
* @return array |
*/ |
function _elementToArray(&$element, $required, $error) |
{ |
$ret = parent::_elementToArray($element, $required, $error); |
if ('group' == $ret['type']) { |
$ret['html'] = $element->toHtml(); |
// we don't need the elements, see the array structure |
unset($ret['elements']); |
} |
if (($required || $error) && !empty($this->_required)){ |
$this->_renderRequired($ret['label'], $ret['html'], $required, $error); |
} |
if ($error && !empty($this->_error)) { |
$this->_renderError($ret['label'], $ret['html'], $error); |
$ret['error'] = $error; |
} |
// create keys for elements grouped by native group or name |
if (strstr($ret['name'], '[') or $this->_currentGroup) { |
preg_match('/([^]]*)\\[([^]]*)\\]/', $ret['name'], $matches); |
if (isset($matches[1])) { |
$sKeysSub = substr_replace($ret['name'], '', 0, strlen($matches[1])); |
$sKeysSub = str_replace( |
array('[' , ']', '[\'\']'), |
array('[\'', '\']', '[]' ), |
$sKeysSub |
); |
$sKeys = '[\'' . $matches[1] . '\']' . $sKeysSub; |
} else { |
$sKeys = '[\'' . $ret['name'] . '\']'; |
} |
// special handling for elements in native groups |
if ($this->_currentGroup) { |
// skip unnamed group items unless radios: no name -> no static access |
// identification: have the same key string as the parent group |
if ($this->_currentGroup['keys'] == $sKeys and 'radio' != $ret['type']) { |
return false; |
} |
// reduce string of keys by remove leading group keys |
if (0 === strpos($sKeys, $this->_currentGroup['keys'])) { |
$sKeys = substr_replace($sKeys, '', 0, strlen($this->_currentGroup['keys'])); |
} |
} |
// element without a name |
} elseif ($ret['name'] == '') { |
$sKeys = '[\'element_' . $this->_elementIdx . '\']'; |
// other elements |
} else { |
$sKeys = '[\'' . $ret['name'] . '\']'; |
} |
// for radios: add extra key from value |
if ('radio' == $ret['type'] and substr($sKeys, -2) != '[]') { |
$sKeys .= '[\'' . $ret['value'] . '\']'; |
} |
$this->_elementIdx++; |
$ret['keys'] = $sKeys; |
return $ret; |
} // end func _elementToArray |
/** |
* Stores an array representation of an element in the form array |
* |
* @access private |
* @param array Array representation of an element |
* @return void |
*/ |
function _storeArray($elAry) |
{ |
if ($elAry) { |
$sKeys = $elAry['keys']; |
unset($elAry['keys']); |
// where should we put this element... |
if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) { |
$toEval = '$this->_currentGroup' . $sKeys . ' = $elAry;'; |
} else { |
$toEval = '$this->_ary' . $sKeys . ' = $elAry;'; |
} |
eval($toEval); |
} |
return; |
} |
/** |
* 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 |
* @param boolean The element required |
* @param string The element error |
* @see setRequiredTemplate() |
* @access private |
* @return void |
*/ |
function _renderRequired(&$label, &$html, &$required, &$error) |
{ |
$this->_tpl->assign(array( |
'label' => $label, |
'html' => $html, |
'required' => $required, |
'error' => $error |
)); |
if (!empty($label) && strpos($this->_required, $this->_tpl->left_delimiter . '$label') !== false) { |
$label = $this->_tplFetch($this->_required); |
} |
if (!empty($html) && strpos($this->_required, $this->_tpl->left_delimiter . '$html') !== false) { |
$html = $this->_tplFetch($this->_required); |
} |
$this->_tpl->clear_assign(array('label', 'html', '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() |
* @access private |
* @return void |
*/ |
function _renderError(&$label, &$html, &$error) |
{ |
$this->_tpl->assign(array('label' => '', 'html' => '', 'error' => $error)); |
$error = $this->_tplFetch($this->_error); |
$this->_tpl->assign(array('label' => $label, 'html' => $html)); |
if (!empty($label) && strpos($this->_error, $this->_tpl->left_delimiter . '$label') !== false) { |
$label = $this->_tplFetch($this->_error); |
} elseif (!empty($html) && strpos($this->_error, $this->_tpl->left_delimiter . '$html') !== false) { |
$html = $this->_tplFetch($this->_error); |
} |
$this->_tpl->clear_assign(array('label', 'html', 'error')); |
} // end func _renderError |
/** |
* Process an template sourced in a string with Smarty |
* |
* Smarty has no core function to render a template given as a string. |
* So we use the smarty eval plugin function to do this. |
* |
* @param string The template source |
* @access private |
* @return void |
*/ |
function _tplFetch($tplSource) |
{ |
if (!function_exists('smarty_function_eval')) { |
require SMARTY_DIR . '/plugins/function.eval.php'; |
} |
return smarty_function_eval(array('var' => $tplSource), $this->_tpl); |
}// end func _tplFetch |
/** |
* 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. You |
* can use the full smarty syntax here, especially a custom modifier for I18N. |
* For example: |
* {if $required}<span style="color: red;">*</span>{/if}{$label|translate} |
* will put a red star in front of the label if the element is required and |
* translate the label. |
* |
* |
* @param string The required element template |
* @access public |
* @return void |
*/ |
function setRequiredTemplate($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} placeholder. |
* For example: |
* {if $error}<span style="color: red;">{$error}</span>{/if}<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, use |
* the {$form.errors} part of the rendered array that collects all raw error |
* messages. |
* |
* If you want to place all error messages manually, do not specify {$html} |
* nor {$label}. |
* |
* Groups can have special layouts. With this kind of groups, you have to |
* place the formated error message manually. In this case, use {$form.group.error} |
* where you want the formated error message to appear in the form. |
* |
* @param string The element error template |
* @access public |
* @return void |
*/ |
function setErrorTemplate($template) |
{ |
$this->_error = $template; |
} // end func setErrorTemplate |
} |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/ObjectFlexy.php |
---|
New file |
0,0 → 1,261 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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: Ron McClain <ron@humaniq.com> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: ObjectFlexy.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once("HTML/QuickForm/Renderer/Object.php"); |
/** |
* QuickForm renderer for Flexy template engine, static version. |
* |
* A static renderer for HTML_Quickform. Makes a QuickFormFlexyObject |
* from the form content suitable for use with a Flexy template |
* |
* Usage: |
* $form =& new HTML_QuickForm('form', 'POST'); |
* $template =& new HTML_Template_Flexy(); |
* $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template); |
* $renderer->setHtmlTemplate("html.html"); |
* $renderer->setLabelTemplate("label.html"); |
* $form->accept($renderer); |
* $view = new StdClass; |
* $view->form = $renderer->toObject(); |
* $template->compile("mytemplate.html"); |
* |
* Based on the code for HTML_QuickForm_Renderer_ArraySmarty |
* |
* @see QuickFormFlexyObject |
* @access public |
*/ |
class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object |
{ |
/** |
* HTML_Template_Flexy instance |
* @var object $_flexy |
*/ |
var $_flexy; |
/** |
* Current element index |
* @var integer $_elementIdx |
*/ |
var $_elementIdx; |
/** |
* The current element index inside a group |
* @var integer $_groupElementIdx |
*/ |
var $_groupElementIdx = 0; |
/** |
* Name of template file for form html |
* @var string $_html |
* @see setRequiredTemplate() |
*/ |
var $_html = ''; |
/** |
* Name of template file for form labels |
* @var string $label |
* @see setErrorTemplate() |
*/ |
var $label = ''; |
/** |
* Class of the element objects, so you can add your own |
* element methods |
* @var string $_elementType |
*/ |
var $_elementType = 'QuickformFlexyElement'; |
/** |
* Constructor |
* |
* @param $flexy object HTML_Template_Flexy instance |
* @public |
*/ |
function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy) |
{ |
$this->HTML_QuickForm_Renderer_Object(true); |
$this->_obj = new QuickformFlexyForm(); |
$this->_flexy =& $flexy; |
} // end constructor |
function renderHeader(&$header) |
{ |
if($name = $header->getName()) { |
$this->_obj->header->$name = $header->toHtml(); |
} else { |
$this->_obj->header[$this->_sectionCount] = $header->toHtml(); |
} |
$this->_currentSection = $this->_sectionCount++; |
} // end func renderHeader |
function startGroup(&$group, $required, $error) |
{ |
parent::startGroup($group, $required, $error); |
$this->_groupElementIdx = 1; |
} //end func startGroup |
/** |
* Creates an object representing an element containing |
* the key for storing this |
* |
* @access private |
* @param element object An HTML_QuickForm_element object |
* @param required bool Whether an element is required |
* @param error string Error associated with the element |
* @return object |
*/ |
function _elementToObject(&$element, $required, $error) |
{ |
$ret = parent::_elementToObject($element, $required, $error); |
if($ret->type == 'group') { |
$ret->html = $element->toHtml(); |
unset($ret->elements); |
} |
if(!empty($this->_label)) { |
$this->_renderLabel($ret); |
} |
if(!empty($this->_html)) { |
$this->_renderHtml($ret); |
$ret->error = $error; |
} |
// Create an element key from the name |
if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) { |
if (!$pos) { |
$keys = '->{\'' . $ret->name . '\'}'; |
} else { |
$keys = '->{\'' . str_replace(array('[', ']'), array('\'}->{\'', ''), $ret->name) . '\'}'; |
} |
// special handling for elements in native groups |
if (is_object($this->_currentGroup)) { |
// skip unnamed group items unless radios: no name -> no static access |
// identification: have the same key string as the parent group |
if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) { |
return false; |
} |
// reduce string of keys by remove leading group keys |
if (0 === strpos($keys, $this->_currentGroup->keys)) { |
$keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys)); |
} |
} |
} elseif (0 == strlen($ret->name)) { |
$keys = '->{\'element_' . $this->_elementIdx . '\'}'; |
} else { |
$keys = '->{\'' . $ret->name . '\'}'; |
} |
// for radios: add extra key from value |
if ('radio' == $ret->type && '[]' != substr($keys, -2)) { |
$keys .= '->{\'' . $ret->value . '\'}'; |
} |
$ret->keys = $keys; |
$this->_elementIdx++; |
return $ret; |
} |
/** |
* Stores an object representation of an element in the |
* QuickformFormObject instance |
* |
* @access private |
* @param elObj object Object representation of an element |
* @return void |
*/ |
function _storeObject($elObj) |
{ |
if ($elObj) { |
$keys = $elObj->keys; |
unset($elObj->keys); |
if(is_object($this->_currentGroup) && ('group' != $elObj->type)) { |
$code = '$this->_currentGroup' . $keys . ' = $elObj;'; |
} else { |
$code = '$this->_obj' . $keys . ' = $elObj;'; |
} |
eval($code); |
} |
} |
/** |
* Set the filename of the template to render html elements. |
* In your template, {html} is replaced by the unmodified html. |
* If the element is required, {required} will be true. |
* Eg. |
* {if:error} |
* <font color="red" size="1">{error:h}</font><br /> |
* {end:} |
* {html:h} |
* |
* @access public |
* @param template string Filename of template |
* @return void |
*/ |
function setHtmlTemplate($template) |
{ |
$this->_html = $template; |
} |
/** |
* Set the filename of the template to render form labels |
* In your template, {label} is replaced by the unmodified label. |
* {error} will be set to the error, if any. {required} will |
* be true if this is a required field |
* Eg. |
* {if:required} |
* <font color="orange" size="1">*</font> |
* {end:} |
* {label:h} |
* |
* @access public |
* @param template string Filename of template |
* @return void |
*/ |
function setLabelTemplate($template) |
{ |
$this->_label = $template; |
} |
function _renderLabel(&$ret) |
{ |
$this->_flexy->compile($this->_label); |
$ret->label = $this->_flexy->bufferedOutputObject($ret); |
} |
function _renderHtml(&$ret) |
{ |
$this->_flexy->compile($this->_html); |
$ret->html = $this->_flexy->bufferedOutputObject($ret); |
} |
} // end class HTML_QuickForm_Renderer_ObjectFlexy |
/** |
* Adds nothing to QuickformForm, left for backwards compatibility |
*/ |
class QuickformFlexyForm extends QuickformForm |
{ |
} |
/** |
* Adds nothing to QuickformElement, left for backwards compatibility |
*/ |
class QuickformFlexyElement extends QuickformElement |
{ |
} |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/ITDynamic.php |
---|
New file |
0,0 → 1,287 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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: Alexey Borzov <borz_off@cs.msu.su> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: ITDynamic.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once 'HTML/QuickForm/Renderer.php'; |
/** |
* A concrete renderer for HTML_QuickForm, using Integrated Templates. |
* |
* This is a "dynamic" renderer, which means that concrete form look |
* is defined at runtime. This also means that you can define |
* <b>one</b> template file for <b>all</b> your forms. That template |
* should contain a block for every element 'look' appearing in your |
* forms and also some special blocks (consult the examples). If a |
* special block is not set for an element, the renderer falls back to |
* a default one. |
* |
* @author Alexey Borzov <borz_off@cs.msu.su> |
* @access public |
*/ |
class HTML_QuickForm_Renderer_ITDynamic extends HTML_QuickForm_Renderer |
{ |
/** |
* A template class (HTML_Template_ITX or HTML_Template_Sigma) instance |
* @var object |
*/ |
var $_tpl = null; |
/** |
* The errors that were not shown near concrete fields go here |
* @var array |
*/ |
var $_errors = array(); |
/** |
* Show the block with required note? |
* @var bool |
*/ |
var $_showRequired = false; |
/** |
* A separator for group elements |
* @var mixed |
*/ |
var $_groupSeparator = null; |
/** |
* The current element index inside a group |
* @var integer |
*/ |
var $_groupElementIdx = 0; |
/** |
* Blocks to use for different elements |
* @var array |
*/ |
var $_elementBlocks = array(); |
/** |
* Block to use for headers |
* @var string |
*/ |
var $_headerBlock = null; |
/** |
* Constructor |
* |
* @param object An HTML_Template_ITX/HTML_Template_Sigma object to use |
*/ |
function HTML_QuickForm_Renderer_ITDynamic(&$tpl) |
{ |
$this->HTML_QuickForm_Renderer(); |
$this->_tpl =& $tpl; |
$this->_tpl->setCurrentBlock('qf_main_loop'); |
} |
function finishForm(&$form) |
{ |
// display errors above form |
if (!empty($this->_errors) && $this->_tpl->blockExists('qf_error_loop')) { |
foreach ($this->_errors as $error) { |
$this->_tpl->setVariable('qf_error', $error); |
$this->_tpl->parse('qf_error_loop'); |
} |
} |
// show required note |
if ($this->_showRequired) { |
$this->_tpl->setVariable('qf_required_note', $form->getRequiredNote()); |
} |
// assign form attributes |
$this->_tpl->setVariable('qf_attributes', $form->getAttributes(true)); |
// assign javascript validation rules |
$this->_tpl->setVariable('qf_javascript', $form->getValidationScript()); |
} |
function renderHeader(&$header) |
{ |
$blockName = $this->_matchBlock($header); |
if ('qf_header' == $blockName && isset($this->_headerBlock)) { |
$blockName = $this->_headerBlock; |
} |
$this->_tpl->setVariable('qf_header', $header->toHtml()); |
$this->_tpl->parse($blockName); |
$this->_tpl->parse('qf_main_loop'); |
} |
function renderElement(&$element, $required, $error) |
{ |
$blockName = $this->_matchBlock($element); |
// are we inside a group? |
if ('qf_main_loop' != $this->_tpl->currentBlock) { |
if (0 != $this->_groupElementIdx && $this->_tpl->placeholderExists('qf_separator', $blockName)) { |
if (is_array($this->_groupSeparator)) { |
$this->_tpl->setVariable('qf_separator', $this->_groupSeparator[($this->_groupElementIdx - 1) % count($this->_groupSeparator)]); |
} else { |
$this->_tpl->setVariable('qf_separator', (string)$this->_groupSeparator); |
} |
} |
$this->_groupElementIdx++; |
} elseif(!empty($error)) { |
// show the error message or keep it for later use |
if ($this->_tpl->blockExists($blockName . '_error')) { |
$this->_tpl->setVariable('qf_error', $error); |
} else { |
$this->_errors[] = $error; |
} |
} |
// show an '*' near the required element |
if ($required) { |
$this->_showRequired = true; |
if ($this->_tpl->blockExists($blockName . '_required')) { |
$this->_tpl->touchBlock($blockName . '_required'); |
} |
} |
// Prepare multiple labels |
$labels = $element->getLabel(); |
if (is_array($labels)) { |
$mainLabel = array_shift($labels); |
} else { |
$mainLabel = $labels; |
} |
// render the element itself with its main label |
$this->_tpl->setVariable('qf_element', $element->toHtml()); |
if ($this->_tpl->placeholderExists('qf_label', $blockName)) { |
$this->_tpl->setVariable('qf_label', $mainLabel); |
} |
// render extra labels, if any |
if (is_array($labels)) { |
foreach($labels as $key => $label) { |
$key = is_int($key)? $key + 2: $key; |
if ($this->_tpl->blockExists($blockName . '_label_' . $key)) { |
$this->_tpl->setVariable('qf_label_' . $key, $label); |
} |
} |
} |
$this->_tpl->parse($blockName); |
$this->_tpl->parseCurrentBlock(); |
} |
function renderHidden(&$element) |
{ |
$this->_tpl->setVariable('qf_hidden', $element->toHtml()); |
$this->_tpl->parse('qf_hidden_loop'); |
} |
function startGroup(&$group, $required, $error) |
{ |
$blockName = $this->_matchBlock($group); |
$this->_tpl->setCurrentBlock($blockName . '_loop'); |
$this->_groupElementIdx = 0; |
$this->_groupSeparator = is_null($group->_separator)? ' ': $group->_separator; |
// show an '*' near the required element |
if ($required) { |
$this->_showRequired = true; |
if ($this->_tpl->blockExists($blockName . '_required')) { |
$this->_tpl->touchBlock($blockName . '_required'); |
} |
} |
// show the error message or keep it for later use |
if (!empty($error)) { |
if ($this->_tpl->blockExists($blockName . '_error')) { |
$this->_tpl->setVariable('qf_error', $error); |
} else { |
$this->_errors[] = $error; |
} |
} |
$this->_tpl->setVariable('qf_group_label', $group->getLabel()); |
} |
function finishGroup(&$group) |
{ |
$this->_tpl->parse($this->_matchBlock($group)); |
$this->_tpl->setCurrentBlock('qf_main_loop'); |
$this->_tpl->parseCurrentBlock(); |
} |
/** |
* Returns the name of a block to use for element rendering |
* |
* If a name was not explicitly set via setElementBlock(), it tries |
* the names '{prefix}_{element type}' and '{prefix}_{element}', where |
* prefix is either 'qf' or the name of the current group's block |
* |
* @param object An HTML_QuickForm_element object |
* @access private |
* @return string block name |
*/ |
function _matchBlock(&$element) |
{ |
$name = $element->getName(); |
$type = $element->getType(); |
if (isset($this->_elementBlocks[$name]) && $this->_tpl->blockExists($this->_elementBlocks[$name])) { |
if (('group' == $type) || ($this->_elementBlocks[$name] . '_loop' != $this->_tpl->currentBlock)) { |
return $this->_elementBlocks[$name]; |
} |
} |
if ('group' != $type && 'qf_main_loop' != $this->_tpl->currentBlock) { |
$prefix = substr($this->_tpl->currentBlock, 0, -5); // omit '_loop' postfix |
} else { |
$prefix = 'qf'; |
} |
if ($this->_tpl->blockExists($prefix . '_' . $type)) { |
return $prefix . '_' . $type; |
} elseif ($this->_tpl->blockExists($prefix . '_' . $name)) { |
return $prefix . '_' . $name; |
} else { |
return $prefix . '_element'; |
} |
} |
/** |
* Sets the block to use for element rendering |
* |
* @param mixed element name or array ('element name' => 'block name') |
* @param string block name if $elementName is not an array |
* @access public |
* @return void |
*/ |
function setElementBlock($elementName, $blockName = null) |
{ |
if (is_array($elementName)) { |
$this->_elementBlocks = array_merge($this->_elementBlocks, $elementName); |
} else { |
$this->_elementBlocks[$elementName] = $blockName; |
} |
} |
/** |
* Sets the name of a block to use for header rendering |
* |
* @param string block name |
* @access public |
* @return void |
*/ |
function setHeaderBlock($blockName) |
{ |
$this->_headerBlock = $blockName; |
} |
} |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/QuickHtml.php |
---|
New file |
0,0 → 1,203 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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. | |
// +----------------------------------------------------------------------+ |
// | Authors: Jason Rust <jrust@rustyparts.com> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: QuickHtml.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once('HTML/QuickForm/Renderer/Default.php'); |
/** |
* A renderer that makes it quick and easy to create customized forms. |
* |
* This renderer has three main distinctives: an easy way to create |
* custom-looking forms, the ability to separate the creation of form |
* elements from their display, and being able to use QuickForm in |
* widget-based template systems. See the online docs for more info. |
* For a usage example see: docs/renderers/QuickHtml_example.php |
* |
* @access public |
* @package QuickForm |
*/ |
class HTML_QuickForm_Renderer_QuickHtml extends HTML_QuickForm_Renderer_Default { |
// {{{ properties |
/** |
* The array of rendered elements |
* @var array |
*/ |
var $renderedElements = array(); |
// }}} |
// {{{ constructor |
/** |
* Constructor |
* |
* @access public |
* @return void |
*/ |
function HTML_QuickForm_Renderer_QuickHtml() |
{ |
$this->HTML_QuickForm_Renderer_Default(); |
// The default templates aren't used for this renderer |
$this->clearAllTemplates(); |
} // end constructor |
// }}} |
// {{{ toHtml() |
/** |
* returns the HTML generated for the form |
* |
* @param string $data (optional) Any extra data to put before the end of the form |
* |
* @access public |
* @return string |
*/ |
function toHtml($data = '') |
{ |
// Render any elements that haven't been rendered explicitly by elementToHtml() |
foreach (array_keys($this->renderedElements) as $key) { |
if (!$this->renderedElements[$key]['rendered']) { |
$this->renderedElements[$key]['rendered'] = true; |
$data .= $this->renderedElements[$key]['html'] . "\n"; |
} |
} |
// Insert the extra data and form elements at the end of the form |
$this->_html = str_replace('</form>', $data . "\n</form>", $this->_html); |
return $this->_html; |
} // end func toHtml |
// }}} |
// {{{ elementToHtml() |
/** |
* Gets the html for an element and marks it as rendered. |
* |
* @param string $elementName The element name |
* @param string $elementValue (optional) The value of the element. This is only useful |
* for elements that have the same name (i.e. radio and checkbox), but |
* different values |
* |
* @access public |
* @return string The html for the QuickForm element |
*/ |
function elementToHtml($elementName, $elementValue = null) |
{ |
$elementKey = null; |
// Find the key for the element |
foreach ($this->renderedElements as $key => $data) { |
if ($data['name'] == $elementName && |
// See if the value must match as well |
(is_null($elementValue) || |
$data['value'] == $elementValue)) { |
$elementKey = $key; |
break; |
} |
} |
if (is_null($elementKey)) { |
$msg = is_null($elementValue) ? "Element $elementName does not exist." : |
"Element $elementName with value of $elementValue does not exist."; |
return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true); |
} else { |
if ($this->renderedElements[$elementKey]['rendered']) { |
$msg = is_null($elementValue) ? "Element $elementName has already been rendered." : |
"Element $elementName with value of $elementValue has already been rendered."; |
return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true); |
} else { |
$this->renderedElements[$elementKey]['rendered'] = true; |
return $this->renderedElements[$elementKey]['html']; |
} |
} |
} // end func elementToHtml |
// }}} |
// {{{ renderElement() |
/** |
* Gets the html for an element and adds it to the array by calling |
* parent::renderElement() |
* |
* @param object An HTML_QuickForm_element object |
* @param bool Whether an element is required |
* @param string An error message associated with an element |
* |
* @access public |
* @return mixed HTML string of element if $immediateRender is set, else we just add the |
* html to the global _html string |
*/ |
function renderElement(&$element, $required, $error) |
{ |
$this->_html = ''; |
parent::renderElement($element, $required, $error); |
if (!$this->_inGroup) { |
$this->renderedElements[] = array( |
'name' => $element->getName(), |
'value' => $element->getValue(), |
'html' => $this->_html, |
'rendered' => false); |
} |
$this->_html = ''; |
} // end func renderElement |
// }}} |
// {{{ renderHidden() |
/** |
* Gets the html for a hidden element and adds it to the array. |
* |
* @param object An HTML_QuickForm_hidden object being visited |
* @access public |
* @return void |
*/ |
function renderHidden(&$element) |
{ |
$this->renderedElements[] = array( |
'name' => $element->getName(), |
'value' => $element->getValue(), |
'html' => $element->toHtml(), |
'rendered' => false); |
} // end func renderHidden |
// }}} |
// {{{ finishGroup() |
/** |
* Gets the html for the group element and adds it to the array by calling |
* parent::finishGroup() |
* |
* @param object An HTML_QuickForm_group object being visited |
* @access public |
* @return void |
*/ |
function finishGroup(&$group) |
{ |
$this->_html = ''; |
parent::finishGroup($group); |
$this->renderedElements[] = array( |
'name' => $group->getName(), |
'value' => $group->getValue(), |
'html' => $this->_html, |
'rendered' => false); |
$this->_html = ''; |
} // end func finishGroup |
// }}} |
} // end class HTML_QuickForm_Renderer_QuickHtml |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/Default.php |
---|
New file |
0,0 → 1,474 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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. | |
// +----------------------------------------------------------------------+ |
// | Authors: Alexey Borzov <borz_off@cs.msu.su> | |
// | Adam Daniel <adaniel1@eesus.jnj.com> | |
// | Bertrand Mansion <bmansion@mamasam.com> | |
// +----------------------------------------------------------------------+ |
// |
// $Id: Default.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $ |
require_once('HTML/QuickForm/Renderer.php'); |
/** |
* A concrete renderer for HTML_QuickForm, |
* based on QuickForm 2.x built-in one |
* |
* @access public |
*/ |
class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer |
{ |
/** |
* The HTML of the form |
* @var string |
* @access private |
*/ |
var $_html; |
/** |
* Header Template string |
* @var string |
* @access private |
*/ |
var $_headerTemplate = |
"\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>"; |
/** |
* Element template string |
* @var string |
* @access private |
*/ |
var $_elementTemplate = |
"\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>"; |
/** |
* Form template string |
* @var string |
* @access private |
*/ |
var $_formTemplate = |
"\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>"; |
/** |
* Required Note template string |
* @var string |
* @access private |
*/ |
var $_requiredNoteTemplate = |
"\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>"; |
/** |
* Array containing the templates for customised elements |
* @var array |
* @access private |
*/ |
var $_templates = array(); |
/** |
* Array containing the templates for group wraps. |
* |
* These templates are wrapped around group elements and groups' own |
* templates wrap around them. This is set by setGroupTemplate(). |
* |
* @var array |
* @access private |
*/ |
var $_groupWraps = array(); |
/** |
* Array containing the templates for elements within groups |
* @var array |
* @access private |
*/ |
var $_groupTemplates = array(); |
/** |
* True if we are inside a group |
* @var bool |
* @access private |
*/ |
var $_inGroup = false; |
/** |
* Array with HTML generated for group elements |
* @var array |
* @access private |
*/ |
var $_groupElements = array(); |
/** |
* Template for an element inside a group |
* @var string |
* @access private |
*/ |
var $_groupElementTemplate = ''; |
/** |
* HTML that wraps around the group elements |
* @var string |
* @access private |
*/ |
var $_groupWrap = ''; |
/** |
* HTML for the current group |
* @var string |
* @access private |
*/ |
var $_groupTemplate = ''; |
/** |
* Collected HTML of the hidden fields |
* @var string |
* @access private |
*/ |
var $_hiddenHtml = ''; |
/** |
* Constructor |
* |
* @access public |
*/ |
function HTML_QuickForm_Renderer_Default() |
{ |
$this->HTML_QuickForm_Renderer(); |
} // end constructor |
/** |
* returns the HTML generated for the form |
* |
* @access public |
* @return string |
*/ |
function toHtml() |
{ |
// _hiddenHtml is cleared in finishForm(), so this only matters when |
// finishForm() was not called (e.g. group::toHtml(), bug #3511) |
return $this->_hiddenHtml . $this->_html; |
} // end func toHtml |
/** |
* Called when visiting a form, before processing any form elements |
* |
* @param object An HTML_QuickForm object being visited |
* @access public |
* @return void |
*/ |
function startForm(&$form) |
{ |
$this->_html = ''; |
$this->_hiddenHtml = ''; |
} // end func startForm |
/** |
* Called when visiting a form, after processing all form elements |
* Adds required note, form attributes, validation javascript and form content. |
* |
* @param object An HTML_QuickForm object being visited |
* @access public |
* @return void |
*/ |
function finishForm(&$form) |
{ |
// add a required note, if one is needed |
if (!empty($form->_required) && !$form->_freezeAll) { |
$this->_html .= str_replace('{requiredNote}', $form->getRequiredNote(), $this->_requiredNoteTemplate); |
} |
// add form attributes and content |
$html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate); |
if (strpos($this->_formTemplate, '{hidden}')) { |
$html = str_replace('{hidden}', $this->_hiddenHtml, $html); |
} else { |
$this->_html .= $this->_hiddenHtml; |
} |
$this->_hiddenHtml = ''; |
$this->_html = str_replace('{content}', $this->_html, $html); |
// add a validation script |
if ('' != ($script = $form->getValidationScript())) { |
$this->_html = $script . "\n" . $this->_html; |
} |
} // end func finishForm |
/** |
* Called when visiting a header element |
* |
* @param object An HTML_QuickForm_header element being visited |
* @access public |
* @return void |
*/ |
function renderHeader(&$header) |
{ |
$name = $header->getName(); |
if (!empty($name) && isset($this->_templates[$name])) { |
$this->_html .= str_replace('{header}', $header->toHtml(), $this->_templates[$name]); |
} else { |
$this->_html .= str_replace('{header}', $header->toHtml(), $this->_headerTemplate); |
} |
} // end func renderHeader |
/** |
* Helper method for renderElement |
* |
* @param string Element name |
* @param mixed Element label (if using an array of labels, you should set the appropriate template) |
* @param bool Whether an element is required |
* @param string Error message associated with the element |
* @access private |
* @see renderElement() |
* @return string Html for element |
*/ |
function _prepareTemplate($name, $label, $required, $error) |
{ |
if (is_array($label)) { |
$nameLabel = array_shift($label); |
} else { |
$nameLabel = $label; |
} |
if (isset($this->_templates[$name])) { |
$html = str_replace('{label}', $nameLabel, $this->_templates[$name]); |
} else { |
$html = str_replace('{label}', $nameLabel, $this->_elementTemplate); |
} |
if ($required) { |
$html = str_replace('<!-- BEGIN required -->', '', $html); |
$html = str_replace('<!-- END required -->', '', $html); |
} else { |
$html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html); |
} |
if (isset($error)) { |
$html = str_replace('{error}', $error, $html); |
$html = str_replace('<!-- BEGIN error -->', '', $html); |
$html = str_replace('<!-- END error -->', '', $html); |
} else { |
$html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i", '', $html); |
} |
if (is_array($label)) { |
foreach($label as $key => $text) { |
$key = is_int($key)? $key + 2: $key; |
$html = str_replace("{label_{$key}}", $text, $html); |
$html = str_replace("<!-- BEGIN label_{$key} -->", '', $html); |
$html = str_replace("<!-- END label_{$key} -->", '', $html); |
} |
} |
if (strpos($html, '{label_')) { |
$html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $html); |
} |
return $html; |
} // end func _prepareTemplate |
/** |
* Renders an element Html |
* 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 |
* @access public |
* @return void |
*/ |
function renderElement(&$element, $required, $error) |
{ |
if (!$this->_inGroup) { |
$html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error); |
$this->_html .= str_replace('{element}', $element->toHtml(), $html); |
} elseif (!empty($this->_groupElementTemplate)) { |
$html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate); |
if ($required) { |
$html = str_replace('<!-- BEGIN required -->', '', $html); |
$html = str_replace('<!-- END required -->', '', $html); |
} else { |
$html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/i", '', $html); |
} |
$this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html); |
} else { |
$this->_groupElements[] = $element->toHtml(); |
} |
} // end func renderElement |
/** |
* Renders an hidden element |
* Called when visiting a hidden element |
* |
* @param object An HTML_QuickForm_hidden object being visited |
* @access public |
* @return void |
*/ |
function renderHidden(&$element) |
{ |
$this->_hiddenHtml .= $element->toHtml() . "\n"; |
} // end func renderHidden |
/** |
* Called when visiting a raw HTML/text pseudo-element |
* |
* @param object An HTML_QuickForm_html element being visited |
* @access public |
* @return void |
*/ |
function renderHtml(&$data) |
{ |
$this->_html .= $data->toHtml(); |
} // end func renderHtml |
/** |
* 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 |
* @access public |
* @return void |
*/ |
function startGroup(&$group, $required, $error) |
{ |
$name = $group->getName(); |
$this->_groupTemplate = $this->_prepareTemplate($name, $group->getLabel(), $required, $error); |
$this->_groupElementTemplate = empty($this->_groupTemplates[$name])? '': $this->_groupTemplates[$name]; |
$this->_groupWrap = empty($this->_groupWraps[$name])? '': $this->_groupWraps[$name]; |
$this->_groupElements = array(); |
$this->_inGroup = true; |
} // end func startGroup |
/** |
* Called when visiting a group, after processing all group elements |
* |
* @param object An HTML_QuickForm_group object being visited |
* @access public |
* @return void |
*/ |
function finishGroup(&$group) |
{ |
$separator = $group->_separator; |
if (is_array($separator)) { |
$count = count($separator); |
$html = ''; |
for ($i = 0; $i < count($this->_groupElements); $i++) { |
$html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i]; |
} |
} else { |
if (is_null($separator)) { |
$separator = ' '; |
} |
$html = implode((string)$separator, $this->_groupElements); |
} |
if (!empty($this->_groupWrap)) { |
$html = str_replace('{content}', $html, $this->_groupWrap); |
} |
$this->_html .= str_replace('{element}', $html, $this->_groupTemplate); |
$this->_inGroup = false; |
} // end func finishGroup |
/** |
* Sets element template |
* |
* @param string The HTML surrounding an element |
* @param string (optional) Name of the element to apply template for |
* @access public |
* @return void |
*/ |
function setElementTemplate($html, $element = null) |
{ |
if (is_null($element)) { |
$this->_elementTemplate = $html; |
} else { |
$this->_templates[$element] = $html; |
} |
} // end func setElementTemplate |
/** |
* Sets template for a group wrapper |
* |
* This template is contained within a group-as-element template |
* set via setTemplate() and contains group's element templates, set |
* via setGroupElementTemplate() |
* |
* @param string The HTML surrounding group elements |
* @param string Name of the group to apply template for |
* @access public |
* @return void |
*/ |
function setGroupTemplate($html, $group) |
{ |
$this->_groupWraps[$group] = $html; |
} // end func setGroupTemplate |
/** |
* Sets element template for elements within a group |
* |
* @param string The HTML surrounding an element |
* @param string Name of the group to apply template for |
* @access public |
* @return void |
*/ |
function setGroupElementTemplate($html, $group) |
{ |
$this->_groupTemplates[$group] = $html; |
} // end func setGroupElementTemplate |
/** |
* Sets header template |
* |
* @param string The HTML surrounding the header |
* @access public |
* @return void |
*/ |
function setHeaderTemplate($html) |
{ |
$this->_headerTemplate = $html; |
} // end func setHeaderTemplate |
/** |
* Sets form template |
* |
* @param string The HTML surrounding the form tags |
* @access public |
* @return void |
*/ |
function setFormTemplate($html) |
{ |
$this->_formTemplate = $html; |
} // end func setFormTemplate |
/** |
* Sets the note indicating required fields template |
* |
* @param string The HTML surrounding the required note |
* @access public |
* @return void |
*/ |
function setRequiredNoteTemplate($html) |
{ |
$this->_requiredNoteTemplate = $html; |
} // end func setRequiredNoteTemplate |
/** |
* Clears all the HTML out of the templates that surround notes, elements, etc. |
* Useful when you want to use addData() to create a completely custom form look |
* |
* @access public |
* @return void |
*/ |
function clearAllTemplates() |
{ |
$this->setElementTemplate('{element}'); |
$this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n"); |
$this->setRequiredNoteTemplate(''); |
$this->_templates = array(); |
} // end func clearAllTemplates |
} // end class HTML_QuickForm_Renderer_Default |
?> |
/tags/v2.0-narmer/api/pear/HTML/QuickForm/Renderer/ITStatic.php |
---|
New file |
0,0 → 1,490 |
<?php |
/* vim: set expandtab tabstop=4 shiftwidth=4: */ |
// +----------------------------------------------------------------------+ |
// | PHP version 4.0 | |
// +----------------------------------------------------------------------+ |
// | 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.2 2005-09-20 17:01:22 ddelon 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> |
* @access public |
*/ |
class HTML_QuickForm_Renderer_ITStatic extends HTML_QuickForm_Renderer |
{ |
/** |
* An HTML_Template_IT or some other API compatible Template instance |
* @var object |
*/ |
var $_tpl = null; |
/** |
* Rendered form name |
* @var string |
*/ |
var $_formName = 'form'; |
/** |
* The errors that were not shown near concrete fields go here |
* @var array |
*/ |
var $_errors = array(); |
/** |
* Show the block with required note? |
* @var bool |
*/ |
var $_showRequired = false; |
/** |
* Which group are we currently parsing ? |
* @var string |
*/ |
var $_inGroup; |
/** |
* Index of the element in its group |
* @var int |
*/ |
var $_elementIndex = 0; |
/** |
* If elements have been added with the same name |
* @var array |
*/ |
var $_duplicateElements = array(); |
/** |
* How to handle the required tag for required fields |
* @var string |
*/ |
var $_required = '{label}<font size="1" color="red">*</font>'; |
/** |
* How to handle error messages in form validation |
* @var string |
*/ |
var $_error = '<font color="red">{error}</font><br />{html}'; |
/** |
* Collected HTML for hidden elements, if needed |
* @var string |
*/ |
var $_hidden = ''; |
/** |
* Constructor |
* |
* @param object An HTML_Template_IT or other compatible Template object to use |
*/ |
function HTML_QuickForm_Renderer_ITStatic(&$tpl) |
{ |
$this->HTML_QuickForm_Renderer(); |
$this->_tpl =& $tpl; |
} // end constructor |
/** |
* Called when visiting a form, before processing any form elements |
* |
* @param object An HTML_QuickForm object being visited |
* @access public |
* @return void |
*/ |
function startForm(&$form) |
{ |
$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; |
} |
} |
} // end func startForm |
/** |
* Called when visiting a form, after processing all form elements |
* |
* @param object An HTML_QuickForm object being visited |
* @access public |
* @return void |
*/ |
function finishForm(&$form) |
{ |
// 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'); |
} |
} |
// show required note |
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()); |
} // end func finishForm |
/** |
* Called when visiting a header element |
* |
* @param object An HTML_QuickForm_header element being visited |
* @access public |
* @return void |
*/ |
function renderHeader(&$header) |
{ |
$name = $header->getName(); |
$varName = $this->_formName.'_header'; |
// Find placeHolder |
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 |
* @access public |
* @return void |
*/ |
function renderElement(&$element, $required, $error) |
{ |
$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.'_'; |
$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; |
} |
if (!empty($label)) { |
if (is_array($label)) { |
foreach ($label as $key => $value) { |
$this->_tpl->setVariable($varName.'label_'.$key, $value); |
} |
} else { |
$this->_tpl->setVariable($varName.'label', $label); |
} |
} |
$this->_tpl->setVariable($varName.'html', $html); |
} |
} else { |
$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]++; |
} else { |
$varName = $this->_formName.'_'.$name; |
} |
$label = $element->getLabel(); |
$html = $element->toHtml(); |
if ($required) { |
$this->_showRequired = true; |
$this->_renderRequired($label, $html); |
} |
if (!empty($error)) { |
$this->_renderError($label, $html, $error); |
} |
if (is_array($label)) { |
foreach ($label as $key => $value) { |
$this->_tpl->setVariable($varName.'_label_'.$key, $value); |
} |
} else { |
$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 |
* @access public |
* @return void |
*/ |
function renderHidden(&$element) |
{ |
if ($this->_tpl->placeholderExists($this->_formName . '_hidden')) { |
$this->_hidden .= $element->toHtml(); |
} else { |
$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 |
* @access public |
* @return void |
*/ |
function startGroup(&$group, $required, $error) |
{ |
$name = $group->getName(); |
$varName = $this->_formName.'_'.$name; |
$this->_elementIndex = 0; |
$html = $this->_tpl->placeholderExists($varName.'_html') ? $group->toHtml() : ''; |
$label = $group->getLabel(); |
if ($required) { |
$this->_renderRequired($label, $html); |
} |
if (!empty($error)) { |
$this->_renderError($label, $html, $error); |
} |
if (!empty($html)) { |
$this->_tpl->setVariable($varName.'_html', $html); |
} else { |
// Uses error blocks to set the special groups layout error |
// <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error --> |
if (!empty($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); |
array_pop($this->_errors); |
} |
} |
if (is_array($label)) { |
foreach ($label as $key => $value) { |
$this->_tpl->setVariable($varName.'_label_'.$key, $value); |
} |
} else { |
$this->_tpl->setVariable($varName.'_label', $label); |
} |
$this->_inGroup = $varName; |
} // end func startGroup |
/** |
* Called when visiting a group, after processing all group elements |
* |
* @param object An HTML_QuickForm_group object being visited |
* @access public |
* @return void |
*/ |
function finishGroup(&$group) |
{ |
$this->_inGroup = ''; |
} // 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. |
* For example: |
* <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 |
* @access public |
* @return void |
*/ |
function setRequiredTemplate($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. |
* For example: |
* <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 |
* {html} nor {label}. |
* |
* 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 |
* @access public |
* @return void |
*/ |
function setErrorTemplate($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() |
* @access private |
* @return void |
*/ |
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); |
if (is_array($label)) { |
$label[0] = $this->_getTplBlock($tplBlock); |
} else { |
$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); |
} |
} else { |
if (!empty($label) && strpos($this->_required, '{label}') !== false) { |
if (is_array($label)) { |
$label[0] = str_replace('{label}', $label[0], $this->_required); |
} else { |
$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() |
* @access private |
* @return void |
*/ |
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); |
if (is_array($label)) { |
$label[0] = $this->_getTplBlock($tplBlock); |
} else { |
$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) { |
if (is_array($label)) { |
$label[0] = str_replace(array('{label}', '{error}'), array($label[0], $error), $this->_error); |
} else { |
$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); |
} else { |
$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); |
} else { |
$oldClear = $this->_tpl->clearCache; |
$this->_tpl->clearCache = true; |
$ret = $this->_tpl->get($block); |
$this->_tpl->clearCache = $oldClear; |
} |
return $ret; |
} |
} // end class HTML_QuickForm_Renderer_ITStatic |
?> |