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 * @subpackage Decorator
18 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
20 */
21
22/** Zend_Form_Decorator_Abstract */
23
24/**
25 * Zend_Form_Decorator_Fieldset
26 *
27 * Any options passed will be used as HTML attributes of the fieldset tag.
28 *
29 *
30 * @category   Zend
31 * @package    Zend_Form
32 * @subpackage Decorator
33 * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
34 * @license    http://framework.zend.com/license/new-bsd     New BSD License
35 * @version    $Id$
36 */
37class Zend_Form_Decorator_Fieldset extends Zend_Form_Decorator_Abstract
38{
39    /**
40     * Attribs that should be removed prior to rendering
41     * @var array
42     */
43    public $stripAttribs = array(
44        'action',
45        'enctype',
46        'helper',
47        'method',
48        'name',
49        'accept-charset',
50    );
51
52    /**
53     * Fieldset legend
54     * @var string
55     */
56    protected $_legend;
57
58    /**
59     * Default placement: surround content
60     * @var string
61     */
62    protected $_placement = null;
63
64    /**
65     * Get options
66     *
67     * Merges in element attributes as well.
68     *
69     * @return array
70     */
71    public function getOptions()
72    {
73        $options = parent::getOptions();
74        if (null !== ($element = $this->getElement())) {
75            $attribs = $element->getAttribs();
76            $options = array_merge($attribs, $options);
77            $this->setOptions($options);
78        }
79        return $options;
80    }
81
82    /**
83     * Set legend
84     *
85     * @param  string $value
86     * @return Zend_Form_Decorator_Fieldset
87     */
88    public function setLegend($value)
89    {
90        $this->_legend = (string) $value;
91        return $this;
92    }
93
94    /**
95     * Get legend
96     *
97     * @return string
98     */
99    public function getLegend()
100    {
101        $legend = $this->_legend;
102        if ((null === $legend) && (null !== ($element = $this->getElement()))) {
103            if (method_exists($element, 'getLegend')) {
104                $legend = $element->getLegend();
105                $this->setLegend($legend);
106            }
107        }
108        if ((null === $legend) && (null !== ($legend = $this->getOption('legend')))) {
109            $this->setLegend($legend);
110            $this->removeOption('legend');
111        }
112
113        return $legend;
114    }
115
116    /**
117     * Render a fieldset
118     *
119     * @param  string $content
120     * @return string
121     */
122    public function render($content)
123    {
124        $element = $this->getElement();
125        $view    = $element->getView();
126        if (null === $view) {
127            return $content;
128        }
129
130        $legend  = $this->getLegend();
131        $attribs = $this->getOptions();
132        $name    = $element->getFullyQualifiedName();
133        $id      = (string)$element->getId();
134
135        if ((!array_key_exists('id', $attribs) || $attribs['id'] == $id) && '' !== $id) {
136            $attribs['id'] = 'fieldset-' . $id;
137        }
138
139        if (null !== $legend) {
140            if (null !== ($translator = $element->getTranslator())) {
141                $legend = $translator->translate($legend);
142            }
143
144            $attribs['legend'] = $legend;
145        }
146
147        foreach (array_keys($attribs) as $attrib) {
148            $testAttrib = strtolower($attrib);
149            if (in_array($testAttrib, $this->stripAttribs)) {
150                unset($attribs[$attrib]);
151            }
152        }
153
154        return $view->fieldset($name, $content, $attribs);
155    }
156}
157