]> src.bluestatic.org Git - isso.git/blob - pagination.php
Removing BSLoader and we'll just go with a singleton instance of BSRegister
[isso.git] / pagination.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] 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 [#]gpl[#] 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
24 * pagination.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Pagination System
31 *
32 * On many pages, it is necessary to limit the amount of records to display.
33 * Using this class, you can set the maximum and minimum values to display,
34 * and then the input variables for page number and perpage. This will
35 * then create a page navigator and manage the SQL LIMIT statements.
36 *
37 * Templates required:
38 * pagenav_bit - The individual page numbers in the page navigator
39 * pagenav - The entirity of the page navigtaor
40 *
41 * @author Blue Static
42 * @copyright Copyright ©2002 - [#]year[#], Blue Static
43 * @version $Revision$
44 * @package ISSO
45 *
46 */
47 class Pagination
48 {
49 /**
50 * Current page number
51 * @var integer
52 * @access private
53 */
54 var $page;
55
56 /**
57 * Per-page value
58 * @var integer
59 * @access private
60 */
61 var $perpage;
62
63 /**
64 * Number of page links
65 * @var integer
66 * @access private
67 */
68 var $pagelinks;
69
70 /**
71 * Total number of results
72 * @var integer
73 * @access private
74 */
75 var $total;
76
77 /**
78 * Total number of pages
79 * @var integer
80 * @access private
81 */
82 var $pagecount;
83
84 /**
85 * Name of page variable
86 * @var array
87 * @access private
88 */
89 var $pagevar;
90
91 /**
92 * Name of per-page variable
93 * @var integer
94 * @access private
95 */
96 var $perpagevar;
97
98 /**
99 * Maximum number of per-page results
100 * @var integer
101 * @access private
102 */
103 var $maxperpage = 100;
104
105 /**
106 * Default number of per-page results
107 * @var integer
108 * @access private
109 */
110 var $defaultperpage = 20;
111
112 /**
113 * The processing callback function for individual pagenav bits
114 * @var string
115 * @access private
116 */
117 var $bitprocessor = ':undefined:';
118
119 /**
120 * The processing callback function for the entire pagenav system
121 * @var string
122 * @access private
123 */
124 var $pagenavprocessor = ':undefined:';
125
126 // ###################################################################
127 /**
128 * Constructor
129 */
130 function __construct(&$registry)
131 {
132 $this->registry =& $registry;
133 }
134
135 // ###################################################################
136 /**
137 * (PHP 4) Constructor
138 */
139 function Pagination(&$registry)
140 {
141 $this->__construct($registry);
142 }
143
144 // ###################################################################
145 /**
146 * Callback function for the processing of an indivdual page. Needs
147 * the signature:
148 * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
149 *
150 * @access public
151 *
152 * @param string Callback function
153 */
154 function setBitProcessor($callback)
155 {
156 $this->bitprocessor = $callback;
157 }
158
159 // ###################################################################
160 /**
161 * Callback function for the processing the entire page navigator. Needs
162 * the signature:
163 * 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)
164 *
165 * @access public
166 *
167 * @param string Callback function
168 */
169 function setNavigatorProcessor($callback)
170 {
171 $this->pagenavprocessor = $callback;
172 }
173
174 // ###################################################################
175 /**
176 * Returns the current page number
177 *
178 * @access public
179 *
180 * @return integer Current page
181 */
182 function getPage()
183 {
184 return $this->page;
185 }
186
187 // ###################################################################
188 /**
189 * Returns the current perpage value
190 *
191 * @access public
192 *
193 * @return integer Current perpage
194 */
195 function getPerPage()
196 {
197 return $this->perpage;
198 }
199
200 // ###################################################################
201 /**
202 * Sets total
203 *
204 * @access public
205 *
206 * @param integer Total number
207 */
208 function setTotal($total)
209 {
210 $this->total = $total;
211 }
212
213 // ###################################################################
214 /**
215 * Returns the number of pages to be in the navigator
216 *
217 * @access public
218 *
219 * @param integer Number of pages
220 */
221 function getPageCount()
222 {
223 return $this->pagecount;
224 }
225
226 // ###################################################################
227 /**
228 * Sets pagelinks
229 *
230 * @access public
231 *
232 * @param integer Number of page links
233 */
234 function setPageLinks($pagelinks)
235 {
236 $this->pagelinks = $pagelinks;
237 }
238
239 // ###################################################################
240 /**
241 * Sets pagevar
242 *
243 * @access public
244 *
245 * @param string Page variable
246 */
247 function setPageVar($pagevar)
248 {
249 $this->pagevar = $pagevar;
250 }
251
252 // ###################################################################
253 /**
254 * Sets perpagevar
255 *
256 * @access public
257 *
258 * @param string Per-page variable
259 */
260 function setPerPageVar($perpagevar)
261 {
262 $this->perpagevar = $perpagevar;
263 }
264
265 // ###################################################################
266 /**
267 * Sets maxperpage
268 *
269 * @access public
270 *
271 * @param integer Maximum number per page
272 */
273 function setMaxPerPage($maxperpage)
274 {
275 $this->maxperpage = $maxperpage;
276 }
277
278 // ###################################################################
279 /**
280 * Sets defaultperpage
281 *
282 * @access public
283 *
284 * @param integer Total number
285 */
286 function setDefaultPerPage($defaultperpage)
287 {
288 $this->defaultperpage = $defaultperpage;
289 }
290
291
292 // ###################################################################
293 /**
294 * Takes all of the information from the set() functions and then
295 * prepares all of the data through verification
296 *
297 * @access public
298 */
299 function processIncomingData()
300 {
301 $this->page = $this->registry->input_clean($this->pagevar, TYPE_INT);
302 $this->perpage = $this->registry->input_clean($this->perpagevar, TYPE_INT);
303 $this->pagelinks = $this->registry->clean($this->pagelinks, TYPE_INT);
304
305 if ($this->page <= 0)
306 {
307 $this->page = 1;
308 }
309
310 if ($this->perpage <= 0)
311 {
312 $this->perpage = $this->defaultperpage;
313 }
314 if ($this->perpage > $this->maxperpage)
315 {
316 $this->perpage = $this->maxperpage;
317 }
318
319 $this->perpage = $this->registry->clean($this->perpage, TYPE_INT);
320 }
321
322 // ###################################################################
323 /**
324 * Takes the variables and splits up the pages
325 *
326 * @access public
327 */
328 function splitPages()
329 {
330 $this->pagecount = ceil($this->total / $this->perpage);
331 if ($this->pagelinks == 0)
332 {
333 $this->pagelinks = $this->pagecount;
334 }
335 }
336
337 // ###################################################################
338 /**
339 * Returns the lower limit of the pages
340 *
341 * @access public
342 *
343 * @param integer Page number
344 *
345 * @return integer Lower result limit
346 */
347 function fetchLimit($page = null)
348 {
349 if ($page === null)
350 {
351 $page = $this->page;
352 }
353
354 $limit = $page * $this->perpage;
355
356 if ($page < 1)
357 {
358 $page = 1;
359 $limit = 0;
360 }
361 else if ($page > $this->pagecount)
362 {
363 $page = $this->pagecount - 1;
364 $limit = $this->total;
365 }
366
367 if ($limit < 0)
368 {
369 return 0;
370 }
371 else if ($limit > $this->total)
372 {
373 return $this->total;
374 }
375 else
376 {
377 return $limit;
378 }
379 }
380
381 // ###################################################################
382 /**
383 * Constructs the page navigator
384 *
385 * @access public
386 *
387 * @param string Base link path
388 *
389 * @return string Generated HTML page navigator
390 */
391 function constructPageNav($baselink)
392 {
393 global $bugsys;
394
395 // handle base link
396 if (strpos($baselink, '?') === false)
397 {
398 $baselink .= '?';
399 }
400 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
401 {
402 $baselink .= '&amp;';
403 }
404
405 // first page number in page nav
406 $startpage = $this->page - $this->pagelinks;
407 if ($startpage < 1)
408 {
409 $startpage = 1;
410 }
411
412 // last page number in page nav
413 $endpage = $this->page + $this->pagelinks;
414 if ($endpage > $this->pagecount)
415 {
416 $endpage = $this->pagecount;
417 }
418
419 // prev page in page nav
420 $prevpage = $this->page - 1;
421 if ($prevpage < 1)
422 {
423 $prevpage = 1;
424 }
425
426 // next page in page nav
427 $nextpage = $this->page + 1;
428 if ($nextpage > $this->pagecount)
429 {
430 $nextpage = $this->pagecount;
431 }
432
433 // show the prev page
434 $show['prev'] = true;
435 if ($this->page == $startpage)
436 {
437 $show['prev'] = false;
438 }
439
440 // show the next page
441 $show['next'] = true;
442 if ($this->page == $endpage)
443 {
444 $show['next'] = false;
445 }
446
447 // show the first page
448 $show['first'] = false;
449 if ($startpage > 1)
450 {
451 $show['first'] = true;
452 }
453
454 // show the last page
455 $show['last'] = false;
456 if ($endpage < $this->pagecount)
457 {
458 $show['last'] = true;
459 }
460
461 // construct the page bits
462 $bits = '';
463 $call = $this->bitprocessor;
464 for ($i = $startpage; $i <= $endpage; $i++)
465 {
466 if ($i == $this->page)
467 {
468 $nolink = true;
469 }
470 else
471 {
472 $nolink = false;
473 }
474
475 $bits .= $call($baselink, $nolink, $i, $this);
476 }
477
478 $call = $this->pagenavprocessor;
479 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
480 }
481 }
482
483 /*=====================================================================*\
484 || ###################################################################
485 || # $HeadURL$
486 || # $Id$
487 || ###################################################################
488 \*=====================================================================*/
489 ?>