web/Zend/Form/Decorator/File.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 /** Zend_Form_Decorator_Marker_File_Interface */
       
    26 require_once 'Zend/Form/Decorator/Marker/File/Interface.php';
       
    27 
       
    28 /** Zend_File_Transfer_Adapter_Http */
       
    29 require_once 'Zend/File/Transfer/Adapter/Http.php';
       
    30 
       
    31 /**
       
    32  * Zend_Form_Decorator_File
       
    33  *
       
    34  * Fixes the rendering for all subform and multi file elements
       
    35  *
       
    36  * @category   Zend
       
    37  * @package    Zend_Form
       
    38  * @subpackage Decorator
       
    39  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    40  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    41  * @version    $Id: File.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    42  */
       
    43 class Zend_Form_Decorator_File
       
    44     extends Zend_Form_Decorator_Abstract
       
    45     implements Zend_Form_Decorator_Marker_File_Interface
       
    46 {
       
    47     /**
       
    48      * Attributes that should not be passed to helper
       
    49      * @var array
       
    50      */
       
    51     protected $_attribBlacklist = array('helper', 'placement', 'separator', 'value');
       
    52 
       
    53     /**
       
    54      * Default placement: append
       
    55      * @var string
       
    56      */
       
    57     protected $_placement = 'APPEND';
       
    58 
       
    59     /**
       
    60      * Get attributes to pass to file helper
       
    61      *
       
    62      * @return array
       
    63      */
       
    64     public function getAttribs()
       
    65     {
       
    66         $attribs   = $this->getOptions();
       
    67 
       
    68         if (null !== ($element = $this->getElement())) {
       
    69             $attribs = array_merge($attribs, $element->getAttribs());
       
    70         }
       
    71 
       
    72         foreach ($this->_attribBlacklist as $key) {
       
    73             if (array_key_exists($key, $attribs)) {
       
    74                 unset($attribs[$key]);
       
    75             }
       
    76         }
       
    77 
       
    78         return $attribs;
       
    79     }
       
    80 
       
    81     /**
       
    82      * Render a form file
       
    83      *
       
    84      * @param  string $content
       
    85      * @return string
       
    86      */
       
    87     public function render($content)
       
    88     {
       
    89         $element = $this->getElement();
       
    90         if (!$element instanceof Zend_Form_Element) {
       
    91             return $content;
       
    92         }
       
    93 
       
    94         $view = $element->getView();
       
    95         if (!$view instanceof Zend_View_Interface) {
       
    96             return $content;
       
    97         }
       
    98 
       
    99         $name      = $element->getName();
       
   100         $attribs   = $this->getAttribs();
       
   101         if (!array_key_exists('id', $attribs)) {
       
   102             $attribs['id'] = $name;
       
   103         }
       
   104 
       
   105         $separator = $this->getSeparator();
       
   106         $placement = $this->getPlacement();
       
   107         $markup    = array();
       
   108         $size      = $element->getMaxFileSize();
       
   109         if ($size > 0) {
       
   110             $element->setMaxFileSize(0);
       
   111             $markup[] = $view->formHidden('MAX_FILE_SIZE', $size);
       
   112         }
       
   113 
       
   114         if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) {
       
   115             $markup[] = $view->formHidden(ini_get('apc.rfc1867_name'), uniqid(), array('id' => 'progress_key'));
       
   116         } else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) {
       
   117             $markup[] = $view->formHidden('UPLOAD_IDENTIFIER', uniqid(), array('id' => 'progress_key'));
       
   118         }
       
   119 
       
   120         if ($element->isArray()) {
       
   121             $name .= "[]";
       
   122             $count = $element->getMultiFile();
       
   123             for ($i = 0; $i < $count; ++$i) {
       
   124                 $htmlAttribs        = $attribs;
       
   125                 $htmlAttribs['id'] .= '-' . $i;
       
   126                 $markup[] = $view->formFile($name, $htmlAttribs);
       
   127             }
       
   128         } else {
       
   129             $markup[] = $view->formFile($name, $attribs);
       
   130         }
       
   131 
       
   132         $markup = implode($separator, $markup);
       
   133 
       
   134         switch ($placement) {
       
   135             case self::PREPEND:
       
   136                 return $markup . $separator . $content;
       
   137             case self::APPEND:
       
   138             default:
       
   139                 return $content . $separator . $markup;
       
   140         }
       
   141     }
       
   142 }