These two files need to be swapped
[isso.git] / Pagination.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version 2 of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Pagination System (Pagination.php)
24 *
25 * @package ISSO
26 */
27
28 /**
29 * Pagination System
30 *
31 * On many pages, it is necessary to limit the amount of records to display.
32 * Using this class, you can set the maximum and minimum values to display,
33 * and then the input variables for page number and perpage. This will
34 * then create a page navigator and manage the SQL LIMIT statements.
35 *
36 * @author Blue Static
37 * @copyright Copyright (c)2005 - 2008, Blue Static
38 * @package ISSO
39 *
40 */
41 class BSPagination
42 {
43 /**
44 * Current page number
45 * @var integer
46 */
47 private $page;
48
49 /**
50 * Per-page value
51 * @var integer
52 */
53 private $perpage;
54
55 /**
56 * Number of page links
57 * @var integer
58 */
59 private $pagelinks;
60
61 /**
62 * Total number of results
63 * @var integer
64 */
65 private $total;
66
67 /**
68 * Total number of pages
69 * @var integer
70 */
71 private $pagecount;
72
73 /**
74 * Name of page variable
75 * @var array
76 */
77 private $pagevar = 'p';
78
79 /**
80 * Name of per-page variable
81 * @var integer
82 */
83 private $perpagevar = 'pp';
84
85 /**
86 * Maximum number of per-page results
87 * @var integer
88 */
89 private $maxperpage = 100;
90
91 /**
92 * Default number of per-page results
93 * @var integer
94 */
95 private $defaultperpage = 20;
96
97 /**
98 * The processing callback public function for individual pagenav bits
99 * @var string
100 */
101 private $bitprocessor = ':undefined:';
102
103 /**
104 * The processing callback public function for the entire pagenav system
105 * @var string
106 */
107 private $pagenavprocessor = ':undefined:';
108
109 /**
110 * Constructor
111 */
112 public function __construct()
113 {
114 if (!BSApp::$input instanceof BSInput)
115 {
116 throw new Exception('BSApp::$input is not an instance of BSInput');
117 }
118 }
119
120 // ###################################################################
121 /**
122 * Callback public function for the processing of an indivdual page. Needs
123 * the signature:
124 * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
125 *
126 * @param string Callback function
127 */
128 public function setBitProcessor($callback)
129 {
130 $this->bitprocessor = $callback;
131 }
132
133 // ###################################################################
134 /**
135 * Callback public function for the processing the entire page navigator. Needs
136 * the signature:
137 * public string callback(string $baseLink, integer $nextPage, integer $prevPage array $show['first'=>first page, 'last'=>last page, 'prev'=>previous page, 'next'=>next page], string $bits, Pagination $this)
138 *
139 * @param string Callback function
140 */
141 public function setNavigatorProcessor($callback)
142 {
143 $this->pagenavprocessor = $callback;
144 }
145
146 // ###################################################################
147 /**
148 * Returns the current page number
149 *
150 * @return integer Current page
151 */
152 public function getPage()
153 {
154 return $this->page;
155 }
156
157 // ###################################################################
158 /**
159 * Returns the current perpage value
160 *
161 * @return integer Current perpage
162 */
163 public function getPerPage()
164 {
165 return $this->perpage;
166 }
167
168 // ###################################################################
169 /**
170 * Sets total
171 *
172 * @param integer Total number
173 */
174 public function setTotal($total)
175 {
176 $this->total = $total;
177 }
178
179 // ###################################################################
180 /**
181 * Returns the number of pages to be in the navigator
182 *
183 * @param integer Number of pages
184 */
185 public function getPageCount()
186 {
187 return $this->pagecount;
188 }
189
190 // ###################################################################
191 /**
192 * Sets pagelinks
193 *
194 * @param integer Number of page links
195 */
196 public function setPageLinks($pagelinks)
197 {
198 $this->pagelinks = $pagelinks;
199 }
200
201 // ###################################################################
202 /**
203 * Sets pagevar
204 *
205 * @param string Page variable
206 */
207 public function setPageVar($pagevar)
208 {
209 $this->pagevar = $pagevar;
210 }
211
212 // ###################################################################
213 /**
214 * Sets perpagevar
215 *
216 * @param string Per-page variable
217 */
218 public function setPerPageVar($perpagevar)
219 {
220 $this->perpagevar = $perpagevar;
221 }
222
223 // ###################################################################
224 /**
225 * Sets maxperpage
226 *
227 * @param integer Maximum number per page
228 */
229 public function setMaxPerPage($maxperpage)
230 {
231 $this->maxperpage = $maxperpage;
232 }
233
234 // ###################################################################
235 /**
236 * Sets defaultperpage
237 *
238 * @param integer Total number
239 */
240 public function setDefaultPerPage($defaultperpage)
241 {
242 $this->defaultperpage = $defaultperpage;
243 }
244
245 // ###################################################################
246 /**
247 * Takes all of the information from the set() functions and then
248 * prepares all of the data through verification
249 */
250 public function processIncomingData()
251 {
252 $this->page = BSApp::$input->inputClean($this->pagevar, TYPE_INT);
253 $this->perpage = BSApp::$input->inputClean($this->perpagevar, TYPE_INT);
254 $this->pagelinks = BSApp::$input->clean($this->pagelinks, TYPE_INT);
255
256 if ($this->page <= 0)
257 {
258 $this->page = 1;
259 }
260
261 if ($this->perpage <= 0)
262 {
263 $this->perpage = $this->defaultperpage;
264 }
265 if ($this->perpage > $this->maxperpage)
266 {
267 $this->perpage = $this->maxperpage;
268 }
269
270 $this->perpage = BSApp::$input->clean($this->perpage, TYPE_INT);
271 }
272
273 // ###################################################################
274 /**
275 * Takes the variables and splits up the pages
276 */
277 public function splitPages()
278 {
279 $this->pagecount = ceil($this->total / $this->perpage);
280 if ($this->pagelinks == 0)
281 {
282 $this->pagelinks = $this->pagecount;
283 }
284 }
285
286 // ###################################################################
287 /**
288 * Returns the lower limit of the pages
289 *
290 * @param integer Page number
291 *
292 * @return integer Lower result limit
293 */
294 public function fetchLimit($page = null)
295 {
296 if ($page === null)
297 {
298 $page = $this->page;
299 }
300
301 $limit = $page * $this->perpage;
302
303 if ($page < 1)
304 {
305 $page = 1;
306 $limit = 0;
307 }
308 else if ($page > $this->pagecount)
309 {
310 $page = $this->pagecount - 1;
311 $limit = $this->total;
312 }
313
314 if ($limit < 0)
315 {
316 return 0;
317 }
318 else if ($limit > $this->total)
319 {
320 return $this->total;
321 }
322 else
323 {
324 return $limit;
325 }
326 }
327
328 // ###################################################################
329 /**
330 * Constructs the page navigator
331 *
332 * @param string Base link path
333 * @param bool Add a ? or a & to the path so it's link-friendly
334 *
335 * @return string Generated HTML page navigator
336 */
337 public function constructPageNav($baselink, $addParam = true)
338 {
339 global $bugsys;
340
341 // handle base link
342 if ($addParam)
343 {
344 if (strpos($baselink, '?') === false)
345 {
346 $baselink .= '?';
347 }
348 else if (!preg_match('#\?$#', $baselink) && !preg_match('#(&|&amp;)$#', $baselink))
349 {
350 $baselink .= '&amp;';
351 }
352 }
353
354 // first page number in page nav
355 $startpage = $this->page - $this->pagelinks;
356 if ($startpage < 1)
357 {
358 $startpage = 1;
359 }
360
361 // last page number in page nav
362 $endpage = $this->page + $this->pagelinks;
363 if ($endpage > $this->pagecount)
364 {
365 $endpage = $this->pagecount;
366 }
367
368 // prev page in page nav
369 $prevpage = $this->page - 1;
370 if ($prevpage < 1)
371 {
372 $prevpage = 1;
373 }
374
375 // next page in page nav
376 $nextpage = $this->page + 1;
377 if ($nextpage > $this->pagecount)
378 {
379 $nextpage = $this->pagecount;
380 }
381
382 // show the prev page
383 $show['prev'] = true;
384 if ($this->page == $startpage)
385 {
386 $show['prev'] = false;
387 }
388
389 // show the next page
390 $show['next'] = true;
391 if ($this->page == $endpage)
392 {
393 $show['next'] = false;
394 }
395
396 // show the first page
397 $show['first'] = false;
398 if ($startpage > 1)
399 {
400 $show['first'] = true;
401 }
402
403 // show the last page
404 $show['last'] = false;
405 if ($endpage < $this->pagecount)
406 {
407 $show['last'] = true;
408 }
409
410 // construct the page bits
411 $bits = '';
412 $call = $this->bitprocessor;
413 for ($i = $startpage; $i <= $endpage; $i++)
414 {
415 if ($i == $this->page)
416 {
417 $nolink = true;
418 }
419 else
420 {
421 $nolink = false;
422 }
423
424 $bits .= $call($baselink, $nolink, $i, $this);
425 }
426
427 $call = $this->pagenavprocessor;
428 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
429 }
430 }
431
432 ?>