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;
85 // ###################################################################
87 * Callback public function for the processing of an indivdual page link
89 * @param string The base link
90 * @param boolean Wether or not the item is the current page
91 * @param integer Page number
93 * @return string Compiled HTML
95 protected abstract function _bitProcessor($baselink, $isCurrent, $pagenumber);
97 // ###################################################################
99 * Callback public function for the processing the entire page navigator
101 * @param string The base link
102 * @param integer Next page number
103 * @param integer Previous page number
104 * @param array Show options: array('first' => (boolean)first page link, 'last' => boolean, 'prev' => boolean, 'next' => boolean)
105 * @param string Bits to process
107 * @return string Compiled HTML
109 protected abstract function _navigationProcessor($baselink, $next, $prev, $show, $bits);
112 * Set the Pagination->perpage and Pagination->page variables
114 protected abstract function _setVariables();
119 public function __construct()
121 if (!BSApp
::$input instanceof BSInput
)
123 throw new Exception('BSApp::$input is not an instance of BSInput');
127 // ###################################################################
129 * Returns the current page number
131 * @return integer Current page
133 public function getPage()
138 // ###################################################################
140 * Returns the current perpage value
142 * @return integer Current perpage
144 public function getPerPage()
146 return $this->perpage
;
149 // ###################################################################
153 * @param integer Total number
155 public function setTotal($total)
157 $this->total
= $total;
160 // ###################################################################
162 * Returns the number of pages to be in the navigator
164 * @param integer Number of pages
166 public function getPageCount()
168 return $this->pagecount
;
171 // ###################################################################
173 * Takes all of the information from the set() functions and then
174 * prepares all of the data through verification
176 public function processIncomingData()
178 $this->_setVariables();
179 $this->page
= BSApp
::$input->clean($this->page
, TYPE_INT
);
180 $this->perpage
= BSApp
::$input->clean($this->perpage
, TYPE_INT
);
181 $this->pagelinks
= BSApp
::$input->clean($this->pagelinks
, TYPE_INT
);
183 if ($this->page
<= 0)
188 if ($this->perpage
<= 0)
190 $this->perpage
= $this->defaultperpage
;
192 if ($this->perpage
> $this->maxperpage
)
194 $this->perpage
= $this->maxperpage
;
197 $this->perpage
= BSApp
::$input->clean($this->perpage
, TYPE_INT
);
200 // ###################################################################
202 * Takes the variables and splits up the pages
204 public function splitPages()
206 $this->pagecount
= ceil($this->total
/ $this->perpage
);
207 if ($this->pagelinks
== 0)
209 $this->pagelinks
= $this->pagecount
;
213 // ###################################################################
215 * Returns the lower limit of the pages
217 * @param integer Page number
219 * @return integer Lower result limit
221 public function fetchLimit($page = null
)
228 $limit = $page * $this->perpage
;
235 else if ($page > $this->pagecount
)
237 $page = $this->pagecount
- 1;
238 $limit = $this->total
;
245 else if ($limit > $this->total
)
255 // ###################################################################
257 * Constructs the page navigator
259 * @param string Base link path
260 * @param bool Add a ? or a & to the path so it's link-friendly
262 * @return string Generated HTML page navigator
264 public function constructPageNav($baselink, $addParam = true
)
269 if (strpos($baselink, '?') === false
)
273 else if (!strpos($baselink, '#\?$#') && !strpos($baselink, '&'))
275 $baselink .= '&';
279 // first page number in page nav
280 $startpage = $this->page
- $this->pagelinks
;
286 // last page number in page nav
287 $endpage = $this->page +
$this->pagelinks
;
288 if ($endpage > $this->pagecount
)
290 $endpage = $this->pagecount
;
293 // prev page in page nav
294 $prevpage = $this->page
- 1;
300 // next page in page nav
301 $nextpage = $this->page +
1;
302 if ($nextpage > $this->pagecount
)
304 $nextpage = $this->pagecount
;
307 // show the prev page
308 $show['prev'] = true
;
309 if ($this->page
== $startpage)
311 $show['prev'] = false
;
314 // show the next page
315 $show['next'] = true
;
316 if ($this->page
== $endpage)
318 $show['next'] = false
;
321 // show the first page
322 $show['first'] = false
;
325 $show['first'] = true
;
328 // show the last page
329 $show['last'] = false
;
330 if ($endpage < $this->pagecount
)
332 $show['last'] = true
;
335 // construct the page bits
337 for ($i = $startpage; $i <= $endpage; $i++
)
339 if ($i == $this->page
)
348 $bits .= $this->_bitProcessor($baselink, $nolink, $i);
351 return $this->_navigationProcessor($baselink, $nextpage, $prevpage, $show, $bits);