PageRenderTime 36ms CodeModel.GetById 31ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 0ms

/zf/library/Zend/Form/Decorator/FormElements.php

http://github.com/eryx/php-framework-benchmark
PHP | 136 lines | 74 code | 10 blank | 52 comment | 18 complexity | 51664abf6609f3312753458edfe4420b MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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-2011 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 */
 23require_once 'Zend/Form/Decorator/Abstract.php';
 24
 25/**
 26 * Zend_Form_Decorator_FormElements
 27 *
 28 * Render all form elements registered with current form
 29 *
 30 * Accepts following options:
 31 * - separator: Separator to use between elements
 32 *
 33 * Any other options passed will be used as HTML attributes of the form tag.
 34 *
 35 * @category   Zend
 36 * @package    Zend_Form
 37 * @subpackage Decorator
 38 * @copyright  Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
 39 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 40 * @version    $Id: FormElements.php 24453 2011-09-09 15:14:24Z matthew $
 41 */
 42class Zend_Form_Decorator_FormElements extends Zend_Form_Decorator_Abstract
 43{
 44    /**
 45     * Merges given two belongsTo (array notation) strings
 46     *
 47     * @param  string $baseBelongsTo
 48     * @param  string $belongsTo
 49     * @return string
 50     */
 51    public function mergeBelongsTo($baseBelongsTo, $belongsTo)
 52    {
 53        $endOfArrayName = strpos($belongsTo, '[');
 54
 55        if ($endOfArrayName === false) {
 56            return $baseBelongsTo . '[' . $belongsTo . ']';
 57        }
 58
 59        $arrayName = substr($belongsTo, 0, $endOfArrayName);
 60
 61        return $baseBelongsTo . '[' . $arrayName . ']' . substr($belongsTo, $endOfArrayName);
 62    }
 63
 64    /**
 65     * Render form elements
 66     *
 67     * @param  string $content
 68     * @return string
 69     */
 70    public function render($content)
 71    {
 72        $form    = $this->getElement();
 73        if ((!$form instanceof Zend_Form) && (!$form instanceof Zend_Form_DisplayGroup)) {
 74            return $content;
 75        }
 76
 77        $belongsTo      = ($form instanceof Zend_Form) ? $form->getElementsBelongTo() : null;
 78        $elementContent = '';
 79        $displayGroups  = ($form instanceof Zend_Form) ? $form->getDisplayGroups() : array();
 80        $separator      = $this->getSeparator();
 81        $translator     = $form->getTranslator();
 82        $items          = array();
 83        $view           = $form->getView();
 84        foreach ($form as $item) {
 85            $item->setView($view)
 86                 ->setTranslator($translator);
 87            if ($item instanceof Zend_Form_Element) {
 88                foreach ($displayGroups as $group) {
 89                    $elementName = $item->getName();
 90                    $element     = $group->getElement($elementName);
 91                    if ($element) {
 92                        // Element belongs to display group; only render in that
 93                        // context.
 94                        continue 2;
 95                    }
 96                }
 97                $item->setBelongsTo($belongsTo);
 98            } elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) {
 99                if ($item->isArray()) {
100                    $name = $this->mergeBelongsTo($belongsTo, $item->getElementsBelongTo());
101                    $item->setElementsBelongTo($name, true);
102                } else {
103                    $item->setElementsBelongTo($belongsTo, true);
104                }
105            } elseif (!empty($belongsTo) && ($item instanceof Zend_Form_DisplayGroup)) {
106                foreach ($item as $element) {
107                    $element->setBelongsTo($belongsTo);
108                }
109            }
110
111            $items[] = $item->render();
112
113            if (($item instanceof Zend_Form_Element_File)
114                || (($item instanceof Zend_Form)
115                    && (Zend_Form::ENCTYPE_MULTIPART == $item->getEnctype()))
116                || (($item instanceof Zend_Form_DisplayGroup)
117                    && (Zend_Form::ENCTYPE_MULTIPART == $item->getAttrib('enctype')))
118            ) {
119                if ($form instanceof Zend_Form) {
120                    $form->setEnctype(Zend_Form::ENCTYPE_MULTIPART);
121                } elseif ($form instanceof Zend_Form_DisplayGroup) {
122                    $form->setAttrib('enctype', Zend_Form::ENCTYPE_MULTIPART);
123                }
124            }
125        }
126        $elementContent = implode($separator, $items);
127
128        switch ($this->getPlacement()) {
129            case self::PREPEND:
130                return $elementContent . $separator . $content;
131            case self::APPEND:
132            default:
133                return $content . $separator . $elementContent;
134        }
135    }
136}