File indexing completed on 2024-05-26 06:03:00

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_Image
0027  *
0028  * Accepts the options:
0029  * - separator: separator to use between image and content (defaults to PHP_EOL)
0030  * - placement: whether to append or prepend label to content (defaults to append)
0031  * - tag: if set, used to wrap the label in an additional HTML tag
0032  *
0033  * Any other options passed will be used as HTML attributes of the image 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_Image extends Zend_Form_Decorator_Abstract
0043 {
0044     /**
0045      * Attributes that should not be passed to helper
0046      * @var array
0047      */
0048     protected $_attribBlacklist = array('helper', 'placement', 'separator', 'tag');
0049 
0050     /**
0051      * Default placement: append
0052      * @var string
0053      */
0054     protected $_placement = 'APPEND';
0055 
0056     /**
0057      * HTML tag with which to surround image
0058      * @var string
0059      */
0060     protected $_tag;
0061 
0062     /**
0063      * Set HTML tag with which to surround label
0064      *
0065      * @param  string $tag
0066      * @return Zend_Form_Decorator_Image
0067      */
0068     public function setTag($tag)
0069     {
0070         $this->_tag = (string) $tag;
0071         return $this;
0072     }
0073 
0074     /**
0075      * Get HTML tag, if any, with which to surround label
0076      *
0077      * @return void
0078      */
0079     public function getTag()
0080     {
0081         if (null === $this->_tag) {
0082             $tag = $this->getOption('tag');
0083             if (null !== $tag) {
0084                 $this->removeOption('tag');
0085                 $this->setTag($tag);
0086             }
0087             return $tag;
0088         }
0089 
0090         return $this->_tag;
0091     }
0092 
0093     /**
0094      * Get attributes to pass to image helper
0095      *
0096      * @return array
0097      */
0098     public function getAttribs()
0099     {
0100         $attribs = $this->getOptions();
0101 
0102         if (null !== ($element = $this->getElement())) {
0103             $attribs['alt'] = $element->getLabel();
0104             $attribs = array_merge($attribs, $element->getAttribs());
0105         }
0106 
0107         foreach ($this->_attribBlacklist as $key) {
0108             if (array_key_exists($key, $attribs)) {
0109                 unset($attribs[$key]);
0110             }
0111         }
0112 
0113         return $attribs;
0114     }
0115 
0116     /**
0117      * Render a form image
0118      *
0119      * @param  string $content
0120      * @return string
0121      */
0122     public function render($content)
0123     {
0124         $element = $this->getElement();
0125         $view    = $element->getView();
0126         if (null === $view) {
0127             return $content;
0128         }
0129 
0130         $tag           = $this->getTag();
0131         $placement     = $this->getPlacement();
0132         $separator     = $this->getSeparator();
0133         $name          = $element->getFullyQualifiedName();
0134         $attribs       = $this->getAttribs();
0135         $attribs['id'] = $element->getId();
0136 
0137         $image = $view->formImage($name, $element->getImageValue(), $attribs);
0138 
0139         if (null !== $tag) {
0140             // require_once 'Zend/Form/Decorator/HtmlTag.php';
0141             $decorator = new Zend_Form_Decorator_HtmlTag();
0142             $decorator->setOptions(array('tag' => $tag));
0143             $image = $decorator->render($image);
0144         }
0145 
0146         switch ($placement) {
0147             case self::PREPEND:
0148                 return $image . $separator . $content;
0149             case self::APPEND:
0150             default:
0151                 return $content . $separator . $image;
0152         }
0153     }
0154 }