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_ViewScript
0027  *
0028  * Render a view script as a decorator
0029  *
0030  * Accepts the options:
0031  * - separator: separator to use between view script content and provided content (defaults to PHP_EOL)
0032  * - placement: whether to append or prepend view script content to provided content (defaults to prepend)
0033  * - viewScript: view script to use
0034  * - viewModule: module that view script is in (optional)
0035  *
0036  * The view script is rendered as a partial; the element being decorated is
0037  * passed in as the 'element' variable:
0038  * <code>
0039  * // in view script:
0040  * echo $this->element->getLabel();
0041  * </code>
0042  *
0043  * Any options other than separator, placement, viewScript, and viewModule are passed to
0044  * the partial as local variables.
0045  *
0046  * @category   Zend
0047  * @package    Zend_Form
0048  * @subpackage Decorator
0049  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0050  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0051  * @version    $Id$
0052  */
0053 class Zend_Form_Decorator_ViewScript extends Zend_Form_Decorator_Abstract
0054 {
0055     /**
0056      * Default placement: append
0057      * @var string
0058      */
0059     protected $_placement = 'APPEND';
0060 
0061     /**
0062      * View script to render
0063      * @var string
0064      */
0065     protected $_viewScript;
0066 
0067     /**
0068      * View script module
0069      * @var string
0070      */
0071     protected $_viewModule;
0072 
0073     /**
0074      * Set view script
0075      *
0076      * @param  string $script
0077      * @return Zend_Form_Decorator_ViewScript
0078      */
0079     public function setViewScript($script)
0080     {
0081         $this->_viewScript = (string) $script;
0082         return $this;
0083     }
0084 
0085     /**
0086      * Get view script
0087      *
0088      * @return string|null
0089      */
0090     public function getViewScript()
0091     {
0092         if (null === $this->_viewScript) {
0093             if (null !== ($element = $this->getElement())) {
0094                 if (null !== ($viewScript = $element->getAttrib('viewScript'))) {
0095                     $this->setViewScript($viewScript);
0096                     return $viewScript;
0097                 }
0098             }
0099 
0100             if (null !== ($viewScript = $this->getOption('viewScript'))) {
0101                 $this->setViewScript($viewScript)
0102                      ->removeOption('viewScript');
0103             }
0104         }
0105 
0106         return $this->_viewScript;
0107     }
0108 
0109     /**
0110      * Set view script module
0111      *
0112      * @param  string $module
0113      * @return Zend_Form_Decorator_ViewScript
0114      */
0115     public function setViewModule($viewModule)
0116     {
0117         $this->_viewModule = (string) $viewModule;
0118         return $this;
0119     }
0120 
0121     /**
0122      * Get view script module
0123      *
0124      * @return string|null
0125      */
0126     public function getViewModule()
0127     {
0128         if (null === $this->_viewModule) {
0129             if (null !== ($element = $this->getElement())) {
0130                 if (null !== ($viewModule = $element->getAttrib('viewModule'))) {
0131                     $this->setViewModule($viewModule);
0132                     return $viewModule;
0133                 }
0134             }
0135 
0136             if (null !== ($viewModule = $this->getOption('viewModule'))) {
0137                 $this->setViewModule($viewModule)
0138                      ->removeOption('viewModule');
0139             }
0140         }
0141 
0142         return $this->_viewModule;
0143     }
0144 
0145     /**
0146      * Render a view script
0147      *
0148      * @param  string $content
0149      * @return string
0150      */
0151     public function render($content)
0152     {
0153         $element = $this->getElement();
0154         $view    = $element->getView();
0155         if (null === $view) {
0156             return $content;
0157         }
0158 
0159         $viewScript = $this->getViewScript();
0160         if (empty($viewScript)) {
0161             // require_once 'Zend/Form/Exception.php';
0162             throw new Zend_Form_Exception('No view script registered with ViewScript decorator');
0163         }
0164 
0165         $separator = $this->getSeparator();
0166         $placement = $this->getPlacement();
0167 
0168         $vars              = $this->getOptions();
0169         $vars['element']   = $element;
0170         $vars['content']   = $content;
0171         $vars['decorator'] = $this;
0172 
0173         $viewModule = $this->getViewModule();
0174         if (empty($viewModule)) {
0175             $renderedContent = $view->partial($viewScript, $vars);
0176         } else {
0177             $renderedContent = $view->partial($viewScript, $viewModule, $vars);
0178         }
0179 
0180         // Get placement again to see if it has changed
0181         $placement = $this->getPlacement();
0182 
0183         switch ($placement) {
0184             case self::PREPEND:
0185                 return $renderedContent . $separator . $content;
0186             case self::APPEND:
0187                 return $content . $separator . $renderedContent;
0188             default:
0189                 return $renderedContent;
0190         }
0191     }
0192 }