root/trunk/patForms/Renderer/Flexy.php

Revision 284, 4.2 kB (checked in by sfuchs, 3 years ago)

some corrections to docblocks and comments

Line 
1 <?php
2 /**
3  * patForms Renderer for Flexy
4  *
5  * $Id$
6  *
7  * @package       patForms
8  * @subpackage    Renderer
9  */
10
11 /**
12  * Error: Flexy is not available
13  */
14 define('PATFORMS_RENDERER_FLEXY_ERROR_NO_CLASS', 'Renderer:Flexy:001');
15
16
17 /**
18  * patForms Renderer for Flexy
19  *
20  * This requires Flexy to work. See http://pear.php.net/package/HTML_Template_Flexy
21  *
22  * @package       patForms
23  * @subpackage    Renderer
24  * @author        Sven Fuchs <svenfuchs@artweb-design.de>
25  * @license       LGPL, see license.txt for details
26  * @link          http://www.php-tools.net
27  */
28 class patForms_Renderer_Flexy extends patForms_Renderer
29 {
30    /**
31     * Stores forms data
32     *
33     * @access    private
34     * @var       array
35     */
36     var $forms = null;
37
38    /**
39     * Stores the template object
40     *
41     * @access    private
42     * @var       object Flexy
43     */
44     var $_tmpl = null;
45
46    /**
47     * Stores the name of the element attributes that will
48     * be replaced in the template.
49     *
50     * @access    private
51     * @var       array
52     */
53     var $_attributes = array(
54         'label',
55         'title',
56         'display'
57     );
58
59    /**
60     * Sets the template object, that will be used to render
61     * the page.
62     *
63     * @access    public
64     * @param    object Flexy
65     */
66     function setTemplate(&$tmpl)
67     {
68         $this->_tmpl = &$tmpl;
69     }
70
71    /**
72     * Sets a list of attributes to replace in the template
73     * in addition of the default attributes list.
74     *
75     * @access   public
76     * @param    array   The list of attributes
77     * @see      $_attributes
78     */
79     function setAttributes($attributes)
80     {
81         $this->_attributes = array_merge( $this->_attributes, $attributes );
82     }
83
84    /**
85     * Serializes the form elements and sets them to a Flexy
86     * object.
87     *
88     * Arguments that can be passed:
89     * - template = an instance of HTML_Template_Flexy
90     * - tmplDir  = directory in which templates are stored
91     * - compileDir = directory in which templates are compiled
92     *
93     * @access   public
94     * @param    object  Reference to the patForms object
95     * @param    array   optional arguments for the renderer
96     * @return   object     Flexy
97     */
98     function &render(&$patForms, $args = array())
99     {
100         if (is_object($args)) {
101             $args = array('template' => $args);
102         }
103
104         // set the template
105         if (isset($args['template'])) {
106             $this->_tmpl = $args['template'];
107         }
108
109         // create a new template instance
110         if ($this->_tmpl === null) {
111             if (!class_exists('HTML_Template_Flexy')) {
112                 return patErrorManager::raiseError(PATFORMS_RENDERER_FLEXY_ERROR_NO_CLASS, 'No instance of Flexy has been passed, nor has the class been loaded.');
113             }
114             $options = array();
115             if (isset($args['tmplDir'])) {
116                 $options['templateDir'] = $args['tmplDir'];
117             }
118             if (isset($args['compileDir'])) {
119                 $options['compileDir'] = $args['compileDir'];
120             }
121             $this->_tmpl = new HTML_Template_Flexy($options);
122         }
123
124         // load the template
125         if (isset($args['tmplFile'])) {
126             $this->_tmpl->compile($args['tmplFile']);
127         }
128
129         // render start and end tags
130         $data = array(
131             'start' => $patForms->serializeStart(),
132             'end' => $patForms->serializeEnd(),
133             'elements' => array()
134         );
135
136         // render the elements
137         foreach ($patForms->getElements() as $element) {
138
139             $ptr = &$data['elements'][$element->getName()];
140             $ptr['tag'] = $element->serialize();
141
142             foreach ($this->_attributes as $name) {
143                 $ptr[$name] = (string) $element->getAttribute($name);
144             }
145         }
146
147         // render errors if present
148         if ($validationErrors = $patForms->getValidationErrors()) {
149             foreach($validationErrors as $name => $errors) {
150                 if (empty($errors)) {
151                     continue;
152                 }
153
154                 foreach($errors as $error) {
155                     $data['errors'][$name] = $error;
156                     $data['errors'][$name]['field'] = $name;
157                     $data['errors'][$name]['isFormError'] = ($name == '__form');
158                 }
159             }
160         }
161
162         $this->forms[$patForms->getName()] = $data;
163         $this->_tmpl->setData('forms', $this->forms);
164
165         return $this->_tmpl;
166     }
167 }
168 ?>
Note: See TracBrowser for help on using the browser.