/library/Zend/View/Helper/PaginationControl.php

https://github.com/Exercise/zf2 · PHP · 140 lines · 58 code · 15 blank · 67 comment · 10 complexity · 297d5329dbe95f25f475e56eb5d18ce6 MD5 · raw file

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