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  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @category   Zend
0024  * @package    Zend_View
0025  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0026  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0027  */
0028 class Zend_View_Helper_PaginationControl
0029 {
0030     /**
0031      * View instance
0032      *
0033      * @var Zend_View_Instance
0034      */
0035     public $view = null;
0036 
0037     /**
0038      * Default view partial
0039      *
0040      * @var string|array
0041      */
0042     protected static $_defaultViewPartial = null;
0043 
0044     /**
0045      * Sets the view instance.
0046      *
0047      * @param  Zend_View_Interface $view View instance
0048      * @return Zend_View_Helper_PaginationControl
0049      */
0050     public function setView(Zend_View_Interface $view)
0051     {
0052         $this->view = $view;
0053         return $this;
0054     }
0055 
0056     /**
0057      * Sets the default view partial.
0058      *
0059      * @param string|array $partial View partial
0060      */
0061     public static function setDefaultViewPartial($partial)
0062     {
0063         self::$_defaultViewPartial = $partial;
0064     }
0065 
0066     /**
0067      * Gets the default view partial
0068      *
0069      * @return string|array
0070      */
0071     public static function getDefaultViewPartial()
0072     {
0073         return self::$_defaultViewPartial;
0074     }
0075 
0076     /**
0077      * Render the provided pages.  This checks if $view->paginator is set and,
0078      * if so, uses that.  Also, if no scrolling style or partial are specified,
0079      * the defaults will be used (if set).
0080      *
0081      * @param  Zend_Paginator (Optional) $paginator
0082      * @param  string $scrollingStyle (Optional) Scrolling style
0083      * @param  string $partial (Optional) View partial
0084      * @param  array|string $params (Optional) params to pass to the partial
0085      * @return string
0086      * @throws Zend_View_Exception
0087      */
0088     public function paginationControl(Zend_Paginator $paginator = null, $scrollingStyle = null, $partial = null, $params = null)
0089     {
0090         if ($paginator === null) {
0091             if (isset($this->view->paginator) and $this->view->paginator !== null and $this->view->paginator instanceof Zend_Paginator) {
0092                 $paginator = $this->view->paginator;
0093             } else {
0094                 /**
0095                  * @see Zend_View_Exception
0096                  */
0097                 // require_once 'Zend/View/Exception.php';
0098 
0099                 $e = new Zend_View_Exception('No paginator instance provided or incorrect type');
0100                 $e->setView($this->view);
0101                 throw $e;
0102             }
0103         }
0104 
0105         if ($partial === null) {
0106             if (self::$_defaultViewPartial === null) {
0107                 /**
0108                  * @see Zend_View_Exception
0109                  */
0110                 // require_once 'Zend/View/Exception.php';
0111                 $e = new Zend_View_Exception('No view partial provided and no default set');
0112                 $e->setView($this->view);
0113                 throw $e;
0114             }
0115 
0116             $partial = self::$_defaultViewPartial;
0117         }
0118 
0119         $pages = get_object_vars($paginator->getPages($scrollingStyle));
0120 
0121         if ($params !== null) {
0122             $pages = array_merge($pages, (array) $params);
0123         }
0124 
0125         if (is_array($partial)) {
0126             if (count($partial) != 2) {
0127                 /**
0128                  * @see Zend_View_Exception
0129                  */
0130                 // require_once 'Zend/View/Exception.php';
0131                 $e = new Zend_View_Exception('A view partial supplied as an array must contain two values: the filename and its module');
0132                 $e->setView($this->view);
0133                 throw $e;
0134             }
0135 
0136             if ($partial[1] !== null) {
0137                 return $this->view->partial($partial[0], $partial[1], $pages);
0138             }
0139 
0140             $partial = $partial[0];
0141         }
0142 
0143         return $this->view->partial($partial, $pages);
0144     }
0145 }