Subversion Repositories Applications.papyrus

Rev

Rev 320 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 320 Rev 443
1
<?php
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
6
// | Copyright (c) 1997, 1998, 1999, 2000, 2001 The PHP Group             |
7
// +----------------------------------------------------------------------+
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
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        |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
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   |
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          |
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.               |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
15
// +----------------------------------------------------------------------+
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
16
// | Authors: Adam Daniel <adaniel1@eesus.jnj.com>                        |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
17
// |          Bertrand Mansion <bmansion@mamasam.com>                     |
18
// +----------------------------------------------------------------------+
18
// +----------------------------------------------------------------------+
19
//
19
//
20
// $Id: group.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
20
// $Id: group.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
21
 
21
 
22
require_once("HTML/QuickForm/element.php");
22
require_once("HTML/QuickForm/element.php");
23
 
23
 
24
/**
24
/**
25
 * HTML class for a form element group
25
 * HTML class for a form element group
26
 * 
26
 * 
27
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
27
 * @author       Adam Daniel <adaniel1@eesus.jnj.com>
28
 * @author       Bertrand Mansion <bmansion@mamasam.com>
28
 * @author       Bertrand Mansion <bmansion@mamasam.com>
29
 * @version      1.0
29
 * @version      1.0
30
 * @since        PHP4.04pl1
30
 * @since        PHP4.04pl1
31
 * @access       public
31
 * @access       public
32
 */
32
 */
33
class HTML_QuickForm_group extends HTML_QuickForm_element
33
class HTML_QuickForm_group extends HTML_QuickForm_element
34
{
34
{
35
    // {{{ properties
35
    // {{{ properties
36
        
36
        
37
    /**
37
    /**
38
     * Name of the element
38
     * Name of the element
39
     * @var       string
39
     * @var       string
40
     * @since     1.0
40
     * @since     1.0
41
     * @access    private
41
     * @access    private
42
     */
42
     */
43
    var $_name = '';
43
    var $_name = '';
44
 
44
 
45
    /**
45
    /**
46
     * Array of grouped elements
46
     * Array of grouped elements
47
     * @var       array
47
     * @var       array
48
     * @since     1.0
48
     * @since     1.0
49
     * @access    private
49
     * @access    private
50
     */
50
     */
51
    var $_elements = array();
51
    var $_elements = array();
52
 
52
 
53
    /**
53
    /**
54
     * String to separate elements
54
     * String to separate elements
55
     * @var       mixed
55
     * @var       mixed
56
     * @since     2.5
56
     * @since     2.5
57
     * @access    private
57
     * @access    private
58
     */
58
     */
59
    var $_separator = null;
59
    var $_separator = null;
60
 
60
 
61
    /**
61
    /**
62
     * Required elements in this group
62
     * Required elements in this group
63
     * @var       array
63
     * @var       array
64
     * @since     2.5
64
     * @since     2.5
65
     * @access    private
65
     * @access    private
66
     */
66
     */
67
    var $_required = array();
67
    var $_required = array();
68
 
68
 
69
   /**
69
   /**
70
    * Whether to change elements' names to $groupName[$elementName] or leave them as is 
70
    * Whether to change elements' names to $groupName[$elementName] or leave them as is 
71
    * @var      bool
71
    * @var      bool
72
    * @since    3.0
72
    * @since    3.0
73
    * @access   private
73
    * @access   private
74
    */
74
    */
75
    var $_appendName = true;
75
    var $_appendName = true;
76
 
76
 
77
    // }}}
77
    // }}}
78
    // {{{ constructor
78
    // {{{ constructor
79
 
79
 
80
    /**
80
    /**
81
     * Class constructor
81
     * Class constructor
82
     * 
82
     * 
83
     * @param     string    $elementName    (optional)Group name
83
     * @param     string    $elementName    (optional)Group name
84
     * @param     array     $elementLabel   (optional)Group label
84
     * @param     array     $elementLabel   (optional)Group label
85
     * @param     array     $elements       (optional)Group elements
85
     * @param     array     $elements       (optional)Group elements
86
     * @param     mixed     $separator      (optional)Use a string for one separator,
86
     * @param     mixed     $separator      (optional)Use a string for one separator,
87
     *                                      use an array to alternate the separators.
87
     *                                      use an array to alternate the separators.
88
     * @param     bool      $appendName     (optional)whether to change elements' names to
88
     * @param     bool      $appendName     (optional)whether to change elements' names to
89
     *                                      the form $groupName[$elementName] or leave 
89
     *                                      the form $groupName[$elementName] or leave 
90
     *                                      them as is.
90
     *                                      them as is.
91
     * @since     1.0
91
     * @since     1.0
92
     * @access    public
92
     * @access    public
93
     * @return    void
93
     * @return    void
94
     */
94
     */
95
    function HTML_QuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true)
95
    function HTML_QuickForm_group($elementName=null, $elementLabel=null, $elements=null, $separator=null, $appendName = true)
96
    {
96
    {
97
        $this->HTML_QuickForm_element($elementName, $elementLabel);
97
        $this->HTML_QuickForm_element($elementName, $elementLabel);
98
        $this->_type = 'group';
98
        $this->_type = 'group';
99
        if (isset($elements) && is_array($elements)) {
99
        if (isset($elements) && is_array($elements)) {
100
            $this->setElements($elements);
100
            $this->setElements($elements);
101
        }
101
        }
102
        if (isset($separator)) {
102
        if (isset($separator)) {
103
            $this->_separator = $separator;
103
            $this->_separator = $separator;
104
        }
104
        }
105
        if (isset($appendName)) {
105
        if (isset($appendName)) {
106
            $this->_appendName = $appendName;
106
            $this->_appendName = $appendName;
107
        }
107
        }
108
    } //end constructor
108
    } //end constructor
109
    
109
    
110
    // }}}
110
    // }}}
111
    // {{{ setName()
111
    // {{{ setName()
112
 
112
 
113
    /**
113
    /**
114
     * Sets the group name
114
     * Sets the group name
115
     * 
115
     * 
116
     * @param     string    $name   Group name
116
     * @param     string    $name   Group name
117
     * @since     1.0
117
     * @since     1.0
118
     * @access    public
118
     * @access    public
119
     * @return    void
119
     * @return    void
120
     */
120
     */
121
    function setName($name)
121
    function setName($name)
122
    {
122
    {
123
        $this->_name = $name;
123
        $this->_name = $name;
124
    } //end func setName
124
    } //end func setName
125
    
125
    
126
    // }}}
126
    // }}}
127
    // {{{ getName()
127
    // {{{ getName()
128
 
128
 
129
    /**
129
    /**
130
     * Returns the group name
130
     * Returns the group name
131
     * 
131
     * 
132
     * @since     1.0
132
     * @since     1.0
133
     * @access    public
133
     * @access    public
134
     * @return    string
134
     * @return    string
135
     */
135
     */
136
    function getName()
136
    function getName()
137
    {
137
    {
138
        return $this->_name;
138
        return $this->_name;
139
    } //end func getName
139
    } //end func getName
140
 
140
 
141
    // }}}
141
    // }}}
142
    // {{{ setValue()
142
    // {{{ setValue()
143
 
143
 
144
    /**
144
    /**
145
     * Sets values for group's elements
145
     * Sets values for group's elements
146
     * 
146
     * 
147
     * @param     mixed    Values for group's elements
147
     * @param     mixed    Values for group's elements
148
     * @since     1.0
148
     * @since     1.0
149
     * @access    public
149
     * @access    public
150
     * @return    void
150
     * @return    void
151
     */
151
     */
152
    function setValue($value)
152
    function setValue($value)
153
    {
153
    {
154
        if (empty($this->_elements)) {
-
 
155
            $this->_createElements();
154
        $this->_createElementsIfNotExist();
156
        }
-
 
157
        foreach (array_keys($this->_elements) as $key) {
155
        foreach (array_keys($this->_elements) as $key) {
158
            if (!$this->_appendName) {
156
            if (!$this->_appendName) {
159
                $v = $this->_elements[$key]->_findValue($value);
157
                $v = $this->_elements[$key]->_findValue($value);
160
                if (null !== $v) {
158
                if (null !== $v) {
161
                    $this->_elements[$key]->onQuickFormEvent('setGroupValue', $v, $this);
159
                    $this->_elements[$key]->onQuickFormEvent('setGroupValue', $v, $this);
162
                }
160
                }
163
 
161
 
164
            } else {
162
            } else {
165
                $elementName = $this->_elements[$key]->getName();
163
                $elementName = $this->_elements[$key]->getName();
166
                $index       = (!empty($elementName)) ? $elementName : $key;
164
                $index       = strlen($elementName) ? $elementName : $key;
167
                if (is_array($value)) {
165
                if (is_array($value)) {
168
                    if (isset($value[$index])) {
166
                    if (isset($value[$index])) {
169
                        $this->_elements[$key]->onQuickFormEvent('setGroupValue', $value[$index], $this);
167
                        $this->_elements[$key]->onQuickFormEvent('setGroupValue', $value[$index], $this);
170
                    }
168
                    }
171
                } elseif (isset($value)) {
169
                } elseif (isset($value)) {
172
                    $this->_elements[$key]->onQuickFormEvent('setGroupValue', $value, $this);
170
                    $this->_elements[$key]->onQuickFormEvent('setGroupValue', $value, $this);
173
                }
171
                }
174
            }
172
            }
175
        }
173
        }
176
    } //end func setValue
174
    } //end func setValue
177
    
175
    
178
    // }}}
176
    // }}}
179
    // {{{ getValue()
177
    // {{{ getValue()
180
 
178
 
181
    /**
179
    /**
182
     * Returns the value of the group
180
     * Returns the value of the group
183
     *
181
     *
184
     * @since     1.0
182
     * @since     1.0
185
     * @access    public
183
     * @access    public
186
     * @return    mixed
184
     * @return    mixed
187
     */
185
     */
188
    function getValue()
186
    function getValue()
189
    {
187
    {
190
        $value = null;
188
        $value = null;
191
        foreach (array_keys($this->_elements) as $key) {
189
        foreach (array_keys($this->_elements) as $key) {
192
            $element =& $this->_elements[$key];
190
            $element =& $this->_elements[$key];
193
            switch ($element->getType()) {
191
            switch ($element->getType()) {
194
                case 'radio': 
192
                case 'radio': 
195
                    $v = $element->getChecked()? $element->getValue(): null;
193
                    $v = $element->getChecked()? $element->getValue(): null;
196
                    break;
194
                    break;
197
                case 'checkbox': 
195
                case 'checkbox': 
198
                    $v = $element->getChecked()? true: null;
196
                    $v = $element->getChecked()? true: null;
199
                    break;
197
                    break;
200
                default:
198
                default:
201
                    $v = $element->getValue();
199
                    $v = $element->getValue();
202
            }
200
            }
203
            if (null !== $v) {
201
            if (null !== $v) {
204
                $elementName = $element->getName();
202
                $elementName = $element->getName();
205
                if (is_null($elementName)) {
203
                if (is_null($elementName)) {
206
                    $value = $v;
204
                    $value = $v;
207
                } else {
205
                } else {
208
                    if (!is_array($value)) {
206
                    if (!is_array($value)) {
209
                        $value = is_null($value)? array(): array($value);
207
                        $value = is_null($value)? array(): array($value);
210
                    }
208
                    }
211
                    if ('' == $elementName) {
209
                    if ('' === $elementName) {
212
                        $value[] = $v;
210
                        $value[] = $v;
213
                    } else {
211
                    } else {
214
                        $value[$elementName] = $v;
212
                        $value[$elementName] = $v;
215
                    }
213
                    }
216
                }
214
                }
217
            }
215
            }
218
        }
216
        }
219
        return $value;
217
        return $value;
220
    } // end func getValue
218
    } // end func getValue
221
 
219
 
222
    // }}}
220
    // }}}
223
    // {{{ setElements()
221
    // {{{ setElements()
224
 
222
 
225
    /**
223
    /**
226
     * Sets the grouped elements
224
     * Sets the grouped elements
227
     *
225
     *
228
     * @param     array     $elements   Array of elements
226
     * @param     array     $elements   Array of elements
229
     * @since     1.1
227
     * @since     1.1
230
     * @access    public
228
     * @access    public
231
     * @return    void
229
     * @return    void
232
     */
230
     */
233
    function setElements($elements)
231
    function setElements($elements)
234
    {
232
    {
235
        $this->_elements = array_values($elements);
233
        $this->_elements = array_values($elements);
236
        if ($this->_flagFrozen) {
234
        if ($this->_flagFrozen) {
237
            $this->freeze();
235
            $this->freeze();
238
        }
236
        }
239
    } // end func setElements
237
    } // end func setElements
240
 
238
 
241
    // }}}
239
    // }}}
242
    // {{{ getElements()
240
    // {{{ getElements()
243
 
241
 
244
    /**
242
    /**
245
     * Gets the grouped elements
243
     * Gets the grouped elements
246
     *
244
     *
247
     * @since     2.4
245
     * @since     2.4
248
     * @access    public
246
     * @access    public
249
     * @return    array
247
     * @return    array
250
     */
248
     */
251
    function &getElements()
249
    function &getElements()
252
    {
250
    {
-
 
251
        $this->_createElementsIfNotExist();
253
        return $this->_elements;
252
        return $this->_elements;
254
    } // end func getElements
253
    } // end func getElements
255
 
254
 
256
    // }}}
255
    // }}}
257
    // {{{ getGroupType()
256
    // {{{ getGroupType()
258
 
257
 
259
    /**
258
    /**
260
     * Gets the group type based on its elements
259
     * Gets the group type based on its elements
261
     * Will return 'mixed' if elements contained in the group
260
     * Will return 'mixed' if elements contained in the group
262
     * are of different types.
261
     * are of different types.
263
     *
262
     *
264
     * @access    public
263
     * @access    public
265
     * @return    string    group elements type
264
     * @return    string    group elements type
266
     */
265
     */
267
    function getGroupType()
266
    function getGroupType()
268
    {
267
    {
269
        if (empty($this->_elements)) {
-
 
270
            $this->_createElements();
268
        $this->_createElementsIfNotExist();
271
        }
-
 
272
        $prevType = '';
269
        $prevType = '';
273
        foreach (array_keys($this->_elements) as $key) {
270
        foreach (array_keys($this->_elements) as $key) {
274
            $type = $this->_elements[$key]->getType();
271
            $type = $this->_elements[$key]->getType();
275
            if ($type != $prevType && $prevType != '') {
272
            if ($type != $prevType && $prevType != '') {
276
                return 'mixed';
273
                return 'mixed';
277
            }
274
            }
278
            $prevType = $type;
275
            $prevType = $type;
279
        }
276
        }
280
        return $type;
277
        return $type;
281
    } // end func getGroupType
278
    } // end func getGroupType
282
 
279
 
283
    // }}}
280
    // }}}
284
    // {{{ toHtml()
281
    // {{{ toHtml()
285
 
282
 
286
    /**
283
    /**
287
     * Returns Html for the group
284
     * Returns Html for the group
288
     * 
285
     * 
289
     * @since       1.0
286
     * @since       1.0
290
     * @access      public
287
     * @access      public
291
     * @return      string
288
     * @return      string
292
     */
289
     */
293
    function toHtml()
290
    function toHtml()
294
    {
291
    {
295
        include_once('HTML/QuickForm/Renderer/Default.php');
292
        include_once('HTML/QuickForm/Renderer/Default.php');
296
        $renderer =& new HTML_QuickForm_Renderer_Default();
293
        $renderer =& new HTML_QuickForm_Renderer_Default();
297
        $renderer->setElementTemplate('{element}');
294
        $renderer->setElementTemplate('{element}');
298
        $this->accept($renderer);
295
        $this->accept($renderer);
299
        return $renderer->toHtml();
296
        return $renderer->toHtml();
300
    } //end func toHtml
297
    } //end func toHtml
301
    
298
    
302
    // }}}
299
    // }}}
303
    // {{{ getElementName()
300
    // {{{ getElementName()
304
 
301
 
305
    /**
302
    /**
306
     * Returns the element name inside the group such as found in the html form
303
     * Returns the element name inside the group such as found in the html form
307
     * 
304
     * 
308
     * @param     mixed     $index  Element name or element index in the group
305
     * @param     mixed     $index  Element name or element index in the group
309
     * @since     3.0
306
     * @since     3.0
310
     * @access    public
307
     * @access    public
311
     * @return    mixed     string with element name, false if not found
308
     * @return    mixed     string with element name, false if not found
312
     */
309
     */
313
    function getElementName($index)
310
    function getElementName($index)
314
    {
311
    {
315
        if (empty($this->_elements)) {
-
 
316
            $this->_createElements();
312
        $this->_createElementsIfNotExist();
317
        }
-
 
318
        $elementName = false;
313
        $elementName = false;
319
        if (is_int($index) && isset($this->_elements[$index])) {
314
        if (is_int($index) && isset($this->_elements[$index])) {
320
            $elementName = $this->_elements[$index]->getName();
315
            $elementName = $this->_elements[$index]->getName();
321
            if (isset($elementName) && $elementName == '') {
316
            if (isset($elementName) && $elementName == '') {
322
                $elementName = $index;
317
                $elementName = $index;
323
            }
318
            }
324
            if ($this->_appendName) {
319
            if ($this->_appendName) {
325
                if (is_null($elementName)) {
320
                if (is_null($elementName)) {
326
                    $elementName = $this->getName();
321
                    $elementName = $this->getName();
327
                } else {
322
                } else {
328
                    $elementName = $this->getName().'['.$elementName.']';
323
                    $elementName = $this->getName().'['.$elementName.']';
329
                }
324
                }
330
            }
325
            }
331
 
326
 
332
        } elseif (is_string($index)) {
327
        } elseif (is_string($index)) {
333
            foreach (array_keys($this->_elements) as $key) {
328
            foreach (array_keys($this->_elements) as $key) {
334
                $elementName = $this->_elements[$key]->getName();
329
                $elementName = $this->_elements[$key]->getName();
335
                if ($index == $elementName) {
330
                if ($index == $elementName) {
336
                    if ($this->_appendName) {
331
                    if ($this->_appendName) {
337
                        $elementName = $this->getName().'['.$elementName.']';
332
                        $elementName = $this->getName().'['.$elementName.']';
338
                    }
333
                    }
339
                    break;
334
                    break;
340
                } elseif ($this->_appendName && $this->getName().'['.$elementName.']' == $index) {
335
                } elseif ($this->_appendName && $this->getName().'['.$elementName.']' == $index) {
341
                    break;
336
                    break;
342
                }
337
                }
343
            }
338
            }
344
        }
339
        }
345
        return $elementName;
340
        return $elementName;
346
    } //end func getElementName
341
    } //end func getElementName
347
 
342
 
348
    // }}}
343
    // }}}
349
    // {{{ getFrozenHtml()
344
    // {{{ getFrozenHtml()
350
 
345
 
351
    /**
346
    /**
352
     * Returns the value of field without HTML tags
347
     * Returns the value of field without HTML tags
353
     * 
348
     * 
354
     * @since     1.3
349
     * @since     1.3
355
     * @access    public
350
     * @access    public
356
     * @return    string
351
     * @return    string
357
     */
352
     */
358
    function getFrozenHtml()
353
    function getFrozenHtml()
359
    {
354
    {
360
        $flags = array();
355
        $flags = array();
361
        if (empty($this->_elements)) {
-
 
362
            $this->_createElements();
356
        $this->_createElementsIfNotExist();
363
        }
-
 
364
        foreach (array_keys($this->_elements) as $key) {
357
        foreach (array_keys($this->_elements) as $key) {
365
            if (false === ($flags[$key] = $this->_elements[$key]->isFrozen())) {
358
            if (false === ($flags[$key] = $this->_elements[$key]->isFrozen())) {
366
                $this->_elements[$key]->freeze();
359
                $this->_elements[$key]->freeze();
367
            }
360
            }
368
        }
361
        }
369
        $html = $this->toHtml();
362
        $html = $this->toHtml();
370
        foreach (array_keys($this->_elements) as $key) {
363
        foreach (array_keys($this->_elements) as $key) {
371
            if (!$flags[$key]) {
364
            if (!$flags[$key]) {
372
                $this->_elements[$key]->unfreeze();
365
                $this->_elements[$key]->unfreeze();
373
            }
366
            }
374
        }
367
        }
375
        return $html;
368
        return $html;
376
    } //end func getFrozenHtml
369
    } //end func getFrozenHtml
377
 
370
 
378
    // }}}
371
    // }}}
379
    // {{{ onQuickFormEvent()
372
    // {{{ onQuickFormEvent()
380
 
373
 
381
    /**
374
    /**
382
     * Called by HTML_QuickForm whenever form event is made on this element
375
     * Called by HTML_QuickForm whenever form event is made on this element
383
     *
376
     *
384
     * @param     string    $event  Name of event
377
     * @param     string    $event  Name of event
385
     * @param     mixed     $arg    event arguments
378
     * @param     mixed     $arg    event arguments
386
     * @param     object    $caller calling object
379
     * @param     object    $caller calling object
387
     * @since     1.0
380
     * @since     1.0
388
     * @access    public
381
     * @access    public
389
     * @return    void
382
     * @return    void
390
     */
383
     */
391
    function onQuickFormEvent($event, $arg, &$caller)
384
    function onQuickFormEvent($event, $arg, &$caller)
392
    {
385
    {
393
        switch ($event) {
386
        switch ($event) {
394
            case 'updateValue':
387
            case 'updateValue':
395
                if (empty($this->_elements)) {
-
 
396
                    $this->_createElements();
388
                $this->_createElementsIfNotExist();
397
                }
-
 
398
                foreach (array_keys($this->_elements) as $key) {
389
                foreach (array_keys($this->_elements) as $key) {
399
                    if ($this->_appendName) {
390
                    if ($this->_appendName) {
400
                        $elementName = $this->_elements[$key]->getName();
391
                        $elementName = $this->_elements[$key]->getName();
401
                        if (is_null($elementName)) {
392
                        if (is_null($elementName)) {
402
                            $this->_elements[$key]->setName($this->getName());
393
                            $this->_elements[$key]->setName($this->getName());
403
                        } elseif ('' == $elementName) {
394
                        } elseif ('' === $elementName) {
404
                            $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
395
                            $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
405
                        } else {
396
                        } else {
406
                            $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
397
                            $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
407
                        }
398
                        }
408
                    }
399
                    }
409
                    $this->_elements[$key]->onQuickFormEvent('updateValue', $arg, $caller);
400
                    $this->_elements[$key]->onQuickFormEvent('updateValue', $arg, $caller);
410
                    if ($this->_appendName) {
401
                    if ($this->_appendName) {
411
                        $this->_elements[$key]->setName($elementName);
402
                        $this->_elements[$key]->setName($elementName);
412
                    }
403
                    }
413
                }
404
                }
414
                break;
405
                break;
415
 
406
 
416
            default:
407
            default:
417
                parent::onQuickFormEvent($event, $arg, $caller);
408
                parent::onQuickFormEvent($event, $arg, $caller);
418
        }
409
        }
419
        return true;
410
        return true;
420
    } // end func onQuickFormEvent
411
    } // end func onQuickFormEvent
421
 
412
 
422
    // }}}
413
    // }}}
423
    // {{{ accept()
414
    // {{{ accept()
424
 
415
 
425
   /**
416
   /**
426
    * Accepts a renderer
417
    * Accepts a renderer
427
    *
418
    *
428
    * @param object     An HTML_QuickForm_Renderer object
419
    * @param object     An HTML_QuickForm_Renderer object
429
    * @param bool       Whether a group is required
420
    * @param bool       Whether a group is required
430
    * @param string     An error message associated with a group
421
    * @param string     An error message associated with a group
431
    * @access public
422
    * @access public
432
    * @return void 
423
    * @return void 
433
    */
424
    */
434
    function accept(&$renderer, $required = false, $error = null)
425
    function accept(&$renderer, $required = false, $error = null)
435
    {
426
    {
436
        if (empty($this->_elements)) {
-
 
437
            $this->_createElements();
427
        $this->_createElementsIfNotExist();
438
        }
-
 
439
        $renderer->startGroup($this, $required, $error);
428
        $renderer->startGroup($this, $required, $error);
440
        $name = $this->getName();
429
        $name = $this->getName();
441
        foreach (array_keys($this->_elements) as $key) {
430
        foreach (array_keys($this->_elements) as $key) {
442
            $element =& $this->_elements[$key];
431
            $element =& $this->_elements[$key];
443
            
432
            
444
            if ($this->_appendName) {
433
            if ($this->_appendName) {
445
                $elementName = $element->getName();
434
                $elementName = $element->getName();
446
                if (isset($elementName)) {
435
                if (isset($elementName)) {
447
                    $element->setName($name . '['. (strlen($elementName)? $elementName: $key) .']');
436
                    $element->setName($name . '['. (strlen($elementName)? $elementName: $key) .']');
448
                } else {
437
                } else {
449
                    $element->setName($name);
438
                    $element->setName($name);
450
                }
439
                }
451
            }
440
            }
452
 
441
 
453
            $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
442
            $required = !$element->isFrozen() && in_array($element->getName(), $this->_required);
454
 
443
 
455
            $element->accept($renderer, $required);
444
            $element->accept($renderer, $required);
456
 
445
 
457
            // restore the element's name
446
            // restore the element's name
458
            if ($this->_appendName) {
447
            if ($this->_appendName) {
459
                $element->setName($elementName);
448
                $element->setName($elementName);
460
            }
449
            }
461
        }
450
        }
462
        $renderer->finishGroup($this);
451
        $renderer->finishGroup($this);
463
    } // end func accept
452
    } // end func accept
464
 
453
 
465
    // }}}
454
    // }}}
466
    // {{{ exportValue()
455
    // {{{ exportValue()
467
 
456
 
468
   /**
457
   /**
469
    * As usual, to get the group's value we access its elements and call
458
    * As usual, to get the group's value we access its elements and call
470
    * their exportValue() methods
459
    * their exportValue() methods
471
    */
460
    */
472
    function exportValue(&$submitValues, $assoc = false)
461
    function exportValue(&$submitValues, $assoc = false)
473
    {
462
    {
474
        $value = null;
463
        $value = null;
475
        foreach (array_keys($this->_elements) as $key) {
464
        foreach (array_keys($this->_elements) as $key) {
476
            $elementName = $this->_elements[$key]->getName();
465
            $elementName = $this->_elements[$key]->getName();
477
            if ($this->_appendName) {
466
            if ($this->_appendName) {
478
                if (is_null($elementName)) {
467
                if (is_null($elementName)) {
479
                    $this->_elements[$key]->setName($this->getName());
468
                    $this->_elements[$key]->setName($this->getName());
480
                } elseif ('' == $elementName) {
469
                } elseif ('' === $elementName) {
481
                    $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
470
                    $this->_elements[$key]->setName($this->getName() . '[' . $key . ']');
482
                } else {
471
                } else {
483
                    $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
472
                    $this->_elements[$key]->setName($this->getName() . '[' . $elementName . ']');
484
                }
473
                }
485
            }
474
            }
486
            $v = $this->_elements[$key]->exportValue($submitValues, $assoc);
475
            $v = $this->_elements[$key]->exportValue($submitValues, $assoc);
487
            if ($this->_appendName) {
476
            if ($this->_appendName) {
488
                $this->_elements[$key]->setName($elementName);
477
                $this->_elements[$key]->setName($elementName);
489
            }
478
            }
490
            if (null !== $v) {
479
            if (null !== $v) {
491
                // Make $value an array, we will use it like one
480
                // Make $value an array, we will use it like one
492
                if (null === $value) {
481
                if (null === $value) {
493
                    $value = array();
482
                    $value = array();
494
                }
483
                }
495
                if ($assoc) {
484
                if ($assoc) {
496
                    // just like HTML_QuickForm::exportValues()
485
                    // just like HTML_QuickForm::exportValues()
497
                    $value = HTML_QuickForm::arrayMerge($value, $v);
486
                    $value = HTML_QuickForm::arrayMerge($value, $v);
498
                } else {
487
                } else {
499
                    // just like getValue(), but should work OK every time here
488
                    // just like getValue(), but should work OK every time here
500
                    if (is_null($elementName)) {
489
                    if (is_null($elementName)) {
501
                        $value = $v;
490
                        $value = $v;
502
                    } elseif ('' == $elementName) {
491
                    } elseif ('' === $elementName) {
503
                        $value[] = $v;
492
                        $value[] = $v;
504
                    } else {
493
                    } else {
505
                        $value[$elementName] = $v;
494
                        $value[$elementName] = $v;
506
                    }
495
                    }
507
                }
496
                }
508
            }
497
            }
509
        }
498
        }
510
        // do not pass the value through _prepareValue, we took care of this already
499
        // do not pass the value through _prepareValue, we took care of this already
511
        return $value;
500
        return $value;
512
    }
501
    }
513
 
502
 
514
    // }}}
503
    // }}}
515
    // {{{ _createElements()
504
    // {{{ _createElements()
516
 
505
 
517
   /**
506
   /**
518
    * Creates the group's elements.
507
    * Creates the group's elements.
519
    * 
508
    * 
520
    * This should be overriden by child classes that need to create their 
509
    * This should be overriden by child classes that need to create their 
521
    * elements. The method will be called automatically when needed, calling
510
    * elements. The method will be called automatically when needed, calling
522
    * it from the constructor is discouraged as the constructor is usually
511
    * it from the constructor is discouraged as the constructor is usually
523
    * called _twice_ on element creation, first time with _no_ parameters.
512
    * called _twice_ on element creation, first time with _no_ parameters.
524
    * 
513
    * 
525
    * @access private
514
    * @access private
526
    * @abstract
515
    * @abstract
527
    */
516
    */
528
    function _createElements()
517
    function _createElements()
529
    {
518
    {
530
        // abstract
519
        // abstract
531
    }
520
    }
-
 
521
 
-
 
522
    // }}}
-
 
523
    // {{{ _createElementsIfNotExist()
-
 
524
 
-
 
525
   /**
-
 
526
    * A wrapper around _createElements()
-
 
527
    *
-
 
528
    * This method calls _createElements() if the group's _elements array
-
 
529
    * is empty. It also performs some updates, e.g. freezes the created
-
 
530
    * elements if the group is already frozen.
-
 
531
    *
-
 
532
    * @access private
-
 
533
    */
-
 
534
    function _createElementsIfNotExist()
-
 
535
    {
-
 
536
        if (empty($this->_elements)) {
-
 
537
            $this->_createElements();
-
 
538
            if ($this->_flagFrozen) {
-
 
539
                $this->freeze();
-
 
540
            }
-
 
541
        }
-
 
542
    }
532
 
543
 
533
    // }}}
544
    // }}}
534
    // {{{ freeze()
545
    // {{{ freeze()
535
 
546
 
536
    function freeze()
547
    function freeze()
537
    {
548
    {
538
        parent::freeze();
549
        parent::freeze();
539
        foreach (array_keys($this->_elements) as $key) {
550
        foreach (array_keys($this->_elements) as $key) {
540
            $this->_elements[$key]->freeze();
551
            $this->_elements[$key]->freeze();
541
        }
552
        }
542
    }
553
    }
543
 
554
 
544
    // }}}
555
    // }}}
545
    // {{{ unfreeze()
556
    // {{{ unfreeze()
546
 
557
 
547
    function unfreeze()
558
    function unfreeze()
548
    {
559
    {
549
        parent::unfreeze();
560
        parent::unfreeze();
550
        foreach (array_keys($this->_elements) as $key) {
561
        foreach (array_keys($this->_elements) as $key) {
551
            $this->_elements[$key]->unfreeze();
562
            $this->_elements[$key]->unfreeze();
552
        }
563
        }
553
    }
564
    }
554
 
565
 
555
    // }}}
566
    // }}}
556
    // {{{ setPersistantFreeze()
567
    // {{{ setPersistantFreeze()
557
 
568
 
558
    function setPersistantFreeze($persistant = false)
569
    function setPersistantFreeze($persistant = false)
559
    {
570
    {
560
        parent::setPersistantFreeze($persistant);
571
        parent::setPersistantFreeze($persistant);
561
        foreach (array_keys($this->_elements) as $key) {
572
        foreach (array_keys($this->_elements) as $key) {
562
            $this->_elements[$key]->setPersistantFreeze($persistant);
573
            $this->_elements[$key]->setPersistantFreeze($persistant);
563
        }
574
        }
564
    }
575
    }
565
 
576
 
566
    // }}}
577
    // }}}
567
} //end class HTML_QuickForm_group
578
} //end class HTML_QuickForm_group
568
?>
579
?>