File indexing completed on 2024-12-29 05:27:31

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_Controller
0017  * @subpackage Zend_Controller_Action_Helper
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  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Controller_Action_Helper_Abstract
0025  */
0026 // require_once 'Zend/Controller/Action/Helper/Abstract.php';
0027 
0028 /**
0029  * @see Zend_View
0030  */
0031 // require_once 'Zend/View.php';
0032 
0033 /**
0034  * View script integration
0035  *
0036  * Zend_Controller_Action_Helper_ViewRenderer provides transparent view
0037  * integration for action controllers. It allows you to create a view object
0038  * once, and populate it throughout all actions. Several global options may be
0039  * set:
0040  *
0041  * - noController: if set true, render() will not look for view scripts in
0042  *   subdirectories named after the controller
0043  * - viewSuffix: what view script filename suffix to use
0044  *
0045  * The helper autoinitializes the action controller view preDispatch(). It
0046  * determines the path to the class file, and then determines the view base
0047  * directory from there. It also uses the module name as a class prefix for
0048  * helpers and views such that if your module name is 'Search', it will set the
0049  * helper class prefix to 'Search_View_Helper' and the filter class prefix to ;
0050  * 'Search_View_Filter'.
0051  *
0052  * Usage:
0053  * <code>
0054  * // In your bootstrap:
0055  * Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
0056  *
0057  * // In your action controller methods:
0058  * $viewHelper = $this->_helper->getHelper('view');
0059  *
0060  * // Don't use controller subdirectories
0061  * $viewHelper->setNoController(true);
0062  *
0063  * // Specify a different script to render:
0064  * $this->_helper->viewRenderer('form');
0065  *
0066  * </code>
0067  *
0068  * @uses       Zend_Controller_Action_Helper_Abstract
0069  * @package    Zend_Controller
0070  * @subpackage Zend_Controller_Action_Helper
0071  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0072  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0073  */
0074 class Zend_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Action_Helper_Abstract
0075 {
0076     /**
0077      * @var Zend_View_Interface
0078      */
0079     public $view;
0080 
0081     /**
0082      * Word delimiters
0083      * @var array
0084      */
0085     protected $_delimiters;
0086 
0087     /**
0088      * @var Zend_Filter_Inflector
0089      */
0090     protected $_inflector;
0091 
0092     /**
0093      * Inflector target
0094      * @var string
0095      */
0096     protected $_inflectorTarget = '';
0097 
0098     /**
0099      * Current module directory
0100      * @var string
0101      */
0102     protected $_moduleDir = '';
0103 
0104     /**
0105      * Whether or not to autorender using controller name as subdirectory;
0106      * global setting (not reset at next invocation)
0107      * @var boolean
0108      */
0109     protected $_neverController = false;
0110 
0111     /**
0112      * Whether or not to autorender postDispatch; global setting (not reset at
0113      * next invocation)
0114      * @var boolean
0115      */
0116     protected $_neverRender     = false;
0117 
0118     /**
0119      * Whether or not to use a controller name as a subdirectory when rendering
0120      * @var boolean
0121      */
0122     protected $_noController    = false;
0123 
0124     /**
0125      * Whether or not to autorender postDispatch; per controller/action setting (reset
0126      * at next invocation)
0127      * @var boolean
0128      */
0129     protected $_noRender        = false;
0130 
0131     /**
0132      * Characters representing path delimiters in the controller
0133      * @var string|array
0134      */
0135     protected $_pathDelimiters;
0136 
0137     /**
0138      * Which named segment of the response to utilize
0139      * @var string
0140      */
0141     protected $_responseSegment = null;
0142 
0143     /**
0144      * Which action view script to render
0145      * @var string
0146      */
0147     protected $_scriptAction    = null;
0148 
0149     /**
0150      * View object basePath
0151      * @var string
0152      */
0153     protected $_viewBasePathSpec = ':moduleDir/views';
0154 
0155     /**
0156      * View script path specification string
0157      * @var string
0158      */
0159     protected $_viewScriptPathSpec = ':controller/:action.:suffix';
0160 
0161     /**
0162      * View script path specification string, minus controller segment
0163      * @var string
0164      */
0165     protected $_viewScriptPathNoControllerSpec = ':action.:suffix';
0166 
0167     /**
0168      * View script suffix
0169      * @var string
0170      */
0171     protected $_viewSuffix      = 'phtml';
0172 
0173     /**
0174      * Constructor
0175      *
0176      * Optionally set view object and options.
0177      *
0178      * @param  Zend_View_Interface $view
0179      * @param  array               $options
0180      * @return void
0181      */
0182     public function __construct(Zend_View_Interface $view = null, array $options = array())
0183     {
0184         if (null !== $view) {
0185             $this->setView($view);
0186         }
0187 
0188         if (!empty($options)) {
0189             $this->_setOptions($options);
0190         }
0191     }
0192 
0193     /**
0194      * Clone - also make sure the view is cloned.
0195      *
0196      * @return void
0197      */
0198     public function __clone()
0199     {
0200         if (isset($this->view) && $this->view instanceof Zend_View_Interface) {
0201             $this->view = clone $this->view;
0202 
0203         }
0204     }
0205 
0206     /**
0207      * Set the view object
0208      *
0209      * @param  Zend_View_Interface $view
0210      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0211      */
0212     public function setView(Zend_View_Interface $view)
0213     {
0214         $this->view = $view;
0215         return $this;
0216     }
0217 
0218     /**
0219      * Get current module name
0220      *
0221      * @return string
0222      */
0223     public function getModule()
0224     {
0225         $request = $this->getRequest();
0226         $module  = $request->getModuleName();
0227         if (null === $module) {
0228             $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
0229         }
0230 
0231         return $module;
0232     }
0233 
0234     /**
0235      * Get module directory
0236      *
0237      * @throws Zend_Controller_Action_Exception
0238      * @return string
0239      */
0240     public function getModuleDirectory()
0241     {
0242         $module    = $this->getModule();
0243         $moduleDir = $this->getFrontController()->getControllerDirectory($module);
0244         if ((null === $moduleDir) || is_array($moduleDir)) {
0245             /**
0246              * @see Zend_Controller_Action_Exception
0247              */
0248             // require_once 'Zend/Controller/Action/Exception.php';
0249             throw new Zend_Controller_Action_Exception('ViewRenderer cannot locate module directory for module "' . $module . '"');
0250         }
0251         $this->_moduleDir = dirname($moduleDir);
0252         return $this->_moduleDir;
0253     }
0254 
0255     /**
0256      * Get inflector
0257      *
0258      * @return Zend_Filter_Inflector
0259      */
0260     public function getInflector()
0261     {
0262         if (null === $this->_inflector) {
0263             /**
0264              * @see Zend_Filter_Inflector
0265              */
0266             // require_once 'Zend/Filter/Inflector.php';
0267             /**
0268              * @see Zend_Filter_PregReplace
0269              */
0270             // require_once 'Zend/Filter/PregReplace.php';
0271             /**
0272              * @see Zend_Filter_Word_UnderscoreToSeparator
0273              */
0274             // require_once 'Zend/Filter/Word/UnderscoreToSeparator.php';
0275             $this->_inflector = new Zend_Filter_Inflector();
0276             $this->_inflector->setStaticRuleReference('moduleDir', $this->_moduleDir) // moduleDir must be specified before the less specific 'module'
0277                  ->addRules(array(
0278                      ':module'     => array('Word_CamelCaseToDash', 'StringToLower'),
0279                      ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower', new Zend_Filter_PregReplace('/\./', '-')),
0280                      ':action'     => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower'),
0281                  ))
0282                  ->setStaticRuleReference('suffix', $this->_viewSuffix)
0283                  ->setTargetReference($this->_inflectorTarget);
0284         }
0285 
0286         // Ensure that module directory is current
0287         $this->getModuleDirectory();
0288 
0289         return $this->_inflector;
0290     }
0291 
0292     /**
0293      * Set inflector
0294      *
0295      * @param  Zend_Filter_Inflector $inflector
0296      * @param  boolean               $reference Whether the moduleDir, target, and suffix should be set as references to ViewRenderer properties
0297      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0298      */
0299     public function setInflector(Zend_Filter_Inflector $inflector, $reference = false)
0300     {
0301         $this->_inflector = $inflector;
0302         if ($reference) {
0303             $this->_inflector->setStaticRuleReference('suffix', $this->_viewSuffix)
0304                  ->setStaticRuleReference('moduleDir', $this->_moduleDir)
0305                  ->setTargetReference($this->_inflectorTarget);
0306         }
0307         return $this;
0308     }
0309 
0310     /**
0311      * Set inflector target
0312      *
0313      * @param  string $target
0314      * @return void
0315      */
0316     protected function _setInflectorTarget($target)
0317     {
0318         $this->_inflectorTarget = (string) $target;
0319     }
0320 
0321     /**
0322      * Set internal module directory representation
0323      *
0324      * @param  string $dir
0325      * @return void
0326      */
0327     protected function _setModuleDir($dir)
0328     {
0329         $this->_moduleDir = (string) $dir;
0330     }
0331 
0332     /**
0333      * Get internal module directory representation
0334      *
0335      * @return string
0336      */
0337     protected function _getModuleDir()
0338     {
0339         return $this->_moduleDir;
0340     }
0341 
0342     /**
0343      * Generate a class prefix for helper and filter classes
0344      *
0345      * @return string
0346      */
0347     protected function _generateDefaultPrefix()
0348     {
0349         $default = 'Zend_View';
0350         if (null === $this->_actionController) {
0351             return $default;
0352         }
0353 
0354         $class = get_class($this->_actionController);
0355 
0356         if (!strstr($class, '_')) {
0357             return $default;
0358         }
0359 
0360         $module = $this->getModule();
0361         if ('default' == $module) {
0362             return $default;
0363         }
0364 
0365         $prefix = substr($class, 0, strpos($class, '_')) . '_View';
0366 
0367         return $prefix;
0368     }
0369 
0370     /**
0371      * Retrieve base path based on location of current action controller
0372      *
0373      * @return string
0374      */
0375     protected function _getBasePath()
0376     {
0377         if (null === $this->_actionController) {
0378             return './views';
0379         }
0380 
0381         $inflector = $this->getInflector();
0382         $this->_setInflectorTarget($this->getViewBasePathSpec());
0383 
0384         $dispatcher = $this->getFrontController()->getDispatcher();
0385         $request = $this->getRequest();
0386 
0387         $parts = array(
0388             'module'     => (($moduleName = $request->getModuleName()) != '') ? $dispatcher->formatModuleName($moduleName) : $moduleName,
0389             'controller' => $request->getControllerName(),
0390             'action'     => $dispatcher->formatActionName($request->getActionName())
0391             );
0392 
0393         $path = $inflector->filter($parts);
0394         return $path;
0395     }
0396 
0397     /**
0398      * Set options
0399      *
0400      * @param  array $options
0401      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0402      */
0403     protected function _setOptions(array $options)
0404     {
0405         foreach ($options as $key => $value)
0406         {
0407             switch ($key) {
0408                 case 'neverRender':
0409                 case 'neverController':
0410                 case 'noController':
0411                 case 'noRender':
0412                     $property = '_' . $key;
0413                     $this->{$property} = ($value) ? true : false;
0414                     break;
0415                 case 'responseSegment':
0416                 case 'scriptAction':
0417                 case 'viewBasePathSpec':
0418                 case 'viewScriptPathSpec':
0419                 case 'viewScriptPathNoControllerSpec':
0420                 case 'viewSuffix':
0421                     $property = '_' . $key;
0422                     $this->{$property} = (string) $value;
0423                     break;
0424                 default:
0425                     break;
0426             }
0427         }
0428 
0429         return $this;
0430     }
0431 
0432     /**
0433      * Initialize the view object
0434      *
0435      * $options may contain the following keys:
0436      * - neverRender - flag dis/enabling postDispatch() autorender (affects all subsequent calls)
0437      * - noController - flag indicating whether or not to look for view scripts in subdirectories named after the controller
0438      * - noRender - flag indicating whether or not to autorender postDispatch()
0439      * - responseSegment - which named response segment to render a view script to
0440      * - scriptAction - what action script to render
0441      * - viewBasePathSpec - specification to use for determining view base path
0442      * - viewScriptPathSpec - specification to use for determining view script paths
0443      * - viewScriptPathNoControllerSpec - specification to use for determining view script paths when noController flag is set
0444      * - viewSuffix - what view script filename suffix to use
0445      *
0446      * @param  string $path
0447      * @param  string $prefix
0448      * @param  array  $options
0449      * @throws Zend_Controller_Action_Exception
0450      * @return void
0451      */
0452     public function initView($path = null, $prefix = null, array $options = array())
0453     {
0454         if (null === $this->view) {
0455             $this->setView(new Zend_View());
0456         }
0457 
0458         // Reset some flags every time
0459         $options['noController'] = (isset($options['noController'])) ? $options['noController'] : false;
0460         $options['noRender']     = (isset($options['noRender'])) ? $options['noRender'] : false;
0461         $this->_scriptAction     = null;
0462         $this->_responseSegment  = null;
0463 
0464         // Set options first; may be used to determine other initializations
0465         $this->_setOptions($options);
0466 
0467         // Get base view path
0468         if (empty($path)) {
0469             $path = $this->_getBasePath();
0470             if (empty($path)) {
0471                 /**
0472                  * @see Zend_Controller_Action_Exception
0473                  */
0474                 // require_once 'Zend/Controller/Action/Exception.php';
0475                 throw new Zend_Controller_Action_Exception('ViewRenderer initialization failed: retrieved view base path is empty');
0476             }
0477         }
0478 
0479         if (null === $prefix) {
0480             $prefix = $this->_generateDefaultPrefix();
0481         }
0482 
0483         // Determine if this path has already been registered
0484         $currentPaths = $this->view->getScriptPaths();
0485         $path         = str_replace(array('/', '\\'), '/', $path);
0486         $pathExists   = false;
0487         foreach ($currentPaths as $tmpPath) {
0488             $tmpPath = str_replace(array('/', '\\'), '/', $tmpPath);
0489             if (strstr($tmpPath, $path)) {
0490                 $pathExists = true;
0491                 break;
0492             }
0493         }
0494         if (!$pathExists) {
0495             $this->view->addBasePath($path, $prefix);
0496         }
0497 
0498         // Register view with action controller (unless already registered)
0499         if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
0500             $this->_actionController->view       = $this->view;
0501             $this->_actionController->viewSuffix = $this->_viewSuffix;
0502         }
0503     }
0504 
0505     /**
0506      * init - initialize view
0507      *
0508      * @return void
0509      */
0510     public function init()
0511     {
0512         if ($this->getFrontController()->getParam('noViewRenderer')) {
0513             return;
0514         }
0515 
0516         $this->initView();
0517     }
0518 
0519     /**
0520      * Set view basePath specification
0521      *
0522      * Specification can contain one or more of the following:
0523      * - :moduleDir - current module directory
0524      * - :controller - name of current controller in the request
0525      * - :action - name of current action in the request
0526      * - :module - name of current module in the request
0527      *
0528      * @param  string $path
0529      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0530      */
0531     public function setViewBasePathSpec($path)
0532     {
0533         $this->_viewBasePathSpec = (string) $path;
0534         return $this;
0535     }
0536 
0537     /**
0538      * Retrieve the current view basePath specification string
0539      *
0540      * @return string
0541      */
0542     public function getViewBasePathSpec()
0543     {
0544         return $this->_viewBasePathSpec;
0545     }
0546 
0547     /**
0548      * Set view script path specification
0549      *
0550      * Specification can contain one or more of the following:
0551      * - :moduleDir - current module directory
0552      * - :controller - name of current controller in the request
0553      * - :action - name of current action in the request
0554      * - :module - name of current module in the request
0555      *
0556      * @param  string $path
0557      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0558      */
0559     public function setViewScriptPathSpec($path)
0560     {
0561         $this->_viewScriptPathSpec = (string) $path;
0562         return $this;
0563     }
0564 
0565     /**
0566      * Retrieve the current view script path specification string
0567      *
0568      * @return string
0569      */
0570     public function getViewScriptPathSpec()
0571     {
0572         return $this->_viewScriptPathSpec;
0573     }
0574 
0575     /**
0576      * Set view script path specification (no controller variant)
0577      *
0578      * Specification can contain one or more of the following:
0579      * - :moduleDir - current module directory
0580      * - :controller - name of current controller in the request
0581      * - :action - name of current action in the request
0582      * - :module - name of current module in the request
0583      *
0584      * :controller will likely be ignored in this variant.
0585      *
0586      * @param  string $path
0587      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0588      */
0589     public function setViewScriptPathNoControllerSpec($path)
0590     {
0591         $this->_viewScriptPathNoControllerSpec = (string) $path;
0592         return $this;
0593     }
0594 
0595     /**
0596      * Retrieve the current view script path specification string (no controller variant)
0597      *
0598      * @return string
0599      */
0600     public function getViewScriptPathNoControllerSpec()
0601     {
0602         return $this->_viewScriptPathNoControllerSpec;
0603     }
0604 
0605     /**
0606      * Get a view script based on an action and/or other variables
0607      *
0608      * Uses values found in current request if no values passed in $vars.
0609      *
0610      * If {@link $_noController} is set, uses {@link $_viewScriptPathNoControllerSpec};
0611      * otherwise, uses {@link $_viewScriptPathSpec}.
0612      *
0613      * @param  string $action
0614      * @param  array  $vars
0615      * @return string
0616      */
0617     public function getViewScript($action = null, array $vars = array())
0618     {
0619         $request = $this->getRequest();
0620         if ((null === $action) && (!isset($vars['action']))) {
0621             $action = $this->getScriptAction();
0622             if (null === $action) {
0623                 $action = $request->getActionName();
0624             }
0625             $vars['action'] = $action;
0626         } elseif (null !== $action) {
0627             $vars['action'] = $action;
0628         }
0629         
0630         $replacePattern = array('/[^a-z0-9]+$/i', '/^[^a-z0-9]+/i');
0631         $vars['action'] = preg_replace($replacePattern, '', $vars['action']);
0632 
0633         $inflector = $this->getInflector();
0634         if ($this->getNoController() || $this->getNeverController()) {
0635             $this->_setInflectorTarget($this->getViewScriptPathNoControllerSpec());
0636         } else {
0637             $this->_setInflectorTarget($this->getViewScriptPathSpec());
0638         }
0639         return $this->_translateSpec($vars);
0640     }
0641 
0642     /**
0643      * Set the neverRender flag (i.e., globally dis/enable autorendering)
0644      *
0645      * @param  boolean $flag
0646      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0647      */
0648     public function setNeverRender($flag = true)
0649     {
0650         $this->_neverRender = ($flag) ? true : false;
0651         return $this;
0652     }
0653 
0654     /**
0655      * Retrieve neverRender flag value
0656      *
0657      * @return boolean
0658      */
0659     public function getNeverRender()
0660     {
0661         return $this->_neverRender;
0662     }
0663 
0664     /**
0665      * Set the noRender flag (i.e., whether or not to autorender)
0666      *
0667      * @param  boolean $flag
0668      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0669      */
0670     public function setNoRender($flag = true)
0671     {
0672         $this->_noRender = ($flag) ? true : false;
0673         return $this;
0674     }
0675 
0676     /**
0677      * Retrieve noRender flag value
0678      *
0679      * @return boolean
0680      */
0681     public function getNoRender()
0682     {
0683         return $this->_noRender;
0684     }
0685 
0686     /**
0687      * Set the view script to use
0688      *
0689      * @param  string $name
0690      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0691      */
0692     public function setScriptAction($name)
0693     {
0694         $this->_scriptAction = (string) $name;
0695         return $this;
0696     }
0697 
0698     /**
0699      * Retrieve view script name
0700      *
0701      * @return string
0702      */
0703     public function getScriptAction()
0704     {
0705         return $this->_scriptAction;
0706     }
0707 
0708     /**
0709      * Set the response segment name
0710      *
0711      * @param  string $name
0712      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0713      */
0714     public function setResponseSegment($name)
0715     {
0716         if (null === $name) {
0717             $this->_responseSegment = null;
0718         } else {
0719             $this->_responseSegment = (string) $name;
0720         }
0721 
0722         return $this;
0723     }
0724 
0725     /**
0726      * Retrieve named response segment name
0727      *
0728      * @return string
0729      */
0730     public function getResponseSegment()
0731     {
0732         return $this->_responseSegment;
0733     }
0734 
0735     /**
0736      * Set the noController flag (i.e., whether or not to render into controller subdirectories)
0737      *
0738      * @param  boolean $flag
0739      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0740      */
0741     public function setNoController($flag = true)
0742     {
0743         $this->_noController = ($flag) ? true : false;
0744         return $this;
0745     }
0746 
0747     /**
0748      * Retrieve noController flag value
0749      *
0750      * @return boolean
0751      */
0752     public function getNoController()
0753     {
0754         return $this->_noController;
0755     }
0756 
0757     /**
0758      * Set the neverController flag (i.e., whether or not to render into controller subdirectories)
0759      *
0760      * @param  boolean $flag
0761      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0762      */
0763     public function setNeverController($flag = true)
0764     {
0765         $this->_neverController = ($flag) ? true : false;
0766         return $this;
0767     }
0768 
0769     /**
0770      * Retrieve neverController flag value
0771      *
0772      * @return boolean
0773      */
0774     public function getNeverController()
0775     {
0776         return $this->_neverController;
0777     }
0778 
0779     /**
0780      * Set view script suffix
0781      *
0782      * @param  string $suffix
0783      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0784      */
0785     public function setViewSuffix($suffix)
0786     {
0787         $this->_viewSuffix = (string) $suffix;
0788         return $this;
0789     }
0790 
0791     /**
0792      * Get view script suffix
0793      *
0794      * @return string
0795      */
0796     public function getViewSuffix()
0797     {
0798         return $this->_viewSuffix;
0799     }
0800 
0801     /**
0802      * Set options for rendering a view script
0803      *
0804      * @param  string  $action       View script to render
0805      * @param  string  $name         Response named segment to render to
0806      * @param  boolean $noController Whether or not to render within a subdirectory named after the controller
0807      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
0808      */
0809     public function setRender($action = null, $name = null, $noController = null)
0810     {
0811         if (null !== $action) {
0812             $this->setScriptAction($action);
0813         }
0814 
0815         if (null !== $name) {
0816             $this->setResponseSegment($name);
0817         }
0818 
0819         if (null !== $noController) {
0820             $this->setNoController($noController);
0821         }
0822 
0823         return $this;
0824     }
0825 
0826     /**
0827      * Inflect based on provided vars
0828      *
0829      * Allowed variables are:
0830      * - :moduleDir - current module directory
0831      * - :module - current module name
0832      * - :controller - current controller name
0833      * - :action - current action name
0834      * - :suffix - view script file suffix
0835      *
0836      * @param  array $vars
0837      * @return string
0838      */
0839     protected function _translateSpec(array $vars = array())
0840     {
0841         $inflector  = $this->getInflector();
0842         $request    = $this->getRequest();
0843         $dispatcher = $this->getFrontController()->getDispatcher();
0844 
0845         // Format module name
0846         $module = $dispatcher->formatModuleName($request->getModuleName());
0847 
0848         // Format controller name
0849         // require_once 'Zend/Filter/Word/CamelCaseToDash.php';
0850         $filter     = new Zend_Filter_Word_CamelCaseToDash();
0851         $controller = $filter->filter($request->getControllerName());
0852         $controller = $dispatcher->formatControllerName($controller);
0853         if ('Controller' == substr($controller, -10)) {
0854             $controller = substr($controller, 0, -10);
0855         }
0856 
0857         // Format action name
0858         $action = $dispatcher->formatActionName($request->getActionName());
0859 
0860         $params     = compact('module', 'controller', 'action');
0861         foreach ($vars as $key => $value) {
0862             switch ($key) {
0863                 case 'module':
0864                 case 'controller':
0865                 case 'action':
0866                 case 'moduleDir':
0867                 case 'suffix':
0868                     $params[$key] = (string) $value;
0869                     break;
0870                 default:
0871                     break;
0872             }
0873         }
0874 
0875         if (isset($params['suffix'])) {
0876             $origSuffix = $this->getViewSuffix();
0877             $this->setViewSuffix($params['suffix']);
0878         }
0879         if (isset($params['moduleDir'])) {
0880             $origModuleDir = $this->_getModuleDir();
0881             $this->_setModuleDir($params['moduleDir']);
0882         }
0883 
0884         $filtered = $inflector->filter($params);
0885 
0886         if (isset($params['suffix'])) {
0887             $this->setViewSuffix($origSuffix);
0888         }
0889         if (isset($params['moduleDir'])) {
0890             $this->_setModuleDir($origModuleDir);
0891         }
0892 
0893         return $filtered;
0894     }
0895 
0896     /**
0897      * Render a view script (optionally to a named response segment)
0898      *
0899      * Sets the noRender flag to true when called.
0900      *
0901      * @param  string $script
0902      * @param  string $name
0903      * @return void
0904      */
0905     public function renderScript($script, $name = null)
0906     {
0907         if (null === $name) {
0908             $name = $this->getResponseSegment();
0909         }
0910 
0911         $this->getResponse()->appendBody(
0912             $this->view->render($script),
0913             $name
0914         );
0915 
0916         $this->setNoRender();
0917     }
0918 
0919     /**
0920      * Render a view based on path specifications
0921      *
0922      * Renders a view based on the view script path specifications.
0923      *
0924      * @param  string  $action
0925      * @param  string  $name
0926      * @param  boolean $noController
0927      * @return void
0928      */
0929     public function render($action = null, $name = null, $noController = null)
0930     {
0931         $this->setRender($action, $name, $noController);
0932         $path = $this->getViewScript();
0933         $this->renderScript($path, $name);
0934     }
0935 
0936     /**
0937      * Render a script based on specification variables
0938      *
0939      * Pass an action, and one or more specification variables (view script suffix)
0940      * to determine the view script path, and render that script.
0941      *
0942      * @param  string $action
0943      * @param  array  $vars
0944      * @param  string $name
0945      * @return void
0946      */
0947     public function renderBySpec($action = null, array $vars = array(), $name = null)
0948     {
0949         if (null !== $name) {
0950             $this->setResponseSegment($name);
0951         }
0952 
0953         $path = $this->getViewScript($action, $vars);
0954 
0955         $this->renderScript($path);
0956     }
0957 
0958     /**
0959      * postDispatch - auto render a view
0960      *
0961      * Only autorenders if:
0962      * - _noRender is false
0963      * - action controller is present
0964      * - request has not been re-dispatched (i.e., _forward() has not been called)
0965      * - response is not a redirect
0966      *
0967      * @return void
0968      */
0969     public function postDispatch()
0970     {
0971         if ($this->_shouldRender()) {
0972             $this->render();
0973         }
0974     }
0975 
0976     /**
0977      * Should the ViewRenderer render a view script?
0978      *
0979      * @return boolean
0980      */
0981     protected function _shouldRender()
0982     {
0983         return (!$this->getFrontController()->getParam('noViewRenderer')
0984             && !$this->_neverRender
0985             && !$this->_noRender
0986             && (null !== $this->_actionController)
0987             && $this->getRequest()->isDispatched()
0988             && !$this->getResponse()->isRedirect()
0989         );
0990     }
0991 
0992     /**
0993      * Use this helper as a method; proxies to setRender()
0994      *
0995      * @param  string  $action
0996      * @param  string  $name
0997      * @param  boolean $noController
0998      * @return void
0999      */
1000     public function direct($action = null, $name = null, $noController = null)
1001     {
1002         $this->setRender($action, $name, $noController);
1003     }
1004 }