]>
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 \*=====================================================================*/
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 ©2002 - [#]year[#], Blue Static
57 * Number of page links
63 * Total number of results
69 * Total number of pages
75 * Name of page variable
78 private $pagevar = 'p';
81 * Name of per-page variable
84 private $perpagevar = 'pp';
87 * Maximum number of per-page results
90 private $maxperpage = 100;
93 * Default number of per-page results
96 private $defaultperpage = 20;
99 * The processing callback public function for individual pagenav bits
102 private $bitprocessor = ':undefined:';
105 * The processing callback public function for the entire pagenav system
108 private $pagenavprocessor = ':undefined:';
110 // ###################################################################
112 * Callback public function for the processing of an indivdual page. Needs
114 * public string callback(string $baseLink, boolean $noLink, integer $pageNumber, Pagination $this)
116 * @param string Callback function
118 public function setBitProcessor($callback)
120 $this->bitprocessor
= $callback;
123 // ###################################################################
125 * Callback public function for the processing the entire page navigator. Needs
127 * 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)
129 * @param string Callback function
131 public function setNavigatorProcessor($callback)
133 $this->pagenavprocessor
= $callback;
136 // ###################################################################
138 * Returns the current page number
140 * @return integer Current page
142 public function getPage()
147 // ###################################################################
149 * Returns the current perpage value
151 * @return integer Current perpage
153 public function getPerPage()
155 return $this->perpage
;
158 // ###################################################################
162 * @param integer Total number
164 public function setTotal($total)
166 $this->total
= $total;
169 // ###################################################################
171 * Returns the number of pages to be in the navigator
173 * @param integer Number of pages
175 public function getPageCount()
177 return $this->pagecount
;
180 // ###################################################################
184 * @param integer Number of page links
186 public function setPageLinks($pagelinks)
188 $this->pagelinks
= $pagelinks;
191 // ###################################################################
195 * @param string Page variable
197 public function setPageVar($pagevar)
199 $this->pagevar
= $pagevar;
202 // ###################################################################
206 * @param string Per-page variable
208 public function setPerPageVar($perpagevar)
210 $this->perpagevar
= $perpagevar;
213 // ###################################################################
217 * @param integer Maximum number per page
219 public function setMaxPerPage($maxperpage)
221 $this->maxperpage
= $maxperpage;
224 // ###################################################################
226 * Sets defaultperpage
228 * @param integer Total number
230 public function setDefaultPerPage($defaultperpage)
232 $this->defaultperpage
= $defaultperpage;
235 // ###################################################################
237 * Takes all of the information from the set() functions and then
238 * prepares all of the data through verification
240 public function processIncomingData()
242 $input = BSRegister
::GetType('Input');
245 BSRegister
::Debug('ISSO/Input not loaded, so manually doing so');
246 $input = BSRegister
::LoadModule('Input');
249 $this->page
= $input->input_clean($this->pagevar
, TYPE_INT
);
250 $this->perpage
= $input->input_clean($this->perpagevar
, TYPE_INT
);
251 $this->pagelinks
= $input->clean($this->pagelinks
, TYPE_INT
);
253 if ($this->page
<= 0)
258 if ($this->perpage
<= 0)
260 $this->perpage
= $this->defaultperpage
;
262 if ($this->perpage
> $this->maxperpage
)
264 $this->perpage
= $this->maxperpage
;
267 $this->perpage
= $input->clean($this->perpage
, TYPE_INT
);
270 // ###################################################################
272 * Takes the variables and splits up the pages
274 public function splitPages()
276 $this->pagecount
= ceil($this->total
/ $this->perpage
);
277 if ($this->pagelinks
== 0)
279 $this->pagelinks
= $this->pagecount
;
283 // ###################################################################
285 * Returns the lower limit of the pages
287 * @param integer Page number
289 * @return integer Lower result limit
291 public function fetchLimit($page = null)
298 $limit = $page * $this->perpage
;
305 else if ($page > $this->pagecount
)
307 $page = $this->pagecount
- 1;
308 $limit = $this->total
;
315 else if ($limit > $this->total
)
325 // ###################################################################
327 * Constructs the page navigator
329 * @param string Base link path
331 * @return string Generated HTML page navigator
333 public function constructPageNav($baselink)
338 if (strpos($baselink, '?') === false)
342 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&)$#', $baselink))
344 $baselink .= '&';
347 // first page number in page nav
348 $startpage = $this->page
- $this->pagelinks
;
354 // last page number in page nav
355 $endpage = $this->page +
$this->pagelinks
;
356 if ($endpage > $this->pagecount
)
358 $endpage = $this->pagecount
;
361 // prev page in page nav
362 $prevpage = $this->page
- 1;
368 // next page in page nav
369 $nextpage = $this->page +
1;
370 if ($nextpage > $this->pagecount
)
372 $nextpage = $this->pagecount
;
375 // show the prev page
376 $show['prev'] = true;
377 if ($this->page
== $startpage)
379 $show['prev'] = false;
382 // show the next page
383 $show['next'] = true;
384 if ($this->page
== $endpage)
386 $show['next'] = false;
389 // show the first page
390 $show['first'] = false;
393 $show['first'] = true;
396 // show the last page
397 $show['last'] = false;
398 if ($endpage < $this->pagecount
)
400 $show['last'] = true;
403 // construct the page bits
405 $call = $this->bitprocessor
;
406 for ($i = $startpage; $i <= $endpage; $i++
)
408 if ($i == $this->page
)
417 $bits .= $call($baselink, $nolink, $i, $this);
420 $call = $this->pagenavprocessor
;
421 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
425 /*=====================================================================*\
426 || ###################################################################
429 || ###################################################################
430 \*=====================================================================*/