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