File indexing completed on 2024-12-22 05:37:14

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_View
0017  * @subpackage Helper
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @version    $Id$
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  */
0022 
0023 /** Zend_View_Helper_Abstract.php */
0024 // require_once 'Zend/View/Helper/Abstract.php';
0025 
0026 /**
0027  * Helper for rendering a template fragment in its own variable scope.
0028  *
0029  * @package    Zend_View
0030  * @subpackage Helper
0031  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0032  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0033  */
0034 class Zend_View_Helper_Partial extends Zend_View_Helper_Abstract
0035 {
0036     /**
0037      * Variable to which object will be assigned
0038      * @var string
0039      */
0040     protected $_objectKey;
0041 
0042     /**
0043      * Renders a template fragment within a variable scope distinct from the
0044      * calling View object.
0045      *
0046      * If no arguments are passed, returns the helper instance.
0047      *
0048      * If the $model is an array, it is passed to the view object's assign()
0049      * method.
0050      *
0051      * If the $model is an object, it first checks to see if the object
0052      * implements a 'toArray' method; if so, it passes the result of that
0053      * method to to the view object's assign() method. Otherwise, the result of
0054      * get_object_vars() is passed.
0055      *
0056      * @param  string $name Name of view script
0057      * @param  string|array $module If $model is empty, and $module is an array,
0058      *                              these are the variables to populate in the
0059      *                              view. Otherwise, the module in which the
0060      *                              partial resides
0061      * @param  array $model Variables to populate in the view
0062      * @return string|Zend_View_Helper_Partial
0063      */
0064     public function partial($name = null, $module = null, $model = null)
0065     {
0066         if (0 == func_num_args()) {
0067             return $this;
0068         }
0069 
0070         $view = $this->cloneView();
0071         if (isset($this->partialCounter)) {
0072             $view->partialCounter = $this->partialCounter;
0073         }
0074         if (isset($this->partialTotalCount)) {
0075             $view->partialTotalCount = $this->partialTotalCount;
0076         }
0077 
0078         if ((null !== $module) && is_string($module)) {
0079             // require_once 'Zend/Controller/Front.php';
0080             $moduleDir = Zend_Controller_Front::getInstance()->getControllerDirectory($module);
0081             if (null === $moduleDir) {
0082                 // require_once 'Zend/View/Helper/Partial/Exception.php';
0083                 $e = new Zend_View_Helper_Partial_Exception('Cannot render partial; module does not exist');
0084                 $e->setView($this->view);
0085                 throw $e;
0086             }
0087             $viewsDir = dirname($moduleDir) . '/views';
0088             $view->addBasePath($viewsDir);
0089         } elseif ((null == $model) && (null !== $module)
0090             && (is_array($module) || is_object($module)))
0091         {
0092             $model = $module;
0093         }
0094 
0095         if (!empty($model)) {
0096             if (is_array($model)) {
0097                 $view->assign($model);
0098             } elseif (is_object($model)) {
0099                 if (null !== ($objectKey = $this->getObjectKey())) {
0100                     $view->assign($objectKey, $model);
0101                 } elseif (method_exists($model, 'toArray')) {
0102                     $view->assign($model->toArray());
0103                 } else {
0104                     $view->assign(get_object_vars($model));
0105                 }
0106             }
0107         }
0108 
0109         return $view->render($name);
0110     }
0111 
0112     /**
0113      * Clone the current View
0114      *
0115      * @return Zend_View_Interface
0116      */
0117     public function cloneView()
0118     {
0119         $view = clone $this->view;
0120         $view->clearVars();
0121         return $view;
0122     }
0123 
0124     /**
0125      * Set object key
0126      *
0127      * @param  string $key
0128      * @return Zend_View_Helper_Partial
0129      */
0130     public function setObjectKey($key)
0131     {
0132         if (null === $key) {
0133             $this->_objectKey = null;
0134         } else {
0135             $this->_objectKey = (string) $key;
0136         }
0137 
0138         return $this;
0139     }
0140 
0141     /**
0142      * Retrieve object key
0143      *
0144      * The objectKey is the variable to which an object in the iterator will be
0145      * assigned.
0146      *
0147      * @return null|string
0148      */
0149     public function getObjectKey()
0150     {
0151         return $this->_objectKey;
0152     }
0153 }