root/trunk/patForms/Renderer/PhpTal.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 PHPTal
4  *
5  * $Id$
6  *
7  * @package       patForms
8  * @subpackage    Renderer
9  */
10
11 /**
12  * Error: PHPTal is not available
13  */
14 define('PATFORMS_RENDERER_PHPTAL_ERROR_NO_CLASS', 'Renderer:PhpTal:001');
15
16
17 /**
18  * patForms Renderer for PHPTal
19  *
20  * This requires PHPTal to work. See http://phptal.motion-twin.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_PhpTal 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 PHPTal
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 PHPTal object, that will be used to render the page.
61     *
62     * @access    public
63     * @param    object PHPTal
64     */
65     function setTemplate(&$tmpl)
66     {
67         $this->_tmpl = &$tmpl;
68     }
69
70    /**
71     * Sets a list of attributes to replace in the template
72     * in addition of the default attributes list.
73     *
74     * @access   public
75     * @param    array   The list of attributes
76     * @see      $_attributes
77     */
78     function setAttributes($attributes)
79     {
80         $this->_attributes = array_merge( $this->_attributes, $attributes );
81     }
82
83    /**
84     * Serializes the form elements and sets them to a PHPTal
85     * object.
86     *
87     * Arguments that can be passed:
88     * - template = an instance of PHPTal
89     * - tmplFile = name of the template file
90     * - tmplDir  = directory in which templates are stored
91     *
92     * @access   public
93     * @param    object  Reference to the patForms object
94     * @param    array   optional arguments for the renderer
95     * @return   object     PHPTal
96     */
97     function &render(&$patForms, $args = array())
98     {
99         if (is_object($args)) {
100             $args = array('template' => $args);
101         }
102
103         // set the template
104         if (isset($args['template'])) {
105             $this->_tmpl = $args['template'];
106         }
107
108         // create a new template instance
109         if ($this->_tmpl === null) {
110             if (!class_exists('PHPTAL')) {
111                 return patErrorManager::raiseError(PATFORMS_RENDERER_PHPTAL_ERROR_NO_CLASS, 'No instance of PHPTal has been passed, nor has the class been loaded.');
112             }
113             $this->_tmpl = new PHPTal();
114         }
115
116         // set the base directory
117         if (isset($args['tmplDir'])) {
118             $this->_tmpl->setTemplateRepository($args['tmplDir']);
119         }
120
121         // load the template
122         if (isset($args['tmplFile'])) {
123             $result = $this->_tmpl->setTemplate($args['tmplFile']);
124         }
125
126         // render start and end tags
127         $data = array(
128             'start' => $patForms->serializeStart(),
129             'end' => $patForms->serializeEnd(),
130             'elements' => array()
131         );
132
133         // render the elements
134         foreach ($patForms->getElements() as $element) {
135
136             $ptr = &$data['elements'][$element->getName()];
137             $ptr['tag'] = $element->serialize();
138
139             foreach ($this->_attributes as $name) {
140                 $ptr[$name] = (string) $element->getAttribute($name);
141             }
142         }
143
144         // render errors if present
145         if ($validationErrors = $patForms->getValidationErrors()) {
146             foreach($validationErrors as $name => $errors) {
147                 if (empty($errors)) {
148                     continue;
149                 }
150
151                 foreach($errors as $error) {
152                     $data['errors'][$name] = $error;
153                     $data['errors'][$name]['field'] = $name;
154                     $data['errors'][$name]['isFormError'] = ($name == '__form');
155                 }
156             }
157         }
158
159         $this->forms[$patForms->getName()] = $data;
160         $this->_tmpl->set('forms', $this->forms);
161
162         return $this->_tmpl;
163     }
164 }
165 ?>
Note: See TracBrowser for help on using the browser.