-
Notifications
You must be signed in to change notification settings - Fork 4
/
pagination.php
146 lines (130 loc) · 5.26 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
<?php
/**
* Pico Pagination Plugin
*
* @author Andrew Meyer
* @link http://rewdy.com
* @license http://opensource.org/licenses/MIT
* @version 1.4
*/
class Pagination extends AbstractPicoPlugin {
public $config = array();
public $offset = 0;
public $page_number = 0;
public $total_pages = 1;
public $paged_pages = array();
public function __construct(Pico $pico)
{
parent::__construct($pico);
$this->config = array(
'limit' => 5,
'next_text' => 'Next >',
'prev_text' => '< Previous',
'page_indicator' => 'page',
'output_format' => 'links',
'flip_links' => false,
'filter_date' => true,
'sub_page' => false,
);
}
public function onConfigLoaded(&$settings)
{
// Pull config options for site config
if (isset($settings['pagination_limit']))
$this->config['limit'] = $settings['pagination_limit'];
if (isset($settings['pagination_next_text']))
$this->config['next_text'] = $settings['pagination_next_text'];
if (isset($settings['pagination_prev_text']))
$this->config['prev_text'] = $settings['pagination_prev_text'];
if (isset($settings['pagination_flip_links']))
$this->config['flip_links'] = $settings['pagination_flip_links'];
if (isset($settings['pagination_filter_date']))
$this->config['filter_date'] = $settings['pagination_filter_date'];
if (isset($settings['pagination_page_indicator']))
$this->config['page_indicator'] = $settings['pagination_page_indicator'];
if (isset($settings['pagination_output_format']))
$this->config['output_format'] = $settings['pagination_output_format'];
if (isset($settings['pagination_sub_page']))
$this->config['sub_page'] = $settings['pagination_sub_page'];
}
public function onPagesLoaded(&$pages, &$currentPage, &$previousPage, &$nextPage)
{
// Filter the pages returned based on the pagination options
$this->offset = ($this->page_number-1) * $this->config['limit'];
// if filter_date is true, it filters so only dated items are returned.
if ($this->config['filter_date']) {
$show_pages = array();
foreach($pages as $key=>$page) {
if ($page['date']) {
$show_pages[$key] = $page;
}
}
} else {
$show_pages = $pages;
}
// get total pages before show_pages is sliced
$this->total_pages = ceil(count($show_pages) / $this->config['limit']);
// slice show_pages to the limit
$show_pages = array_slice($show_pages, $this->offset, $this->config['limit']);
// set filtered pages to paged_pages
$this->paged_pages = $show_pages;
}
public function onPageRendering(&$twig, &$twigVariables, &$templateName)
{
// Override 404 header
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
// Set a bunch of view vars
// send the paged pages in separate var
if ($this->paged_pages)
$twigVariables['paged_pages'] = $this->paged_pages;
// set var for page_number
if ($this->page_number)
$twigVariables['page_number'] = $this->page_number;
// set var for total pages
if ($this->total_pages)
$twigVariables['total_pages'] = $this->total_pages;
// set var for page_indicator
$twigVariables['page_indicator'] = $this->config['page_indicator'];
// build pagination links
// set next and back link vars to empty. links will be added below if they are available.
$twigVariables['next_page_link'] = $twigVariables['prev_page_link'] = '';
$pagination_parts = array();
if ($this->page_number > 1) {
$prev_path = $this->getBaseUrl() . '/' . $this->config['page_indicator'] . '/' . ($this->page_number - 1);
$pagination_parts['prev_link'] = $twigVariables['prev_page_link'] = '<a href="' . $prev_path . '" id="prev_page_link">' . $this->config['prev_text'] . '</a>';
}
if ($this->page_number < $this->total_pages) {
$next_path = $this->getBaseUrl() . '/' . $this->config['page_indicator'] . '/' . ($this->page_number + 1);
$pagination_parts['next_link'] = $twigVariables['next_page_link'] = '<a href="' . $next_path . '" id="next_page_link">' . $this->config['next_text'] . '</a>';
}
// reverse order if flip_links is on
if ($this->config['flip_links']) {
$pagination_parts = array_reverse($pagination_parts);
}
// create pagination links output
if ($this->config['output_format'] == "list") {
$twigVariables['pagination_links'] = '<ul id="pagination"><li>' . implode('</li><li>', array_values($pagination_parts)) . '</li></ul>';
} else {
$twigVariables['pagination_links'] = implode(' ', array_values($pagination_parts));
}
// set page of page var
$twigVariables['page_of_page'] = "Page " . $this->page_number . " of " . $this->total_pages . ".";
}
public function onRequestUrl(&$url)
{
// checks for page # in URL
$pattern = '/' . $this->config['page_indicator'] . '\/[0-9]*$/';
if (preg_match($pattern, $url)) {
$page_numbers = explode('/', $url);
$page_number = $page_numbers[count($page_numbers)-1];
$this->page_number = $page_number;
if ($this->config['sub_page']) {
$url = $this->config['page_indicator'];
} else {
$url = preg_replace($pattern, '', $url);
}
} else {
$this->page_number = 1;
}
}
}