web/Zend/Form/Decorator/Image.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 /**
       
    26  * Zend_Form_Decorator_Image
       
    27  *
       
    28  * Accepts the options:
       
    29  * - separator: separator to use between image and content (defaults to PHP_EOL)
       
    30  * - placement: whether to append or prepend label to content (defaults to append)
       
    31  * - tag: if set, used to wrap the label in an additional HTML tag
       
    32  *
       
    33  * Any other options passed will be used as HTML attributes of the image tag.
       
    34  *
       
    35  * @category   Zend
       
    36  * @package    Zend_Form
       
    37  * @subpackage Decorator
       
    38  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    39  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    40  * @version    $Id: Image.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    41  */
       
    42 class Zend_Form_Decorator_Image extends Zend_Form_Decorator_Abstract
       
    43 {
       
    44     /**
       
    45      * Attributes that should not be passed to helper
       
    46      * @var array
       
    47      */
       
    48     protected $_attribBlacklist = array('helper', 'placement', 'separator', 'tag');
       
    49 
       
    50     /**
       
    51      * Default placement: append
       
    52      * @var string
       
    53      */
       
    54     protected $_placement = 'APPEND';
       
    55 
       
    56     /**
       
    57      * HTML tag with which to surround image
       
    58      * @var string
       
    59      */
       
    60     protected $_tag;
       
    61 
       
    62     /**
       
    63      * Set HTML tag with which to surround label
       
    64      *
       
    65      * @param  string $tag
       
    66      * @return Zend_Form_Decorator_Image
       
    67      */
       
    68     public function setTag($tag)
       
    69     {
       
    70         $this->_tag = (string) $tag;
       
    71         return $this;
       
    72     }
       
    73 
       
    74     /**
       
    75      * Get HTML tag, if any, with which to surround label
       
    76      *
       
    77      * @return void
       
    78      */
       
    79     public function getTag()
       
    80     {
       
    81         if (null === $this->_tag) {
       
    82             $tag = $this->getOption('tag');
       
    83             if (null !== $tag) {
       
    84                 $this->removeOption('tag');
       
    85                 $this->setTag($tag);
       
    86             }
       
    87             return $tag;
       
    88         }
       
    89 
       
    90         return $this->_tag;
       
    91     }
       
    92 
       
    93     /**
       
    94      * Get attributes to pass to image helper
       
    95      *
       
    96      * @return array
       
    97      */
       
    98     public function getAttribs()
       
    99     {
       
   100         $attribs = $this->getOptions();
       
   101 
       
   102         if (null !== ($element = $this->getElement())) {
       
   103             $attribs['alt'] = $element->getLabel();
       
   104             $attribs = array_merge($attribs, $element->getAttribs());
       
   105         }
       
   106 
       
   107         foreach ($this->_attribBlacklist as $key) {
       
   108             if (array_key_exists($key, $attribs)) {
       
   109                 unset($attribs[$key]);
       
   110             }
       
   111         }
       
   112 
       
   113         return $attribs;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Render a form image
       
   118      *
       
   119      * @param  string $content
       
   120      * @return string
       
   121      */
       
   122     public function render($content)
       
   123     {
       
   124         $element = $this->getElement();
       
   125         $view    = $element->getView();
       
   126         if (null === $view) {
       
   127             return $content;
       
   128         }
       
   129 
       
   130         $tag           = $this->getTag();
       
   131         $placement     = $this->getPlacement();
       
   132         $separator     = $this->getSeparator();
       
   133         $name          = $element->getFullyQualifiedName();
       
   134         $attribs       = $this->getAttribs();
       
   135         $attribs['id'] = $element->getId();
       
   136 
       
   137         $image = $view->formImage($name, $element->getImageValue(), $attribs);
       
   138 
       
   139         if (null !== $tag) {
       
   140             require_once 'Zend/Form/Decorator/HtmlTag.php';
       
   141             $decorator = new Zend_Form_Decorator_HtmlTag();
       
   142             $decorator->setOptions(array('tag' => $tag));
       
   143             $image = $decorator->render($image);
       
   144         }
       
   145 
       
   146         switch ($placement) {
       
   147             case self::PREPEND:
       
   148                 return $image . $separator . $content;
       
   149             case self::APPEND:
       
   150             default:
       
   151                 return $content . $separator . $image;
       
   152         }
       
   153     }
       
   154 }