PaginationControl.php 4.4 KB

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