1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the alphaz Framework. |
5
|
|
|
* |
6
|
|
|
* @author Muhammad Umer Farooq (Malik) <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/alphazframework/framework |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
* @since 1.0.0 |
13
|
|
|
* |
14
|
|
|
* @license MIT |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace alphaz\Common; |
18
|
|
|
|
19
|
|
|
class Pagination |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Total items. |
23
|
|
|
* |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
* |
26
|
|
|
* @var int |
27
|
|
|
*/ |
28
|
|
|
private $totalItems; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Item per page. |
32
|
|
|
* |
33
|
|
|
* @since 1.0.0 |
34
|
|
|
* |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $itemPerPage = 6; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Current page. |
41
|
|
|
* |
42
|
|
|
* @since 1.0.0 |
43
|
|
|
* |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
private $current; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Site base url. |
50
|
|
|
* |
51
|
|
|
* @since 1.0.0 |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $baseUrl; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Url append. |
59
|
|
|
* |
60
|
|
|
* @since 1.0.0 |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $urlAppend; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* ul tag class. |
68
|
|
|
* |
69
|
|
|
* @since 1.0.0 |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $ulCLass; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* li tag class. |
77
|
|
|
* |
78
|
|
|
* @since 1.0.0 |
79
|
|
|
* |
80
|
|
|
* @var string |
81
|
|
|
*/ |
82
|
|
|
private $liClass; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* a tag class. |
86
|
|
|
* |
87
|
|
|
* @since 1.0.0 |
88
|
|
|
* |
89
|
|
|
* @var string |
90
|
|
|
*/ |
91
|
|
|
private $aClass; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* __construct. |
95
|
|
|
* |
96
|
|
|
* @param (int) $items int total count. |
97
|
|
|
* @param (int) $perPage item in per page. |
98
|
|
|
* @param (int) $current current page. |
99
|
|
|
* @param (string) $urlAppend sub url. |
100
|
|
|
* @param (string) $ulClass ul class value. |
101
|
|
|
* @param (string) $liClass li class value. |
102
|
|
|
* @param (string) $aClass a class value |
103
|
|
|
* |
104
|
|
|
* @since 1.0.0 |
105
|
|
|
*/ |
106
|
|
|
public function __construct($total = 10, $perPage = 6, $current = 1, $urlAppend = '/', $ulCLass = 'pagination', $liClass = 'page-item', $aClass = 'page-link') |
107
|
|
|
{ |
108
|
|
|
$this->setTotalItems($total); |
109
|
|
|
$this->setItmPerPage($perPage); |
110
|
|
|
$this->setCurrentPage($current); |
111
|
|
|
$this->setBaseUrl(); |
112
|
|
|
$this->setUrlAppend($urlAppend); |
113
|
|
|
$this->ulCLass = $ulCLass; |
114
|
|
|
$this->liClass = $liClass; |
115
|
|
|
$this->aClass = $aClass; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Append the url. |
120
|
|
|
* |
121
|
|
|
* @param $append int sub url to be appended |
122
|
|
|
* |
123
|
|
|
* @since 1.0.0 |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function setUrlAppend($append) |
128
|
|
|
{ |
129
|
|
|
$this->urlAppend = $append; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set the current page. |
134
|
|
|
* |
135
|
|
|
* @param (int) $current current page. |
136
|
|
|
* |
137
|
|
|
* @since 1.0.0 |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function setCurrentPage($current) |
142
|
|
|
{ |
143
|
|
|
return ($current >= 0) ? $this->current = $current : false; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the base url. |
148
|
|
|
* |
149
|
|
|
* @since 1.0.0 |
150
|
|
|
* |
151
|
|
|
* @return object |
152
|
|
|
*/ |
153
|
|
|
public function setBaseUrl() |
154
|
|
|
{ |
155
|
|
|
$this->baseUrl = site_base_url(); |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Set the per page item. |
162
|
|
|
* |
163
|
|
|
* @param(int) $items item per page item. |
164
|
|
|
* |
165
|
|
|
* @since 1.0.0 |
166
|
|
|
* |
167
|
|
|
* @return bool |
168
|
|
|
*/ |
169
|
|
|
public function setItmPerPage($item) |
170
|
|
|
{ |
171
|
|
|
return ($item > 0) ? $this->itemPerPage = $item : false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set the total items. |
176
|
|
|
* |
177
|
|
|
* @param (int) $items total item count. |
178
|
|
|
* |
179
|
|
|
* @since 1.0.0 |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function setTotalItems($items) |
184
|
|
|
{ |
185
|
|
|
return ($items > 0) ? $this->totalItems = $items : false; |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Generate pagination link. |
190
|
|
|
* |
191
|
|
|
* @param (int) $number page number. |
192
|
|
|
* |
193
|
|
|
* @since 1.0.0 |
194
|
|
|
* |
195
|
|
|
* @return mixed |
196
|
|
|
*/ |
197
|
|
|
public function generateLink($number) |
198
|
|
|
{ |
199
|
|
|
return $this->baseUrl.$this->urlAppend.$number.' '; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Generate the pagination. |
204
|
|
|
* |
205
|
|
|
* @since 1.0.0 |
206
|
|
|
* |
207
|
|
|
* @return mixed |
208
|
|
|
*/ |
209
|
|
|
public function pagination() |
210
|
|
|
{ |
211
|
|
|
$pageCount = ceil($this->totalItems / $this->itemPerPage); |
212
|
|
|
if ($this->current >= 1 && $this->current <= $pageCount) { |
213
|
|
|
$current_range = [$this->current - 2 < 1 ? 1 : $this->current - 2, $this->current + 2 > $pageCount ? $pageCount : $this->current + 2]; |
214
|
|
|
$first_page = $this->current > 5 ? '<li><a href="'.$this->baseUrl.$this->urlAppend.'1'.'" class="'.$this->aClass.'">'.printl('first:page:pagination').'</a></li>'.($this->current < 5 ? ', ' : ' <li class="'.$this->liClass.'"><a href="#!" class="'.$this->aClass.' disable" disabled >...</a></li> ') : null; |
215
|
|
|
$last_page = $this->current < $pageCount - 2 ? ($this->current > $pageCount - 4 ? ', ' : ' <li class="'.$this->liClass.' disable"><a href="#!" class="'.$this->aClass.'" disabled >...</a></li> ').'<li><a href="'.$this->baseUrl.$this->urlAppend.$pageCount.'" class="'.$this->aClass.'">'.printl('last:page:pagination').'</a></li>' : null; |
216
|
|
|
$previous_page = $this->current > 1 ? '<li class="'.$this->liClass.'"><a class="'.$this->aClass.'"href="'.$this->baseUrl.$this->urlAppend.($this->current - 1).'">'.printl('prev:page:pagination').'</a></li> ' : null; |
217
|
|
|
$next_page = $this->current < $pageCount ? ' <li class="'.$this->liClass.'"><a class="'.$this->aClass.'" href="'.$this->baseUrl.$this->urlAppend.($this->current + 1).'">'.printl('next:page:pagination').'</a></li>' : null; |
218
|
|
|
for ($x = $current_range[0]; $x <= $current_range[1]; $x++) { |
219
|
|
|
$pages[] = '<li class="'.$this->liClass.'"active"><a class="'.$this->aClass.'" href="'.$this->baseUrl.$this->urlAppend.$x.'" '.($x == $this->current ? 'class="'.$this->aClass.'"' : '').'>'.$x.'</a></li>'; |
220
|
|
|
} |
221
|
|
|
if ($pageCount > 1) { |
222
|
|
|
return '<ul class="'.$this->ulCLass.'"> '.$previous_page.$first_page.implode(', ', $pages).$last_page.$next_page.'</ul>'; |
|
|
|
|
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* __Tostring. |
229
|
|
|
* |
230
|
|
|
* @since 1.0.0 |
231
|
|
|
* |
232
|
|
|
* @return mixed |
233
|
|
|
*/ |
234
|
|
|
public function __toString() |
235
|
|
|
{ |
236
|
|
|
$this->pagination(); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|