web/lib/Zend/Controller/Action/Helper/ViewRenderer.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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_Controller
       
    17  * @subpackage Zend_Controller_Action_Helper
       
    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  * @version    $Id: ViewRenderer.php 20261 2010-01-13 18:55:25Z matthew $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Controller_Action_Helper_Abstract
       
    25  */
       
    26 require_once 'Zend/Controller/Action/Helper/Abstract.php';
       
    27 
       
    28 /**
       
    29  * @see Zend_View
       
    30  */
       
    31 require_once 'Zend/View.php';
       
    32 
       
    33 /**
       
    34  * View script integration
       
    35  *
       
    36  * Zend_Controller_Action_Helper_ViewRenderer provides transparent view
       
    37  * integration for action controllers. It allows you to create a view object
       
    38  * once, and populate it throughout all actions. Several global options may be
       
    39  * set:
       
    40  *
       
    41  * - noController: if set true, render() will not look for view scripts in
       
    42  *   subdirectories named after the controller
       
    43  * - viewSuffix: what view script filename suffix to use
       
    44  *
       
    45  * The helper autoinitializes the action controller view preDispatch(). It
       
    46  * determines the path to the class file, and then determines the view base
       
    47  * directory from there. It also uses the module name as a class prefix for
       
    48  * helpers and views such that if your module name is 'Search', it will set the
       
    49  * helper class prefix to 'Search_View_Helper' and the filter class prefix to ;
       
    50  * 'Search_View_Filter'.
       
    51  *
       
    52  * Usage:
       
    53  * <code>
       
    54  * // In your bootstrap:
       
    55  * Zend_Controller_Action_HelperBroker::addHelper(new Zend_Controller_Action_Helper_ViewRenderer());
       
    56  *
       
    57  * // In your action controller methods:
       
    58  * $viewHelper = $this->_helper->getHelper('view');
       
    59  *
       
    60  * // Don't use controller subdirectories
       
    61  * $viewHelper->setNoController(true);
       
    62  *
       
    63  * // Specify a different script to render:
       
    64  * $this->_helper->viewRenderer('form');
       
    65  *
       
    66  * </code>
       
    67  *
       
    68  * @uses       Zend_Controller_Action_Helper_Abstract
       
    69  * @package    Zend_Controller
       
    70  * @subpackage Zend_Controller_Action_Helper
       
    71  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    72  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    73  */
       
    74 class Zend_Controller_Action_Helper_ViewRenderer extends Zend_Controller_Action_Helper_Abstract
       
    75 {
       
    76     /**
       
    77      * @var Zend_View_Interface
       
    78      */
       
    79     public $view;
       
    80 
       
    81     /**
       
    82      * Word delimiters
       
    83      * @var array
       
    84      */
       
    85     protected $_delimiters;
       
    86 
       
    87     /**
       
    88      * @var Zend_Filter_Inflector
       
    89      */
       
    90     protected $_inflector;
       
    91 
       
    92     /**
       
    93      * Inflector target
       
    94      * @var string
       
    95      */
       
    96     protected $_inflectorTarget = '';
       
    97 
       
    98     /**
       
    99      * Current module directory
       
   100      * @var string
       
   101      */
       
   102     protected $_moduleDir = '';
       
   103 
       
   104     /**
       
   105      * Whether or not to autorender using controller name as subdirectory;
       
   106      * global setting (not reset at next invocation)
       
   107      * @var boolean
       
   108      */
       
   109     protected $_neverController = false;
       
   110 
       
   111     /**
       
   112      * Whether or not to autorender postDispatch; global setting (not reset at
       
   113      * next invocation)
       
   114      * @var boolean
       
   115      */
       
   116     protected $_neverRender     = false;
       
   117 
       
   118     /**
       
   119      * Whether or not to use a controller name as a subdirectory when rendering
       
   120      * @var boolean
       
   121      */
       
   122     protected $_noController    = false;
       
   123 
       
   124     /**
       
   125      * Whether or not to autorender postDispatch; per controller/action setting (reset
       
   126      * at next invocation)
       
   127      * @var boolean
       
   128      */
       
   129     protected $_noRender        = false;
       
   130 
       
   131     /**
       
   132      * Characters representing path delimiters in the controller
       
   133      * @var string|array
       
   134      */
       
   135     protected $_pathDelimiters;
       
   136 
       
   137     /**
       
   138      * Which named segment of the response to utilize
       
   139      * @var string
       
   140      */
       
   141     protected $_responseSegment = null;
       
   142 
       
   143     /**
       
   144      * Which action view script to render
       
   145      * @var string
       
   146      */
       
   147     protected $_scriptAction    = null;
       
   148 
       
   149     /**
       
   150      * View object basePath
       
   151      * @var string
       
   152      */
       
   153     protected $_viewBasePathSpec = ':moduleDir/views';
       
   154 
       
   155     /**
       
   156      * View script path specification string
       
   157      * @var string
       
   158      */
       
   159     protected $_viewScriptPathSpec = ':controller/:action.:suffix';
       
   160 
       
   161     /**
       
   162      * View script path specification string, minus controller segment
       
   163      * @var string
       
   164      */
       
   165     protected $_viewScriptPathNoControllerSpec = ':action.:suffix';
       
   166 
       
   167     /**
       
   168      * View script suffix
       
   169      * @var string
       
   170      */
       
   171     protected $_viewSuffix      = 'phtml';
       
   172 
       
   173     /**
       
   174      * Constructor
       
   175      *
       
   176      * Optionally set view object and options.
       
   177      *
       
   178      * @param  Zend_View_Interface $view
       
   179      * @param  array               $options
       
   180      * @return void
       
   181      */
       
   182     public function __construct(Zend_View_Interface $view = null, array $options = array())
       
   183     {
       
   184         if (null !== $view) {
       
   185             $this->setView($view);
       
   186         }
       
   187 
       
   188         if (!empty($options)) {
       
   189             $this->_setOptions($options);
       
   190         }
       
   191     }
       
   192 
       
   193     /**
       
   194      * Clone - also make sure the view is cloned.
       
   195      *
       
   196      * @return void
       
   197      */
       
   198     public function __clone()
       
   199     {
       
   200         if (isset($this->view) && $this->view instanceof Zend_View_Interface) {
       
   201             $this->view = clone $this->view;
       
   202 
       
   203         }
       
   204     }
       
   205 
       
   206     /**
       
   207      * Set the view object
       
   208      *
       
   209      * @param  Zend_View_Interface $view
       
   210      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   211      */
       
   212     public function setView(Zend_View_Interface $view)
       
   213     {
       
   214         $this->view = $view;
       
   215         return $this;
       
   216     }
       
   217 
       
   218     /**
       
   219      * Get current module name
       
   220      *
       
   221      * @return string
       
   222      */
       
   223     public function getModule()
       
   224     {
       
   225         $request = $this->getRequest();
       
   226         $module  = $request->getModuleName();
       
   227         if (null === $module) {
       
   228             $module = $this->getFrontController()->getDispatcher()->getDefaultModule();
       
   229         }
       
   230 
       
   231         return $module;
       
   232     }
       
   233 
       
   234     /**
       
   235      * Get module directory
       
   236      *
       
   237      * @throws Zend_Controller_Action_Exception
       
   238      * @return string
       
   239      */
       
   240     public function getModuleDirectory()
       
   241     {
       
   242         $module    = $this->getModule();
       
   243         $moduleDir = $this->getFrontController()->getControllerDirectory($module);
       
   244         if ((null === $moduleDir) || is_array($moduleDir)) {
       
   245             /**
       
   246              * @see Zend_Controller_Action_Exception
       
   247              */
       
   248             require_once 'Zend/Controller/Action/Exception.php';
       
   249             throw new Zend_Controller_Action_Exception('ViewRenderer cannot locate module directory for module "' . $module . '"');
       
   250         }
       
   251         $this->_moduleDir = dirname($moduleDir);
       
   252         return $this->_moduleDir;
       
   253     }
       
   254 
       
   255     /**
       
   256      * Get inflector
       
   257      *
       
   258      * @return Zend_Filter_Inflector
       
   259      */
       
   260     public function getInflector()
       
   261     {
       
   262         if (null === $this->_inflector) {
       
   263             /**
       
   264              * @see Zend_Filter_Inflector
       
   265              */
       
   266             require_once 'Zend/Filter/Inflector.php';
       
   267             /**
       
   268              * @see Zend_Filter_PregReplace
       
   269              */
       
   270             require_once 'Zend/Filter/PregReplace.php';
       
   271             /**
       
   272              * @see Zend_Filter_Word_UnderscoreToSeparator
       
   273              */
       
   274             require_once 'Zend/Filter/Word/UnderscoreToSeparator.php';
       
   275             $this->_inflector = new Zend_Filter_Inflector();
       
   276             $this->_inflector->setStaticRuleReference('moduleDir', $this->_moduleDir) // moduleDir must be specified before the less specific 'module'
       
   277                  ->addRules(array(
       
   278                      ':module'     => array('Word_CamelCaseToDash', 'StringToLower'),
       
   279                      ':controller' => array('Word_CamelCaseToDash', new Zend_Filter_Word_UnderscoreToSeparator('/'), 'StringToLower', new Zend_Filter_PregReplace('/\./', '-')),
       
   280                      ':action'     => array('Word_CamelCaseToDash', new Zend_Filter_PregReplace('#[^a-z0-9' . preg_quote('/', '#') . ']+#i', '-'), 'StringToLower'),
       
   281                  ))
       
   282                  ->setStaticRuleReference('suffix', $this->_viewSuffix)
       
   283                  ->setTargetReference($this->_inflectorTarget);
       
   284         }
       
   285 
       
   286         // Ensure that module directory is current
       
   287         $this->getModuleDirectory();
       
   288 
       
   289         return $this->_inflector;
       
   290     }
       
   291 
       
   292     /**
       
   293      * Set inflector
       
   294      *
       
   295      * @param  Zend_Filter_Inflector $inflector
       
   296      * @param  boolean               $reference Whether the moduleDir, target, and suffix should be set as references to ViewRenderer properties
       
   297      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   298      */
       
   299     public function setInflector(Zend_Filter_Inflector $inflector, $reference = false)
       
   300     {
       
   301         $this->_inflector = $inflector;
       
   302         if ($reference) {
       
   303             $this->_inflector->setStaticRuleReference('suffix', $this->_viewSuffix)
       
   304                  ->setStaticRuleReference('moduleDir', $this->_moduleDir)
       
   305                  ->setTargetReference($this->_inflectorTarget);
       
   306         }
       
   307         return $this;
       
   308     }
       
   309 
       
   310     /**
       
   311      * Set inflector target
       
   312      *
       
   313      * @param  string $target
       
   314      * @return void
       
   315      */
       
   316     protected function _setInflectorTarget($target)
       
   317     {
       
   318         $this->_inflectorTarget = (string) $target;
       
   319     }
       
   320 
       
   321     /**
       
   322      * Set internal module directory representation
       
   323      *
       
   324      * @param  string $dir
       
   325      * @return void
       
   326      */
       
   327     protected function _setModuleDir($dir)
       
   328     {
       
   329         $this->_moduleDir = (string) $dir;
       
   330     }
       
   331 
       
   332     /**
       
   333      * Get internal module directory representation
       
   334      *
       
   335      * @return string
       
   336      */
       
   337     protected function _getModuleDir()
       
   338     {
       
   339         return $this->_moduleDir;
       
   340     }
       
   341 
       
   342     /**
       
   343      * Generate a class prefix for helper and filter classes
       
   344      *
       
   345      * @return string
       
   346      */
       
   347     protected function _generateDefaultPrefix()
       
   348     {
       
   349         $default = 'Zend_View';
       
   350         if (null === $this->_actionController) {
       
   351             return $default;
       
   352         }
       
   353 
       
   354         $class = get_class($this->_actionController);
       
   355 
       
   356         if (!strstr($class, '_')) {
       
   357             return $default;
       
   358         }
       
   359 
       
   360         $module = $this->getModule();
       
   361         if ('default' == $module) {
       
   362             return $default;
       
   363         }
       
   364 
       
   365         $prefix = substr($class, 0, strpos($class, '_')) . '_View';
       
   366 
       
   367         return $prefix;
       
   368     }
       
   369 
       
   370     /**
       
   371      * Retrieve base path based on location of current action controller
       
   372      *
       
   373      * @return string
       
   374      */
       
   375     protected function _getBasePath()
       
   376     {
       
   377         if (null === $this->_actionController) {
       
   378             return './views';
       
   379         }
       
   380 
       
   381         $inflector = $this->getInflector();
       
   382         $this->_setInflectorTarget($this->getViewBasePathSpec());
       
   383 
       
   384         $dispatcher = $this->getFrontController()->getDispatcher();
       
   385         $request = $this->getRequest();
       
   386 
       
   387         $parts = array(
       
   388             'module'     => (($moduleName = $request->getModuleName()) != '') ? $dispatcher->formatModuleName($moduleName) : $moduleName,
       
   389             'controller' => $request->getControllerName(),
       
   390             'action'     => $dispatcher->formatActionName($request->getActionName())
       
   391             );
       
   392 
       
   393         $path = $inflector->filter($parts);
       
   394         return $path;
       
   395     }
       
   396 
       
   397     /**
       
   398      * Set options
       
   399      *
       
   400      * @param  array $options
       
   401      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   402      */
       
   403     protected function _setOptions(array $options)
       
   404     {
       
   405         foreach ($options as $key => $value)
       
   406         {
       
   407             switch ($key) {
       
   408                 case 'neverRender':
       
   409                 case 'neverController':
       
   410                 case 'noController':
       
   411                 case 'noRender':
       
   412                     $property = '_' . $key;
       
   413                     $this->{$property} = ($value) ? true : false;
       
   414                     break;
       
   415                 case 'responseSegment':
       
   416                 case 'scriptAction':
       
   417                 case 'viewBasePathSpec':
       
   418                 case 'viewScriptPathSpec':
       
   419                 case 'viewScriptPathNoControllerSpec':
       
   420                 case 'viewSuffix':
       
   421                     $property = '_' . $key;
       
   422                     $this->{$property} = (string) $value;
       
   423                     break;
       
   424                 default:
       
   425                     break;
       
   426             }
       
   427         }
       
   428 
       
   429         return $this;
       
   430     }
       
   431 
       
   432     /**
       
   433      * Initialize the view object
       
   434      *
       
   435      * $options may contain the following keys:
       
   436      * - neverRender - flag dis/enabling postDispatch() autorender (affects all subsequent calls)
       
   437      * - noController - flag indicating whether or not to look for view scripts in subdirectories named after the controller
       
   438      * - noRender - flag indicating whether or not to autorender postDispatch()
       
   439      * - responseSegment - which named response segment to render a view script to
       
   440      * - scriptAction - what action script to render
       
   441      * - viewBasePathSpec - specification to use for determining view base path
       
   442      * - viewScriptPathSpec - specification to use for determining view script paths
       
   443      * - viewScriptPathNoControllerSpec - specification to use for determining view script paths when noController flag is set
       
   444      * - viewSuffix - what view script filename suffix to use
       
   445      *
       
   446      * @param  string $path
       
   447      * @param  string $prefix
       
   448      * @param  array  $options
       
   449      * @throws Zend_Controller_Action_Exception
       
   450      * @return void
       
   451      */
       
   452     public function initView($path = null, $prefix = null, array $options = array())
       
   453     {
       
   454         if (null === $this->view) {
       
   455             $this->setView(new Zend_View());
       
   456         }
       
   457 
       
   458         // Reset some flags every time
       
   459         $options['noController'] = (isset($options['noController'])) ? $options['noController'] : false;
       
   460         $options['noRender']     = (isset($options['noRender'])) ? $options['noRender'] : false;
       
   461         $this->_scriptAction     = null;
       
   462         $this->_responseSegment  = null;
       
   463 
       
   464         // Set options first; may be used to determine other initializations
       
   465         $this->_setOptions($options);
       
   466 
       
   467         // Get base view path
       
   468         if (empty($path)) {
       
   469             $path = $this->_getBasePath();
       
   470             if (empty($path)) {
       
   471                 /**
       
   472                  * @see Zend_Controller_Action_Exception
       
   473                  */
       
   474                 require_once 'Zend/Controller/Action/Exception.php';
       
   475                 throw new Zend_Controller_Action_Exception('ViewRenderer initialization failed: retrieved view base path is empty');
       
   476             }
       
   477         }
       
   478 
       
   479         if (null === $prefix) {
       
   480             $prefix = $this->_generateDefaultPrefix();
       
   481         }
       
   482 
       
   483         // Determine if this path has already been registered
       
   484         $currentPaths = $this->view->getScriptPaths();
       
   485         $path         = str_replace(array('/', '\\'), '/', $path);
       
   486         $pathExists   = false;
       
   487         foreach ($currentPaths as $tmpPath) {
       
   488             $tmpPath = str_replace(array('/', '\\'), '/', $tmpPath);
       
   489             if (strstr($tmpPath, $path)) {
       
   490                 $pathExists = true;
       
   491                 break;
       
   492             }
       
   493         }
       
   494         if (!$pathExists) {
       
   495             $this->view->addBasePath($path, $prefix);
       
   496         }
       
   497 
       
   498         // Register view with action controller (unless already registered)
       
   499         if ((null !== $this->_actionController) && (null === $this->_actionController->view)) {
       
   500             $this->_actionController->view       = $this->view;
       
   501             $this->_actionController->viewSuffix = $this->_viewSuffix;
       
   502         }
       
   503     }
       
   504 
       
   505     /**
       
   506      * init - initialize view
       
   507      *
       
   508      * @return void
       
   509      */
       
   510     public function init()
       
   511     {
       
   512         if ($this->getFrontController()->getParam('noViewRenderer')) {
       
   513             return;
       
   514         }
       
   515 
       
   516         $this->initView();
       
   517     }
       
   518 
       
   519     /**
       
   520      * Set view basePath specification
       
   521      *
       
   522      * Specification can contain one or more of the following:
       
   523      * - :moduleDir - current module directory
       
   524      * - :controller - name of current controller in the request
       
   525      * - :action - name of current action in the request
       
   526      * - :module - name of current module in the request
       
   527      *
       
   528      * @param  string $path
       
   529      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   530      */
       
   531     public function setViewBasePathSpec($path)
       
   532     {
       
   533         $this->_viewBasePathSpec = (string) $path;
       
   534         return $this;
       
   535     }
       
   536 
       
   537     /**
       
   538      * Retrieve the current view basePath specification string
       
   539      *
       
   540      * @return string
       
   541      */
       
   542     public function getViewBasePathSpec()
       
   543     {
       
   544         return $this->_viewBasePathSpec;
       
   545     }
       
   546 
       
   547     /**
       
   548      * Set view script path specification
       
   549      *
       
   550      * Specification can contain one or more of the following:
       
   551      * - :moduleDir - current module directory
       
   552      * - :controller - name of current controller in the request
       
   553      * - :action - name of current action in the request
       
   554      * - :module - name of current module in the request
       
   555      *
       
   556      * @param  string $path
       
   557      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   558      */
       
   559     public function setViewScriptPathSpec($path)
       
   560     {
       
   561         $this->_viewScriptPathSpec = (string) $path;
       
   562         return $this;
       
   563     }
       
   564 
       
   565     /**
       
   566      * Retrieve the current view script path specification string
       
   567      *
       
   568      * @return string
       
   569      */
       
   570     public function getViewScriptPathSpec()
       
   571     {
       
   572         return $this->_viewScriptPathSpec;
       
   573     }
       
   574 
       
   575     /**
       
   576      * Set view script path specification (no controller variant)
       
   577      *
       
   578      * Specification can contain one or more of the following:
       
   579      * - :moduleDir - current module directory
       
   580      * - :controller - name of current controller in the request
       
   581      * - :action - name of current action in the request
       
   582      * - :module - name of current module in the request
       
   583      *
       
   584      * :controller will likely be ignored in this variant.
       
   585      *
       
   586      * @param  string $path
       
   587      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   588      */
       
   589     public function setViewScriptPathNoControllerSpec($path)
       
   590     {
       
   591         $this->_viewScriptPathNoControllerSpec = (string) $path;
       
   592         return $this;
       
   593     }
       
   594 
       
   595     /**
       
   596      * Retrieve the current view script path specification string (no controller variant)
       
   597      *
       
   598      * @return string
       
   599      */
       
   600     public function getViewScriptPathNoControllerSpec()
       
   601     {
       
   602         return $this->_viewScriptPathNoControllerSpec;
       
   603     }
       
   604 
       
   605     /**
       
   606      * Get a view script based on an action and/or other variables
       
   607      *
       
   608      * Uses values found in current request if no values passed in $vars.
       
   609      *
       
   610      * If {@link $_noController} is set, uses {@link $_viewScriptPathNoControllerSpec};
       
   611      * otherwise, uses {@link $_viewScriptPathSpec}.
       
   612      *
       
   613      * @param  string $action
       
   614      * @param  array  $vars
       
   615      * @return string
       
   616      */
       
   617     public function getViewScript($action = null, array $vars = array())
       
   618     {
       
   619         $request = $this->getRequest();
       
   620         if ((null === $action) && (!isset($vars['action']))) {
       
   621             $action = $this->getScriptAction();
       
   622             if (null === $action) {
       
   623                 $action = $request->getActionName();
       
   624             }
       
   625             $vars['action'] = $action;
       
   626         } elseif (null !== $action) {
       
   627             $vars['action'] = $action;
       
   628         }
       
   629 
       
   630         $inflector = $this->getInflector();
       
   631         if ($this->getNoController() || $this->getNeverController()) {
       
   632             $this->_setInflectorTarget($this->getViewScriptPathNoControllerSpec());
       
   633         } else {
       
   634             $this->_setInflectorTarget($this->getViewScriptPathSpec());
       
   635         }
       
   636         return $this->_translateSpec($vars);
       
   637     }
       
   638 
       
   639     /**
       
   640      * Set the neverRender flag (i.e., globally dis/enable autorendering)
       
   641      *
       
   642      * @param  boolean $flag
       
   643      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   644      */
       
   645     public function setNeverRender($flag = true)
       
   646     {
       
   647         $this->_neverRender = ($flag) ? true : false;
       
   648         return $this;
       
   649     }
       
   650 
       
   651     /**
       
   652      * Retrieve neverRender flag value
       
   653      *
       
   654      * @return boolean
       
   655      */
       
   656     public function getNeverRender()
       
   657     {
       
   658         return $this->_neverRender;
       
   659     }
       
   660 
       
   661     /**
       
   662      * Set the noRender flag (i.e., whether or not to autorender)
       
   663      *
       
   664      * @param  boolean $flag
       
   665      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   666      */
       
   667     public function setNoRender($flag = true)
       
   668     {
       
   669         $this->_noRender = ($flag) ? true : false;
       
   670         return $this;
       
   671     }
       
   672 
       
   673     /**
       
   674      * Retrieve noRender flag value
       
   675      *
       
   676      * @return boolean
       
   677      */
       
   678     public function getNoRender()
       
   679     {
       
   680         return $this->_noRender;
       
   681     }
       
   682 
       
   683     /**
       
   684      * Set the view script to use
       
   685      *
       
   686      * @param  string $name
       
   687      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   688      */
       
   689     public function setScriptAction($name)
       
   690     {
       
   691         $this->_scriptAction = (string) $name;
       
   692         return $this;
       
   693     }
       
   694 
       
   695     /**
       
   696      * Retrieve view script name
       
   697      *
       
   698      * @return string
       
   699      */
       
   700     public function getScriptAction()
       
   701     {
       
   702         return $this->_scriptAction;
       
   703     }
       
   704 
       
   705     /**
       
   706      * Set the response segment name
       
   707      *
       
   708      * @param  string $name
       
   709      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   710      */
       
   711     public function setResponseSegment($name)
       
   712     {
       
   713         if (null === $name) {
       
   714             $this->_responseSegment = null;
       
   715         } else {
       
   716             $this->_responseSegment = (string) $name;
       
   717         }
       
   718 
       
   719         return $this;
       
   720     }
       
   721 
       
   722     /**
       
   723      * Retrieve named response segment name
       
   724      *
       
   725      * @return string
       
   726      */
       
   727     public function getResponseSegment()
       
   728     {
       
   729         return $this->_responseSegment;
       
   730     }
       
   731 
       
   732     /**
       
   733      * Set the noController flag (i.e., whether or not to render into controller subdirectories)
       
   734      *
       
   735      * @param  boolean $flag
       
   736      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   737      */
       
   738     public function setNoController($flag = true)
       
   739     {
       
   740         $this->_noController = ($flag) ? true : false;
       
   741         return $this;
       
   742     }
       
   743 
       
   744     /**
       
   745      * Retrieve noController flag value
       
   746      *
       
   747      * @return boolean
       
   748      */
       
   749     public function getNoController()
       
   750     {
       
   751         return $this->_noController;
       
   752     }
       
   753 
       
   754     /**
       
   755      * Set the neverController flag (i.e., whether or not to render into controller subdirectories)
       
   756      *
       
   757      * @param  boolean $flag
       
   758      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   759      */
       
   760     public function setNeverController($flag = true)
       
   761     {
       
   762         $this->_neverController = ($flag) ? true : false;
       
   763         return $this;
       
   764     }
       
   765 
       
   766     /**
       
   767      * Retrieve neverController flag value
       
   768      *
       
   769      * @return boolean
       
   770      */
       
   771     public function getNeverController()
       
   772     {
       
   773         return $this->_neverController;
       
   774     }
       
   775 
       
   776     /**
       
   777      * Set view script suffix
       
   778      *
       
   779      * @param  string $suffix
       
   780      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   781      */
       
   782     public function setViewSuffix($suffix)
       
   783     {
       
   784         $this->_viewSuffix = (string) $suffix;
       
   785         return $this;
       
   786     }
       
   787 
       
   788     /**
       
   789      * Get view script suffix
       
   790      *
       
   791      * @return string
       
   792      */
       
   793     public function getViewSuffix()
       
   794     {
       
   795         return $this->_viewSuffix;
       
   796     }
       
   797 
       
   798     /**
       
   799      * Set options for rendering a view script
       
   800      *
       
   801      * @param  string  $action       View script to render
       
   802      * @param  string  $name         Response named segment to render to
       
   803      * @param  boolean $noController Whether or not to render within a subdirectory named after the controller
       
   804      * @return Zend_Controller_Action_Helper_ViewRenderer Provides a fluent interface
       
   805      */
       
   806     public function setRender($action = null, $name = null, $noController = null)
       
   807     {
       
   808         if (null !== $action) {
       
   809             $this->setScriptAction($action);
       
   810         }
       
   811 
       
   812         if (null !== $name) {
       
   813             $this->setResponseSegment($name);
       
   814         }
       
   815 
       
   816         if (null !== $noController) {
       
   817             $this->setNoController($noController);
       
   818         }
       
   819 
       
   820         return $this;
       
   821     }
       
   822 
       
   823     /**
       
   824      * Inflect based on provided vars
       
   825      *
       
   826      * Allowed variables are:
       
   827      * - :moduleDir - current module directory
       
   828      * - :module - current module name
       
   829      * - :controller - current controller name
       
   830      * - :action - current action name
       
   831      * - :suffix - view script file suffix
       
   832      *
       
   833      * @param  array $vars
       
   834      * @return string
       
   835      */
       
   836     protected function _translateSpec(array $vars = array())
       
   837     {
       
   838         $inflector  = $this->getInflector();
       
   839         $request    = $this->getRequest();
       
   840         $dispatcher = $this->getFrontController()->getDispatcher();
       
   841         $module     = $dispatcher->formatModuleName($request->getModuleName());
       
   842         $controller = $request->getControllerName();
       
   843         $action     = $dispatcher->formatActionName($request->getActionName());
       
   844 
       
   845         $params     = compact('module', 'controller', 'action');
       
   846         foreach ($vars as $key => $value) {
       
   847             switch ($key) {
       
   848                 case 'module':
       
   849                 case 'controller':
       
   850                 case 'action':
       
   851                 case 'moduleDir':
       
   852                 case 'suffix':
       
   853                     $params[$key] = (string) $value;
       
   854                     break;
       
   855                 default:
       
   856                     break;
       
   857             }
       
   858         }
       
   859 
       
   860         if (isset($params['suffix'])) {
       
   861             $origSuffix = $this->getViewSuffix();
       
   862             $this->setViewSuffix($params['suffix']);
       
   863         }
       
   864         if (isset($params['moduleDir'])) {
       
   865             $origModuleDir = $this->_getModuleDir();
       
   866             $this->_setModuleDir($params['moduleDir']);
       
   867         }
       
   868 
       
   869         $filtered = $inflector->filter($params);
       
   870 
       
   871         if (isset($params['suffix'])) {
       
   872             $this->setViewSuffix($origSuffix);
       
   873         }
       
   874         if (isset($params['moduleDir'])) {
       
   875             $this->_setModuleDir($origModuleDir);
       
   876         }
       
   877 
       
   878         return $filtered;
       
   879     }
       
   880 
       
   881     /**
       
   882      * Render a view script (optionally to a named response segment)
       
   883      *
       
   884      * Sets the noRender flag to true when called.
       
   885      *
       
   886      * @param  string $script
       
   887      * @param  string $name
       
   888      * @return void
       
   889      */
       
   890     public function renderScript($script, $name = null)
       
   891     {
       
   892         if (null === $name) {
       
   893             $name = $this->getResponseSegment();
       
   894         }
       
   895 
       
   896         $this->getResponse()->appendBody(
       
   897             $this->view->render($script),
       
   898             $name
       
   899         );
       
   900 
       
   901         $this->setNoRender();
       
   902     }
       
   903 
       
   904     /**
       
   905      * Render a view based on path specifications
       
   906      *
       
   907      * Renders a view based on the view script path specifications.
       
   908      *
       
   909      * @param  string  $action
       
   910      * @param  string  $name
       
   911      * @param  boolean $noController
       
   912      * @return void
       
   913      */
       
   914     public function render($action = null, $name = null, $noController = null)
       
   915     {
       
   916         $this->setRender($action, $name, $noController);
       
   917         $path = $this->getViewScript();
       
   918         $this->renderScript($path, $name);
       
   919     }
       
   920 
       
   921     /**
       
   922      * Render a script based on specification variables
       
   923      *
       
   924      * Pass an action, and one or more specification variables (view script suffix)
       
   925      * to determine the view script path, and render that script.
       
   926      *
       
   927      * @param  string $action
       
   928      * @param  array  $vars
       
   929      * @param  string $name
       
   930      * @return void
       
   931      */
       
   932     public function renderBySpec($action = null, array $vars = array(), $name = null)
       
   933     {
       
   934         if (null !== $name) {
       
   935             $this->setResponseSegment($name);
       
   936         }
       
   937 
       
   938         $path = $this->getViewScript($action, $vars);
       
   939 
       
   940         $this->renderScript($path);
       
   941     }
       
   942 
       
   943     /**
       
   944      * postDispatch - auto render a view
       
   945      *
       
   946      * Only autorenders if:
       
   947      * - _noRender is false
       
   948      * - action controller is present
       
   949      * - request has not been re-dispatched (i.e., _forward() has not been called)
       
   950      * - response is not a redirect
       
   951      *
       
   952      * @return void
       
   953      */
       
   954     public function postDispatch()
       
   955     {
       
   956         if ($this->_shouldRender()) {
       
   957             $this->render();
       
   958         }
       
   959     }
       
   960 
       
   961     /**
       
   962      * Should the ViewRenderer render a view script?
       
   963      *
       
   964      * @return boolean
       
   965      */
       
   966     protected function _shouldRender()
       
   967     {
       
   968         return (!$this->getFrontController()->getParam('noViewRenderer')
       
   969             && !$this->_neverRender
       
   970             && !$this->_noRender
       
   971             && (null !== $this->_actionController)
       
   972             && $this->getRequest()->isDispatched()
       
   973             && !$this->getResponse()->isRedirect()
       
   974         );
       
   975     }
       
   976 
       
   977     /**
       
   978      * Use this helper as a method; proxies to setRender()
       
   979      *
       
   980      * @param  string  $action
       
   981      * @param  string  $name
       
   982      * @param  boolean $noController
       
   983      * @return void
       
   984      */
       
   985     public function direct($action = null, $name = null, $noController = null)
       
   986     {
       
   987         $this->setRender($action, $name, $noController);
       
   988     }
       
   989 }