]>
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 = BSApp
::GetType('Input');
245 BSApp
::Debug('ISSO/Input not loaded, so manually doing so');
246 $input = BSApp
::LoadModule('Input');
249 $this->page
= $input->inputClean($this->pagevar
, TYPE_INT
);
250 $this->perpage
= $input->inputClean($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
330 * @param bool Add a ? or a & to the path so it's link-friendly
332 * @return string Generated HTML page navigator
334 public function constructPageNav($baselink, $addParam = true)
341 if (strpos($baselink, '?') === false)
345 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&)$#', $baselink))
347 $baselink .= '&';
351 // first page number in page nav
352 $startpage = $this->page
- $this->pagelinks
;
358 // last page number in page nav
359 $endpage = $this->page +
$this->pagelinks
;
360 if ($endpage > $this->pagecount
)
362 $endpage = $this->pagecount
;
365 // prev page in page nav
366 $prevpage = $this->page
- 1;
372 // next page in page nav
373 $nextpage = $this->page +
1;
374 if ($nextpage > $this->pagecount
)
376 $nextpage = $this->pagecount
;
379 // show the prev page
380 $show['prev'] = true;
381 if ($this->page
== $startpage)
383 $show['prev'] = false;
386 // show the next page
387 $show['next'] = true;
388 if ($this->page
== $endpage)
390 $show['next'] = false;
393 // show the first page
394 $show['first'] = false;
397 $show['first'] = true;
400 // show the last page
401 $show['last'] = false;
402 if ($endpage < $this->pagecount
)
404 $show['last'] = true;
407 // construct the page bits
409 $call = $this->bitprocessor
;
410 for ($i = $startpage; $i <= $endpage; $i++
)
412 if ($i == $this->page
)
421 $bits .= $call($baselink, $nolink, $i, $this);
424 $call = $this->pagenavprocessor
;
425 return $call($baselink, $nextpage, $prevpage, $show, $bits, $this);
429 /*=====================================================================*\
430 || ###################################################################
433 || ###################################################################
434 \*=====================================================================*/