2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * QuickForm renderer for Flexy template engine, static version.
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package HTML_QuickForm
17 * @author Ron McClain <ron@humaniq.com>
18 * @copyright 2001-2011 The PHP Group
19 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @link http://pear.php.net/package/HTML_QuickForm
25 * A concrete renderer for HTML_QuickForm, makes an object from form contents
27 require_once 'HTML/QuickForm/Renderer/Object.php';
30 * QuickForm renderer for Flexy template engine, static version.
32 * A static renderer for HTML_Quickform. Makes a QuickFormFlexyObject
33 * from the form content suitable for use with a Flexy template
37 * $form =& new HTML_QuickForm('form', 'POST');
38 * $template =& new HTML_Template_Flexy();
39 * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
40 * $renderer->setHtmlTemplate("html.html");
41 * $renderer->setLabelTemplate("label.html");
42 * $form->accept($renderer);
43 * $view = new StdClass;
44 * $view->form = $renderer->toObject();
45 * $template->compile("mytemplate.html");
48 * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
51 * @package HTML_QuickForm
52 * @author Ron McClain <ron@humaniq.com>
53 * @version Release: 3.2.16
56 class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object
62 * HTML_Template_Flexy instance
68 * Current element index
69 * @var integer $_elementIdx
74 * The current element index inside a group
75 * @var integer $_groupElementIdx
77 var $_groupElementIdx = 0;
80 * Name of template file for form html
82 * @see setRequiredTemplate()
87 * Name of template file for form labels
89 * @see setErrorTemplate()
94 * Class of the element objects, so you can add your own
96 * @var string $_elementType
98 var $_elementType = 'QuickformFlexyElement';
104 * @param HTML_Template_Flexy template object to use
107 function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
109 $this->HTML_QuickForm_Renderer_Object(true);
110 $this->_obj = new QuickformFlexyForm();
111 $this->_flexy =& $flexy;
114 function renderHeader(&$header)
116 if($name = $header->getName()) {
117 $this->_obj->header->$name = $header->toHtml();
119 $this->_obj->header[$this->_sectionCount] = $header->toHtml();
121 $this->_currentSection = $this->_sectionCount++;
122 } // end func renderHeader
124 function startGroup(&$group, $required, $error)
126 parent::startGroup($group, $required, $error);
127 $this->_groupElementIdx = 1;
128 } //end func startGroup
131 * Creates an object representing an element containing
132 * the key for storing this
135 * @param HTML_QuickForm_element form element being rendered
136 * @param bool Whether an element is required
137 * @param string Error associated with the element
140 function _elementToObject(&$element, $required, $error)
142 $ret = parent::_elementToObject($element, $required, $error);
143 if($ret->type == 'group') {
144 $ret->html = $element->toHtml();
145 unset($ret->elements);
147 if(!empty($this->_label)) {
148 $this->_renderLabel($ret);
151 if(!empty($this->_html)) {
152 $this->_renderHtml($ret);
153 $ret->error = $error;
156 // Create an element key from the name
157 if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
159 $keys = '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->name) . '\'}';
161 $keys = '->{\'' . str_replace(
162 array('\\', '\'', '[', ']'), array('\\\\', '\\\'', '\'}->{\'', ''),
166 // special handling for elements in native groups
167 if (is_object($this->_currentGroup)) {
168 // skip unnamed group items unless radios: no name -> no static access
169 // identification: have the same key string as the parent group
170 if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
173 // reduce string of keys by remove leading group keys
174 if (0 === strpos($keys, $this->_currentGroup->keys)) {
175 $keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys));
178 } elseif (0 == strlen($ret->name)) {
179 $keys = '->{\'element_' . $this->_elementIdx . '\'}';
181 $keys = '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->name) . '\'}';
183 // for radios: add extra key from value
184 if ('radio' == $ret->type && '[]' != substr($keys, -2)) {
185 $keys .= '->{\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret->value) . '\'}';
188 $this->_elementIdx++;
193 * Stores an object representation of an element in the
194 * QuickformFormObject instance
197 * @param QuickformElement Object representation of an element
200 function _storeObject($elObj)
203 $keys = $elObj->keys;
205 if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
206 $code = '$this->_currentGroup' . $keys . ' = $elObj;';
208 $code = '$this->_obj' . $keys . ' = $elObj;';
215 * Set the filename of the template to render html elements.
216 * In your template, {html} is replaced by the unmodified html.
217 * If the element is required, {required} will be true.
221 * <font color="red" size="1">{error:h}</font><br />
227 * @param string Filename of template
230 function setHtmlTemplate($template)
232 $this->_html = $template;
236 * Set the filename of the template to render form labels
237 * In your template, {label} is replaced by the unmodified label.
238 * {error} will be set to the error, if any. {required} will
239 * be true if this is a required field
243 * <font color="orange" size="1">*</font>
249 * @param string Filename of template
252 function setLabelTemplate($template)
254 $this->_label = $template;
257 function _renderLabel(&$ret)
259 $this->_flexy->compile($this->_label);
260 $ret->label = $this->_flexy->bufferedOutputObject($ret);
263 function _renderHtml(&$ret)
265 $this->_flexy->compile($this->_html);
266 $ret->html = $this->_flexy->bufferedOutputObject($ret);
268 } // end class HTML_QuickForm_Renderer_ObjectFlexy
271 * Adds nothing to QuickformForm, left for backwards compatibility
274 * @package HTML_QuickForm
277 class QuickformFlexyForm extends QuickformForm
282 * Adds nothing to QuickformElement, left for backwards compatibility
285 * @package HTML_QuickForm
288 class QuickformFlexyElement extends QuickformElement