2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
5 || # Copyright (c)2005-2008 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)2005 - 2008, 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:';
112 public function __construct()
114 if (!BSApp
::$input instanceof BSInput
)
116 throw new Exception('BSApp::$input is not an instance of BSInput');
120 // ###################################################################
122 * Callback public function for the processing of an indivdual page. Needs
124 * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
126 * @param string Callback function
128 public function setBitProcessor($callback)
130 $this->bitprocessor
= $callback;
133 // ###################################################################
135 * Callback public function for the processing the entire page navigator. Needs
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)
139 * @param string Callback function
141 public function setNavigatorProcessor($callback)
143 $this->pagenavprocessor
= $callback;
146 // ###################################################################
148 * Returns the current page number
150 * @return integer Current page
152 public function getPage()
157 // ###################################################################
159 * Returns the current perpage value
161 * @return integer Current perpage
163 public function getPerPage()
165 return $this->perpage
;
168 // ###################################################################
172 * @param integer Total number
174 public function setTotal($total)
176 $this->total
= $total;
179 // ###################################################################
181 * Returns the number of pages to be in the navigator
183 * @param integer Number of pages
185 public function getPageCount()
187 return $this->pagecount
;
190 // ###################################################################
194 * @param integer Number of page links
196 public function setPageLinks($pagelinks)
198 $this->pagelinks
= $pagelinks;
201 // ###################################################################
205 * @param string Page variable
207 public function setPageVar($pagevar)
209 $this->pagevar
= $pagevar;
212 // ###################################################################
216 * @param string Per-page variable
218 public function setPerPageVar($perpagevar)
220 $this->perpagevar
= $perpagevar;
223 // ###################################################################
227 * @param integer Maximum number per page
229 public function setMaxPerPage($maxperpage)
231 $this->maxperpage
= $maxperpage;
234 // ###################################################################
236 * Sets defaultperpage
238 * @param integer Total number
240 public function setDefaultPerPage($defaultperpage)
242 $this->defaultperpage
= $defaultperpage;
245 // ###################################################################
247 * Takes all of the information from the set() functions and then
248 * prepares all of the data through verification
250 public function processIncomingData()
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
);
256 if ($this->page
<= 0)
261 if ($this->perpage
<= 0)
263 $this->perpage
= $this->defaultperpage
;
265 if ($this->perpage
> $this->maxperpage
)
267 $this->perpage
= $this->maxperpage
;
270 $this->perpage
= BSApp
::$input->clean($this->perpage
, TYPE_INT
);
273 // ###################################################################
275 * Takes the variables and splits up the pages
277 public function splitPages()
279 $this->pagecount
= ceil($this->total
/ $this->perpage
);
280 if ($this->pagelinks
== 0)
282 $this->pagelinks
= $this->pagecount
;
286 // ###################################################################
288 * Returns the lower limit of the pages
290 * @param integer Page number
292 * @return integer Lower result limit
294 public function fetchLimit($page = null
)
301 $limit = $page * $this->perpage
;
308 else if ($page > $this->pagecount
)
310 $page = $this->pagecount
- 1;
311 $limit = $this->total
;
318 else if ($limit > $this->total
)
328 // ###################################################################
330 * Constructs the page navigator
332 * @param string Base link path
333 * @param bool Add a ? or a & to the path so it's link-friendly
335 * @return string Generated HTML page navigator
337 public function constructPageNav($baselink, $addParam = true
)
344 if (strpos($baselink, '?') === false
)
348 else if (!preg_match('#\?$#', $baselink) && !preg_match('#(&|&)$#', $baselink))
350 $baselink .= '&';
354 // first page number in page nav
355 $startpage = $this->page
- $this->pagelinks
;
361 // last page number in page nav
362 $endpage = $this->page +
$this->pagelinks
;
363 if ($endpage > $this->pagecount
)
365 $endpage = $this->pagecount
;
368 // prev page in page nav
369 $prevpage = $this->page
- 1;
375 // next page in page nav
376 $nextpage = $this->page +
1;
377 if ($nextpage > $this->pagecount
)
379 $nextpage = $this->pagecount
;
382 // show the prev page
383 $show['prev'] = true
;
384 if ($this->page
== $startpage)
386 $show['prev'] = false
;
389 // show the next page
390 $show['next'] = true
;
391 if ($this->page
== $endpage)
393 $show['next'] = false
;
396 // show the first page
397 $show['first'] = false
;
400 $show['first'] = true
;
403 // show the last page
404 $show['last'] = false
;
405 if ($endpage < $this->pagecount
)
407 $show['last'] = true
;
410 // construct the page bits
412 $call = $this->bitprocessor
;
413 for ($i = $startpage; $i <= $endpage; $i++
)
415 if ($i == $this->page
)
424 $bits .= $call($baselink, $nolink, $i, $this);
427 $call = $this->pagenavprocessor
;
428 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);