root/trunk/patForms/Renderer/Savant3.php

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

some corrections to docblocks and comments

Line 
1 <?php
2 /**
3  * patForms Renderer for Savant3
4  *
5  * $Id$
6  *
7  * @package       patForms
8  * @subpackage    Renderer
9  */
10
11 /**
12  * Error: Savant3 is not available
13  */
14 define('PATFORMS_RENDERER_SAVANT3_ERROR_NO_CLASS', 'Renderer:Savant3:001');
15
16
17 /**
18  * patForms Renderer for Savant3
19  *
20  * This requires Savant3 to work. See http://www.phpsavant.com
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_Savant3 extends patForms_Renderer
29 {
30    /**
31     * Stores forms data
32     *
33     * @access    private
34     * @var       array
35     */
36     var $forms = array();
37
38    /**
39     * Stores the template object
40     *
41     * @access    private
42     * @var       object Savant3
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 Savant3 object, that will be used to render
61     * the page.
62     *
63     * @access    public
64     * @param    object Savant3
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 Savant3
86     * object.
87     *
88     * Arguments that can be passed:
89     * - template = an instance of Savant3
90     * - tmplFile = name of the template file
91     * - tmplDir  = directory in which templates are stored
92     *
93     * @access   public
94     * @param    object  Reference to the patForms object
95     * @param    array   optional arguments for the renderer
96     * @return   object     Savant3
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('Savant3')) {
112                 return patErrorManager::raiseError(PATFORMS_RENDERER_SAVANT3_ERROR_NO_CLASS, 'No instance of Savant3 has been passed, nor has the class been loaded.');
113             }
114             $this->_tmpl = new Savant3();
115         }
116
117         // set the base directory
118         if (isset($args['tmplDir'])) {
119             $this->_tmpl->setPath('template', $args['tmplDir']);
120         }
121
122         // load the template
123         if (isset($args['tmplFile'])) {
124             $result = $this->_tmpl->setTemplate($args['tmplFile']);
125         }
126
127         // render start and end tags
128         $data = array(
129             'start' => $patForms->serializeStart(),
130             'end' => $patForms->serializeEnd(),
131             'elements' => array()
132         );
133
134         // render the elements
135         foreach ($patForms->getElements() as $element) {
136
137             $ptr = &$data['elements'][$element->getName()];
138             $ptr['tag'] = $element->serialize();
139
140             foreach ($this->_attributes as $name) {
141                 $ptr[$name] = (string) $element->getAttribute($name);
142             }
143         }
144
145         // render errors if present
146         if ($validationErrors = $patForms->getValidationErrors()) {
147             foreach($validationErrors as $name => $errors) {
148                 if (empty($errors)) {
149                     continue;
150                 }
151
152                 foreach($errors as $error) {
153                     $data['errors'][$name] = $error;
154                     $data['errors'][$name]['field'] = $name;
155                     $data['errors'][$name]['isFormError'] = ($name == '__form');
156                 }
157             }
158         }
159
160         $this->forms[$patForms->getName()] = $data;
161         $this->_tmpl->assign('forms', $this->forms);
162
163         return $this->_tmpl;
164     }
165 }
166 ?>
Note: See TracBrowser for help on using the browser.