1
|
|
|
<?php |
2
|
|
|
namespace Pager; |
3
|
|
|
|
4
|
|
|
class Pagination |
5
|
|
|
{ |
6
|
|
|
public static $current; |
7
|
|
|
protected $queryString; |
8
|
|
|
|
9
|
|
|
protected static $page; |
10
|
|
|
protected static $pageURL; |
11
|
|
|
protected static $lastpage; |
12
|
|
|
protected static $totalPages; |
13
|
|
|
|
14
|
|
|
public $pagerClass = 'pagination justify-content-center'; |
15
|
|
|
public $liClass = 'page-item'; |
16
|
|
|
public $liActiveClass = 'active'; |
17
|
|
|
public $aClass = 'page-link'; |
18
|
|
|
public $aActiveClass = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Sets the class assigned to the UL element of the pagination object |
22
|
|
|
* @param string $class This should be the class or classes that you wish to give to the pagination object |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
2 |
|
public function setPaginationClass($class) |
26
|
|
|
{ |
27
|
2 |
|
if ((!empty(trim($class)))) { |
28
|
2 |
|
$this->pagerClass = $class; |
29
|
|
|
} |
30
|
2 |
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Returns the class to give to the pagination object |
35
|
|
|
* @return string The pagination class will be returned |
36
|
|
|
*/ |
37
|
4 |
|
public function getPaginationClass() |
38
|
|
|
{ |
39
|
4 |
|
return $this->pagerClass; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets the default li class |
44
|
|
|
* @param string $class This should be the class that you want to all to all li elements |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
1 |
|
public function setLiClass($class) |
48
|
|
|
{ |
49
|
1 |
|
$this->liClass = trim($class); |
50
|
1 |
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the current li class |
55
|
|
|
* @return string This should be the class to assign on li elements |
56
|
|
|
*/ |
57
|
3 |
|
public function getLiClass() |
58
|
|
|
{ |
59
|
3 |
|
return $this->liClass; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the active class to assign on the li elements |
64
|
|
|
* @param string $class This should be the class to assign on active elements |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
1 |
|
public function setLiActiveClass($class) |
68
|
|
|
{ |
69
|
1 |
|
$this->liActiveClass = trim($class); |
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Returns the class to assign to active li elements |
75
|
|
|
* @return string This should be the class to assign on active elements |
76
|
|
|
*/ |
77
|
4 |
|
public function getLiActiveClass() |
78
|
|
|
{ |
79
|
4 |
|
return $this->liActiveClass; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Sets the default class on a elements |
85
|
|
|
* @param string $class This should be the class to add to a elements |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
1 |
|
public function setAClass($class) |
89
|
|
|
{ |
90
|
1 |
|
$this->aClass = trim($class); |
91
|
1 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the class assigned to a elements |
96
|
|
|
* @return string Returns the class assigned to a elements |
97
|
|
|
*/ |
98
|
3 |
|
public function getAClass() |
99
|
|
|
{ |
100
|
3 |
|
return $this->aClass; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sets the class to assign to active a elements |
105
|
|
|
* @param string $class This should be the class to add to active a elements |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
1 |
|
public function setAActiveClass($class) |
109
|
|
|
{ |
110
|
1 |
|
$this->aActiveClass = trim($class); |
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the class assigned to active a elements |
116
|
|
|
* @return string This should be the class to add to active a elements |
117
|
|
|
*/ |
118
|
3 |
|
public function getAActiveClass() |
119
|
|
|
{ |
120
|
3 |
|
return $this->aActiveClass; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns paging buttons for the number of records |
125
|
|
|
* @param int $records The total number of records |
126
|
|
|
* @param string $pageURL The URL of the page you are creating the paging for |
127
|
|
|
* @param int $start The start number for the results |
128
|
|
|
* @param int $maxshown The number of records that are shown on each page |
129
|
|
|
* @param int $numpagesshown The number of pagination buttons to display |
130
|
|
|
* @param boolean $arrows If you want arrows to display before and after for next and previous set to true (default) else set to false |
131
|
|
|
* @param array $additional Any additional get values to include in the URL |
132
|
|
|
* @return string|false Returns the pagination menu if required else will return false |
133
|
|
|
*/ |
134
|
4 |
|
public function paging($records, $pageURL, $start = 0, $maxshown = 50, $numpagesshown = 11, $arrows = true, $additional = array()) |
135
|
|
|
{ |
136
|
4 |
|
self::$pageURL = $pageURL; |
137
|
4 |
|
$this->queryString = $additional; |
138
|
4 |
|
if ($records > $maxshown) { |
139
|
3 |
|
self::$current = $start >= 1 ? intval($start) : 1; |
140
|
3 |
|
self::$totalPages = ceil(intval($records) / ($maxshown >= 1 ? intval($maxshown) : 1)); |
141
|
3 |
|
self::$lastpage = self::$totalPages; |
142
|
3 |
|
$this->getPage($records, $maxshown, $numpagesshown); |
143
|
|
|
|
144
|
3 |
|
$paging = '<ul class="'.$this->getPaginationClass().'">'.$this->preLinks($arrows); |
145
|
3 |
|
while (self::$page <= self::$lastpage) { |
146
|
3 |
|
$paging .= $this->buildLink(self::$page, self::$page, (self::$current == self::$page)); |
147
|
3 |
|
self::$page = (self::$page + 1); |
148
|
|
|
} |
149
|
3 |
|
return $paging.$this->postLinks($arrows).'</ul>'; |
150
|
|
|
} |
151
|
1 |
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Build a link item with the given values |
156
|
|
|
* @param string $link This should be any additional items to be included as part of the link |
157
|
|
|
* @param mixed $page This should be the link test on the link normally set as numbers but may be anything like arrows or dots etc |
158
|
|
|
* @param boolean $current If this is the current link item set this as true so the class is added to the link item |
159
|
|
|
* @return string This will return the paging item as a string |
160
|
|
|
*/ |
161
|
3 |
|
protected function buildLink($link, $page, $current = false) |
162
|
|
|
{ |
163
|
3 |
|
return '<li'.(!empty($this->getLiClass()) || ($current === true && !empty($this->getLiActiveClass())) ? ' class="'.trim($this->getLiClass().(($current === true && !empty($this->getLiActiveClass())) ? ' '.$this->getLiActiveClass() : '').'"') : '').'><a href="'.self::$pageURL.(!empty($this->buildQueryString($link)) ? '?'.$this->buildQueryString($link) : '').'" title="Page '.$page.'"'.(!empty($this->getAClass()) || ($current === true && !empty($this->getAActiveClass())) ? ' class="'.trim($this->getAClass().(($current === true && !empty($this->getAActiveClass())) ? ' '.$this->getAActiveClass() : '')).'"' : '').'>'.$page.'</a></li>'; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Builds the query string to add to the URL |
168
|
|
|
* @param mixed $page If the page variable is set to a number will add the page number to the query string else will not add any additional items |
169
|
|
|
* @return string The complete string will be returned to add to the link item |
170
|
|
|
*/ |
171
|
3 |
|
protected function buildQueryString($page) |
172
|
|
|
{ |
173
|
3 |
|
$pageInfo = is_numeric($page) ? ['page' => $page] : []; |
174
|
3 |
|
return http_build_query(array_filter(array_merge($pageInfo, $this->queryString)), '', '&'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Gets the current page |
179
|
|
|
* @param int $records The total number of records |
180
|
|
|
* @param int $maxshown The number of records that are shown on each page |
181
|
|
|
* @param int $numpages The number of pagination buttons to display |
182
|
|
|
* return void Nothing is returned |
183
|
|
|
*/ |
184
|
3 |
|
protected function getPage($records, $maxshown, $numpages) |
185
|
|
|
{ |
186
|
3 |
|
$show = floor($numpages / 2); |
187
|
3 |
|
if (self::$lastpage > $numpages) { |
188
|
1 |
|
self::$page = (self::$current > $show ? (self::$current - $show) : 1); |
189
|
1 |
|
if (self::$current < (self::$lastpage - $show)) { |
190
|
1 |
|
self::$lastpage = ((self::$current <= $show) ? (self::$current + ($numpages - self::$current)) : (self::$current + $show)); |
191
|
|
|
} else { |
192
|
1 |
|
self::$page = self::$current - ($numpages - ((ceil(intval($records) / ($maxshown >= 1 ? intval($maxshown) : 1)) - self::$current)) - 1); |
193
|
|
|
} |
194
|
|
|
} else { |
195
|
3 |
|
self::$page = 1; |
196
|
|
|
} |
197
|
3 |
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Returns the previous arrows as long as arrows is set to true and the page is not the first page |
201
|
|
|
* @param boolean $arrows If you want to display previous arrows set to true else set to false |
202
|
|
|
* @return string Any previous link arrows will be returned as a string |
203
|
|
|
*/ |
204
|
3 |
|
protected function preLinks($arrows = true) |
205
|
|
|
{ |
206
|
3 |
|
$paging = ''; |
207
|
3 |
|
if (self::$current != 1 && $arrows) { |
208
|
2 |
|
if (self::$current != 2) { |
209
|
2 |
|
$paging .= $this->buildLink('', '«'); |
210
|
|
|
} |
211
|
2 |
|
$paging .= $this->buildLink((self::$current - 1), '<'); |
212
|
|
|
} |
213
|
3 |
|
return $paging; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Returns the next arrows as long as arrows is set to true and the page is not the last page |
218
|
|
|
* @param boolean $arrows If you want to display next arrows set to true else set to false |
219
|
|
|
* @return string Any next link arrows will be returned as a string |
220
|
|
|
*/ |
221
|
3 |
|
protected function postLinks($arrows = true) |
222
|
|
|
{ |
223
|
3 |
|
$paging = ''; |
224
|
3 |
|
if (self::$current != self::$totalPages && $arrows) { |
225
|
3 |
|
$paging .= $this->buildLink((self::$current + 1), '>'); |
226
|
3 |
|
if (self::$current != (self::$totalPages - 1)) { |
227
|
2 |
|
$paging .= $this->buildLink(self::$totalPages, '»'); |
228
|
|
|
} |
229
|
|
|
} |
230
|
3 |
|
return $paging; |
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|