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 /** Zend_Form_Decorator_Marker_File_Interface */
0026 // require_once 'Zend/Form/Decorator/Marker/File/Interface.php';
0027 
0028 /** Zend_File_Transfer_Adapter_Http */
0029 // require_once 'Zend/File/Transfer/Adapter/Http.php';
0030 
0031 /**
0032  * Zend_Form_Decorator_File
0033  *
0034  * Fixes the rendering for all subform and multi file elements
0035  *
0036  * @category   Zend
0037  * @package    Zend_Form
0038  * @subpackage Decorator
0039  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0040  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0041  * @version    $Id$
0042  */
0043 class Zend_Form_Decorator_File
0044     extends Zend_Form_Decorator_Abstract
0045     implements Zend_Form_Decorator_Marker_File_Interface
0046 {
0047     /**
0048      * Attributes that should not be passed to helper
0049      * @var array
0050      */
0051     protected $_attribBlacklist = array('helper', 'placement', 'separator', 'value');
0052 
0053     /**
0054      * Default placement: append
0055      * @var string
0056      */
0057     protected $_placement = 'APPEND';
0058 
0059     /**
0060      * Get attributes to pass to file helper
0061      *
0062      * @return array
0063      */
0064     public function getAttribs()
0065     {
0066         $attribs   = $this->getOptions();
0067 
0068         if (null !== ($element = $this->getElement())) {
0069             $attribs = array_merge($attribs, $element->getAttribs());
0070         }
0071 
0072         foreach ($this->_attribBlacklist as $key) {
0073             if (array_key_exists($key, $attribs)) {
0074                 unset($attribs[$key]);
0075             }
0076         }
0077 
0078         return $attribs;
0079     }
0080 
0081     /**
0082      * Render a form file
0083      *
0084      * @param  string $content
0085      * @return string
0086      */
0087     public function render($content)
0088     {
0089         $element = $this->getElement();
0090         if (!$element instanceof Zend_Form_Element) {
0091             return $content;
0092         }
0093 
0094         $view = $element->getView();
0095         if (!$view instanceof Zend_View_Interface) {
0096             return $content;
0097         }
0098 
0099         $name      = $element->getName();
0100         $attribs   = $this->getAttribs();
0101         if (!array_key_exists('id', $attribs)) {
0102             $attribs['id'] = $name;
0103         }
0104 
0105         $separator = $this->getSeparator();
0106         $placement = $this->getPlacement();
0107         $markup    = array();
0108         $size      = $element->getMaxFileSize();
0109         if ($size > 0) {
0110             $element->setMaxFileSize(0);
0111             $markup[] = $view->formHidden('MAX_FILE_SIZE', $size);
0112         }
0113 
0114         if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) {
0115             $markup[] = $view->formHidden(ini_get('apc.rfc1867_name'), uniqid(), array('id' => 'progress_key'));
0116         } else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) {
0117             $markup[] = $view->formHidden('UPLOAD_IDENTIFIER', uniqid(), array('id' => 'progress_key'));
0118         }
0119 
0120         $helper = $element->helper;
0121         if ($element->isArray()) {
0122             $name .= "[]";
0123             $count = $element->getMultiFile();
0124             for ($i = 0; $i < $count; ++$i) {
0125                 $htmlAttribs        = $attribs;
0126                 $htmlAttribs['id'] .= '-' . $i;
0127                 $markup[] = $view->$helper($name, $htmlAttribs);
0128             }
0129         } else {
0130             $markup[] = $view->$helper($name, $attribs);
0131         }
0132 
0133         $markup = implode($separator, $markup);
0134 
0135         switch ($placement) {
0136             case self::PREPEND:
0137                 return $markup . $separator . $content;
0138             case self::APPEND:
0139             default:
0140                 return $content . $separator . $markup;
0141         }
0142     }
0143 }