|
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_View |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: PaginationControl.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @category Zend |
|
24 * @package Zend_View |
|
25 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
26 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
27 */ |
|
28 class Zend_View_Helper_PaginationControl |
|
29 { |
|
30 /** |
|
31 * View instance |
|
32 * |
|
33 * @var Zend_View_Instance |
|
34 */ |
|
35 public $view = null; |
|
36 |
|
37 /** |
|
38 * Default view partial |
|
39 * |
|
40 * @var string|array |
|
41 */ |
|
42 protected static $_defaultViewPartial = null; |
|
43 |
|
44 /** |
|
45 * Sets the view instance. |
|
46 * |
|
47 * @param Zend_View_Interface $view View instance |
|
48 * @return Zend_View_Helper_PaginationControl |
|
49 */ |
|
50 public function setView(Zend_View_Interface $view) |
|
51 { |
|
52 $this->view = $view; |
|
53 return $this; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Sets the default view partial. |
|
58 * |
|
59 * @param string|array $partial View partial |
|
60 */ |
|
61 public static function setDefaultViewPartial($partial) |
|
62 { |
|
63 self::$_defaultViewPartial = $partial; |
|
64 } |
|
65 |
|
66 /** |
|
67 * Gets the default view partial |
|
68 * |
|
69 * @return string|array |
|
70 */ |
|
71 public static function getDefaultViewPartial() |
|
72 { |
|
73 return self::$_defaultViewPartial; |
|
74 } |
|
75 |
|
76 /** |
|
77 * Render the provided pages. This checks if $view->paginator is set and, |
|
78 * if so, uses that. Also, if no scrolling style or partial are specified, |
|
79 * the defaults will be used (if set). |
|
80 * |
|
81 * @param Zend_Paginator (Optional) $paginator |
|
82 * @param string $scrollingStyle (Optional) Scrolling style |
|
83 * @param string $partial (Optional) View partial |
|
84 * @param array|string $params (Optional) params to pass to the partial |
|
85 * @return string |
|
86 * @throws Zend_View_Exception |
|
87 */ |
|
88 public function paginationControl(Zend_Paginator $paginator = null, $scrollingStyle = null, $partial = null, $params = null) |
|
89 { |
|
90 if ($paginator === null) { |
|
91 if (isset($this->view->paginator) and $this->view->paginator !== null and $this->view->paginator instanceof Zend_Paginator) { |
|
92 $paginator = $this->view->paginator; |
|
93 } else { |
|
94 /** |
|
95 * @see Zend_View_Exception |
|
96 */ |
|
97 require_once 'Zend/View/Exception.php'; |
|
98 |
|
99 $e = new Zend_View_Exception('No paginator instance provided or incorrect type'); |
|
100 $e->setView($this->view); |
|
101 throw $e; |
|
102 } |
|
103 } |
|
104 |
|
105 if ($partial === null) { |
|
106 if (self::$_defaultViewPartial === null) { |
|
107 /** |
|
108 * @see Zend_View_Exception |
|
109 */ |
|
110 require_once 'Zend/View/Exception.php'; |
|
111 $e = new Zend_View_Exception('No view partial provided and no default set'); |
|
112 $e->setView($this->view); |
|
113 throw $e; |
|
114 } |
|
115 |
|
116 $partial = self::$_defaultViewPartial; |
|
117 } |
|
118 |
|
119 $pages = get_object_vars($paginator->getPages($scrollingStyle)); |
|
120 |
|
121 if ($params !== null) { |
|
122 $pages = array_merge($pages, (array) $params); |
|
123 } |
|
124 |
|
125 if (is_array($partial)) { |
|
126 if (count($partial) != 2) { |
|
127 /** |
|
128 * @see Zend_View_Exception |
|
129 */ |
|
130 require_once 'Zend/View/Exception.php'; |
|
131 $e = new Zend_View_Exception('A view partial supplied as an array must contain two values: the filename and its module'); |
|
132 $e->setView($this->view); |
|
133 throw $e; |
|
134 } |
|
135 |
|
136 if ($partial[1] !== null) { |
|
137 return $this->view->partial($partial[0], $partial[1], $pages); |
|
138 } |
|
139 |
|
140 $partial = $partial[0]; |
|
141 } |
|
142 |
|
143 return $this->view->partial($partial, $pages); |
|
144 } |
|
145 } |