]>
src.bluestatic.org Git - isso.git/blob - pagination.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
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
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 \*=====================================================================*/
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.
38 * pagenav_bit - The individual page numbers in the page navigator
39 * pagenav - The entirity of the page navigtaor
42 * @copyright Copyright ©2002 - [#]year[#], Blue Static
64 * Number of page links
71 * Total number of results
78 * Total number of pages
85 * Name of page variable
92 * Name of per-page variable
99 * Maximum number of per-page results
103 var $maxperpage = 100;
106 * Default number of per-page results
110 var $defaultperpage = 20;
113 * The processing callback function for individual pagenav bits
117 var $bitprocessor = ':undefined:';
120 * The processing callback function for the entire pagenav system
124 var $pagenavprocessor = ':undefined:';
126 // ###################################################################
130 function __construct(&$registry)
132 $this->registry
=& $registry;
135 // ###################################################################
137 * (PHP 4) Constructor
139 function Pagination(&$registry)
141 $this->__construct($registry);
144 // ###################################################################
146 * Callback function for the processing of an indivdual page. Needs
148 * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
152 * @param string Callback function
154 function setBitProcessor($callback)
156 $this->bitprocessor
= $callback;
159 // ###################################################################
161 * Callback function for the processing the entire page navigator. Needs
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)
167 * @param string Callback function
169 function setNavigatorProcessor($callback)
171 $this->pagenavprocessor
= $callback;
174 // ###################################################################
176 * Returns the current page number
180 * @return integer Current page
187 // ###################################################################
189 * Returns the current perpage value
193 * @return integer Current perpage
195 function getPerPage()
197 return $this->perpage
;
200 // ###################################################################
206 * @param integer Total number
208 function setTotal($total)
210 $this->total
= $total;
213 // ###################################################################
215 * Returns the number of pages to be in the navigator
219 * @param integer Number of pages
221 function getPageCount()
223 return $this->pagecount
;
226 // ###################################################################
232 * @param integer Number of page links
234 function setPageLinks($pagelinks)
236 $this->pagelinks
= $pagelinks;
239 // ###################################################################
245 * @param string Page variable
247 function setPageVar($pagevar)
249 $this->pagevar
= $pagevar;
252 // ###################################################################
258 * @param string Per-page variable
260 function setPerPageVar($perpagevar)
262 $this->perpagevar
= $perpagevar;
265 // ###################################################################
271 * @param integer Maximum number per page
273 function setMaxPerPage($maxperpage)
275 $this->maxperpage
= $maxperpage;
278 // ###################################################################
280 * Sets defaultperpage
284 * @param integer Total number
286 function setDefaultPerPage($defaultperpage)
288 $this->defaultperpage
= $defaultperpage;
292 // ###################################################################
294 * Takes all of the information from the set() functions and then
295 * prepares all of the data through verification
299 function processIncomingData()
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
);
305 if ($this->page
<= 0)
310 if ($this->perpage
<= 0)
312 $this->perpage
= $this->defaultperpage
;
314 if ($this->perpage
> $this->maxperpage
)
316 $this->perpage
= $this->maxperpage
;
319 $this->perpage
= $this->registry
->clean($this->perpage
, TYPE_INT
);
322 // ###################################################################
324 * Takes the variables and splits up the pages
328 function splitPages()
330 $this->pagecount
= ceil($this->total
/ $this->perpage
);
331 if ($this->pagelinks
== 0)
333 $this->pagelinks
= $this->pagecount
;
337 // ###################################################################
339 * Returns the lower limit of the pages
343 * @param integer Page number
345 * @return integer Lower result limit
347 function fetchLimit($page = null)
354 $limit = $page * $this->perpage
;
361 else if ($page > $this->pagecount
)
363 $page = $this->pagecount
- 1;
364 $limit = $this->total
;
371 else if ($limit > $this->total
)
381 // ###################################################################
383 * Constructs the page navigator
387 * @param string Base link path
389 * @return string Generated HTML page navigator
391 function constructPageNav($baselink)
396 if (strpos($baselink, '?') === false)
400 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&)$#', $baselink))
402 $baselink .= '&';
405 // first page number in page nav
406 $startpage = $this->page
- $this->pagelinks
;
412 // last page number in page nav
413 $endpage = $this->page +
$this->pagelinks
;
414 if ($endpage > $this->pagecount
)
416 $endpage = $this->pagecount
;
419 // prev page in page nav
420 $prevpage = $this->page
- 1;
426 // next page in page nav
427 $nextpage = $this->page +
1;
428 if ($nextpage > $this->pagecount
)
430 $nextpage = $this->pagecount
;
433 // show the prev page
434 $show['prev'] = true;
435 if ($this->page
== $startpage)
437 $show['prev'] = false;
440 // show the next page
441 $show['next'] = true;
442 if ($this->page
== $endpage)
444 $show['next'] = false;
447 // show the first page
448 $show['first'] = false;
451 $show['first'] = true;
454 // show the last page
455 $show['last'] = false;
456 if ($endpage < $this->pagecount
)
458 $show['last'] = true;
461 // construct the page bits
463 $call = $this->bitprocessor
;
464 for ($i = $startpage; $i <= $endpage; $i++
)
466 if ($i == $this->page
)
475 $bits .= $call($baselink, $nolink, $i, $this);
478 $call = $this->pagenavprocessor
;
479 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
483 /*=====================================================================*\
484 || ###################################################################
487 || ###################################################################
488 \*=====================================================================*/