Deleting old functions.php; moving Functions2.php to Functions.php
[isso.git] / pagination.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Pagination System
24 * pagination.php
25 *
26 * @package ISSO
27 */
28
29 /**
30 * Pagination System
31 *
32 * On many pages, it is necessary to limit the amount of records to display.
33 * Using this class, you can set the maximum and minimum values to display,
34 * and then the input variables for page number and perpage. This will
35 * then create a page navigator and manage the SQL LIMIT statements.
36 *
37 * Templates required:
38 * pagenav_bit - The individual page numbers in the page navigator
39 * pagenav - The entirity of the page navigtaor
40 *
41 * @author Blue Static
42 * @copyright Copyright ©2002 - [#]year[#], Blue Static
43 * @version $Revision$
44 * @package ISSO
45 *
46 */
47 class Pagination
48 {
49 /**
50 * Current page number
51 * @var integer
52 */
53 private $page;
54
55 /**
56 * Per-page value
57 * @var integer
58 */
59 private $perpage;
60
61 /**
62 * Number of page links
63 * @var integer
64 */
65 private $pagelinks;
66
67 /**
68 * Total number of results
69 * @var integer
70 */
71 private $total;
72
73 /**
74 * Total number of pages
75 * @var integer
76 */
77 private $pagecount;
78
79 /**
80 * Name of page variable
81 * @var array
82 */
83 private $pagevar;
84
85 /**
86 * Name of per-page variable
87 * @var integer
88 */
89 private $perpagevar;
90
91 /**
92 * Maximum number of per-page results
93 * @var integer
94 */
95 private $maxperpage = 100;
96
97 /**
98 * Default number of per-page results
99 * @var integer
100 */
101 private $defaultperpage = 20;
102
103 /**
104 * ISSO fields
105 * @var array
106 */
107 private $fields = array(
108 'total' => array(REQ_YES, null, false),
109 'pagelinks' => array(REQ_YES, null, false),
110 'pagevar' => array(REQ_YES, null, false),
111 'perpagevar' => array(REQ_YES, null, false),
112 'maxperpage' => array(REQ_YES, null, true),
113 'defaultperpage' => array(REQ_YES, null, true)
114 );
115
116 // ###################################################################
117 /**
118 * Constructor
119 */
120 public function __construct(&$registry)
121 {
122 $this->registry =& $registry;
123 }
124
125 // ###################################################################
126 /**
127 * Sets an ISSO field
128 *
129 * @param string Field name
130 * @param mixed Value of the field
131 */
132 public function set($name, $value)
133 {
134 $this->registry->do_set($name, $value, 'pagination');
135 }
136
137 // ###################################################################
138 /**
139 * Gets an ISSO field
140 *
141 * @param string Field name
142 *
143 * @return mixed Value of the field
144 */
145 public function get($fieldname)
146 {
147 return $this->registry->do_get($fieldname, 'pagination');
148 }
149
150 /**
151 * Takes the variables and splits up the pages
152 */
153 public function split_pages()
154 {
155 $this->page = $this->registry->input_clean($this->pagevar, TYPE_INT);
156 $this->perpage = $this->registry->input_clean($this->perpagevar, TYPE_INT);
157 $this->pagelinks = $this->registry->input_clean($this->pagelinkx, TYPE_INT);
158
159 if ($this->page <= 0)
160 {
161 $this->page = 1;
162 }
163
164 if ($this->perpage <= 0)
165 {
166 $this->perpage = $this->defaultperpage;
167 }
168 if ($this->perpage > $this->maxperpage['maxpp'])
169 {
170 $this->perpage = $this->maxperpage['maxpp'];
171 }
172
173 $this->perpage = $this->registry->clean($this->perpage, TYPE_INT);
174
175 $this->pagecount = ceil($this->total / $this->perpage);
176 }
177
178 /**
179 * Returns the lower limit of the pages
180 *
181 * @param integer Page number
182 *
183 * @return integer Lower result limit
184 */
185 public function fetch_limit($page = null)
186 {
187 if ($page === null)
188 {
189 $page = $this->page;
190 }
191
192 $limit = $page * $this->perpage;
193
194 if ($page < 1)
195 {
196 $page = 1;
197 $limit = 0;
198 }
199 else if ($page > $this->pagecount)
200 {
201 $page = $this->pagecount - 1;
202 $limit = $this->total;
203 }
204
205 if ($limit < 0)
206 {
207 return 0;
208 }
209 else if ($limit > $this->total)
210 {
211 return $this->total;
212 }
213 else
214 {
215 return $limit;
216 }
217 }
218
219 /**
220 * Constructs the page navigator
221 *
222 * @param string Base link path
223 *
224 * @return string Generated HTML page navigator
225 */
226 public function construct_page_nav($baselink)
227 {
228 global $bugsys;
229
230 // handle base link
231 if (strpos($baselink, '?') === false)
232 {
233 $baselink .= '?';
234 }
235 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
236 {
237 $baselink .= '&amp;';
238 }
239
240 // first page number in page nav
241 $startpage = $this->page - $this->pagelinks;
242 if ($startpage < 1)
243 {
244 $startpage = 1;
245 }
246
247 // last page number in page nav
248 $endpage = $this->page + $this->pagelinks;
249 if ($endpage > $this->pagecount)
250 {
251 $endpage = $this->pagecount;
252 }
253
254 // prev page in page nav
255 $prevpage = $this->page - 1;
256 if ($prevpage < 1)
257 {
258 $prevpage = 1;
259 }
260
261 // next page in page nav
262 $nextpage = $this->page + 1;
263 if ($nextpage > $this->pagecount)
264 {
265 $nextpage = $this->pagecount;
266 }
267
268 // show the prev page
269 $show['prev'] = true;
270 if ($this->page == $startpage)
271 {
272 $show['prev'] = false;
273 }
274
275 // show the next page
276 $show['next'] = true;
277 if ($this->page == $endpage)
278 {
279 $show['next'] = false;
280 }
281
282 // show the first page
283 $show['first'] = false;
284 if ($startpage > 1)
285 {
286 $show['first'] = true;
287 }
288
289 // show the last page
290 $show['last'] = false;
291 if ($endpage < $this->pagecount)
292 {
293 $show['last'] = true;
294 }
295
296 // construct the page bits
297 for ($i = $startpage; $i <= $endpage; $i++)
298 {
299 if ($i == $this->page)
300 {
301 $nolink = true;
302 }
303 else
304 {
305 $nolink = false;
306 }
307
308 eval('$pagebits[] .= "' . $this->registry->modules['template']->fetch('pagenav_bit') . '";');
309 }
310
311 $pagebits = implode(",\n", $pagebits);
312
313 eval('$pagenav = "' . $this->registry->modules['template']->fetch('pagenav') . '";');
314
315 return $pagenav;
316 }
317 }
318
319 /*=====================================================================*\
320 || ###################################################################
321 || # $HeadURL$
322 || # $Id$
323 || ###################################################################
324 \*=====================================================================*/
325 ?>