root/trunk/patForms/Collection.php

Revision 319, 4.6 kB (checked in by schst, 3 years ago)

Added getElementByName() and getElementById()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * patForms collection
4  *
5  * $Id$
6  *
7  * @access        protected
8  * @package        patForms
9  * @subpackage    Rules
10  */
11
12 /**
13  * patForms collection
14  *
15  * This is used as a container for several patForms objects.
16  *
17  * @access        protected
18  * @package        patForms
19  * @author        Stephan Schmidt <schst@php-tools.net>
20  * @license        LGPL, see license.txt for details
21  * @link        http://www.php-tools.net
22  */
23 class patForms_Collection
24 {
25    /**
26     * forms in the collection
27     *
28     * @access    public
29     * @var        array
30     */
31     var    $forms = array();
32
33    /**
34     * create a new collection object
35     *
36     * @access    public
37     */
38     function patForms_Collection()
39     {
40     }
41
42    /**
43     * add a new form to the collection
44     *
45     * @access    public
46     * @param    object patForms
47     */
48     function addForm(&$form)
49     {
50         $name = &$form->getName();
51         $this->forms[$name] = &$form;
52     }
53
54    /**
55     * check, whether the collection contains a form
56     *
57     * @access    public
58     * @param    string                name of the form
59     * @return    boolean
60     */
61     function containsForm($name)
62     {
63         if (isset($this->forms[$name])) {
64             return true;
65         }
66         return false;
67     }
68
69    /**
70     * get a form from the collection
71     *
72     * @access    public
73     * @param    string                name of the form
74     * @return    object patForms        form object
75     */
76     function &getForm($name)
77     {
78         if (isset($this->forms[$name])) {
79             return $this->forms[$name];
80         }
81         $null = null;
82         return $null;
83     }
84     
85    /**
86     * sets a renderer object that will be used to render
87     * the form.
88     *
89     * @access    public
90     * @param    object        &$renderer    The renderer object
91     * @return    mixed        $success    True on success, patError object otherwise.
92     * @see        patForms::createRenderer()
93     * @uses        patForms::renderForm()
94     */
95     function setRenderer(&$renderer, $args = array())
96     {
97         if (!is_object($renderer)) {
98             return patErrorManager::raiseError(
99                 PATFORMS_ERROR_INVALID_RENDERER,
100                 'You can only set a patForms_Renderer object with the setRenderer() method, "'.gettype( $renderer ).'" given.'
101             );
102         }
103         
104         foreach (array_keys($this->forms) as $formName) {
105             $this->forms[$formName]->setRenderer($renderer, $args);
106         }
107         return true;
108     }
109     
110    /**
111     * render all forms in the collection
112     *
113     * @access public
114     * @param  mixed        any arguments that should be passed to the renderer
115     */
116     function renderForm($args = null)
117     {
118         foreach (array_keys($this->forms) as $formName) {
119             $this->forms[$formName]->renderForm($args);
120         }
121         return true;
122     }
123
124    /**
125     * Validate all forms in the collection
126     *
127     * @access    public
128     * @param    boolean     Flag to indicate, whether the forms should be validated again, if they already have been validated.
129     * @return    boolean        True if all forms could be validated, false otherwise.
130     */
131     function validateForm($revalidate = false) {
132         $result = true;
133         foreach (array_keys($this->forms) as $formName) {
134             $tmp = $this->forms[$formName]->validateForm($revalidate);
135             $result = $result & $tmp;
136         }
137         return $result;
138     }
139
140    /**
141     * Finalize all forms
142     *
143     * @access    public
144     * @return    bool    $success    Wether all elements could be finalized
145     */
146     function finalizeForm()
147     {
148         $result = true;
149         foreach (array_keys($this->forms) as $formName) {
150             $tmp = $this->forms[$formName]->finalizeForm();
151             $result = $result & $tmp;
152         }
153         return $result;
154     }
155     
156    /**
157     * Get an element from any form
158     *
159     * If more than one form contains an element of the specified name,
160     * only the first will be returned.
161     *
162     * @param    string
163     * @return   mixed
164     */
165     function &getElementByName($name)
166     {
167         if ($name == '__form') {
168             return $this;
169         }
170         foreach (array_keys($this->forms) as $formName) {
171             patErrorManager::pushExpect(PATFORMS_ERROR_ELEMENT_NOT_FOUND);
172             $element = $this->forms[$formName]->getElementByName($name);
173             patErrorManager::popExpect(PATFORMS_ERROR_ELEMENT_NOT_FOUND);
174             if (!patErrorManager::isError($element)) {
175                 return $element;
176             }
177         }
178
179         $error = &patErrorManager::raiseError(
180             PATFORMS_ERROR_ELEMENT_NOT_FOUND,
181             'Element '.$name.' could not be found.'
182         );
183         return $error;
184     }
185
186    /**
187     * Get an element from any form
188     *
189     * If more than one form contains an element with the specified id,
190     * only the first will be returned.
191     *
192     * @param    string
193     * @return   mixed
194     */
195     function &getElementById($id)
196     {
197         foreach (array_keys($this->forms) as $formName) {
198             patErrorManager::pushExpect(PATFORMS_ERROR_ELEMENT_NOT_FOUND);
199             $element = $this->forms[$formName]->getElementById($id);
200             patErrorManager::popExpect(PATFORMS_ERROR_ELEMENT_NOT_FOUND);
201             if (!patErrorManager::isError($element)) {
202                 return $element;
203             }
204         }
205
206         $error = &patErrorManager::raiseError(
207             PATFORMS_ERROR_ELEMENT_NOT_FOUND,
208             'Element '.$name.' could not be found.'
209         );
210         return $error;
211     }
212 }
213 ?>
Note: See TracBrowser for help on using the browser.