File indexing completed on 2024-12-22 05:36:42

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Form
0017  * @subpackage Decorator
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  */
0021 
0022 /** Zend_Form_Decorator_Abstract */
0023 // require_once 'Zend/Form/Decorator/Abstract.php';
0024 
0025 /**
0026  * Zend_Form_Decorator_Form
0027  *
0028  * Render a Zend_Form object.
0029  *
0030  * Accepts following options:
0031  * - separator: Separator to use between elements
0032  * - helper: which view helper to use when rendering form. Should accept three
0033  *   arguments, string content, a name, and an array of attributes.
0034  *
0035  * Any other options passed will be used as HTML attributes of the form tag.
0036  *
0037  * @category   Zend
0038  * @package    Zend_Form
0039  * @subpackage Decorator
0040  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0041  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0042  * @version    $Id$
0043  */
0044 class Zend_Form_Decorator_Form extends Zend_Form_Decorator_Abstract
0045 {
0046     /**
0047      * Default view helper
0048      * @var string
0049      */
0050     protected $_helper = 'form';
0051 
0052     /**
0053      * Set view helper for rendering form
0054      *
0055      * @param  string $helper
0056      * @return Zend_Form_Decorator_Form
0057      */
0058     public function setHelper($helper)
0059     {
0060         $this->_helper = (string) $helper;
0061         return $this;
0062     }
0063 
0064     /**
0065      * Get view helper for rendering form
0066      *
0067      * @return string
0068      */
0069     public function getHelper()
0070     {
0071         if (null !== ($helper = $this->getOption('helper'))) {
0072             $this->setHelper($helper);
0073             $this->removeOption('helper');
0074         }
0075         return $this->_helper;
0076     }
0077 
0078     /**
0079      * Retrieve decorator options
0080      *
0081      * Assures that form action and method are set, and sets appropriate
0082      * encoding type if current method is POST.
0083      *
0084      * @return array
0085      */
0086     public function getOptions()
0087     {
0088         if (null !== ($element = $this->getElement())) {
0089             if ($element instanceof Zend_Form) {
0090                 $element->getAction();
0091                 $method = $element->getMethod();
0092                 if ($method == Zend_Form::METHOD_POST) {
0093                     $this->setOption('enctype', 'application/x-www-form-urlencoded');
0094                 }
0095                 foreach ($element->getAttribs() as $key => $value) {
0096                     $this->setOption($key, $value);
0097                 }
0098             } elseif ($element instanceof Zend_Form_DisplayGroup) {
0099                 foreach ($element->getAttribs() as $key => $value) {
0100                     $this->setOption($key, $value);
0101                 }
0102             }
0103         }
0104 
0105         if (isset($this->_options['method'])) {
0106             $this->_options['method'] = strtolower($this->_options['method']);
0107         }
0108 
0109         return $this->_options;
0110     }
0111 
0112     /**
0113      * Render a form
0114      *
0115      * Replaces $content entirely from currently set element.
0116      *
0117      * @param  string $content
0118      * @return string
0119      */
0120     public function render($content)
0121     {
0122         $form    = $this->getElement();
0123         $view    = $form->getView();
0124         if (null === $view) {
0125             return $content;
0126         }
0127 
0128         $helper        = $this->getHelper();
0129         $attribs       = $this->getOptions();
0130         $name          = $form->getFullyQualifiedName();
0131         $attribs['id'] = $form->getId();
0132         return $view->$helper($name, $attribs, $content);
0133     }
0134 }