~chroot64bit/zivios/gentoo-experimental

« back to all changes in this revision

Viewing changes to application/library/Zend/Form.php

  • Committer: Mustafa A. Hashmi
  • Date: 2008-12-04 13:32:21 UTC
  • Revision ID: mhashmi@zivios.org-20081204133221-0nd1trunwevijj38
Inclusion of new installation framework with ties to zend layout and dojo layout

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Zend Framework
 
4
 *
 
5
 * LICENSE
 
6
 *
 
7
 * This source file is subject to the new BSD license that is bundled
 
8
 * with this package in the file LICENSE.txt.
 
9
 * It is also available through the world-wide-web at this URL:
 
10
 * http://framework.zend.com/license/new-bsd
 
11
 * If you did not receive a copy of the license and are unable to
 
12
 * obtain it through the world-wide-web, please send an email
 
13
 * to license@zend.com so we can send you a copy immediately.
 
14
 *
 
15
 * @category   Zend
 
16
 * @package    Zend_Form
 
17
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
18
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
19
 */
 
20
 
 
21
/** Zend_Validate_Interface */
 
22
require_once 'Zend/Validate/Interface.php';
 
23
 
 
24
/**
 
25
 * Zend_Form
 
26
 * 
 
27
 * @category   Zend
 
28
 * @package    Zend_Form
 
29
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 
30
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 
31
 * @version    $Id: Form.php 12787 2008-11-23 14:17:44Z matthew $
 
32
 */
 
33
class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
 
34
{
 
35
    /**#@+
 
36
     * Plugin loader type constants
 
37
     */
 
38
    const DECORATOR = 'DECORATOR';
 
39
    const ELEMENT = 'ELEMENT';
 
40
    /**#@-*/
 
41
 
 
42
    /**#@+
 
43
     * Method type constants
 
44
     */
 
45
    const METHOD_DELETE = 'delete';
 
46
    const METHOD_GET    = 'get';
 
47
    const METHOD_POST   = 'post';
 
48
    const METHOD_PUT    = 'put';
 
49
    /**#@-*/
 
50
 
 
51
    /**#@+
 
52
     * Encoding type constants
 
53
     */
 
54
    const ENCTYPE_URLENCODED = 'application/x-www-form-urlencoded';
 
55
    const ENCTYPE_MULTIPART  = 'multipart/form-data';
 
56
    /**#@-*/
 
57
 
 
58
    /**
 
59
     * Form metadata and attributes
 
60
     * @var array
 
61
     */
 
62
    protected $_attribs = array();
 
63
 
 
64
    /**
 
65
     * Decorators for rendering
 
66
     * @var array
 
67
     */
 
68
    protected $_decorators = array();
 
69
 
 
70
    /**
 
71
     * Default display group class
 
72
     * @var string
 
73
     */
 
74
    protected $_defaultDisplayGroupClass = 'Zend_Form_DisplayGroup';
 
75
 
 
76
    /**
 
77
     * Form description
 
78
     * @var string
 
79
     */
 
80
    protected $_description;
 
81
 
 
82
    /**
 
83
     * Should we disable loading the default decorators?
 
84
     * @var bool
 
85
     */
 
86
    protected $_disableLoadDefaultDecorators = false;
 
87
 
 
88
    /**
 
89
     * Display group prefix paths
 
90
     * @var array
 
91
     */
 
92
    protected $_displayGroupPrefixPaths = array();
 
93
 
 
94
    /**
 
95
     * Groups of elements grouped for display purposes
 
96
     * @var array
 
97
     */
 
98
    protected $_displayGroups = array();
 
99
 
 
100
    /**
 
101
     * Prefix paths to use when creating elements
 
102
     * @var array
 
103
     */
 
104
    protected $_elementPrefixPaths = array();
 
105
 
 
106
    /**
 
107
     * Form elements
 
108
     * @var array
 
109
     */
 
110
    protected $_elements = array();
 
111
 
 
112
    /**
 
113
     * Array to which elements belong (if any)
 
114
     * @var string
 
115
     */
 
116
    protected $_elementsBelongTo;
 
117
 
 
118
    /**
 
119
     * Custom form-level error messages
 
120
     * @var array
 
121
     */
 
122
    protected $_errorMessages = array();
 
123
 
 
124
    /**
 
125
     * Are there errors in the form?
 
126
     * @var bool
 
127
     */
 
128
    protected $_errorsExist = false;
 
129
 
 
130
    /**
 
131
     * Form order
 
132
     * @var int|null
 
133
     */
 
134
    protected $_formOrder;
 
135
 
 
136
    /**
 
137
     * Whether or not form elements are members of an array
 
138
     * @var bool
 
139
     */
 
140
    protected $_isArray = false;
 
141
 
 
142
    /**
 
143
     * Form legend
 
144
     * @var string
 
145
     */
 
146
    protected $_legend;
 
147
 
 
148
    /**
 
149
     * Plugin loaders
 
150
     * @var array
 
151
     */
 
152
    protected $_loaders = array();
 
153
 
 
154
    /**
 
155
     * Allowed form methods
 
156
     * @var array
 
157
     */
 
158
    protected $_methods = array('delete', 'get', 'post', 'put');
 
159
 
 
160
    /**
 
161
     * Order in which to display and iterate elements
 
162
     * @var array
 
163
     */
 
164
    protected $_order = array();
 
165
 
 
166
    /**
 
167
     * Whether internal order has been updated or not
 
168
     * @var bool
 
169
     */
 
170
    protected $_orderUpdated = false;
 
171
 
 
172
    /**
 
173
     * Sub form prefix paths
 
174
     * @var array
 
175
     */
 
176
    protected $_subFormPrefixPaths = array();
 
177
 
 
178
    /**
 
179
     * Sub forms
 
180
     * @var array
 
181
     */
 
182
    protected $_subForms = array();
 
183
 
 
184
    /**
 
185
     * @var Zend_Translate
 
186
     */
 
187
    protected $_translator;
 
188
 
 
189
    /**
 
190
     * Global default translation adapter
 
191
     * @var Zend_Translate
 
192
     */
 
193
    protected static $_translatorDefault;
 
194
 
 
195
    /**
 
196
     * is the translator disabled?
 
197
     * @var bool
 
198
     */
 
199
    protected $_translatorDisabled = false;
 
200
 
 
201
    /**
 
202
     * @var Zend_View_Interface
 
203
     */
 
204
    protected $_view;
 
205
 
 
206
    /**
 
207
     * Constructor
 
208
     *
 
209
     * Registers form view helper as decorator
 
210
     * 
 
211
     * @param mixed $options 
 
212
     * @return void
 
213
     */
 
214
    public function __construct($options = null)
 
215
    {
 
216
        if (is_array($options)) {
 
217
            $this->setOptions($options);
 
218
        } elseif ($options instanceof Zend_Config) {
 
219
            $this->setConfig($options);
 
220
        }
 
221
 
 
222
        // Extensions...
 
223
        $this->init();
 
224
 
 
225
        $this->loadDefaultDecorators();
 
226
    }
 
227
 
 
228
    /**
 
229
     * Clone form object and all children
 
230
     * 
 
231
     * @return void
 
232
     */
 
233
    public function __clone()
 
234
    {
 
235
        $elements = array();
 
236
        foreach ($this->getElements() as $name => $element) {
 
237
            $elements[] = clone $element;
 
238
        }
 
239
        $this->setElements($elements);
 
240
 
 
241
        $subForms = array();
 
242
        foreach ($this->getSubForms() as $name => $subForm) {
 
243
            $subForms[$name] = clone $subForm;
 
244
        }
 
245
        $this->setSubForms($subForms);
 
246
 
 
247
        $displayGroups = array();
 
248
        foreach ($this->_displayGroups as $group)  {
 
249
            $clone    = clone $group;
 
250
            $elements = array();
 
251
            foreach ($clone->getElements() as $name => $e) {
 
252
                $elements[] = $this->getElement($name);
 
253
            }
 
254
            $clone->setElements($elements);
 
255
            $displayGroups[] = $clone;
 
256
        }
 
257
        $this->setDisplayGroups($displayGroups);
 
258
    }
 
259
 
 
260
    /**
 
261
     * Reset values of form
 
262
     * 
 
263
     * @return Zend_Form
 
264
     */
 
265
    public function reset()
 
266
    {
 
267
        foreach ($this->getElements() as $element) {
 
268
            $element->setValue(null);
 
269
        }
 
270
        foreach ($this->getSubForms() as $subForm) {
 
271
            $subForm->reset();
 
272
        }
 
273
 
 
274
        return $this;
 
275
    }
 
276
 
 
277
    /**
 
278
     * Initialize form (used by extending classes)
 
279
     * 
 
280
     * @return void
 
281
     */
 
282
    public function init()
 
283
    {
 
284
    }
 
285
 
 
286
    /**
 
287
     * Set form state from options array
 
288
     * 
 
289
     * @param  array $options 
 
290
     * @return Zend_Form
 
291
     */
 
292
    public function setOptions(array $options)
 
293
    {
 
294
        if (isset($options['prefixPath'])) {
 
295
            $this->addPrefixPaths($options['prefixPath']);
 
296
            unset($options['prefixPath']);
 
297
        }
 
298
 
 
299
        if (isset($options['elementPrefixPath'])) {                          
 
300
            $this->addElementPrefixPaths($options['elementPrefixPath']);      
 
301
            unset($options['elementPrefixPath']);                             
 
302
        }
 
303
 
 
304
        if (isset($options['displayGroupPrefixPath'])) {                          
 
305
            $this->addDisplayGroupPrefixPaths($options['displayGroupPrefixPath']);      
 
306
            unset($options['displayGroupPrefixPath']);                             
 
307
        }
 
308
 
 
309
        if (isset($options['elements'])) {
 
310
            $this->setElements($options['elements']);
 
311
            unset($options['elements']);
 
312
        }
 
313
 
 
314
        if (isset($options['elementDecorators'])) {
 
315
            $elementDecorators = $options['elementDecorators'];
 
316
            unset($options['elementDecorators']);
 
317
        }
 
318
 
 
319
        if (isset($options['defaultDisplayGroupClass'])) {
 
320
            $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
 
321
            unset($options['defaultDisplayGroupClass']);
 
322
        }
 
323
 
 
324
        if (isset($options['displayGroupDecorators'])) {
 
325
            $displayGroupDecorators = $options['displayGroupDecorators'];
 
326
            unset($options['displayGroupDecorators']);
 
327
        }
 
328
 
 
329
        if (isset($options['elementsBelongTo'])) {
 
330
            $elementsBelongTo = $options['elementsBelongTo'];
 
331
            unset($options['elementsBelongTo']);
 
332
        }
 
333
 
 
334
        if (isset($options['attribs'])) {
 
335
            $this->addAttribs($options['attribs']);
 
336
            unset($options['attribs']);
 
337
        }
 
338
 
 
339
        $forbidden = array(
 
340
            'Options', 'Config', 'PluginLoader', 'SubForms', 'View', 'Translator',
 
341
            'Attrib', 'Default',
 
342
        );
 
343
 
 
344
        foreach ($options as $key => $value) {
 
345
            $normalized = ucfirst($key);
 
346
            if (in_array($normalized, $forbidden)) {
 
347
                continue;
 
348
            }
 
349
 
 
350
            $method = 'set' . $normalized;
 
351
            if (method_exists($this, $method)) {
 
352
                $this->$method($value);
 
353
            } else {
 
354
                $this->setAttrib($key, $value);
 
355
            }
 
356
        }
 
357
 
 
358
        if (isset($elementDecorators)) {
 
359
            $this->setElementDecorators($elementDecorators);
 
360
        }
 
361
 
 
362
        if (isset($displayGroupDecorators)) {
 
363
            $this->setDisplayGroupDecorators($displayGroupDecorators);
 
364
        }
 
365
 
 
366
        if (isset($elementsBelongTo)) {
 
367
            $this->setElementsBelongTo($elementsBelongTo);
 
368
        }
 
369
 
 
370
        return $this;
 
371
    }
 
372
 
 
373
    /**
 
374
     * Set form state from config object
 
375
     * 
 
376
     * @param  Zend_Config $config 
 
377
     * @return Zend_Form
 
378
     */
 
379
    public function setConfig(Zend_Config $config)
 
380
    {
 
381
        return $this->setOptions($config->toArray());
 
382
    }
 
383
 
 
384
 
 
385
    // Loaders 
 
386
 
 
387
    /**
 
388
     * Set plugin loaders for use with decorators and elements
 
389
     * 
 
390
     * @param  Zend_Loader_PluginLoader_Interface $loader 
 
391
     * @param  string $type 'decorator' or 'element'
 
392
     * @return Zend_Form
 
393
     * @throws Zend_Form_Exception on invalid type
 
394
     */
 
395
    public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type = null)
 
396
    {
 
397
        $type = strtoupper($type);
 
398
        switch ($type) {
 
399
            case self::DECORATOR:
 
400
            case self::ELEMENT:
 
401
                $this->_loaders[$type] = $loader;
 
402
                return $this;
 
403
            default:
 
404
                require_once 'Zend/Form/Exception.php';
 
405
                throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
 
406
        }
 
407
    }
 
408
 
 
409
    /**
 
410
     * Retrieve plugin loader for given type
 
411
     *
 
412
     * $type may be one of:
 
413
     * - decorator
 
414
     * - element
 
415
     *
 
416
     * If a plugin loader does not exist for the given type, defaults are 
 
417
     * created.
 
418
     * 
 
419
     * @param  string $type 
 
420
     * @return Zend_Loader_PluginLoader_Interface
 
421
     */
 
422
    public function getPluginLoader($type = null)
 
423
    {
 
424
        $type = strtoupper($type);
 
425
        if (!isset($this->_loaders[$type])) {
 
426
            switch ($type) {
 
427
                case self::DECORATOR:
 
428
                    $prefixSegment = 'Form_Decorator';
 
429
                    $pathSegment   = 'Form/Decorator';
 
430
                    break;
 
431
                case self::ELEMENT:
 
432
                    $prefixSegment = 'Form_Element';
 
433
                    $pathSegment   = 'Form/Element';
 
434
                    break;
 
435
                default:
 
436
                    require_once 'Zend/Form/Exception.php';
 
437
                    throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
 
438
            }
 
439
 
 
440
            require_once 'Zend/Loader/PluginLoader.php';
 
441
            $this->_loaders[$type] = new Zend_Loader_PluginLoader(
 
442
                array('Zend_' . $prefixSegment . '_' => 'Zend/' . $pathSegment . '/')
 
443
            );
 
444
        }
 
445
 
 
446
        return $this->_loaders[$type];
 
447
    }
 
448
 
 
449
    /**
 
450
     * Add prefix path for plugin loader
 
451
     *
 
452
     * If no $type specified, assumes it is a base path for both filters and 
 
453
     * validators, and sets each according to the following rules:
 
454
     * - decorators: $prefix = $prefix . '_Decorator'
 
455
     * - elements: $prefix = $prefix . '_Element'
 
456
     *
 
457
     * Otherwise, the path prefix is set on the appropriate plugin loader.
 
458
     *
 
459
     * If $type is 'decorators', sets the path in the decorator plugin loader 
 
460
     * for all elements. Additionally, if no $type is provided, 
 
461
     * {@link Zend_Form_Element::addPrefixPath()} is called on each element.
 
462
     * 
 
463
     * @param  string $path 
 
464
     * @return Zend_Form
 
465
     * @throws Zend_Form_Exception for invalid type
 
466
     */
 
467
    public function addPrefixPath($prefix, $path, $type = null) 
 
468
    {
 
469
        $type = strtoupper($type);
 
470
        switch ($type) {
 
471
            case self::DECORATOR:
 
472
            case self::ELEMENT:
 
473
                $loader = $this->getPluginLoader($type);
 
474
                $loader->addPrefixPath($prefix, $path);
 
475
                return $this;
 
476
            case null:
 
477
                $prefix = rtrim($prefix, '_');
 
478
                $path   = rtrim($path, DIRECTORY_SEPARATOR);
 
479
                foreach (array(self::DECORATOR, self::ELEMENT) as $type) {
 
480
                    $cType        = ucfirst(strtolower($type));
 
481
                    $pluginPath   = $path . DIRECTORY_SEPARATOR . $cType . DIRECTORY_SEPARATOR;
 
482
                    $pluginPrefix = $prefix . '_' . $cType;
 
483
                    $loader       = $this->getPluginLoader($type);
 
484
                    $loader->addPrefixPath($pluginPrefix, $pluginPath);
 
485
                }
 
486
                return $this;
 
487
            default:
 
488
                require_once 'Zend/Form/Exception.php';
 
489
                throw new Zend_Form_Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
 
490
        }
 
491
    }
 
492
 
 
493
    /**
 
494
     * Add many prefix paths at once
 
495
     * 
 
496
     * @param  array $spec 
 
497
     * @return Zend_Form
 
498
     */
 
499
    public function addPrefixPaths(array $spec)
 
500
    {
 
501
        if (isset($spec['prefix']) && isset($spec['path'])) {
 
502
            return $this->addPrefixPath($spec['prefix'], $spec['path']);
 
503
        } 
 
504
        foreach ($spec as $type => $paths) {
 
505
            if (is_numeric($type) && is_array($paths)) {
 
506
                $type = null;
 
507
                if (isset($paths['prefix']) && isset($paths['path'])) {
 
508
                    if (isset($paths['type'])) {
 
509
                        $type = $paths['type'];
 
510
                    }
 
511
                    $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
 
512
                }
 
513
            } elseif (!is_numeric($type)) {
 
514
                if (!isset($paths['prefix']) || !isset($paths['path'])) {
 
515
                    continue;
 
516
                }
 
517
                $this->addPrefixPath($paths['prefix'], $paths['path'], $type);
 
518
            }
 
519
        }
 
520
        return $this;
 
521
    }
 
522
 
 
523
    /**
 
524
     * Add prefix path for all elements
 
525
     * 
 
526
     * @param  string $prefix 
 
527
     * @param  string $path 
 
528
     * @param  string $type 
 
529
     * @return Zend_Form
 
530
     */
 
531
    public function addElementPrefixPath($prefix, $path, $type = null)
 
532
    {
 
533
        $this->_elementPrefixPaths[] = array(
 
534
            'prefix' => $prefix, 
 
535
            'path'   => $path, 
 
536
            'type'   => $type,
 
537
        );
 
538
 
 
539
        foreach ($this->getElements() as $element) {
 
540
            $element->addPrefixPath($prefix, $path, $type);
 
541
        }
 
542
 
 
543
        foreach ($this->getSubForms() as $subForm) {
 
544
            $subForm->addElementPrefixPath($prefix, $path, $type);
 
545
        }
 
546
 
 
547
        return $this;
 
548
    }
 
549
 
 
550
    /**
 
551
     * Add prefix paths for all elements
 
552
     * 
 
553
     * @param  array $spec 
 
554
     * @return Zend_Form
 
555
     */
 
556
    public function addElementPrefixPaths(array $spec)
 
557
    {
 
558
        $this->_elementPrefixPaths = $this->_elementPrefixPaths + $spec;
 
559
 
 
560
        foreach ($this->getElements() as $element) {
 
561
            $element->addPrefixPaths($spec);
 
562
        }
 
563
 
 
564
        return $this;
 
565
    }
 
566
 
 
567
    /**
 
568
     * Add prefix path for all display groups
 
569
     * 
 
570
     * @param  string $prefix 
 
571
     * @param  string $path 
 
572
     * @return Zend_Form
 
573
     */
 
574
    public function addDisplayGroupPrefixPath($prefix, $path)
 
575
    {
 
576
        $this->_displayGroupPrefixPaths[] = array(
 
577
            'prefix' => $prefix, 
 
578
            'path'   => $path,
 
579
        );
 
580
 
 
581
        foreach ($this->getDisplayGroups() as $group) {
 
582
            $group->addPrefixPath($prefix, $path);
 
583
        }
 
584
 
 
585
        return $this;
 
586
    }
 
587
 
 
588
    /**
 
589
     * Add multiple display group prefix paths at once
 
590
     * 
 
591
     * @param  array $spec 
 
592
     * @return Zend_Form
 
593
     */
 
594
    public function addDisplayGroupPrefixPaths(array $spec)
 
595
    {
 
596
        foreach ($spec as $key => $value) {
 
597
            if (is_string($value) && !is_numeric($key)) {
 
598
                $this->addDisplayGroupPrefixPath($key, $value);
 
599
                continue;
 
600
            }
 
601
 
 
602
            if (is_string($value) && is_numeric($key)) {
 
603
                continue;
 
604
            }
 
605
 
 
606
            if (is_array($value)) {
 
607
                $count = count($value);
 
608
                if (array_keys($value) === range(0, $count - 1)) {
 
609
                    if ($count < 2) {
 
610
                        continue;
 
611
                    }
 
612
                    $prefix = array_shift($value);
 
613
                    $path   = array_shift($value);
 
614
                    $this->addDisplayGroupPrefixPath($prefix, $path);
 
615
                    continue;
 
616
                }
 
617
                if (array_key_exists('prefix', $value) && array_key_exists('path', $value)) {
 
618
                    $this->addDisplayGroupPrefixPath($value['prefix'], $value['path']);
 
619
                }
 
620
            }
 
621
        }
 
622
        return $this;
 
623
    }
 
624
 
 
625
    // Form metadata:
 
626
    
 
627
    /**
 
628
     * Set form attribute
 
629
     * 
 
630
     * @param  string $key 
 
631
     * @param  mixed $value 
 
632
     * @return Zend_Form
 
633
     */
 
634
    public function setAttrib($key, $value)
 
635
    {
 
636
        $key = (string) $key;
 
637
        $this->_attribs[$key] = $value;
 
638
        return $this;
 
639
    }
 
640
 
 
641
    /**
 
642
     * Add multiple form attributes at once
 
643
     * 
 
644
     * @param  array $attribs 
 
645
     * @return Zend_Form
 
646
     */
 
647
    public function addAttribs(array $attribs)
 
648
    {
 
649
        foreach ($attribs as $key => $value) {
 
650
            $this->setAttrib($key, $value);
 
651
        }
 
652
        return $this;
 
653
    }
 
654
 
 
655
    /**
 
656
     * Set multiple form attributes at once
 
657
     *
 
658
     * Overwrites any previously set attributes.
 
659
     * 
 
660
     * @param  array $attribs 
 
661
     * @return Zend_Form
 
662
     */
 
663
    public function setAttribs(array $attribs)
 
664
    {
 
665
        $this->clearAttribs();
 
666
        return $this->addAttribs($attribs);
 
667
    }
 
668
 
 
669
    /**
 
670
     * Retrieve a single form attribute
 
671
     * 
 
672
     * @param  string $key 
 
673
     * @return mixed
 
674
     */
 
675
    public function getAttrib($key)
 
676
    {
 
677
        $key = (string) $key;
 
678
        if (!isset($this->_attribs[$key])) {
 
679
            return null;
 
680
        }
 
681
 
 
682
        return $this->_attribs[$key];
 
683
    }
 
684
 
 
685
    /**
 
686
     * Retrieve all form attributes/metadata
 
687
     * 
 
688
     * @return array
 
689
     */
 
690
    public function getAttribs()
 
691
    {
 
692
        return $this->_attribs;
 
693
    }
 
694
 
 
695
    /**
 
696
     * Remove attribute
 
697
     * 
 
698
     * @param  string $key 
 
699
     * @return bool
 
700
     */
 
701
    public function removeAttrib($key)
 
702
    {
 
703
        if (isset($this->_attribs[$key])) {
 
704
            unset($this->_attribs[$key]);
 
705
            return true;
 
706
        }
 
707
 
 
708
        return false;
 
709
    }
 
710
 
 
711
    /**
 
712
     * Clear all form attributes
 
713
     * 
 
714
     * @return Zend_Form
 
715
     */
 
716
    public function clearAttribs()
 
717
    {
 
718
        $this->_attribs = array();
 
719
        return $this;
 
720
    }
 
721
 
 
722
    /**
 
723
     * Set form action
 
724
     * 
 
725
     * @param  string $action 
 
726
     * @return Zend_Form
 
727
     */
 
728
    public function setAction($action)
 
729
    {
 
730
        return $this->setAttrib('action', (string) $action);
 
731
    }
 
732
 
 
733
    /**
 
734
     * Get form action
 
735
     *
 
736
     * Sets default to '' if not set.
 
737
     * 
 
738
     * @return string
 
739
     */
 
740
    public function getAction()
 
741
    {
 
742
        $action = $this->getAttrib('action');
 
743
        if (null === $action) {
 
744
            $action = '';
 
745
            $this->setAction($action);
 
746
        }
 
747
        return $action;
 
748
    }
 
749
 
 
750
    /**
 
751
     * Set form method
 
752
     *
 
753
     * Only values in {@link $_methods()} allowed
 
754
     * 
 
755
     * @param  string $method 
 
756
     * @return Zend_Form
 
757
     * @throws Zend_Form_Exception
 
758
     */
 
759
    public function setMethod($method)
 
760
    {
 
761
        $method = strtolower($method);
 
762
        if (!in_array($method, $this->_methods)) {
 
763
            require_once 'Zend/Form/Exception.php';
 
764
            throw new Zend_Form_Exception(sprintf('"%s" is an invalid form method', $method));
 
765
        }
 
766
        $this->setAttrib('method', $method);
 
767
        return $this;
 
768
    }
 
769
 
 
770
    /**
 
771
     * Retrieve form method
 
772
     * 
 
773
     * @return string
 
774
     */
 
775
    public function getMethod()
 
776
    {
 
777
        if (null === ($method = $this->getAttrib('method'))) {
 
778
            $method = self::METHOD_POST;
 
779
            $this->setAttrib('method', $method);
 
780
        }
 
781
        return strtolower($method);
 
782
    }
 
783
 
 
784
    /**
 
785
     * Set encoding type
 
786
     * 
 
787
     * @param  string $value 
 
788
     * @return Zend_Form
 
789
     */
 
790
    public function setEnctype($value)
 
791
    {
 
792
        $this->setAttrib('enctype', $value);
 
793
        return $this;
 
794
    }
 
795
 
 
796
    /**
 
797
     * Get encoding type
 
798
     * 
 
799
     * @return string
 
800
     */
 
801
    public function getEnctype()
 
802
    {
 
803
        if (null === ($enctype = $this->getAttrib('enctype'))) {
 
804
            $enctype = self::ENCTYPE_URLENCODED;
 
805
            $this->setAttrib('enctype', $enctype);
 
806
        }
 
807
        return $this->getAttrib('enctype');
 
808
    }
 
809
 
 
810
    /**
 
811
     * Filter a name to only allow valid variable characters
 
812
     * 
 
813
     * @param  string $value 
 
814
     * @param  bool $allowBrackets
 
815
     * @return string
 
816
     */
 
817
    public function filterName($value, $allowBrackets = false)
 
818
    {
 
819
        $charset = '^a-zA-Z0-9_\x7f-\xff';
 
820
        if ($allowBrackets) {
 
821
            $charset .= '\[\]';
 
822
        }
 
823
        return preg_replace('/[' . $charset . ']/', '', (string) $value);
 
824
    }
 
825
 
 
826
    /**
 
827
     * Set form name
 
828
     * 
 
829
     * @param  string $name 
 
830
     * @return Zend_Form
 
831
     */
 
832
    public function setName($name)
 
833
    {
 
834
        $name = $this->filterName($name);
 
835
        if (('0' !== $name) && empty($name)) {
 
836
            require_once 'Zend/Form/Exception.php';
 
837
            throw new Zend_Form_Exception('Invalid name provided; must contain only valid variable characters and be non-empty');
 
838
        }
 
839
 
 
840
        return $this->setAttrib('name', $name);
 
841
    }
 
842
 
 
843
    /**
 
844
     * Get name attribute
 
845
     * 
 
846
     * @return null|string
 
847
     */
 
848
    public function getName()
 
849
    {
 
850
        return $this->getAttrib('name');
 
851
    }
 
852
 
 
853
    /**
 
854
     * Get fully qualified name
 
855
     *
 
856
     * Places name as subitem of array and/or appends brackets.
 
857
     * 
 
858
     * @return string
 
859
     */
 
860
    public function getFullyQualifiedName()
 
861
    {
 
862
        return $this->getName();
 
863
    }
 
864
 
 
865
    /**
 
866
     * Get element id
 
867
     * 
 
868
     * @return string
 
869
     */
 
870
    public function getId()
 
871
    {
 
872
        if (null !== ($id = $this->getAttrib('id'))) {
 
873
            return $id;
 
874
        }
 
875
 
 
876
        $id = $this->getFullyQualifiedName();
 
877
 
 
878
        // Bail early if no array notation detected
 
879
        if (!strstr($id, '[')) {
 
880
            return $id;
 
881
        }
 
882
 
 
883
        // Strip array notation
 
884
        if ('[]' == substr($id, -2)) {
 
885
            $id = substr($id, 0, strlen($id) - 2);
 
886
        }
 
887
        $id = str_replace('][', '-', $id);
 
888
        $id = str_replace(array(']', '['), '-', $id);
 
889
        $id = trim($id, '-');
 
890
 
 
891
        return $id;
 
892
    }
 
893
 
 
894
    /**
 
895
     * Set form legend
 
896
     * 
 
897
     * @param  string $value 
 
898
     * @return Zend_Form
 
899
     */
 
900
    public function setLegend($value)
 
901
    {
 
902
        $this->_legend = (string) $value;
 
903
        return $this;
 
904
    }
 
905
 
 
906
    /**
 
907
     * Get form legend
 
908
     * 
 
909
     * @return string
 
910
     */
 
911
    public function getLegend()
 
912
    {
 
913
        return $this->_legend;
 
914
    }
 
915
 
 
916
    /**
 
917
     * Set form description
 
918
     * 
 
919
     * @param  string $value 
 
920
     * @return Zend_Form
 
921
     */
 
922
    public function setDescription($value)
 
923
    {
 
924
        $this->_description = (string) $value;
 
925
        return $this;
 
926
    }
 
927
 
 
928
    /**
 
929
     * Retrieve form description
 
930
     * 
 
931
     * @return string
 
932
     */
 
933
    public function getDescription()
 
934
    {
 
935
        return $this->_description;
 
936
    }
 
937
 
 
938
    /**
 
939
     * Set form order
 
940
     * 
 
941
     * @param  int $index 
 
942
     * @return Zend_Form
 
943
     */
 
944
    public function setOrder($index)
 
945
    {
 
946
        $this->_formOrder = (int) $index;
 
947
        return $this;
 
948
    }
 
949
 
 
950
    /**
 
951
     * Get form order
 
952
     * 
 
953
     * @return int|null
 
954
     */
 
955
    public function getOrder()
 
956
    {
 
957
        return $this->_formOrder;
 
958
    }
 
959
 
 
960
    // Element interaction: 
 
961
 
 
962
    /**
 
963
     * Add a new element
 
964
     *
 
965
     * $element may be either a string element type, or an object of type 
 
966
     * Zend_Form_Element. If a string element type is provided, $name must be 
 
967
     * provided, and $options may be optionally provided for configuring the 
 
968
     * element.
 
969
     *
 
970
     * If a Zend_Form_Element is provided, $name may be optionally provided, 
 
971
     * and any provided $options will be ignored.
 
972
     * 
 
973
     * @param  string|Zend_Form_Element $element 
 
974
     * @param  string $name 
 
975
     * @param  array|Zend_Config $options 
 
976
     * @return Zend_Form
 
977
     */
 
978
    public function addElement($element, $name = null, $options = null)
 
979
    {
 
980
        if (is_string($element)) {
 
981
            if (null === $name) {
 
982
                require_once 'Zend/Form/Exception.php';
 
983
                throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
 
984
            }
 
985
 
 
986
            $this->_elements[$name] = $this->createElement($element, $name, $options);
 
987
        } elseif ($element instanceof Zend_Form_Element) {
 
988
            $prefixPaths              = array();
 
989
            $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
 
990
            if (!empty($this->_elementPrefixPaths)) {
 
991
                $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
 
992
            }
 
993
 
 
994
            if (null === $name) {
 
995
                $name = $element->getName();
 
996
            }
 
997
 
 
998
            $this->_elements[$name] = $element;
 
999
            $this->_elements[$name]->addPrefixPaths($prefixPaths);
 
1000
        }
 
1001
 
 
1002
        $this->_order[$name] = $this->_elements[$name]->getOrder();
 
1003
        $this->_orderUpdated = true;
 
1004
        $this->_setElementsBelongTo($name);
 
1005
 
 
1006
        return $this;
 
1007
    }
 
1008
 
 
1009
    /**
 
1010
     * Create an element
 
1011
     *
 
1012
     * Acts as a factory for creating elements. Elements created with this 
 
1013
     * method will not be attached to the form, but will contain element 
 
1014
     * settings as specified in the form object (including plugin loader 
 
1015
     * prefix paths, default decorators, etc.).
 
1016
     * 
 
1017
     * @param  string $type 
 
1018
     * @param  string $name 
 
1019
     * @param  array|Zend_Config $options 
 
1020
     * @return Zend_Form_Element
 
1021
     */
 
1022
    public function createElement($type, $name, $options = null)
 
1023
    {
 
1024
        if (!is_string($type)) {
 
1025
            require_once 'Zend/Form/Exception.php';
 
1026
            throw new Zend_Form_Exception('Element type must be a string indicating type');
 
1027
        }
 
1028
 
 
1029
        if (!is_string($name)) {
 
1030
            require_once 'Zend/Form/Exception.php';
 
1031
            throw new Zend_Form_Exception('Element name must be a string');
 
1032
        }
 
1033
 
 
1034
        $prefixPaths              = array();
 
1035
        $prefixPaths['decorator'] = $this->getPluginLoader('decorator')->getPaths();
 
1036
        if (!empty($this->_elementPrefixPaths)) {
 
1037
            $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
 
1038
        }
 
1039
 
 
1040
        if ($options instanceof Zend_Config) {
 
1041
            $options = $options->toArray();
 
1042
        }
 
1043
 
 
1044
        if ((null === $options) || !is_array($options)) {
 
1045
            $options = array('prefixPath' => $prefixPaths);
 
1046
        } elseif (is_array($options)) {
 
1047
            if (array_key_exists('prefixPath', $options)) {
 
1048
                $options['prefixPath'] = array_merge($prefixPaths, $options['prefixPath']);
 
1049
            } else {
 
1050
                $options['prefixPath'] = $prefixPaths;
 
1051
            }
 
1052
        }
 
1053
 
 
1054
        $class = $this->getPluginLoader(self::ELEMENT)->load($type);
 
1055
        $element = new $class($name, $options);
 
1056
 
 
1057
        return $element;
 
1058
    }
 
1059
 
 
1060
    /**
 
1061
     * Add multiple elements at once
 
1062
     * 
 
1063
     * @param  array $elements 
 
1064
     * @return Zend_Form
 
1065
     */
 
1066
    public function addElements(array $elements)
 
1067
    {
 
1068
        foreach ($elements as $key => $spec) {
 
1069
            $name = null;
 
1070
            if (!is_numeric($key)) {
 
1071
                $name = $key;
 
1072
            }
 
1073
 
 
1074
            if (is_string($spec) || ($spec instanceof Zend_Form_Element)) {
 
1075
                $this->addElement($spec, $name);
 
1076
                continue;
 
1077
            }
 
1078
 
 
1079
            if (is_array($spec)) {
 
1080
                $argc = count($spec);
 
1081
                $options = array();
 
1082
                if (isset($spec['type'])) {
 
1083
                    $type = $spec['type'];
 
1084
                    if (isset($spec['name'])) {
 
1085
                        $name = $spec['name'];
 
1086
                    }
 
1087
                    if (isset($spec['options'])) {
 
1088
                        $options = $spec['options'];
 
1089
                    }
 
1090
                    $this->addElement($type, $name, $options);
 
1091
                } else {
 
1092
                    switch ($argc) {
 
1093
                        case 0:
 
1094
                            continue;
 
1095
                        case (1 <= $argc):
 
1096
                            $type = array_shift($spec);
 
1097
                        case (2 <= $argc):
 
1098
                            if (null === $name) {
 
1099
                                $name = array_shift($spec);
 
1100
                            } else {
 
1101
                                $options = array_shift($spec);
 
1102
                            }
 
1103
                        case (3 <= $argc):
 
1104
                            if (empty($options)) {
 
1105
                                $options = array_shift($spec);
 
1106
                            }
 
1107
                        default:
 
1108
                            $this->addElement($type, $name, $options);
 
1109
                    }
 
1110
                }
 
1111
            }
 
1112
        }
 
1113
        return $this;
 
1114
    }
 
1115
 
 
1116
    /**
 
1117
     * Set form elements (overwrites existing elements)
 
1118
     * 
 
1119
     * @param  array $elements 
 
1120
     * @return Zend_Form
 
1121
     */
 
1122
    public function setElements(array $elements)
 
1123
    {
 
1124
        $this->clearElements();
 
1125
        return $this->addElements($elements);
 
1126
    }
 
1127
 
 
1128
    /**
 
1129
     * Retrieve a single element
 
1130
     * 
 
1131
     * @param  string $name 
 
1132
     * @return Zend_Form_Element|null
 
1133
     */
 
1134
    public function getElement($name)
 
1135
    {
 
1136
        if (array_key_exists($name, $this->_elements)) {
 
1137
            return $this->_elements[$name];
 
1138
        }
 
1139
        return null;
 
1140
    }
 
1141
 
 
1142
    /**
 
1143
     * Retrieve all elements
 
1144
     * 
 
1145
     * @return array
 
1146
     */
 
1147
    public function getElements()
 
1148
    {
 
1149
        return $this->_elements;
 
1150
    }
 
1151
 
 
1152
    /**
 
1153
     * Remove element
 
1154
     * 
 
1155
     * @param  string $name 
 
1156
     * @return boolean
 
1157
     */
 
1158
    public function removeElement($name)
 
1159
    {
 
1160
        $name = (string) $name;
 
1161
        if (isset($this->_elements[$name])) {
 
1162
            unset($this->_elements[$name]);
 
1163
            if (array_key_exists($name, $this->_order)) {
 
1164
                unset($this->_order[$name]);
 
1165
                $this->_orderUpdated = true;
 
1166
            } else {
 
1167
                foreach ($this->_displayGroups as $group) {
 
1168
                    if (null !== $group->getElement($name)) {
 
1169
                        $group->removeElement($name);
 
1170
                    }
 
1171
                }
 
1172
            }
 
1173
            return true;
 
1174
        }
 
1175
 
 
1176
        return false;
 
1177
    }
 
1178
 
 
1179
    /**
 
1180
     * Remove all form elements
 
1181
     * 
 
1182
     * @return Zend_Form
 
1183
     */
 
1184
    public function clearElements()
 
1185
    {
 
1186
        foreach (array_keys($this->_elements) as $key) {
 
1187
            if (array_key_exists($key, $this->_order)) {
 
1188
                unset($this->_order[$key]);
 
1189
            }
 
1190
        }
 
1191
        $this->_elements     = array();
 
1192
        $this->_orderUpdated = true;
 
1193
        return $this;
 
1194
    }
 
1195
 
 
1196
    /**
 
1197
     * Set default values for elements
 
1198
     *
 
1199
     * If an element's name is not specified as a key in the array, its value 
 
1200
     * is set to null.
 
1201
     * 
 
1202
     * @param  array $defaults 
 
1203
     * @return Zend_Form
 
1204
     */
 
1205
    public function setDefaults(array $defaults)
 
1206
    {
 
1207
        foreach ($this->getElements() as $name => $element) {
 
1208
            if (array_key_exists($name, $defaults)) {
 
1209
                $this->setDefault($name, $defaults[$name]);
 
1210
            }
 
1211
        }
 
1212
        foreach ($this->getSubForms() as $name => $form) {
 
1213
            if (array_key_exists($name, $defaults)) {
 
1214
                $form->setDefaults($defaults[$name]);
 
1215
            } else {
 
1216
                $form->setDefaults($defaults);
 
1217
            }
 
1218
        }
 
1219
        return $this;
 
1220
    }
 
1221
 
 
1222
    /**
 
1223
     * Set default value for an element
 
1224
     * 
 
1225
     * @param  string $name 
 
1226
     * @param  mixed $value 
 
1227
     * @return Zend_Form
 
1228
     */
 
1229
    public function setDefault($name, $value)
 
1230
    {
 
1231
        $name = (string) $name;
 
1232
        if ($element = $this->getElement($name)) {
 
1233
            $element->setValue($value);
 
1234
        } else {
 
1235
            if (is_scalar($value)) {
 
1236
                foreach ($this->getSubForms() as $subForm) {
 
1237
                    $subForm->setDefault($name, $value);
 
1238
                }
 
1239
            } elseif (is_array($value) && ($subForm = $this->getSubForm($name))) {
 
1240
                $subForm->setDefaults($value);
 
1241
            } 
 
1242
        }
 
1243
        return $this;
 
1244
    }
 
1245
 
 
1246
    /**
 
1247
     * Retrieve value for single element
 
1248
     * 
 
1249
     * @param  string $name 
 
1250
     * @return mixed
 
1251
     */
 
1252
    public function getValue($name)
 
1253
    {
 
1254
        if ($element = $this->getElement($name)) {
 
1255
            return $element->getValue();
 
1256
        } 
 
1257
        
 
1258
        if ($subForm = $this->getSubForm($name)) {
 
1259
            return $subForm->getValues(true);
 
1260
        } 
 
1261
 
 
1262
        foreach ($this->getSubForms() as $subForm) {
 
1263
            if ($name == $subForm->getElementsBelongTo()) {
 
1264
                return $subForm->getValues(true);
 
1265
            }
 
1266
        }
 
1267
        return null;
 
1268
    }
 
1269
 
 
1270
    /**
 
1271
     * Retrieve all form element values
 
1272
     * 
 
1273
     * @param  bool $suppressArrayNotation
 
1274
     * @return array
 
1275
     */
 
1276
    public function getValues($suppressArrayNotation = false)
 
1277
    {
 
1278
        $values = array();
 
1279
        foreach ($this->getElements() as $key => $element) {
 
1280
            if (!$element->getIgnore()) {
 
1281
                $values[$key] = $element->getValue();
 
1282
            }
 
1283
        }
 
1284
        foreach ($this->getSubForms() as $key => $subForm) {
 
1285
            $fValues = $this->_attachToArray($subForm->getValues(true), $subForm->getElementsBelongTo());
 
1286
            $values = array_merge($values, $fValues);
 
1287
        }
 
1288
 
 
1289
        if (!$suppressArrayNotation && $this->isArray()) {
 
1290
            $values = $this->_attachToArray($values, $this->getElementsBelongTo());
 
1291
        }
 
1292
 
 
1293
        return $values;
 
1294
    }
 
1295
 
 
1296
    /**
 
1297
     * Get unfiltered element value
 
1298
     * 
 
1299
     * @param  string $name 
 
1300
     * @return mixed
 
1301
     */
 
1302
    public function getUnfilteredValue($name)
 
1303
    {
 
1304
        if ($element = $this->getElement($name)) {
 
1305
            return $element->getUnfilteredValue();
 
1306
        }
 
1307
        return null;
 
1308
    }
 
1309
 
 
1310
    /**
 
1311
     * Retrieve all unfiltered element values
 
1312
     * 
 
1313
     * @return array
 
1314
     */
 
1315
    public function getUnfilteredValues()
 
1316
    {
 
1317
        $values = array();
 
1318
        foreach ($this->getElements() as $key => $element) {
 
1319
            $values[$key] = $element->getUnfilteredValue();
 
1320
        }
 
1321
 
 
1322
        return $values;
 
1323
    }
 
1324
 
 
1325
    /**
 
1326
     * Set all elements' filters
 
1327
     * 
 
1328
     * @param  array $filters 
 
1329
     * @return Zend_Form
 
1330
     */
 
1331
    public function setElementFilters(array $filters)
 
1332
    {
 
1333
        foreach ($this->getElements() as $element) {
 
1334
            $element->setFilters($filters);
 
1335
        }
 
1336
        return $this;
 
1337
    }
 
1338
 
 
1339
    /**
 
1340
     * Set name of array elements belong to
 
1341
     * 
 
1342
     * @param  string $array 
 
1343
     * @return Zend_Form
 
1344
     */
 
1345
    public function setElementsBelongTo($array)
 
1346
    {
 
1347
        $origName = $this->getElementsBelongTo();
 
1348
        $name = $this->filterName($array, true);
 
1349
        if (empty($name)) {
 
1350
            $name = null;
 
1351
        }
 
1352
        $this->_elementsBelongTo = $name;
 
1353
 
 
1354
        if (null === $name) {
 
1355
            $this->setIsArray(false);
 
1356
            if (null !== $origName) {
 
1357
                $this->_setElementsBelongTo();
 
1358
            }
 
1359
        } else {
 
1360
            $this->setIsArray(true);
 
1361
            $this->_setElementsBelongTo();
 
1362
        }
 
1363
 
 
1364
        return $this;
 
1365
    }
 
1366
 
 
1367
    /**
 
1368
     * Set array to which elements belong
 
1369
     * 
 
1370
     * @param  string $name Element name
 
1371
     * @return void
 
1372
     */
 
1373
    protected function _setElementsBelongTo($name = null)
 
1374
    {
 
1375
        $array = $this->getElementsBelongTo();
 
1376
 
 
1377
        if (null === $array) {
 
1378
            return;
 
1379
        }
 
1380
 
 
1381
        if (null === $name) {
 
1382
            foreach ($this->getElements() as $element) {
 
1383
                $element->setBelongsTo($array);
 
1384
            }
 
1385
        } else {
 
1386
            if (null !== ($element = $this->getElement($name))) {
 
1387
                $element->setBelongsTo($array);
 
1388
            }
 
1389
        }
 
1390
    }
 
1391
 
 
1392
    /**
 
1393
     * Get name of array elements belong to
 
1394
     * 
 
1395
     * @return string|null
 
1396
     */
 
1397
    public function getElementsBelongTo()
 
1398
    {
 
1399
        if ((null === $this->_elementsBelongTo) && $this->isArray()) {
 
1400
            $name = $this->getName();
 
1401
            if (!empty($name)) {
 
1402
                return $name;
 
1403
            }
 
1404
        }
 
1405
        return $this->_elementsBelongTo;
 
1406
    }
 
1407
 
 
1408
    /**
 
1409
     * Set flag indicating elements belong to array
 
1410
     * 
 
1411
     * @param  bool $flag Value of flag
 
1412
     * @return Zend_Form
 
1413
     */
 
1414
    public function setIsArray($flag)
 
1415
    {
 
1416
        $this->_isArray = (bool) $flag;
 
1417
        return $this;
 
1418
    }
 
1419
 
 
1420
    /**
 
1421
     * Get flag indicating if elements belong to an array
 
1422
     * 
 
1423
     * @return bool
 
1424
     */
 
1425
    public function isArray()
 
1426
    {
 
1427
        return $this->_isArray;
 
1428
    }
 
1429
 
 
1430
    // Element groups: 
 
1431
 
 
1432
    /**
 
1433
     * Add a form group/subform
 
1434
     * 
 
1435
     * @param  Zend_Form $form 
 
1436
     * @param  string $name 
 
1437
     * @param  int $order 
 
1438
     * @return Zend_Form
 
1439
     */
 
1440
    public function addSubForm(Zend_Form $form, $name, $order = null)
 
1441
    {
 
1442
        $name = (string) $name;
 
1443
        foreach ($this->_loaders as $type => $loader) {
 
1444
            $loaderPaths = $loader->getPaths();
 
1445
            foreach ($loaderPaths as $prefix => $paths) {
 
1446
                foreach ($paths as $path) {
 
1447
                    $form->addPrefixPath($prefix, $path, $type);
 
1448
                }
 
1449
            }
 
1450
        }
 
1451
 
 
1452
        if (!empty($this->_elementPrefixPaths)) {
 
1453
            foreach ($this->_elementPrefixPaths as $spec) {
 
1454
                list($prefix, $path, $type) = array_values($spec);
 
1455
                $form->addElementPrefixPath($prefix, $path, $type);
 
1456
            }
 
1457
        }
 
1458
 
 
1459
        if (!empty($this->_displayGroupPrefixPaths)) {
 
1460
            foreach ($this->_displayGroupPrefixPaths as $spec) {
 
1461
                list($prefix, $path) = array_values($spec);
 
1462
                $form->addDisplayGroupPrefixPath($prefix, $path);
 
1463
            }
 
1464
        }
 
1465
 
 
1466
        if (null !== $order) {
 
1467
            $form->setOrder($order);
 
1468
        }
 
1469
 
 
1470
        $form->setName($name);
 
1471
        $this->_subForms[$name] = $form;
 
1472
        $this->_order[$name]    = $order;
 
1473
        $this->_orderUpdated    = true;
 
1474
        return $this;
 
1475
    }
 
1476
 
 
1477
    /**
 
1478
     * Add multiple form subForms/subforms at once
 
1479
     * 
 
1480
     * @param  array $subForms 
 
1481
     * @return Zend_Form
 
1482
     */
 
1483
    public function addSubForms(array $subForms)
 
1484
    {
 
1485
        foreach ($subForms as $key => $spec) {
 
1486
            $name = null;
 
1487
            if (!is_numeric($key)) {
 
1488
                $name = $key;
 
1489
            }
 
1490
 
 
1491
            if ($spec instanceof Zend_Form) {
 
1492
                $this->addSubForm($spec, $name);
 
1493
                continue;
 
1494
            }
 
1495
 
 
1496
            if (is_array($spec)) {
 
1497
                $argc  = count($spec);
 
1498
                $order = null;
 
1499
                switch ($argc) {
 
1500
                    case 0: 
 
1501
                        continue;
 
1502
                    case (1 <= $argc):
 
1503
                        $subForm = array_shift($spec);
 
1504
                    case (2 <= $argc):
 
1505
                        $name  = array_shift($spec);
 
1506
                    case (3 <= $argc):
 
1507
                        $order = array_shift($spec);
 
1508
                    default:
 
1509
                        $this->addSubForm($subForm, $name, $order);
 
1510
                }
 
1511
            }
 
1512
        }
 
1513
        return $this;
 
1514
    }
 
1515
 
 
1516
    /**
 
1517
     * Set multiple form subForms/subforms (overwrites)
 
1518
     * 
 
1519
     * @param  array $subForms 
 
1520
     * @return Zend_Form
 
1521
     */
 
1522
    public function setSubForms(array $subForms)
 
1523
    {
 
1524
        $this->clearSubForms();
 
1525
        return $this->addSubForms($subForms);
 
1526
    }
 
1527
 
 
1528
    /**
 
1529
     * Retrieve a form subForm/subform
 
1530
     * 
 
1531
     * @param  string $name 
 
1532
     * @return Zend_Form|null
 
1533
     */
 
1534
    public function getSubForm($name)
 
1535
    {
 
1536
        $name = (string) $name;
 
1537
        if (isset($this->_subForms[$name])) {
 
1538
            return $this->_subForms[$name];
 
1539
        }
 
1540
        return null;
 
1541
    }
 
1542
 
 
1543
    /**
 
1544
     * Retrieve all form subForms/subforms
 
1545
     * 
 
1546
     * @return array
 
1547
     */
 
1548
    public function getSubForms()
 
1549
    {
 
1550
        return $this->_subForms;
 
1551
    }
 
1552
 
 
1553
    /**
 
1554
     * Remove form subForm/subform
 
1555
     * 
 
1556
     * @param  string $name 
 
1557
     * @return boolean
 
1558
     */
 
1559
    public function removeSubForm($name)
 
1560
    {
 
1561
        $name = (string) $name;
 
1562
        if (array_key_exists($name, $this->_subForms)) {
 
1563
            unset($this->_subForms[$name]);
 
1564
            if (array_key_exists($name, $this->_order)) {
 
1565
                unset($this->_order[$name]);
 
1566
                $this->_orderUpdated = true;
 
1567
            }
 
1568
            return true;
 
1569
        }
 
1570
 
 
1571
        return false;
 
1572
    }
 
1573
 
 
1574
    /**
 
1575
     * Remove all form subForms/subforms
 
1576
     * 
 
1577
     * @return Zend_Form
 
1578
     */
 
1579
    public function clearSubForms()
 
1580
    {
 
1581
        foreach (array_keys($this->_subForms) as $key) {
 
1582
            if (array_key_exists($key, $this->_order)) {
 
1583
                unset($this->_order[$key]);
 
1584
            }
 
1585
        }
 
1586
        $this->_subForms     = array();
 
1587
        $this->_orderUpdated = true;
 
1588
        return $this;
 
1589
    }
 
1590
 
 
1591
 
 
1592
    // Display groups:
 
1593
 
 
1594
    /**
 
1595
     * Set default display group class 
 
1596
     * 
 
1597
     * @param  string $class 
 
1598
     * @return Zend_Form
 
1599
     */
 
1600
    public function setDefaultDisplayGroupClass($class)
 
1601
    {
 
1602
        $this->_defaultDisplayGroupClass = (string) $class;
 
1603
        return $this;
 
1604
    }
 
1605
 
 
1606
    /**
 
1607
     * Retrieve default display group class
 
1608
     * 
 
1609
     * @return string
 
1610
     */
 
1611
    public function getDefaultDisplayGroupClass()
 
1612
    {
 
1613
        return $this->_defaultDisplayGroupClass;
 
1614
    }
 
1615
 
 
1616
    /**
 
1617
     * Add a display group
 
1618
     *
 
1619
     * Groups named elements for display purposes.
 
1620
     *
 
1621
     * If a referenced element does not yet exist in the form, it is omitted.
 
1622
     * 
 
1623
     * @param  array $elements 
 
1624
     * @param  string $name 
 
1625
     * @param  array|Zend_Config $options 
 
1626
     * @return Zend_Form
 
1627
     * @throws Zend_Form_Exception if no valid elements provided
 
1628
     */
 
1629
    public function addDisplayGroup(array $elements, $name, $options = null)
 
1630
    {
 
1631
        $group = array();
 
1632
        foreach ($elements as $element) {
 
1633
            if (isset($this->_elements[$element])) {
 
1634
                $add = $this->getElement($element);
 
1635
                if (null !== $add) {
 
1636
                    unset($this->_order[$element]);
 
1637
                    $group[] = $add;
 
1638
                }
 
1639
            }
 
1640
        }
 
1641
        if (empty($group)) {
 
1642
            require_once 'Zend/Form/Exception.php';
 
1643
            throw new Zend_Form_Exception('No valid elements specified for display group');
 
1644
        }
 
1645
 
 
1646
        $name = (string) $name;
 
1647
 
 
1648
        if (is_array($options)) {
 
1649
            $options['elements'] = $group;
 
1650
        } elseif ($options instanceof Zend_Config) {
 
1651
            $options = $options->toArray();
 
1652
            $options['elements'] = $group;
 
1653
        } else {
 
1654
            $options = array('elements' => $group);
 
1655
        }
 
1656
 
 
1657
        if (isset($options['displayGroupClass'])) {
 
1658
            $class = $options['displayGroupClass'];
 
1659
            unset($options['displayGroupClass']);
 
1660
        } else {
 
1661
            $class = $this->getDefaultDisplayGroupClass();
 
1662
        }
 
1663
 
 
1664
        if (!class_exists($class)) {
 
1665
            require_once 'Zend/Loader.php';
 
1666
            Zend_Loader::loadClass($class);
 
1667
        }
 
1668
        $this->_displayGroups[$name] = new $class(
 
1669
            $name, 
 
1670
            $this->getPluginLoader(self::DECORATOR),
 
1671
            $options
 
1672
        );
 
1673
 
 
1674
        if (!empty($this->_displayGroupPrefixPaths)) {
 
1675
            $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
 
1676
        }
 
1677
 
 
1678
        $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
 
1679
        $this->_orderUpdated = true;
 
1680
        return $this;
 
1681
    }
 
1682
 
 
1683
    /**
 
1684
     * Add a display group object (used with cloning)
 
1685
     * 
 
1686
     * @param  Zend_Form_DisplayGroup $group 
 
1687
     * @param  string|null $name 
 
1688
     * @return Zend_Form
 
1689
     */
 
1690
    protected function _addDisplayGroupObject(Zend_Form_DisplayGroup $group, $name = null)
 
1691
    {
 
1692
        if (null === $name) {
 
1693
            $name = $group->getName();
 
1694
            if (empty($name)) {
 
1695
                require_once 'Zend/Form/Exception.php';
 
1696
                throw new Zend_Form_Exception('Invalid display group added; requires name');
 
1697
            }
 
1698
        }
 
1699
 
 
1700
        $this->_displayGroups[$name] = $group;
 
1701
 
 
1702
        if (!empty($this->_displayGroupPrefixPaths)) {
 
1703
            $this->_displayGroups[$name]->addPrefixPaths($this->_displayGroupPrefixPaths);
 
1704
        }
 
1705
 
 
1706
        $this->_order[$name] = $this->_displayGroups[$name]->getOrder();
 
1707
        $this->_orderUpdated = true;
 
1708
        return $this;
 
1709
    }
 
1710
 
 
1711
    /**
 
1712
     * Add multiple display groups at once
 
1713
     * 
 
1714
     * @param  array $groups 
 
1715
     * @return Zend_Form
 
1716
     */
 
1717
    public function addDisplayGroups(array $groups)
 
1718
    {
 
1719
        foreach ($groups as $key => $spec) {
 
1720
            $name = null;
 
1721
            if (!is_numeric($key)) {
 
1722
                $name = $key;
 
1723
            }
 
1724
 
 
1725
            if ($spec instanceof Zend_Form_DisplayGroup) {
 
1726
                $this->_addDisplayGroupObject($spec);
 
1727
            }
 
1728
 
 
1729
            if (!is_array($spec) || empty($spec)) {
 
1730
                continue;
 
1731
            }
 
1732
 
 
1733
            $argc    = count($spec);
 
1734
            $options = array();
 
1735
 
 
1736
            if (isset($spec['elements'])) {
 
1737
                $elements = $spec['elements'];
 
1738
                if (isset($spec['name'])) {
 
1739
                    $name = $spec['name'];
 
1740
                }
 
1741
                if (isset($spec['options'])) {
 
1742
                    $options = $spec['options'];
 
1743
                }
 
1744
                $this->addDisplayGroup($elements, $name, $options);
 
1745
            } else {
 
1746
                switch ($argc) {
 
1747
                    case (1 <= $argc):
 
1748
                        $elements = array_shift($spec);
 
1749
                        if (!is_array($elements) && (null !== $name)) {
 
1750
                            $elements = array_merge((array) $elements, $spec);
 
1751
                            $this->addDisplayGroup($elements, $name);
 
1752
                            break;
 
1753
                        }
 
1754
                    case (2 <= $argc):
 
1755
                        if (null !== $name) {
 
1756
                            $options = array_shift($spec);
 
1757
                            $this->addDisplayGroup($elements, $name, $options);
 
1758
                            break;
 
1759
                        }
 
1760
                        $name = array_shift($spec);
 
1761
                    case (3 <= $argc):
 
1762
                        $options = array_shift($spec);
 
1763
                    default:
 
1764
                        $this->addDisplayGroup($elements, $name, $options);
 
1765
                }
 
1766
            }
 
1767
        }
 
1768
        return $this;
 
1769
    }
 
1770
 
 
1771
    /**
 
1772
     * Add multiple display groups (overwrites)
 
1773
     * 
 
1774
     * @param  array $groups 
 
1775
     * @return Zend_Form
 
1776
     */
 
1777
    public function setDisplayGroups(array $groups)
 
1778
    {
 
1779
        return $this->clearDisplayGroups()
 
1780
                    ->addDisplayGroups($groups);
 
1781
    }
 
1782
 
 
1783
    /**
 
1784
     * Return a display group
 
1785
     * 
 
1786
     * @param  string $name 
 
1787
     * @return array|null
 
1788
     */
 
1789
    public function getDisplayGroup($name)
 
1790
    {
 
1791
        $name = (string) $name;
 
1792
        if (isset($this->_displayGroups[$name])) {
 
1793
            return $this->_displayGroups[$name];
 
1794
        }
 
1795
 
 
1796
        return null;
 
1797
    }
 
1798
 
 
1799
    /**
 
1800
     * Return all display groups
 
1801
     * 
 
1802
     * @return array
 
1803
     */
 
1804
    public function getDisplayGroups()
 
1805
    {
 
1806
        return $this->_displayGroups;
 
1807
    }
 
1808
 
 
1809
    /**
 
1810
     * Remove a display group by name
 
1811
     * 
 
1812
     * @param  string $name 
 
1813
     * @return boolean
 
1814
     */
 
1815
    public function removeDisplayGroup($name)
 
1816
    {
 
1817
        $name = (string) $name;
 
1818
        if (array_key_exists($name, $this->_displayGroups)) {
 
1819
            foreach ($this->_displayGroups[$name] as $key => $element) {
 
1820
                if (array_key_exists($key, $this->_elements)) {
 
1821
                    $this->_order[$key]  = $element->getOrder();
 
1822
                    $this->_orderUpdated = true;
 
1823
                }
 
1824
            }
 
1825
            unset($this->_displayGroups[$name]);
 
1826
 
 
1827
            if (array_key_exists($name, $this->_order)) {
 
1828
                unset($this->_order[$name]);
 
1829
                $this->_orderUpdated = true;
 
1830
            }
 
1831
            return true;
 
1832
        }
 
1833
 
 
1834
        return false;
 
1835
    }
 
1836
 
 
1837
    /**
 
1838
     * Remove all display groups
 
1839
     * 
 
1840
     * @return Zend_Form
 
1841
     */
 
1842
    public function clearDisplayGroups()
 
1843
    {
 
1844
        foreach ($this->_displayGroups as $key => $group) {
 
1845
            if (array_key_exists($key, $this->_order)) {
 
1846
                unset($this->_order[$key]);
 
1847
            }
 
1848
            foreach ($group as $name => $element) {
 
1849
                if (isset($this->_elements[$name])) {
 
1850
                    $this->_order[$name] = $element->getOrder();
 
1851
                }
 
1852
                $this->_order[$name] = $element->getOrder();
 
1853
            }
 
1854
        }
 
1855
        $this->_displayGroups = array();
 
1856
        $this->_orderUpdated  = true;
 
1857
        return $this;
 
1858
    }
 
1859
 
 
1860
     
 
1861
    // Processing 
 
1862
 
 
1863
    /**
 
1864
     * Populate form
 
1865
     *
 
1866
     * Proxies to {@link setDefaults()}
 
1867
     * 
 
1868
     * @param  array $values 
 
1869
     * @return Zend_Form
 
1870
     */
 
1871
    public function populate(array $values)
 
1872
    {
 
1873
        return $this->setDefaults($values);
 
1874
    }
 
1875
 
 
1876
    /**
 
1877
     * Determine array key name from given value
 
1878
     *
 
1879
     * Given a value such as foo[bar][baz], returns the last element (in this case, 'baz').
 
1880
     * 
 
1881
     * @param  string $value 
 
1882
     * @return string
 
1883
     */
 
1884
    protected function _getArrayName($value)
 
1885
    {
 
1886
        if (empty($value) || !is_string($value)) {
 
1887
            return $value;
 
1888
        }
 
1889
 
 
1890
        if (!strstr($value, '[')) {
 
1891
            return $value;
 
1892
        }
 
1893
 
 
1894
        $endPos = strlen($value) - 1;
 
1895
        if (']' != $value[$endPos]) {
 
1896
            return $value;
 
1897
        }
 
1898
 
 
1899
        $start = strrpos($value, '[') + 1;
 
1900
        $name = substr($value, $start, $endPos - $start);
 
1901
        return $name;
 
1902
    }
 
1903
    
 
1904
    /**
 
1905
     * Extract the value by walking the array using given array path.
 
1906
     *
 
1907
     * Given an array path such as foo[bar][baz], returns the value of the last
 
1908
     * element (in this case, 'baz').
 
1909
     * 
 
1910
     * @param  array $value Array to walk
 
1911
     * @param  string $arrayPath Array notation path of the part to extract
 
1912
     * @return string
 
1913
     */
 
1914
    protected function _dissolveArrayValue($value, $arrayPath)
 
1915
    {
 
1916
        // As long as we have more levels
 
1917
        while ($arrayPos = strpos($arrayPath, '[')) {
 
1918
            // Get the next key in the path
 
1919
            $arrayKey = trim(substr($arrayPath, 0, $arrayPos), ']');
 
1920
 
 
1921
            // Set the potentially final value or the next search point in the array
 
1922
            if (isset($value[$arrayKey])) {
 
1923
                $value = $value[$arrayKey];
 
1924
            }
 
1925
            
 
1926
            // Set the next search point in the path
 
1927
            $arrayPath = trim(substr($arrayPath, $arrayPos + 1), ']');
 
1928
        }
 
1929
 
 
1930
        if (isset($value[$arrayPath])) {
 
1931
            $value = $value[$arrayPath];
 
1932
        }
 
1933
 
 
1934
        return $value;
 
1935
    }
 
1936
 
 
1937
    /**
 
1938
     * Converts given arrayPath to an array and attaches given value at the end of it.
 
1939
     *
 
1940
     * @param  mixed $value The value to attach
 
1941
     * @param  string $arrayPath Given array path to convert and attach to.
 
1942
     * @return array 
 
1943
     */
 
1944
    protected function _attachToArray($value, $arrayPath)
 
1945
    {
 
1946
        // As long as we have more levels
 
1947
        while ($arrayPos = strrpos($arrayPath, '[')) {
 
1948
            // Get the next key in the path
 
1949
            $arrayKey = trim(substr($arrayPath, $arrayPos + 1), ']');
 
1950
 
 
1951
            // Attach
 
1952
            $value = array($arrayKey => $value);
 
1953
            
 
1954
            // Set the next search point in the path
 
1955
            $arrayPath = trim(substr($arrayPath, 0, $arrayPos), ']');
 
1956
        }
 
1957
 
 
1958
        $value = array($arrayPath => $value);
 
1959
 
 
1960
        return $value;
 
1961
    }
 
1962
 
 
1963
    /**
 
1964
     * Validate the form
 
1965
     * 
 
1966
     * @param  array $data 
 
1967
     * @return boolean
 
1968
     */
 
1969
    public function isValid($data)
 
1970
    {
 
1971
        if (!is_array($data)) {
 
1972
            require_once 'Zend/Form/Exception.php';
 
1973
            throw new Zend_Form_Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
 
1974
        }
 
1975
        $translator = $this->getTranslator();
 
1976
        $valid      = true;
 
1977
 
 
1978
        if ($this->isArray()) {
 
1979
            $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
 
1980
        }
 
1981
 
 
1982
        foreach ($this->getElements() as $key => $element) {
 
1983
            $element->setTranslator($translator);
 
1984
            if (!isset($data[$key])) {
 
1985
                $valid = $element->isValid(null, $data) && $valid;
 
1986
            } else {
 
1987
                $valid = $element->isValid($data[$key], $data) && $valid;
 
1988
            }
 
1989
        }
 
1990
        foreach ($this->getSubForms() as $key => $form) {
 
1991
            $form->setTranslator($translator);
 
1992
            if (isset($data[$key])) {
 
1993
                $valid = $form->isValid($data[$key]) && $valid;
 
1994
            } else {
 
1995
                $valid = $form->isValid($data) && $valid;
 
1996
            }
 
1997
        }
 
1998
 
 
1999
        $this->_errorsExist = !$valid;
 
2000
        return $valid;
 
2001
    }
 
2002
 
 
2003
    /**
 
2004
     * Validate a partial form
 
2005
     *
 
2006
     * Does not check for required flags.
 
2007
     * 
 
2008
     * @param  array $data 
 
2009
     * @return boolean
 
2010
     */
 
2011
    public function isValidPartial(array $data)
 
2012
    {
 
2013
        if ($this->isArray()) {
 
2014
            $data = $this->_dissolveArrayValue($data, $this->getElementsBelongTo());
 
2015
        }
 
2016
 
 
2017
        $translator        = $this->getTranslator();
 
2018
        $valid             = true;
 
2019
        $validatedSubForms = array();
 
2020
 
 
2021
        foreach ($data as $key => $value) {
 
2022
            if (null !== ($element = $this->getElement($key))) {
 
2023
                if (null !== $translator) {
 
2024
                    $element->setTranslator($translator);
 
2025
                }
 
2026
                $valid = $element->isValid($value, $data) && $valid;
 
2027
            } elseif (null !== ($subForm = $this->getSubForm($key))) {
 
2028
                if (null !== $translator) {
 
2029
                    $subForm->setTranslator($translator);
 
2030
                }
 
2031
                $valid = $subForm->isValidPartial($data[$key]) && $valid;
 
2032
                $validatedSubForms[] = $key;
 
2033
            } 
 
2034
        }
 
2035
        foreach ($this->getSubForms() as $key => $subForm) {
 
2036
            if (!in_array($key, $validatedSubForms)) {
 
2037
                if (null !== $translator) {
 
2038
                    $subForm->setTranslator($translator);
 
2039
                }
 
2040
 
 
2041
                $valid = $subForm->isValidPartial($data) && $valid;
 
2042
            }
 
2043
        }
 
2044
 
 
2045
        $this->_errorsExist = !$valid;
 
2046
        return $valid;
 
2047
    }
 
2048
 
 
2049
    /**
 
2050
     * Process submitted AJAX data
 
2051
     *
 
2052
     * Checks if provided $data is valid, via {@link isValidPartial()}. If so, 
 
2053
     * it returns JSON-encoded boolean true. If not, it returns JSON-encoded 
 
2054
     * error messages (as returned by {@link getMessages()}).
 
2055
     * 
 
2056
     * @param  array $data 
 
2057
     * @return string JSON-encoded boolean true or error messages
 
2058
     */
 
2059
    public function processAjax(array $data)
 
2060
    {
 
2061
        require_once 'Zend/Json.php';
 
2062
        if ($this->isValidPartial($data)) {
 
2063
            return Zend_Json::encode(true);
 
2064
        }
 
2065
        $messages = $this->getMessages();
 
2066
        return Zend_Json::encode($messages);
 
2067
    }
 
2068
 
 
2069
    /**
 
2070
     * Add a custom error message to return in the event of failed validation
 
2071
     * 
 
2072
     * @param  string $message 
 
2073
     * @return Zend_Form
 
2074
     */
 
2075
    public function addErrorMessage($message)
 
2076
    {
 
2077
        $this->_errorMessages[] = (string) $message;
 
2078
        return $this;
 
2079
    }
 
2080
 
 
2081
    /**
 
2082
     * Add multiple custom error messages to return in the event of failed validation
 
2083
     * 
 
2084
     * @param  array $messages 
 
2085
     * @return Zend_Form
 
2086
     */
 
2087
    public function addErrorMessages(array $messages)
 
2088
    {
 
2089
        foreach ($messages as $message) {
 
2090
            $this->addErrorMessage($message);
 
2091
        }
 
2092
        return $this;
 
2093
    }
 
2094
 
 
2095
    /**
 
2096
     * Same as addErrorMessages(), but clears custom error message stack first
 
2097
     * 
 
2098
     * @param  array $messages 
 
2099
     * @return Zend_Form
 
2100
     */
 
2101
    public function setErrorMessages(array $messages)
 
2102
    {
 
2103
        $this->clearErrorMessages();
 
2104
        return $this->addErrorMessages($messages);
 
2105
    }
 
2106
 
 
2107
    /**
 
2108
     * Retrieve custom error messages
 
2109
     * 
 
2110
     * @return array
 
2111
     */
 
2112
    public function getErrorMessages()
 
2113
    {
 
2114
        return $this->_errorMessages;
 
2115
    }
 
2116
 
 
2117
    /**
 
2118
     * Clear custom error messages stack
 
2119
     * 
 
2120
     * @return Zend_Form
 
2121
     */
 
2122
    public function clearErrorMessages()
 
2123
    {
 
2124
        $this->_errorMessages = array();
 
2125
        return $this;
 
2126
    }
 
2127
 
 
2128
    /**
 
2129
     * Mark the element as being in a failed validation state
 
2130
     * 
 
2131
     * @return Zend_Form
 
2132
     */
 
2133
    public function markAsError()
 
2134
    {
 
2135
        $this->_errorsExist = true;
 
2136
        return $this;
 
2137
    }
 
2138
 
 
2139
    /**
 
2140
     * Add an error message and mark element as failed validation
 
2141
     * 
 
2142
     * @param  string $message 
 
2143
     * @return Zend_Form
 
2144
     */
 
2145
    public function addError($message)
 
2146
    {
 
2147
        $this->addErrorMessage($message);
 
2148
        $this->markAsError();
 
2149
        return $this;
 
2150
    }
 
2151
 
 
2152
    /**
 
2153
     * Add multiple error messages and flag element as failed validation
 
2154
     * 
 
2155
     * @param  array $messages 
 
2156
     * @return Zend_Form
 
2157
     */
 
2158
    public function addErrors(array $messages)
 
2159
    {
 
2160
        foreach ($messages as $message) {
 
2161
            $this->addError($message);
 
2162
        }
 
2163
        return $this;
 
2164
    }
 
2165
 
 
2166
    /**
 
2167
     * Overwrite any previously set error messages and flag as failed validation
 
2168
     * 
 
2169
     * @param  array $messages 
 
2170
     * @return Zend_Form
 
2171
     */
 
2172
    public function setErrors(array $messages)
 
2173
    {
 
2174
        $this->clearErrorMessages();
 
2175
        return $this->addErrors($messages);
 
2176
    }
 
2177
 
 
2178
 
 
2179
    public function persistData()
 
2180
    {
 
2181
    }
 
2182
 
 
2183
    /**
 
2184
     * Are there errors in the form?
 
2185
     * 
 
2186
     * @return bool
 
2187
     */
 
2188
    public function isErrors()
 
2189
    {
 
2190
        return $this->_errorsExist;
 
2191
    }
 
2192
 
 
2193
    /**
 
2194
     * Get error codes for all elements failing validation
 
2195
     * 
 
2196
     * @param  string $name 
 
2197
     * @return array
 
2198
     */
 
2199
    public function getErrors($name = null)
 
2200
    {
 
2201
        $errors = array();
 
2202
        if ((null !== $name) && isset($this->_elements[$name])) {
 
2203
            $errors = $this->getElement($name)->getErrors();
 
2204
        } elseif ((null !== $name) && isset($this->_subForms[$name])) {
 
2205
            $errors = $this->getSubForm($name)->getErrors();
 
2206
        } else {
 
2207
            foreach ($this->_elements as $key => $element) {
 
2208
                $errors[$key] = $element->getErrors();
 
2209
            }
 
2210
            foreach ($this->getSubForms() as $key => $subForm) {
 
2211
                $fErrors = $this->_attachToArray($subForm->getErrors(), $subForm->getElementsBelongTo());
 
2212
                $errors = array_merge($errors, $fErrors);
 
2213
            }
 
2214
        }
 
2215
        return $errors;
 
2216
    }
 
2217
 
 
2218
    /**
 
2219
     * Retrieve error messages from elements failing validations
 
2220
     * 
 
2221
     * @param  string $name 
 
2222
     * @param  bool $suppressArrayNotation
 
2223
     * @return array
 
2224
     */
 
2225
    public function getMessages($name = null, $suppressArrayNotation = false)
 
2226
    {
 
2227
        if ((null !== $name) && isset($this->_elements[$name])) {
 
2228
            return $this->getElement($name)->getMessages();
 
2229
        } 
 
2230
 
 
2231
        if ((null !== $name) && isset($this->_subForms[$name])) {
 
2232
            return $this->getSubForm($name)->getMessages(null, true);
 
2233
        } 
 
2234
 
 
2235
        $arrayKeys = array();
 
2236
        foreach ($this->getSubForms() as $key => $subForm) {
 
2237
            $array = $this->_getArrayName($subForm->getElementsBelongTo());
 
2238
            if (!empty($array)) {
 
2239
                if ($name == $array) {
 
2240
                    return $subForm->getMessages(null, true);
 
2241
                }
 
2242
                $arrayKeys[$key] = $subForm->getElementsBelongTo();
 
2243
            }
 
2244
        }
 
2245
 
 
2246
        $customMessages = $this->_getErrorMessages();
 
2247
        if ($this->isErrors() && !empty($customMessages)) {
 
2248
            return $customMessages;
 
2249
        }
 
2250
 
 
2251
        $messages = array();
 
2252
 
 
2253
        foreach ($this->getElements() as $name => $element) {
 
2254
            $eMessages = $element->getMessages();
 
2255
            if (!empty($eMessages)) {
 
2256
                $messages[$name] = $eMessages;
 
2257
            }
 
2258
        }
 
2259
 
 
2260
        foreach ($this->getSubForms() as $key => $subForm) {
 
2261
            $fMessages = $subForm->getMessages(null, true);
 
2262
            if (!empty($fMessages)) {
 
2263
                if (array_key_exists($key, $arrayKeys)) {
 
2264
                    $fMessages = $this->_attachToArray($fMessages, $arrayKeys[$key]);
 
2265
                    $messages = array_merge($messages, $fMessages);
 
2266
                } else {
 
2267
                    $messages[$key] = $fMessages;
 
2268
                }
 
2269
            }
 
2270
        }
 
2271
 
 
2272
        if (!$suppressArrayNotation && $this->isArray()) {
 
2273
            $messages = $this->_attachToArray($messages, $this->getElementsBelongTo());
 
2274
        }
 
2275
 
 
2276
        return $messages;
 
2277
    }
 
2278
 
 
2279
 
 
2280
    // Rendering 
 
2281
 
 
2282
    /**
 
2283
     * Set view object
 
2284
     * 
 
2285
     * @param  Zend_View_Interface $view 
 
2286
     * @return Zend_Form
 
2287
     */
 
2288
    public function setView(Zend_View_Interface $view = null)
 
2289
    {
 
2290
        $this->_view = $view;
 
2291
        return $this;
 
2292
    }
 
2293
 
 
2294
    /**
 
2295
     * Retrieve view object
 
2296
     *
 
2297
     * If none registered, attempts to pull from ViewRenderer.
 
2298
     * 
 
2299
     * @return Zend_View_Interface|null
 
2300
     */
 
2301
    public function getView()
 
2302
    {
 
2303
        if (null === $this->_view) {
 
2304
            require_once 'Zend/Controller/Action/HelperBroker.php';
 
2305
            $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
 
2306
            $this->setView($viewRenderer->view);
 
2307
        }
 
2308
 
 
2309
        return $this->_view;
 
2310
    }
 
2311
 
 
2312
    /**
 
2313
     * Instantiate a decorator based on class name or class name fragment
 
2314
     * 
 
2315
     * @param  string $name 
 
2316
     * @param  null|array $options 
 
2317
     * @return Zend_Form_Decorator_Interface
 
2318
     */
 
2319
    protected function _getDecorator($name, $options)
 
2320
    {
 
2321
        $class = $this->getPluginLoader(self::DECORATOR)->load($name);
 
2322
        if (null === $options) {
 
2323
            $decorator = new $class;
 
2324
        } else {
 
2325
            $decorator = new $class($options);
 
2326
        }
 
2327
 
 
2328
        return $decorator;
 
2329
    }
 
2330
 
 
2331
    /**
 
2332
     * Add a decorator for rendering the element
 
2333
     * 
 
2334
     * @param  string|Zend_Form_Decorator_Interface $decorator 
 
2335
     * @param  array|Zend_Config $options Options with which to initialize decorator
 
2336
     * @return Zend_Form
 
2337
     */
 
2338
    public function addDecorator($decorator, $options = null)
 
2339
    {
 
2340
        if ($decorator instanceof Zend_Form_Decorator_Interface) {
 
2341
            $name = get_class($decorator);
 
2342
        } elseif (is_string($decorator)) {
 
2343
            $name      = $decorator;
 
2344
            $decorator = array(
 
2345
                'decorator' => $name,
 
2346
                'options'   => $options,
 
2347
            );
 
2348
        } elseif (is_array($decorator)) {
 
2349
            foreach ($decorator as $name => $spec) {
 
2350
                break;
 
2351
            }
 
2352
            if (is_numeric($name)) {
 
2353
                require_once 'Zend/Form/Exception.php';
 
2354
                throw new Zend_Form_Exception('Invalid alias provided to addDecorator; must be alphanumeric string');
 
2355
            }
 
2356
            if (is_string($spec)) {
 
2357
                $decorator = array(
 
2358
                    'decorator' => $spec,
 
2359
                    'options'   => $options,
 
2360
                );
 
2361
            } elseif ($spec instanceof Zend_Form_Decorator_Interface) {
 
2362
                $decorator = $spec;
 
2363
            }
 
2364
        } else {
 
2365
            require_once 'Zend/Form/Exception.php';
 
2366
            throw new Zend_Form_Exception('Invalid decorator provided to addDecorator; must be string or Zend_Form_Decorator_Interface');
 
2367
        }
 
2368
 
 
2369
        $this->_decorators[$name] = $decorator;
 
2370
 
 
2371
        return $this;
 
2372
    }
 
2373
 
 
2374
    /**
 
2375
     * Add many decorators at once
 
2376
     * 
 
2377
     * @param  array $decorators 
 
2378
     * @return Zend_Form
 
2379
     */
 
2380
    public function addDecorators(array $decorators)
 
2381
    {
 
2382
        foreach ($decorators as $decoratorInfo) {
 
2383
            if (is_string($decoratorInfo)) {
 
2384
                $this->addDecorator($decoratorInfo);
 
2385
            } elseif ($decoratorInfo instanceof Zend_Form_Decorator_Interface) {
 
2386
                $this->addDecorator($decoratorInfo);
 
2387
            } elseif (is_array($decoratorInfo)) {
 
2388
                $argc    = count($decoratorInfo);
 
2389
                $options = array();
 
2390
                if (isset($decoratorInfo['decorator'])) {
 
2391
                    $decorator = $decoratorInfo['decorator'];
 
2392
                    if (isset($decoratorInfo['options'])) {
 
2393
                        $options = $decoratorInfo['options'];
 
2394
                    }
 
2395
                    $this->addDecorator($decorator, $options);
 
2396
                } else {
 
2397
                    switch (true) {
 
2398
                        case (0 == $argc):
 
2399
                            break;
 
2400
                        case (1 <= $argc):
 
2401
                            $decorator  = array_shift($decoratorInfo);
 
2402
                        case (2 <= $argc):
 
2403
                            $options = array_shift($decoratorInfo);
 
2404
                        default:
 
2405
                            $this->addDecorator($decorator, $options);
 
2406
                            break;
 
2407
                    }
 
2408
                }
 
2409
            } else {
 
2410
                require_once 'Zend/Form/Exception.php';
 
2411
                throw new Zend_Form_Exception('Invalid decorator passed to addDecorators()');
 
2412
            }
 
2413
        }
 
2414
 
 
2415
        return $this;
 
2416
    }
 
2417
 
 
2418
    /**
 
2419
     * Overwrite all decorators
 
2420
     * 
 
2421
     * @param  array $decorators 
 
2422
     * @return Zend_Form
 
2423
     */
 
2424
    public function setDecorators(array $decorators)
 
2425
    {
 
2426
        $this->clearDecorators();
 
2427
        return $this->addDecorators($decorators);
 
2428
    }
 
2429
 
 
2430
    /**
 
2431
     * Retrieve a registered decorator
 
2432
     * 
 
2433
     * @param  string $name 
 
2434
     * @return false|Zend_Form_Decorator_Abstract
 
2435
     */
 
2436
    public function getDecorator($name)
 
2437
    {
 
2438
        if (!isset($this->_decorators[$name])) {
 
2439
            $len = strlen($name);
 
2440
            foreach ($this->_decorators as $localName => $decorator) {
 
2441
                if ($len > strlen($localName)) {
 
2442
                    continue;
 
2443
                }
 
2444
 
 
2445
                if (0 === substr_compare($localName, $name, -$len, $len, true)) {
 
2446
                    if (is_array($decorator)) {
 
2447
                        return $this->_loadDecorator($decorator, $localName);
 
2448
                    }
 
2449
                    return $decorator;
 
2450
                }
 
2451
            }
 
2452
            return false;
 
2453
        }
 
2454
 
 
2455
        if (is_array($this->_decorators[$name])) {
 
2456
            return $this->_loadDecorator($this->_decorators[$name], $name);
 
2457
        }
 
2458
 
 
2459
        return $this->_decorators[$name];
 
2460
    }
 
2461
 
 
2462
    /**
 
2463
     * Retrieve all decorators
 
2464
     * 
 
2465
     * @return array
 
2466
     */
 
2467
    public function getDecorators()
 
2468
    {
 
2469
        foreach ($this->_decorators as $key => $value) {
 
2470
            if (is_array($value)) {
 
2471
                $this->_loadDecorator($value, $key);
 
2472
            }
 
2473
        }
 
2474
        return $this->_decorators;
 
2475
    }
 
2476
 
 
2477
    /**
 
2478
     * Remove a single decorator
 
2479
     * 
 
2480
     * @param  string $name 
 
2481
     * @return bool
 
2482
     */
 
2483
    public function removeDecorator($name)
 
2484
    {
 
2485
        $decorator = $this->getDecorator($name);
 
2486
        if ($decorator) {
 
2487
            if (array_key_exists($name, $this->_decorators)) {
 
2488
                unset($this->_decorators[$name]);
 
2489
            } else {
 
2490
                $class = get_class($decorator);
 
2491
                if (!array_key_exists($class, $this->_decorators)) {
 
2492
                    return false;
 
2493
                }
 
2494
                unset($this->_decorators[$class]);
 
2495
            }
 
2496
            return true;
 
2497
        }
 
2498
 
 
2499
        return false;
 
2500
    }
 
2501
 
 
2502
    /**
 
2503
     * Clear all decorators
 
2504
     * 
 
2505
     * @return Zend_Form
 
2506
     */
 
2507
    public function clearDecorators()
 
2508
    {
 
2509
        $this->_decorators = array();
 
2510
        return $this;
 
2511
    }
 
2512
 
 
2513
    /**
 
2514
     * Set all element decorators as specified
 
2515
     * 
 
2516
     * @param  array $decorators 
 
2517
     * @param  array|null $elements Specific elements to decorate or exclude from decoration
 
2518
     * @param  bool $include Whether $elements is an inclusion or exclusion list
 
2519
     * @return Zend_Form
 
2520
     */
 
2521
    public function setElementDecorators(array $decorators, array $elements = null, $include = true)
 
2522
    {
 
2523
        if (is_array($elements)) {
 
2524
            if ($include) {
 
2525
                $elementObjs = array();
 
2526
                foreach ($elements as $name) {
 
2527
                    if (null !== ($element = $this->getElement($name))) {
 
2528
                        $elementObjs[] = $element;
 
2529
                    }
 
2530
                }
 
2531
            } else {
 
2532
                $elementObjs = $this->getElements();
 
2533
                foreach ($elements as $name) {
 
2534
                    if (array_key_exists($name, $elementObjs)) {
 
2535
                        unset($elementObjs[$name]);
 
2536
                    }
 
2537
                }
 
2538
            }
 
2539
        } else {
 
2540
            $elementObjs = $this->getElements();
 
2541
        }
 
2542
 
 
2543
        foreach ($elementObjs as $element) {
 
2544
            $element->setDecorators($decorators);
 
2545
        }
 
2546
 
 
2547
        return $this;
 
2548
    }
 
2549
 
 
2550
    /**
 
2551
     * Set all display group decorators as specified
 
2552
     * 
 
2553
     * @param  array $decorators 
 
2554
     * @return Zend_Form
 
2555
     */
 
2556
    public function setDisplayGroupDecorators(array $decorators)
 
2557
    {
 
2558
        foreach ($this->getDisplayGroups() as $group) {
 
2559
            $group->setDecorators($decorators);
 
2560
        }
 
2561
 
 
2562
        return $this;
 
2563
    }
 
2564
 
 
2565
    /**
 
2566
     * Set all subform decorators as specified
 
2567
     * 
 
2568
     * @param  array $decorators 
 
2569
     * @return Zend_Form
 
2570
     */
 
2571
    public function setSubFormDecorators(array $decorators)
 
2572
    {
 
2573
        foreach ($this->getSubForms() as $form) {
 
2574
            $form->setDecorators($decorators);
 
2575
        }
 
2576
 
 
2577
        return $this;
 
2578
    }
 
2579
 
 
2580
    /**
 
2581
     * Render form
 
2582
     * 
 
2583
     * @param  Zend_View_Interface $view 
 
2584
     * @return string
 
2585
     */
 
2586
    public function render(Zend_View_Interface $view = null)
 
2587
    {
 
2588
        if (null !== $view) {
 
2589
            $this->setView($view);
 
2590
        }
 
2591
 
 
2592
        $content = '';
 
2593
        foreach ($this->getDecorators() as $decorator) {
 
2594
            $decorator->setElement($this);
 
2595
            $content = $decorator->render($content);
 
2596
        }
 
2597
        return $content;
 
2598
    }
 
2599
 
 
2600
    /**
 
2601
     * Serialize as string
 
2602
     *
 
2603
     * Proxies to {@link render()}.
 
2604
     * 
 
2605
     * @return string
 
2606
     */
 
2607
    public function __toString()
 
2608
    {
 
2609
        try {
 
2610
            $return = $this->render();
 
2611
            return $return;
 
2612
        } catch (Exception $e) {
 
2613
            $message = "Exception caught by form: " . $e->getMessage()
 
2614
                     . "\nStack Trace:\n" . $e->getTraceAsString();
 
2615
            trigger_error($message, E_USER_WARNING);
 
2616
            return '';
 
2617
        }
 
2618
    }
 
2619
 
 
2620
 
 
2621
    // Localization: 
 
2622
 
 
2623
    /**
 
2624
     * Set translator object
 
2625
     * 
 
2626
     * @param  Zend_Translate|Zend_Translate_Adapter|null $translator 
 
2627
     * @return Zend_Form
 
2628
     */
 
2629
    public function setTranslator($translator = null)
 
2630
    {
 
2631
        if (null === $translator) {
 
2632
            $this->_translator = null;
 
2633
        } elseif ($translator instanceof Zend_Translate_Adapter) {
 
2634
            $this->_translator = $translator;
 
2635
        } elseif ($translator instanceof Zend_Translate) {
 
2636
            $this->_translator = $translator->getAdapter();
 
2637
        } else {
 
2638
            require_once 'Zend/Form/Exception.php';
 
2639
            throw new Zend_Form_Exception('Invalid translator specified');
 
2640
        }
 
2641
 
 
2642
        return $this;
 
2643
    }
 
2644
 
 
2645
    /**
 
2646
     * Set global default translator object
 
2647
     * 
 
2648
     * @param  Zend_Translate|Zend_Translate_Adapter|null $translator 
 
2649
     * @return void
 
2650
     */
 
2651
    public static function setDefaultTranslator($translator = null)
 
2652
    {
 
2653
        if (null === $translator) {
 
2654
            self::$_translatorDefault = null;
 
2655
        } elseif ($translator instanceof Zend_Translate_Adapter) {
 
2656
            self::$_translatorDefault = $translator;
 
2657
        } elseif ($translator instanceof Zend_Translate) {
 
2658
            self::$_translatorDefault = $translator->getAdapter();
 
2659
        } else {
 
2660
            require_once 'Zend/Form/Exception.php';
 
2661
            throw new Zend_Form_Exception('Invalid translator specified');
 
2662
        }
 
2663
    }
 
2664
 
 
2665
    /**
 
2666
     * Retrieve translator object
 
2667
     * 
 
2668
     * @return Zend_Translate|null
 
2669
     */
 
2670
    public function getTranslator()
 
2671
    {
 
2672
        if ($this->translatorIsDisabled()) {
 
2673
            return null;
 
2674
        }
 
2675
 
 
2676
        if (null === $this->_translator) {
 
2677
            return self::getDefaultTranslator();
 
2678
        }
 
2679
 
 
2680
        return $this->_translator;
 
2681
    }
 
2682
 
 
2683
    /**
 
2684
     * Get global default translator object
 
2685
     * 
 
2686
     * @return null|Zend_Translate
 
2687
     */
 
2688
    public static function getDefaultTranslator()
 
2689
    {
 
2690
        if (null === self::$_translatorDefault) {
 
2691
            require_once 'Zend/Registry.php';
 
2692
            if (Zend_Registry::isRegistered('Zend_Translate')) {
 
2693
                $translator = Zend_Registry::get('Zend_Translate');
 
2694
                if ($translator instanceof Zend_Translate_Adapter) {
 
2695
                    return $translator;
 
2696
                } elseif ($translator instanceof Zend_Translate) {
 
2697
                    return $translator->getAdapter();
 
2698
                }
 
2699
            }
 
2700
        }
 
2701
        return self::$_translatorDefault;
 
2702
    }
 
2703
 
 
2704
    /**
 
2705
     * Indicate whether or not translation should be disabled
 
2706
     * 
 
2707
     * @param  bool $flag 
 
2708
     * @return Zend_Form
 
2709
     */
 
2710
    public function setDisableTranslator($flag)
 
2711
    {
 
2712
        $this->_translatorDisabled = (bool) $flag;
 
2713
        return $this;
 
2714
    }
 
2715
 
 
2716
    /**
 
2717
     * Is translation disabled?
 
2718
     * 
 
2719
     * @return bool
 
2720
     */
 
2721
    public function translatorIsDisabled()
 
2722
    {
 
2723
        return $this->_translatorDisabled;
 
2724
    }
 
2725
 
 
2726
    /**
 
2727
     * Overloading: access to elements, form groups, and display groups
 
2728
     * 
 
2729
     * @param  string $name 
 
2730
     * @return Zend_Form_Element|Zend_Form|null
 
2731
     */
 
2732
    public function __get($name)
 
2733
    {
 
2734
        if (isset($this->_elements[$name])) {
 
2735
            return $this->_elements[$name];
 
2736
        } elseif (isset($this->_subForms[$name])) {
 
2737
            return $this->_subForms[$name];
 
2738
        } elseif (isset($this->_displayGroups[$name])) {
 
2739
            return $this->_displayGroups[$name];
 
2740
        }
 
2741
 
 
2742
        return null;
 
2743
    }
 
2744
 
 
2745
    /**
 
2746
     * Overloading: access to elements, form groups, and display groups
 
2747
     * 
 
2748
     * @param  string $name 
 
2749
     * @param  Zend_Form_Element|Zend_Form $value 
 
2750
     * @return void
 
2751
     * @throws Zend_Form_Exception for invalid $value
 
2752
     */
 
2753
    public function __set($name, $value)
 
2754
    {
 
2755
        if ($value instanceof Zend_Form_Element) {
 
2756
            $this->addElement($value, $name);
 
2757
            return;
 
2758
        } elseif ($value instanceof Zend_Form) {
 
2759
            $this->addSubForm($value, $name);
 
2760
            return;
 
2761
        } elseif (is_array($value)) {
 
2762
            $this->addDisplayGroup($value, $name);
 
2763
            return;
 
2764
        }
 
2765
 
 
2766
        require_once 'Zend/Form/Exception.php';
 
2767
        if (is_object($value)) {
 
2768
            $type = get_class($value);
 
2769
        } else {
 
2770
            $type = gettype($value);
 
2771
        }
 
2772
        throw new Zend_Form_Exception('Only form elements and groups may be overloaded; variable of type "' . $type . '" provided');
 
2773
    }
 
2774
 
 
2775
    /**
 
2776
     * Overloading: access to elements, form groups, and display groups
 
2777
     * 
 
2778
     * @param  string $name 
 
2779
     * @return boolean
 
2780
     */
 
2781
    public function __isset($name)
 
2782
    {
 
2783
        if (isset($this->_elements[$name])
 
2784
            || isset($this->_subForms[$name])
 
2785
            || isset($this->_displayGroups[$name]))
 
2786
        {
 
2787
            return true;
 
2788
        }
 
2789
 
 
2790
        return false;
 
2791
    }
 
2792
 
 
2793
    /**
 
2794
     * Overloading: access to elements, form groups, and display groups
 
2795
     * 
 
2796
     * @param  string $name 
 
2797
     * @return void
 
2798
     */
 
2799
    public function __unset($name)
 
2800
    {
 
2801
        if (isset($this->_elements[$name])) {
 
2802
            unset($this->_elements[$name]);
 
2803
        } elseif (isset($this->_subForms[$name])) {
 
2804
            unset($this->_subForms[$name]);
 
2805
        } elseif (isset($this->_displayGroups[$name])) {
 
2806
            unset($this->_displayGroups[$name]);
 
2807
        }
 
2808
    }
 
2809
 
 
2810
    /**
 
2811
     * Overloading: allow rendering specific decorators
 
2812
     *
 
2813
     * Call renderDecoratorName() to render a specific decorator.
 
2814
     * 
 
2815
     * @param  string $method 
 
2816
     * @param  array $args 
 
2817
     * @return string
 
2818
     * @throws Zend_Form_Exception for invalid decorator or invalid method call
 
2819
     */
 
2820
    public function __call($method, $args)
 
2821
    {
 
2822
        if ('render' == substr($method, 0, 6)) {
 
2823
            $decoratorName = substr($method, 6);
 
2824
            if (false !== ($decorator = $this->getDecorator($decoratorName))) {
 
2825
                $decorator->setElement($this);
 
2826
                $seed = '';
 
2827
                if (0 < count($args)) {
 
2828
                    $seed = array_shift($args);
 
2829
                }
 
2830
                return $decorator->render($seed);
 
2831
            }
 
2832
 
 
2833
            require_once 'Zend/Form/Exception.php';
 
2834
            throw new Zend_Form_Exception(sprintf('Decorator by name %s does not exist', $decoratorName));
 
2835
        }
 
2836
 
 
2837
        require_once 'Zend/Form/Exception.php';
 
2838
        throw new Zend_Form_Exception(sprintf('Method %s does not exist', $method));
 
2839
    }
 
2840
 
 
2841
    // Interfaces: Iterator, Countable
 
2842
 
 
2843
    /**
 
2844
     * Current element/subform/display group
 
2845
     * 
 
2846
     * @return Zend_Form_Element|Zend_Form_DisplayGroup|Zend_Form
 
2847
     */
 
2848
    public function current()
 
2849
    {
 
2850
        $this->_sort();
 
2851
        current($this->_order);
 
2852
        $key = key($this->_order);
 
2853
 
 
2854
        if (isset($this->_elements[$key])) {
 
2855
            return $this->getElement($key);
 
2856
        } elseif (isset($this->_subForms[$key])) {
 
2857
            return $this->getSubForm($key);
 
2858
        } elseif (isset($this->_displayGroups[$key])) {
 
2859
            return $this->getDisplayGroup($key);
 
2860
        } else {
 
2861
            require_once 'Zend/Form/Exception.php';
 
2862
            throw new Zend_Form_Exception(sprintf('Corruption detected in form; invalid key ("%s") found in internal iterator', (string) $key));
 
2863
        }
 
2864
    }
 
2865
 
 
2866
    /**
 
2867
     * Current element/subform/display group name
 
2868
     * 
 
2869
     * @return string
 
2870
     */
 
2871
    public function key()
 
2872
    {
 
2873
        $this->_sort();
 
2874
        return key($this->_order);
 
2875
    }
 
2876
 
 
2877
    /**
 
2878
     * Move pointer to next element/subform/display group
 
2879
     * 
 
2880
     * @return void
 
2881
     */
 
2882
    public function next()
 
2883
    {
 
2884
        $this->_sort();
 
2885
        next($this->_order);
 
2886
    }
 
2887
 
 
2888
    /**
 
2889
     * Move pointer to beginning of element/subform/display group loop
 
2890
     * 
 
2891
     * @return void
 
2892
     */
 
2893
    public function rewind()
 
2894
    {
 
2895
        $this->_sort();
 
2896
        reset($this->_order);
 
2897
    }
 
2898
 
 
2899
    /**
 
2900
     * Determine if current element/subform/display group is valid
 
2901
     * 
 
2902
     * @return bool
 
2903
     */
 
2904
    public function valid()
 
2905
    {
 
2906
        $this->_sort();
 
2907
        return (current($this->_order) !== false);
 
2908
    }
 
2909
 
 
2910
    /**
 
2911
     * Count of elements/subforms that are iterable
 
2912
     * 
 
2913
     * @return int
 
2914
     */
 
2915
    public function count()
 
2916
    {
 
2917
        return count($this->_order);
 
2918
    }
 
2919
 
 
2920
    /**
 
2921
     * Set flag to disable loading default decorators
 
2922
     * 
 
2923
     * @param  bool $flag 
 
2924
     * @return Zend_Form
 
2925
     */
 
2926
    public function setDisableLoadDefaultDecorators($flag)
 
2927
    {
 
2928
        $this->_disableLoadDefaultDecorators = (bool) $flag;
 
2929
        return $this;
 
2930
    }
 
2931
 
 
2932
    /**
 
2933
     * Should we load the default decorators?
 
2934
     * 
 
2935
     * @return bool
 
2936
     */
 
2937
    public function loadDefaultDecoratorsIsDisabled()
 
2938
    {
 
2939
        return $this->_disableLoadDefaultDecorators;
 
2940
    }
 
2941
 
 
2942
    /**
 
2943
     * Load the default decorators
 
2944
     * 
 
2945
     * @return void
 
2946
     */
 
2947
    public function loadDefaultDecorators()
 
2948
    {
 
2949
        if ($this->loadDefaultDecoratorsIsDisabled()) {
 
2950
            return;
 
2951
        }
 
2952
 
 
2953
        $decorators = $this->getDecorators();
 
2954
        if (empty($decorators)) {
 
2955
            $this->addDecorator('FormElements')
 
2956
                 ->addDecorator('HtmlTag', array('tag' => 'dl', 'class' => 'zend_form'))
 
2957
                 ->addDecorator('Form');
 
2958
        }
 
2959
    }
 
2960
 
 
2961
    /**
 
2962
     * Sort items according to their order
 
2963
     * 
 
2964
     * @return void
 
2965
     */
 
2966
    protected function _sort()
 
2967
    {
 
2968
        if ($this->_orderUpdated) {
 
2969
            $items = array();
 
2970
            $index = 0;
 
2971
            foreach ($this->_order as $key => $order) {
 
2972
                if (null === $order) {
 
2973
                    if (null === ($order = $this->{$key}->getOrder())) {
 
2974
                        while (array_search($index, $this->_order, true)) {
 
2975
                            ++$index;
 
2976
                        }
 
2977
                        $items[$index] = $key;
 
2978
                        ++$index;
 
2979
                    } else {
 
2980
                        $items[$order] = $key;
 
2981
                    }
 
2982
                } else {
 
2983
                    $items[$order] = $key;
 
2984
                }
 
2985
            }
 
2986
 
 
2987
            $items = array_flip($items);
 
2988
            asort($items);
 
2989
            $this->_order = $items;
 
2990
            $this->_orderUpdated = false;
 
2991
        }
 
2992
    }
 
2993
 
 
2994
    /**
 
2995
     * Lazy-load a decorator
 
2996
     * 
 
2997
     * @param  array $decorator Decorator type and options
 
2998
     * @param  mixed $name Decorator name or alias
 
2999
     * @return Zend_Form_Decorator_Interface
 
3000
     */
 
3001
    protected function _loadDecorator(array $decorator, $name)
 
3002
    {
 
3003
        $sameName = false;
 
3004
        if ($name == $decorator['decorator']) {
 
3005
            $sameName = true;
 
3006
        }
 
3007
 
 
3008
        $instance = $this->_getDecorator($decorator['decorator'], $decorator['options']);
 
3009
        if ($sameName) {
 
3010
            $newName            = get_class($instance);
 
3011
            $decoratorNames     = array_keys($this->_decorators);
 
3012
            $order              = array_flip($decoratorNames);
 
3013
            $order[$newName]    = $order[$name];
 
3014
            $decoratorsExchange = array();
 
3015
            unset($order[$name]);
 
3016
            asort($order);
 
3017
            foreach ($order as $key => $index) {
 
3018
                if ($key == $newName) {
 
3019
                    $decoratorsExchange[$key] = $instance;
 
3020
                    continue;
 
3021
                }
 
3022
                $decoratorsExchange[$key] = $this->_decorators[$key];
 
3023
            }
 
3024
            $this->_decorators = $decoratorsExchange;
 
3025
        } else {
 
3026
            $this->_decorators[$name] = $instance;
 
3027
        }
 
3028
 
 
3029
        return $instance;
 
3030
    }
 
3031
 
 
3032
    /**
 
3033
     * Retrieve optionally translated custom error messages
 
3034
     * 
 
3035
     * @return array
 
3036
     */
 
3037
    protected function _getErrorMessages()
 
3038
    {
 
3039
        $messages   = $this->getErrorMessages();
 
3040
        $translator = $this->getTranslator();
 
3041
        if (null !== $translator) {
 
3042
            foreach ($messages as $key => $message) {
 
3043
                $messages[$key] = $translator->translate($message);
 
3044
            }
 
3045
        }
 
3046
        return $messages;
 
3047
    }
 
3048
}