]>
src.bluestatic.org Git - isso.git/blob - Pagination.php
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
41 abstract class BSPagination
56 * Number of page links
62 * Total number of results
68 * Total number of pages
74 * Maximum number of per-page results
77 protected $maxperpage = 100;
80 * Default number of per-page results
83 protected $defaultperpage = 20;
86 * Callback public function for the processing of an indivdual page link
88 * @param string The base link
89 * @param boolean Wether or not the item is the current page
90 * @param integer Page number
92 * @return string Compiled HTML
94 protected abstract function _bitProcessor($baselink, $isCurrent, $pagenumber);
97 * Callback public function for the processing the entire page navigator
99 * @param string The base link
100 * @param integer Next page number
101 * @param integer Previous page number
102 * @param array Show options: array('first' => (boolean)first page link, 'last' => boolean, 'prev' => boolean, 'next' => boolean)
103 * @param string Bits to process
105 * @return string Compiled HTML
107 protected abstract function _navigationProcessor($baselink, $next, $prev, $show, $bits);
110 * Set the Pagination->perpage and Pagination->page variables
112 protected abstract function _setVariables();
117 public function __construct()
119 if (!BSApp
::$input instanceof BSInput
)
121 throw new Exception('BSApp::$input is not an instance of BSInput');
126 * Returns the current page number
128 * @return integer Current page
130 public function getPage()
136 * Returns the current perpage value
138 * @return integer Current perpage
140 public function getPerPage()
142 return $this->perpage
;
148 * @param integer Total number
150 public function setTotal($total)
152 $this->total
= $total;
156 * Returns the number of pages to be in the navigator
158 * @param integer Number of pages
160 public function getPageCount()
162 return $this->pagecount
;
166 * Takes all of the information from the set() functions and then
167 * prepares all of the data through verification
169 public function processIncomingData()
171 $this->_setVariables();
172 $this->page
= BSApp
::$input->clean($this->page
, TYPE_INT
);
173 $this->perpage
= BSApp
::$input->clean($this->perpage
, TYPE_INT
);
174 $this->pagelinks
= BSApp
::$input->clean($this->pagelinks
, TYPE_INT
);
176 if ($this->page
<= 0)
181 if ($this->perpage
<= 0)
183 $this->perpage
= $this->defaultperpage
;
185 if ($this->perpage
> $this->maxperpage
)
187 $this->perpage
= $this->maxperpage
;
190 $this->perpage
= BSApp
::$input->clean($this->perpage
, TYPE_INT
);
194 * Takes the variables and splits up the pages
196 public function splitPages()
198 $this->pagecount
= ceil($this->total
/ $this->perpage
);
199 if ($this->pagelinks
== 0)
201 $this->pagelinks
= $this->pagecount
;
206 * Returns the lower limit of the pages
208 * @param integer Page number
210 * @return integer Lower result limit
212 public function fetchLimit($page = null)
219 $limit = $page * $this->perpage
;
226 else if ($page > $this->pagecount
)
228 $page = $this->pagecount
- 1;
229 $limit = $this->total
;
236 else if ($limit > $this->total
)
247 * Constructs the page navigator
249 * @param string Base link path
250 * @param bool Add a ? or a & to the path so it's link-friendly
252 * @return string Generated HTML page navigator
254 public function constructPageNav($baselink, $addParam = true)
259 if (strpos($baselink, '?') === false)
263 else if (!strpos($baselink, '#\?$#') && !strpos($baselink, '&'))
265 $baselink .= '&';
269 // first page number in page nav
270 $startpage = $this->page
- $this->pagelinks
;
276 // last page number in page nav
277 $endpage = $this->page +
$this->pagelinks
;
278 if ($endpage > $this->pagecount
)
280 $endpage = $this->pagecount
;
283 // prev page in page nav
284 $prevpage = $this->page
- 1;
290 // next page in page nav
291 $nextpage = $this->page +
1;
292 if ($nextpage > $this->pagecount
)
294 $nextpage = $this->pagecount
;
297 // show the prev page
298 $show['prev'] = true;
299 if ($this->page
== $startpage)
301 $show['prev'] = false;
304 // show the next page
305 $show['next'] = true;
306 if ($this->page
== $endpage)
308 $show['next'] = false;
311 // show the first page
312 $show['first'] = false;
315 $show['first'] = true;
318 // show the last page
319 $show['last'] = false;
320 if ($endpage < $this->pagecount
)
322 $show['last'] = true;
325 // construct the page bits
327 for ($i = $startpage; $i <= $endpage; $i++
)
329 if ($i == $this->page
)
338 $bits .= $this->_bitProcessor($baselink, $nolink, $i);
341 return $this->_navigationProcessor($baselink, $nextpage, $prevpage, $show, $bits);