Subversion Repositories Applications.papyrus

Rev

Rev 1688 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
320 jpm 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
12
// | If you did not receive a copy of the PHP license and are unable to   |
13
// | obtain it through the world-wide-web, please send a note to          |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
16
// | Author: Ron McClain <ron@humaniq.com>                                |
17
// +----------------------------------------------------------------------+
18
//
443 ddelon 19
// $Id: ObjectFlexy.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
320 jpm 20
 
21
require_once("HTML/QuickForm/Renderer/Object.php");
22
 
23
/**
443 ddelon 24
 * QuickForm renderer for Flexy template engine, static version.
25
 *
320 jpm 26
 * A static renderer for HTML_Quickform.  Makes a QuickFormFlexyObject
27
 * from the form content suitable for use with a Flexy template
28
 *
29
 * Usage:
30
 * $form =& new HTML_QuickForm('form', 'POST');
31
 * $template =& new HTML_Template_Flexy();
32
 * $renderer =& new HTML_QuickForm_Renderer_ObjectFlexy(&$template);
33
 * $renderer->setHtmlTemplate("html.html");
34
 * $renderer->setLabelTemplate("label.html");
35
 * $form->accept($renderer);
36
 * $view = new StdClass;
37
 * $view->form = $renderer->toObject();
38
 * $template->compile("mytemplate.html");
39
 *
40
 * Based on the code for HTML_QuickForm_Renderer_ArraySmarty
41
 *
443 ddelon 42
 * @see QuickFormFlexyObject
43
 * @access public
320 jpm 44
 */
443 ddelon 45
class HTML_QuickForm_Renderer_ObjectFlexy extends HTML_QuickForm_Renderer_Object
46
{
320 jpm 47
    /**
48
     * HTML_Template_Flexy instance
49
     * @var object $_flexy
50
     */
51
    var $_flexy;
52
 
53
    /**
54
     * Current element index
55
     * @var integer $_elementIdx
56
     */
57
    var $_elementIdx;
58
 
59
    /**
60
     * The current element index inside a group
61
     * @var integer $_groupElementIdx
62
     */
443 ddelon 63
    var $_groupElementIdx = 0;
320 jpm 64
 
65
    /**
66
     * Name of template file for form html
67
     * @var string $_html
68
     * @see     setRequiredTemplate()
69
     */
70
    var $_html = '';
71
 
72
    /**
73
     * Name of template file for form labels
74
     * @var string $label
75
     * @see        setErrorTemplate()
76
     */
77
    var $label = '';
78
 
79
    /**
80
     * Class of the element objects, so you can add your own
81
     * element methods
82
     * @var string $_elementType
83
     */
84
    var $_elementType = 'QuickformFlexyElement';
85
 
86
    /**
87
     * Constructor
88
     *
89
     * @param $flexy object   HTML_Template_Flexy instance
90
     * @public
91
     */
92
    function HTML_QuickForm_Renderer_ObjectFlexy(&$flexy)
93
    {
94
        $this->HTML_QuickForm_Renderer_Object(true);
95
        $this->_obj = new QuickformFlexyForm();
96
        $this->_flexy =& $flexy;
97
    } // end constructor
98
 
99
    function renderHeader(&$header)
100
    {
101
        if($name = $header->getName()) {
102
            $this->_obj->header->$name = $header->toHtml();
103
        } else {
104
            $this->_obj->header[$this->_sectionCount] = $header->toHtml();
105
        }
106
        $this->_currentSection = $this->_sectionCount++;
107
    } // end func renderHeader
108
 
109
    function startGroup(&$group, $required, $error)
110
    {
111
        parent::startGroup($group, $required, $error);
112
        $this->_groupElementIdx = 1;
113
    } //end func startGroup
114
 
115
    /**
116
     * Creates an object representing an element containing
117
     * the key for storing this
118
     *
443 ddelon 119
     * @access private
320 jpm 120
     * @param element object     An HTML_QuickForm_element object
121
     * @param required bool        Whether an element is required
122
     * @param error string    Error associated with the element
123
     * @return object
124
     */
443 ddelon 125
    function _elementToObject(&$element, $required, $error)
126
    {
320 jpm 127
        $ret = parent::_elementToObject($element, $required, $error);
128
        if($ret->type == 'group') {
129
            $ret->html = $element->toHtml();
130
            unset($ret->elements);
131
        }
132
        if(!empty($this->_label)) {
133
            $this->_renderLabel($ret);
134
        }
135
 
136
        if(!empty($this->_html)) {
137
            $this->_renderHtml($ret);
138
            $ret->error = $error;
139
        }
140
 
141
        // Create an element key from the name
142
        if (false !== ($pos = strpos($ret->name, '[')) || is_object($this->_currentGroup)) {
143
            if (!$pos) {
144
                $keys = '->{\'' . $ret->name . '\'}';
145
            } else {
146
                $keys = '->{\'' . str_replace(array('[', ']'), array('\'}->{\'', ''), $ret->name) . '\'}';
147
            }
148
            // special handling for elements in native groups
149
            if (is_object($this->_currentGroup)) {
150
                // skip unnamed group items unless radios: no name -> no static access
151
                // identification: have the same key string as the parent group
152
                if ($this->_currentGroup->keys == $keys && 'radio' != $ret->type) {
153
                    return false;
154
                }
155
                // reduce string of keys by remove leading group keys
156
                if (0 === strpos($keys, $this->_currentGroup->keys)) {
157
                    $keys = substr_replace($keys, '', 0, strlen($this->_currentGroup->keys));
158
                }
159
            }
160
        } elseif (0 == strlen($ret->name)) {
161
            $keys = '->{\'element_' . $this->_elementIdx . '\'}';
162
        } else {
163
            $keys = '->{\'' . $ret->name . '\'}';
164
        }
165
        // for radios: add extra key from value
166
        if ('radio' == $ret->type && '[]' != substr($keys, -2)) {
167
            $keys .= '->{\'' . $ret->value . '\'}';
168
        }
169
        $ret->keys = $keys;
170
        $this->_elementIdx++;
171
        return $ret;
172
    }
173
 
174
    /**
175
     * Stores an object representation of an element in the
176
     * QuickformFormObject instance
177
     *
443 ddelon 178
     * @access private
320 jpm 179
     * @param elObj object  Object representation of an element
180
     * @return void
181
     */
182
    function _storeObject($elObj)
183
    {
184
        if ($elObj) {
185
            $keys = $elObj->keys;
186
            unset($elObj->keys);
187
            if(is_object($this->_currentGroup) && ('group' != $elObj->type)) {
188
                $code = '$this->_currentGroup' . $keys . ' = $elObj;';
189
            } else {
190
                $code = '$this->_obj' . $keys . ' = $elObj;';
191
            }
192
            eval($code);
193
        }
194
    }
195
 
196
    /**
197
     * Set the filename of the template to render html elements.
198
     * In your template, {html} is replaced by the unmodified html.
199
     * If the element is required, {required} will be true.
200
     * Eg.
201
     * {if:error}
202
     *   <font color="red" size="1">{error:h}</font><br />
203
     * {end:}
204
     * {html:h}
205
     *
443 ddelon 206
     * @access public
320 jpm 207
     * @param template string   Filename of template
208
     * @return void
209
     */
210
    function setHtmlTemplate($template)
211
    {
212
        $this->_html = $template;
213
    }
214
 
215
    /**
216
     * Set the filename of the template to render form labels
217
     * In your template, {label} is replaced by the unmodified label.
218
     * {error} will be set to the error, if any.  {required} will
219
     * be true if this is a required field
220
     * Eg.
221
     * {if:required}
222
     * <font color="orange" size="1">*</font>
223
     * {end:}
224
     * {label:h}
225
     *
443 ddelon 226
     * @access public
320 jpm 227
     * @param template string   Filename of template
228
     * @return void
229
     */
230
    function setLabelTemplate($template)
231
    {
232
        $this->_label = $template;
233
    }
234
 
235
    function _renderLabel(&$ret)
236
    {
237
        $this->_flexy->compile($this->_label);
238
        $ret->label = $this->_flexy->bufferedOutputObject($ret);
239
    }
240
 
241
    function _renderHtml(&$ret)
242
    {
243
        $this->_flexy->compile($this->_html);
244
        $ret->html = $this->_flexy->bufferedOutputObject($ret);
245
    }
246
} // end class HTML_QuickForm_Renderer_ObjectFlexy
247
 
248
/**
443 ddelon 249
 * Adds nothing to QuickformForm, left for backwards compatibility
320 jpm 250
 */
443 ddelon 251
class QuickformFlexyForm extends QuickformForm
252
{
253
}
320 jpm 254
 
255
/**
443 ddelon 256
 * Adds nothing to QuickformElement, left for backwards compatibility
320 jpm 257
 */
443 ddelon 258
class QuickformFlexyElement extends QuickformElement
259
{
260
}
261
?>