web/Zend/Form/Decorator/Description.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_Description
       
    27  *
       
    28  * Accepts the options:
       
    29  * - separator: separator to use between label and content (defaults to PHP_EOL)
       
    30  * - placement: whether to append or prepend label to content (defaults to prepend)
       
    31  * - tag: if set, used to wrap the label in an additional HTML tag
       
    32  * - class: if set, override default class used with HTML tag
       
    33  * - escape: whether or not to escape description (true by default)
       
    34  *
       
    35  * Any other options passed will be used as HTML attributes of the HTML tag used.
       
    36  *
       
    37  * @category   Zend
       
    38  * @package    Zend_Form
       
    39  * @subpackage Decorator
       
    40  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    42  * @version    $Id: Description.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    43  */
       
    44 class Zend_Form_Decorator_Description extends Zend_Form_Decorator_Abstract
       
    45 {
       
    46     /**
       
    47      * Whether or not to escape the description
       
    48      * @var bool
       
    49      */
       
    50     protected $_escape;
       
    51 
       
    52     /**
       
    53      * Default placement: append
       
    54      * @var string
       
    55      */
       
    56     protected $_placement = 'APPEND';
       
    57 
       
    58     /**
       
    59      * HTML tag with which to surround description
       
    60      * @var string
       
    61      */
       
    62     protected $_tag;
       
    63 
       
    64     /**
       
    65      * Set HTML tag with which to surround description
       
    66      *
       
    67      * @param  string $tag
       
    68      * @return Zend_Form_Decorator_Description
       
    69      */
       
    70     public function setTag($tag)
       
    71     {
       
    72         $this->_tag = (string) $tag;
       
    73         return $this;
       
    74     }
       
    75 
       
    76     /**
       
    77      * Get HTML tag, if any, with which to surround description
       
    78      *
       
    79      * @return string
       
    80      */
       
    81     public function getTag()
       
    82     {
       
    83         if (null === $this->_tag) {
       
    84             $tag = $this->getOption('tag');
       
    85             if (null !== $tag) {
       
    86                 $this->removeOption('tag');
       
    87             } else {
       
    88                 $tag = 'p';
       
    89             }
       
    90 
       
    91             $this->setTag($tag);
       
    92             return $tag;
       
    93         }
       
    94 
       
    95         return $this->_tag;
       
    96     }
       
    97 
       
    98     /**
       
    99      * Get class with which to define description
       
   100      *
       
   101      * Defaults to 'hint'
       
   102      *
       
   103      * @return string
       
   104      */
       
   105     public function getClass()
       
   106     {
       
   107         $class = $this->getOption('class');
       
   108         if (null === $class) {
       
   109             $class = 'hint';
       
   110             $this->setOption('class', $class);
       
   111         }
       
   112 
       
   113         return $class;
       
   114     }
       
   115 
       
   116     /**
       
   117      * Set whether or not to escape description
       
   118      *
       
   119      * @param  bool $flag
       
   120      * @return Zend_Form_Decorator_Description
       
   121      */
       
   122     public function setEscape($flag)
       
   123     {
       
   124         $this->_escape = (bool) $flag;
       
   125         return $this;
       
   126     }
       
   127 
       
   128     /**
       
   129      * Get escape flag
       
   130      *
       
   131      * @return true
       
   132      */
       
   133     public function getEscape()
       
   134     {
       
   135         if (null === $this->_escape) {
       
   136             if (null !== ($escape = $this->getOption('escape'))) {
       
   137                 $this->setEscape($escape);
       
   138                 $this->removeOption('escape');
       
   139             } else {
       
   140                 $this->setEscape(true);
       
   141             }
       
   142         }
       
   143 
       
   144         return $this->_escape;
       
   145     }
       
   146 
       
   147     /**
       
   148      * Render a description
       
   149      *
       
   150      * @param  string $content
       
   151      * @return string
       
   152      */
       
   153     public function render($content)
       
   154     {
       
   155         $element = $this->getElement();
       
   156         $view    = $element->getView();
       
   157         if (null === $view) {
       
   158             return $content;
       
   159         }
       
   160 
       
   161         $description = $element->getDescription();
       
   162         $description = trim($description);
       
   163 
       
   164         if (!empty($description) && (null !== ($translator = $element->getTranslator()))) {
       
   165             $description = $translator->translate($description);
       
   166         }
       
   167 
       
   168         if (empty($description)) {
       
   169             return $content;
       
   170         }
       
   171 
       
   172         $separator = $this->getSeparator();
       
   173         $placement = $this->getPlacement();
       
   174         $tag       = $this->getTag();
       
   175         $class     = $this->getClass();
       
   176         $escape    = $this->getEscape();
       
   177 
       
   178         $options   = $this->getOptions();
       
   179 
       
   180         if ($escape) {
       
   181             $description = $view->escape($description);
       
   182         }
       
   183 
       
   184         if (!empty($tag)) {
       
   185             require_once 'Zend/Form/Decorator/HtmlTag.php';
       
   186             $options['tag'] = $tag;
       
   187             $decorator = new Zend_Form_Decorator_HtmlTag($options);
       
   188             $description = $decorator->render($description);
       
   189         }
       
   190 
       
   191         switch ($placement) {
       
   192             case self::PREPEND:
       
   193                 return $description . $separator . $content;
       
   194             case self::APPEND:
       
   195             default:
       
   196                 return $content . $separator . $description;
       
   197         }
       
   198     }
       
   199 }