File indexing completed on 2025-03-02 05:29:24

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_FormElements
0027  *
0028  * Render all form elements registered with current form
0029  *
0030  * Accepts following options:
0031  * - separator: Separator to use between elements
0032  *
0033  * Any other options passed will be used as HTML attributes of the form tag.
0034  *
0035  * @category   Zend
0036  * @package    Zend_Form
0037  * @subpackage Decorator
0038  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0039  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0040  * @version    $Id$
0041  */
0042 class Zend_Form_Decorator_FormElements extends Zend_Form_Decorator_Abstract
0043 {
0044     /**
0045      * Merges given two belongsTo (array notation) strings
0046      *
0047      * @param  string $baseBelongsTo
0048      * @param  string $belongsTo
0049      * @return string
0050      */
0051     public function mergeBelongsTo($baseBelongsTo, $belongsTo)
0052     {
0053         $endOfArrayName = strpos($belongsTo, '[');
0054 
0055         if ($endOfArrayName === false) {
0056             return $baseBelongsTo . '[' . $belongsTo . ']';
0057         }
0058 
0059         $arrayName = substr($belongsTo, 0, $endOfArrayName);
0060 
0061         return $baseBelongsTo . '[' . $arrayName . ']' . substr($belongsTo, $endOfArrayName);
0062     }
0063 
0064     /**
0065      * Render form elements
0066      *
0067      * @param  string $content
0068      * @return string
0069      */
0070     public function render($content)
0071     {
0072         $form    = $this->getElement();
0073         if ((!$form instanceof Zend_Form) && (!$form instanceof Zend_Form_DisplayGroup)) {
0074             return $content;
0075         }
0076 
0077         $belongsTo      = ($form instanceof Zend_Form) ? $form->getElementsBelongTo() : null;
0078         $elementContent = '';
0079         $displayGroups  = ($form instanceof Zend_Form) ? $form->getDisplayGroups() : array();
0080         $separator      = $this->getSeparator();
0081         $translator     = $form->getTranslator();
0082         $items          = array();
0083         $view           = $form->getView();
0084         foreach ($form as $item) {
0085             $item->setView($view);
0086 
0087             // Set translator
0088             if (!$item->hasTranslator()) {
0089                 $item->setTranslator($translator);
0090             }
0091 
0092             if ($item instanceof Zend_Form_Element) {
0093                 foreach ($displayGroups as $group) {
0094                     $elementName = $item->getName();
0095                     $element     = $group->getElement($elementName);
0096                     if ($element) {
0097                         // Element belongs to display group; only render in that
0098                         // context.
0099                         continue 2;
0100                     }
0101                 }
0102                 $item->setBelongsTo($belongsTo);
0103             } elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) {
0104                 if ($item->isArray()) {
0105                     $name = $this->mergeBelongsTo($belongsTo, $item->getElementsBelongTo());
0106                     $item->setElementsBelongTo($name, true);
0107                 } else {
0108                     $item->setElementsBelongTo($belongsTo, true);
0109                 }
0110             } elseif (!empty($belongsTo) && ($item instanceof Zend_Form_DisplayGroup)) {
0111                 foreach ($item as $element) {
0112                     $element->setBelongsTo($belongsTo);
0113                 }
0114             }
0115 
0116             $items[] = $item->render();
0117 
0118             if (($item instanceof Zend_Form_Element_File)
0119                 || (($item instanceof Zend_Form)
0120                     && (Zend_Form::ENCTYPE_MULTIPART == $item->getEnctype()))
0121                 || (($item instanceof Zend_Form_DisplayGroup)
0122                     && (Zend_Form::ENCTYPE_MULTIPART == $item->getAttrib('enctype')))
0123             ) {
0124                 if ($form instanceof Zend_Form) {
0125                     $form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
0126                 } elseif ($form instanceof Zend_Form_DisplayGroup) {
0127                     $form->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);
0128                 }
0129             }
0130         }
0131         $elementContent = implode($separator, $items);
0132 
0133         switch ($this->getPlacement()) {
0134             case self::PREPEND:
0135                 return $elementContent . $separator . $content;
0136             case self::APPEND:
0137             default:
0138                 return $content . $separator . $elementContent;
0139         }
0140     }
0141 }