pagination.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Grav\Plugin;
  3. use Composer\Autoload\ClassLoader;
  4. use Grav\Common\Page\Collection;
  5. use Grav\Common\Page\Interfaces\PageInterface;
  6. use Grav\Common\Plugin;
  7. use Grav\Plugin\Pagination\PaginationHelper;
  8. use Grav\Plugin\Pagination\PaginationPage;
  9. use RocketTheme\Toolbox\Event\Event;
  10. use Twig\TwigFunction;
  11. class PaginationPlugin extends Plugin
  12. {
  13. /**
  14. * @var PaginationHelper
  15. */
  16. protected $pagination;
  17. /**
  18. * @return array
  19. */
  20. public static function getSubscribedEvents()
  21. {
  22. return [
  23. 'onPluginsInitialized' => [
  24. ['autoload', 100001],
  25. ['onPluginsInitialized', 0]
  26. ]
  27. ];
  28. }
  29. /**
  30. * [onPluginsInitialized:100000] Composer autoload.
  31. *
  32. * @return ClassLoader
  33. */
  34. public function autoload()
  35. {
  36. return require __DIR__ . '/vendor/autoload.php';
  37. }
  38. /**
  39. * Initialize configuration
  40. */
  41. public function onPluginsInitialized()
  42. {
  43. if ($this->isAdmin()) {
  44. $this->active = false;
  45. return;
  46. }
  47. class_alias(PaginationHelper::class, 'Grav\\Plugin\\PaginationHelper');
  48. class_alias(PaginationPage::class, 'Grav\\Plugin\\PaginationPage');
  49. $this->enable([
  50. 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
  51. 'onPageInitialized' => ['onPageInitialized', 0],
  52. 'onTwigExtensions' => ['onTwigExtensions', 0]
  53. ]);
  54. }
  55. /**
  56. * Add current directory to twig lookup paths.
  57. */
  58. public function onTwigTemplatePaths()
  59. {
  60. $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
  61. }
  62. /**
  63. * Add Twig Extensions
  64. */
  65. public function onTwigExtensions()
  66. {
  67. // Add Twig functions
  68. $this->grav['twig']->twig()->addFunction(
  69. new TwigFunction('paginate', [$this, 'paginateTwigFunction'])
  70. );
  71. }
  72. /**
  73. * Enable pagination if page has params.pagination = true.
  74. */
  75. public function onPageInitialized()
  76. {
  77. /** @var PageInterface $page */
  78. $page = $this->grav['page'];
  79. if ($page && ($page->value('header.content.pagination') || $page->value('header.pagination'))) {
  80. $this->enable([
  81. 'onCollectionProcessed' => ['onCollectionProcessed', 0],
  82. 'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
  83. ]);
  84. $template = $this->grav['uri']->param('tmpl');
  85. if ($template) {
  86. $page->template($template);
  87. }
  88. }
  89. }
  90. /**
  91. * Create pagination object for the page.
  92. *
  93. * @param Event $event
  94. */
  95. public function onCollectionProcessed(Event $event)
  96. {
  97. /** @var Collection $collection */
  98. $collection = $event['collection'];
  99. $params = $collection->params();
  100. // Only add pagination if it has been enabled for the collection.
  101. if (empty($params['pagination'])) {
  102. return;
  103. }
  104. if (!empty($params['limit']) && $collection->count() > $params['limit']) {
  105. $this->pagination = new PaginationHelper($collection);
  106. $collection->setParams(['pagination' => $this->pagination]);
  107. }
  108. }
  109. /**
  110. * Set needed variables to display pagination.
  111. */
  112. public function onTwigSiteVariables()
  113. {
  114. if ($this->config->get('plugins.pagination.built_in_css')) {
  115. $this->grav['assets']->add('plugin://pagination/css/pagination.css');
  116. }
  117. }
  118. /**
  119. * pagination
  120. *
  121. * @param Collection $collection
  122. * @param int $limit
  123. * @param array $ignore_param_array url parameters to be ignored in page links
  124. */
  125. public function paginateCollection($collection, $limit, $ignore_param_array = [])
  126. {
  127. $collection->setParams(['pagination' => 'true']);
  128. $collection->setParams(['limit' => $limit]);
  129. $collection->setParams(['ignore_params' => $ignore_param_array]);
  130. if ($collection->count() > $limit) {
  131. $this->pagination = new PaginationHelper($collection);
  132. $collection->setParams(['pagination' => $this->pagination]);
  133. $uri = $this->grav['uri'];
  134. $start = ($uri->currentPage() - 1) * $limit;
  135. if ($limit && $collection->count() > $limit) {
  136. $collection->slice($start, $limit);
  137. }
  138. }
  139. }
  140. public function paginateTwigFunction($collection, $limit, $ignore_url_param_array = [])
  141. {
  142. $this->paginateCollection($collection, $limit, $ignore_url_param_array);
  143. }
  144. }