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