1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace puck\helpers; |
5
|
|
|
|
6
|
|
|
class Page { |
7
|
|
|
/** |
8
|
|
|
* set the number of items per page. |
9
|
|
|
* |
10
|
|
|
* @var numeric |
11
|
|
|
*/ |
12
|
|
|
private $_perPage; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* set get parameter for fetching the page number |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $_instance; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* sets the page number. |
23
|
|
|
* |
24
|
|
|
* @var numeric |
25
|
|
|
*/ |
26
|
|
|
private $_page; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* set the limit for the data source |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $_limit; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* set the total number of records/items. |
37
|
|
|
* |
38
|
|
|
* @var numeric |
39
|
|
|
*/ |
40
|
|
|
private $_totalRows=0; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* __construct |
46
|
|
|
* |
47
|
|
|
* pass values when class is istantiated |
48
|
|
|
* |
49
|
|
|
* @param numeric $perPage sets the number of iteems per page |
50
|
|
|
* @param numeric $instance sets the instance for the GET parameter |
51
|
|
|
*/ |
52
|
|
|
public function __construct($perPage, $instance) { |
53
|
|
|
$this->_instance=$instance; |
|
|
|
|
54
|
|
|
$this->_perPage=$perPage; |
55
|
|
|
$this->set_instance(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* get_start |
60
|
|
|
* |
61
|
|
|
* creates the starting point for limiting the dataset |
62
|
|
|
* @return numeric |
63
|
|
|
*/ |
64
|
|
|
public function get_start() { |
65
|
|
|
return ($this->_page * $this->_perPage) - $this->_perPage; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* set_instance |
70
|
|
|
* |
71
|
|
|
* sets the instance parameter, if numeric value is 0 then set to 1 |
72
|
|
|
* |
73
|
|
|
* @var numeric |
74
|
|
|
*/ |
75
|
|
|
private function set_instance() { |
|
|
|
|
76
|
|
|
$this->_page=(int) (!isset($_GET[$this->_instance]) ? 1 : $_GET[$this->_instance]); |
|
|
|
|
77
|
|
|
$this->_page=($this->_page == 0 ? 1 : $this->_page); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* set_total |
82
|
|
|
* |
83
|
|
|
* collect a numberic value and assigns it to the totalRows |
84
|
|
|
* |
85
|
|
|
* @var numeric |
86
|
|
|
*/ |
87
|
|
|
public function set_total($_totalRows) { |
88
|
|
|
$this->_totalRows=$_totalRows; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* get_limit |
93
|
|
|
* |
94
|
|
|
* returns the limit for the data source, calling the get_start method and passing in the number of items perp page |
95
|
|
|
* |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function get_limit() { |
99
|
|
|
return "LIMIT ".$this->get_start().",$this->_perPage"; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
private function getParam() { |
|
|
|
|
104
|
|
|
$vars=$_GET; |
105
|
|
|
if (isset($vars[$this->_instance])) { |
106
|
|
|
unset($vars[$this->_instance]); |
107
|
|
|
} |
108
|
|
|
$str=http_build_query($vars); |
109
|
|
|
$str='&'.$str; |
110
|
|
|
return $str; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* page_links |
115
|
|
|
* |
116
|
|
|
* create the html links for navigating through the dataset |
117
|
|
|
* |
118
|
|
|
* @var sting $path optionally set the path for the link |
119
|
|
|
* @var sting $ext optionally pass in extra parameters to the GET |
120
|
|
|
* @return string returns the html menu |
121
|
|
|
*/ |
122
|
|
|
public function page_links($path='?', $ext=null) |
123
|
|
|
{ |
124
|
|
|
$adjacents="2"; |
125
|
|
|
$prev=$this->_page - 1; |
126
|
|
|
$next=$this->_page + 1; |
127
|
|
|
$lastpage=ceil($this->_totalRows / $this->_perPage); |
128
|
|
|
$lpm1=$lastpage - 1; |
129
|
|
|
$pagination=""; |
130
|
|
|
if ($lastpage > 1) |
131
|
|
|
{ |
132
|
|
|
if ($ext == null) { |
133
|
|
|
$ext=$this->getParam(); |
134
|
|
|
} |
135
|
|
|
$pagination.="<ul class='pager'>"; |
136
|
|
|
if ($this->_page > 1) { |
137
|
|
|
$pagination.="<li class='previous'><a href='".$path."$this->_instance=$prev"."$ext'><<</a></li>"; |
138
|
|
|
} else { |
139
|
|
|
$pagination.="<li class='disabled previous'><span class='disabled'><<</span></li>"; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($lastpage < 7 + ($adjacents * 2)) |
143
|
|
|
{ |
144
|
|
|
for ($counter=1; $counter <= $lastpage; $counter++) |
145
|
|
|
{ |
146
|
|
|
if ($counter == $this->_page) { |
147
|
|
|
$pagination.="<li class='current'><span>$counter</span></li>"; |
148
|
|
|
} else { |
149
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>"; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} elseif ($lastpage > 5 + ($adjacents * 2)) |
153
|
|
|
{ |
154
|
|
|
if ($this->_page < 1 + ($adjacents * 2)) |
155
|
|
|
{ |
156
|
|
|
for ($counter=1; $counter < 4 + ($adjacents * 2); $counter++) |
157
|
|
|
{ |
158
|
|
|
if ($counter == $this->_page) { |
159
|
|
|
$pagination.="<li class='current'><span>$counter</span></li>"; |
160
|
|
|
} else { |
161
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>"; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
$pagination.="..."; |
165
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a></li>"; |
166
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a></li>"; |
167
|
|
|
} elseif ($lastpage - ($adjacents * 2) > $this->_page && $this->_page > ($adjacents * 2)) |
168
|
|
|
{ |
169
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=1"."$ext'>1</a></li>"; |
170
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=2"."$ext'>2</a></li>"; |
171
|
|
|
$pagination.="..."; |
172
|
|
|
for ($counter=$this->_page - $adjacents; $counter <= $this->_page + $adjacents; $counter++) |
173
|
|
|
{ |
174
|
|
|
if ($counter == $this->_page) { |
175
|
|
|
$pagination.="<li class='current'><span>$counter</span></li>"; |
176
|
|
|
} else { |
177
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>"; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
$pagination.=".."; |
181
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$lpm1"."$ext'>$lpm1</a></li>"; |
182
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$lastpage"."$ext'>$lastpage</a></li>"; |
183
|
|
|
} else |
184
|
|
|
{ |
185
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=1"."$ext'>1</a></li>"; |
186
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=2"."$ext'>2</a></li>"; |
187
|
|
|
$pagination.=".."; |
188
|
|
|
for ($counter=$lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++) |
189
|
|
|
{ |
190
|
|
|
if ($counter == $this->_page) { |
191
|
|
|
$pagination.="<li class='current'><span>$counter</span></li>"; |
192
|
|
|
} else { |
193
|
|
|
$pagination.="<li><a href='".$path."$this->_instance=$counter"."$ext'>$counter</a></li>"; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($this->_page < $counter - 1) { |
|
|
|
|
200
|
|
|
$pagination.="<li class='next'><a href='".$path."$this->_instance=$next"."$ext'>>></a></li>"; |
201
|
|
|
} else { |
202
|
|
|
$pagination.="<li class='next disabled'><span class='disabled'>>></span></li>"; |
203
|
|
|
} |
204
|
|
|
$pagination.="</ul>\n"; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
return $pagination; |
209
|
|
|
} |
210
|
|
|
} |
This check marks private properties in classes that are never used. Those properties can be removed.