paginationhelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Grav\Plugin;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Iterator;
  5. use Grav\Common\Page\Collection;
  6. use Grav\Common\Uri;
  7. class PaginationHelper extends Iterator
  8. {
  9. protected $current;
  10. protected $items_per_page;
  11. protected $page_count;
  12. protected $url_params;
  13. /**
  14. * Create and initialize pagination.
  15. *
  16. * @param Collection $collection
  17. */
  18. public function __construct(Collection $collection)
  19. {
  20. parent::__construct();
  21. require_once __DIR__ . '/paginationpage.php';
  22. $grav = Grav::instance();
  23. /** @var Uri $uri */
  24. $uri = $grav['uri'];
  25. $config = $grav['config'];
  26. $this->current = $uri->currentPage();
  27. // get params
  28. $url_params = explode('/', ltrim($uri->params(), '/'));
  29. $params = $collection->params();
  30. foreach ($url_params as $key => $value) {
  31. if (strpos($value, 'page' . $config->get('system.param_sep')) !== false) {
  32. unset($url_params[$key]);
  33. }
  34. if (isset($params['ignore_url_params'])) {
  35. foreach ((array)$params['ignore_params'] as $ignore_param) {
  36. if (strpos($value, $ignore_param . $config->get('system.param_sep')) !== false) {
  37. unset($url_params[$key]);
  38. }
  39. }
  40. }
  41. }
  42. $this->url_params = '/'.implode('/', $url_params);
  43. // check for empty params
  44. if ($this->url_params === '/') {
  45. $this->url_params = '';
  46. }
  47. $this->items_per_page = $params['limit'];
  48. $this->page_count = ceil($collection->count() / $this->items_per_page);
  49. for ($x=1; $x <= $this->page_count; $x++) {
  50. if ($x === 1) {
  51. $this->items[$x] = new PaginationPage($x, '');
  52. } else {
  53. $this->items[$x] = new PaginationPage($x, '/page' . $config->get('system.param_sep') . $x);
  54. }
  55. }
  56. }
  57. /**
  58. * Returns true if current item has previous sibling.
  59. *
  60. * @return bool
  61. */
  62. public function hasPrev()
  63. {
  64. if (array_key_exists($this->current -1, $this->items)) {
  65. return true;
  66. }
  67. return false;
  68. }
  69. /**
  70. * Returns true if current item has next sibling.
  71. *
  72. * @return bool
  73. */
  74. public function hasNext()
  75. {
  76. if (array_key_exists($this->current +1, $this->items)) {
  77. return true;
  78. }
  79. return false;
  80. }
  81. /**
  82. * Return previous url.
  83. *
  84. * @return string|null
  85. */
  86. public function prevUrl()
  87. {
  88. if (array_key_exists($this->current -1, $this->items)) {
  89. return $this->items[$this->current -1]->url;
  90. }
  91. return null;
  92. }
  93. /**
  94. * Return next url.
  95. *
  96. * @return string|null
  97. */
  98. public function nextUrl()
  99. {
  100. if (array_key_exists($this->current +1, $this->items)) {
  101. return $this->items[$this->current +1]->url;
  102. }
  103. return null;
  104. }
  105. public function params()
  106. {
  107. return $this->url_params;
  108. }
  109. }