-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPaginator.php
195 lines (168 loc) · 4.34 KB
/
Paginator.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* Class to paginate a list of items in a old digg style
*
* @author Darko Goleš
* @author Carlos Mafla <gigo6000@hotmail.com>
* @www.inchoo.net
*/
namespace Paginator;
class Paginator {
/**
* @var int current displayed page
*/
protected $currentPage;
/**
* @var int items limit (items per page)
*/
protected $limit;
/**
* @var int total number of pages
*/
protected $numPages;
/**
* @var int items limit (items per page)
*/
protected $itemsCount;
/**
* @var int offset
*/
protected $offset;
/**
* @var int pages to show at left and right of current page
*/
protected $midRange;
/**
* @var array range
*/
protected $range;
/**
* @param int $itemsCount
* @param int $currentPage
* @param int $limit
* @param int $midRange
*/
function __construct($itemsCount, $currentPage = 1, $limit = 20, $midRange = 7)
{
//set total items count from controller
$this->itemsCount = $itemsCount;
$this->currentPage = $currentPage;
$this->limit = $limit;
$this->midRange= $midRange;
//Set defaults
$this->setDefaults();
//Calculate number of pages total
$this->getInternalNumPages();
//Calculate first shown item on current page
$this->calculateOffset();
$this->calculateRange();
}
private function calculateRange()
{
$startRange = $this->currentPage - floor($this->midRange/2);
$endRange = $this->currentPage + floor($this->midRange/2);
if($startRange <= 0)
{
$endRange += abs($startRange)+1;
$startRange = 1;
}
if($endRange > $this->numPages)
{
$startRange -= $endRange-$this->numPages;
$endRange = $this->numPages;
}
$this->range = range($startRange, $endRange);
}
private function setDefaults()
{
//If currentPage is set to null or is set to 0 or less
//set it to default (1)
if ($this->currentPage == null || $this->currentPage < 1)
{
$this->currentPage = 1;
}
//if limit is set to null set it to default (20)
if ($this->limit == null)
{
$this->limit = 20;
//if limit is any number less than 1 then set it to 0 for displaying
//items without limit
}
else if ($this->limit < 1)
{
$this->limit = 0;
}
}
private function getInternalNumPages()
{
//If limit is set to 0 or set to number bigger then total items count
//display all in one page
if ($this->limit < 1 || $this->limit > $this->itemsCount)
{
$this->numPages = 1;
}
else
{
//Calculate rest numbers from dividing operation so we can add one
//more page for this items
$restItemsNum = $this->itemsCount % $this->limit;
//if rest items > 0 then add one more page else just divide items
//by limit
$this->numPages = $restItemsNum > 0 ? intval($this->itemsCount / $this->limit) + 1 : intval($this->itemsCount / $this->limit);
}
}
private function calculateOffset()
{
//Calculet offset for items based on current page number
$this->offset = ($this->currentPage - 1) * $this->limit;
}
/**
* @return int number of pages
*/
public function getNumPages()
{
return $this->numPages;
}
/**
* @return int current page
*/
public function getCurrentPage()
{
return $this->currentPage;
}
/**
* @return string current url
*/
public function getCurrentUrl()
{
return $this->currentUrl;
}
/**
* @return int limit items per page
*/
public function getLimit()
{
return $this->limit;
}
/**
* @return int offset
*/
public function getOffset()
{
return $this->offset;
}
/**
* @return array range
*/
public function getRange()
{
return $this->range;
}
/**
* @return int mid range
*/
public function getMidRange()
{
return $this->midRange;
}
}