Subversion Repositories Applications.papyrus

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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