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 /**
0026  * Zend_Form_Decorator_Description
0027  *
0028  * Accepts the options:
0029  * - separator: separator to use between label and content (defaults to PHP_EOL)
0030  * - placement: whether to append or prepend label to content (defaults to prepend)
0031  * - tag: if set, used to wrap the label in an additional HTML tag
0032  * - class: if set, override default class used with HTML tag
0033  * - escape: whether or not to escape description (true by default)
0034  *
0035  * Any other options passed will be used as HTML attributes of the HTML tag used.
0036  *
0037  * @category   Zend
0038  * @package    Zend_Form
0039  * @subpackage Decorator
0040  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0041  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0042  * @version    $Id$
0043  */
0044 class Zend_Form_Decorator_Description extends Zend_Form_Decorator_Abstract
0045 {
0046     /**
0047      * Whether or not to escape the description
0048      * @var bool
0049      */
0050     protected $_escape;
0051 
0052     /**
0053      * Default placement: append
0054      * @var string
0055      */
0056     protected $_placement = 'APPEND';
0057 
0058     /**
0059      * HTML tag with which to surround description
0060      * @var string
0061      */
0062     protected $_tag;
0063 
0064     /**
0065      * Set HTML tag with which to surround description
0066      *
0067      * @param  string $tag
0068      * @return Zend_Form_Decorator_Description
0069      */
0070     public function setTag($tag)
0071     {
0072         $this->_tag = (string) $tag;
0073         return $this;
0074     }
0075 
0076     /**
0077      * Get HTML tag, if any, with which to surround description
0078      *
0079      * @return string
0080      */
0081     public function getTag()
0082     {
0083         if (null === $this->_tag) {
0084             $tag = $this->getOption('tag');
0085             if (null !== $tag) {
0086                 $this->removeOption('tag');
0087             } else {
0088                 $tag = 'p';
0089             }
0090 
0091             $this->setTag($tag);
0092             return $tag;
0093         }
0094 
0095         return $this->_tag;
0096     }
0097 
0098     /**
0099      * Get class with which to define description
0100      *
0101      * Defaults to 'hint'
0102      *
0103      * @return string
0104      */
0105     public function getClass()
0106     {
0107         $class = $this->getOption('class');
0108         if (null === $class) {
0109             $class = 'hint';
0110             $this->setOption('class', $class);
0111         }
0112 
0113         return $class;
0114     }
0115 
0116     /**
0117      * Set whether or not to escape description
0118      *
0119      * @param  bool $flag
0120      * @return Zend_Form_Decorator_Description
0121      */
0122     public function setEscape($flag)
0123     {
0124         $this->_escape = (bool) $flag;
0125         return $this;
0126     }
0127 
0128     /**
0129      * Get escape flag
0130      *
0131      * @return true
0132      */
0133     public function getEscape()
0134     {
0135         if (null === $this->_escape) {
0136             if (null !== ($escape = $this->getOption('escape'))) {
0137                 $this->setEscape($escape);
0138                 $this->removeOption('escape');
0139             } else {
0140                 $this->setEscape(true);
0141             }
0142         }
0143 
0144         return $this->_escape;
0145     }
0146 
0147     /**
0148      * Render a description
0149      *
0150      * @param  string $content
0151      * @return string
0152      */
0153     public function render($content)
0154     {
0155         $element = $this->getElement();
0156         $view    = $element->getView();
0157         if (null === $view) {
0158             return $content;
0159         }
0160 
0161         $description = $element->getDescription();
0162         $description = trim($description);
0163 
0164         if (!empty($description) && (null !== ($translator = $element->getTranslator()))) {
0165             $description = $translator->translate($description);
0166         }
0167 
0168         if (empty($description)) {
0169             return $content;
0170         }
0171 
0172         $separator = $this->getSeparator();
0173         $placement = $this->getPlacement();
0174         $tag       = $this->getTag();
0175         $class     = $this->getClass();
0176         $escape    = $this->getEscape();
0177 
0178         $options   = $this->getOptions();
0179 
0180         if ($escape) {
0181             $description = $view->escape($description);
0182         }
0183 
0184         if (!empty($tag)) {
0185             // require_once 'Zend/Form/Decorator/HtmlTag.php';
0186             $options['tag'] = $tag;
0187             $decorator = new Zend_Form_Decorator_HtmlTag($options);
0188             $description = $decorator->render($description);
0189         }
0190 
0191         switch ($placement) {
0192             case self::PREPEND:
0193                 return $description . $separator . $content;
0194             case self::APPEND:
0195             default:
0196                 return $content . $separator . $description;
0197         }
0198     }
0199 }