123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456 |
- <?php
- /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
- /**
- * A concrete renderer for HTML_QuickForm, makes an object from form contents
- *
- * PHP versions 4 and 5
- *
- * LICENSE: This source file is subject to version 3.01 of the PHP license
- * that is available through the world-wide-web at the following URI:
- * http://www.php.net/license/3_01.txt If you did not receive a copy of
- * the PHP License and are unable to obtain it through the web, please
- * send a note to license@php.net so we can mail you a copy immediately.
- *
- * @category HTML
- * @package HTML_QuickForm
- * @author Ron McClain <ron@humaniq.com>
- * @copyright 2001-2009 The PHP Group
- * @license http://www.php.net/license/3_01.txt PHP License 3.01
- * @version CVS: $Id: Object.php,v 1.6 2009/04/04 21:34:04 avb Exp $
- * @link http://pear.php.net/package/HTML_QuickForm
- */
- /**
- * A concrete renderer for HTML_QuickForm, makes an object from form contents
- *
- * Based on HTML_Quickform_Renderer_Array code
- *
- * @category HTML
- * @package HTML_QuickForm
- * @author Ron McClain <ron@humaniq.com>
- * @version Release: 3.2.11
- * @since 3.1.1
- */
- class HTML_QuickForm_Renderer_Object extends HTML_QuickForm_Renderer
- {
- /**#@+
- * @access private
- */
- /**
- * The object being generated
- * @var QuickformForm
- */
- 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 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 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 HTML_QuickForm_element form element being rendered
- * @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 QuickformElement 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.
- * <pre>
- * {form.outputJavaScript():h}
- * {form.outputHeader():h}
- * <table>
- * <tr>
- * <td>{form.name.label:h}</td><td>{form.name.html:h}</td>
- * </tr>
- * </table>
- * </form>
- * </pre>
- *
- * @category HTML
- * @package HTML_QuickForm
- * @author Ron McClain <ron@humaniq.com>
- * @version Release: 3.2.11
- * @since 3.1.1
- */
- 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.
- *
- * @category HTML
- * @package HTML_QuickForm
- * @author Ron McClain <ron@humaniq.com>
- * @version Release: 3.2.11
- * @since 3.1.1
- */
- 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
- ?>
|