PaginationHelper.php 3.0 KB

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