]>
src.bluestatic.org Git - isso.git/blob - Pagination.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2002-2007 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 2 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 \*=====================================================================*/
23 * Pagination System (Pagination.php)
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.
37 * @copyright Copyright (c)2002 - 2007, Blue Static
56 * Number of page links
62 * Total number of results
68 * Total number of pages
74 * Name of page variable
77 private $pagevar = 'p';
80 * Name of per-page variable
83 private $perpagevar = 'pp';
86 * Maximum number of per-page results
89 private $maxperpage = 100;
92 * Default number of per-page results
95 private $defaultperpage = 20;
98 * The processing callback public function for individual pagenav bits
101 private $bitprocessor = ':undefined:';
104 * The processing callback public function for the entire pagenav system
107 private $pagenavprocessor = ':undefined:';
109 // ###################################################################
111 * Callback public function for the processing of an indivdual page. Needs
113 * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
115 * @param string Callback function
117 public function setBitProcessor($callback)
119 $this->bitprocessor
= $callback;
122 // ###################################################################
124 * Callback public function for the processing the entire page navigator. Needs
126 * 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)
128 * @param string Callback function
130 public function setNavigatorProcessor($callback)
132 $this->pagenavprocessor
= $callback;
135 // ###################################################################
137 * Returns the current page number
139 * @return integer Current page
141 public function getPage()
146 // ###################################################################
148 * Returns the current perpage value
150 * @return integer Current perpage
152 public function getPerPage()
154 return $this->perpage
;
157 // ###################################################################
161 * @param integer Total number
163 public function setTotal($total)
165 $this->total
= $total;
168 // ###################################################################
170 * Returns the number of pages to be in the navigator
172 * @param integer Number of pages
174 public function getPageCount()
176 return $this->pagecount
;
179 // ###################################################################
183 * @param integer Number of page links
185 public function setPageLinks($pagelinks)
187 $this->pagelinks
= $pagelinks;
190 // ###################################################################
194 * @param string Page variable
196 public function setPageVar($pagevar)
198 $this->pagevar
= $pagevar;
201 // ###################################################################
205 * @param string Per-page variable
207 public function setPerPageVar($perpagevar)
209 $this->perpagevar
= $perpagevar;
212 // ###################################################################
216 * @param integer Maximum number per page
218 public function setMaxPerPage($maxperpage)
220 $this->maxperpage
= $maxperpage;
223 // ###################################################################
225 * Sets defaultperpage
227 * @param integer Total number
229 public function setDefaultPerPage($defaultperpage)
231 $this->defaultperpage
= $defaultperpage;
234 // ###################################################################
236 * Takes all of the information from the set() functions and then
237 * prepares all of the data through verification
239 public function processIncomingData()
241 $input = BSApp
::GetType('Input');
244 BSApp
::Debug('ISSO/Input not loaded, so manually doing so');
245 $input = BSApp
::LoadModule('Input');
248 $this->page
= $input->inputClean($this->pagevar
, TYPE_INT
);
249 $this->perpage
= $input->inputClean($this->perpagevar
, TYPE_INT
);
250 $this->pagelinks
= $input->clean($this->pagelinks
, TYPE_INT
);
252 if ($this->page
<= 0)
257 if ($this->perpage
<= 0)
259 $this->perpage
= $this->defaultperpage
;
261 if ($this->perpage
> $this->maxperpage
)
263 $this->perpage
= $this->maxperpage
;
266 $this->perpage
= $input->clean($this->perpage
, TYPE_INT
);
269 // ###################################################################
271 * Takes the variables and splits up the pages
273 public function splitPages()
275 $this->pagecount
= ceil($this->total
/ $this->perpage
);
276 if ($this->pagelinks
== 0)
278 $this->pagelinks
= $this->pagecount
;
282 // ###################################################################
284 * Returns the lower limit of the pages
286 * @param integer Page number
288 * @return integer Lower result limit
290 public function fetchLimit($page = null)
297 $limit = $page * $this->perpage
;
304 else if ($page > $this->pagecount
)
306 $page = $this->pagecount
- 1;
307 $limit = $this->total
;
314 else if ($limit > $this->total
)
324 // ###################################################################
326 * Constructs the page navigator
328 * @param string Base link path
329 * @param bool Add a ? or a & to the path so it's link-friendly
331 * @return string Generated HTML page navigator
333 public function constructPageNav($baselink, $addParam = true)
340 if (strpos($baselink, '?') === false)
344 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&)$#', $baselink))
346 $baselink .= '&';
350 // first page number in page nav
351 $startpage = $this->page
- $this->pagelinks
;
357 // last page number in page nav
358 $endpage = $this->page +
$this->pagelinks
;
359 if ($endpage > $this->pagecount
)
361 $endpage = $this->pagecount
;
364 // prev page in page nav
365 $prevpage = $this->page
- 1;
371 // next page in page nav
372 $nextpage = $this->page +
1;
373 if ($nextpage > $this->pagecount
)
375 $nextpage = $this->pagecount
;
378 // show the prev page
379 $show['prev'] = true;
380 if ($this->page
== $startpage)
382 $show['prev'] = false;
385 // show the next page
386 $show['next'] = true;
387 if ($this->page
== $endpage)
389 $show['next'] = false;
392 // show the first page
393 $show['first'] = false;
396 $show['first'] = true;
399 // show the last page
400 $show['last'] = false;
401 if ($endpage < $this->pagecount
)
403 $show['last'] = true;
406 // construct the page bits
408 $call = $this->bitprocessor
;
409 for ($i = $startpage; $i <= $endpage; $i++
)
411 if ($i == $this->page
)
420 $bits .= $call($baselink, $nolink, $i, $this);
423 $call = $this->pagenavprocessor
;
424 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);