r357: Completed pagination system
[bugdar.git] / includes / class_pagination.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # BugStrike [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # Copyright ©2002-[#]year[#] by Iris Studios, Inc. All Rights Reserved. # ||
7 || # This file may not be reproduced in any way without permission. # ||
8 || # --------------------------------------------------------------- # ||
9 || # User License Agreement at http://www.iris-studios.com/license/ # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 /**
14 * Class that is responsible for paginating data. Takes passed
15 * parameters and creates pagination links.
16 */
17 class Pagination
18 {
19 /**
20 * Current page number
21 * @var integer
22 */
23 var $page;
24
25 /**
26 * Per-page value
27 * @var integer
28 */
29 var $perpage;
30
31 /**
32 * Number of page links
33 * @var integer
34 */
35 var $pagelinks;
36
37 /**
38 * Total number of results
39 * @var integer
40 */
41 var $total;
42
43 /**
44 * Total number of pages
45 * @var integer
46 */
47 var $pagecount;
48
49 /**
50 * Variable names
51 * @var array
52 */
53 var $vars;
54
55 /**
56 * Constructor: sanitize incoming variables
57 *
58 * @param string Name of page number variable
59 * @param string Name of per-page variable
60 */
61 function Pagination($page, $perpage)
62 {
63 global $bugsys;
64
65 $this->page = intval($bugsys->in["$page"]);
66 $this->perpage = intval($bugsys->in["$perpage"]);
67 $this->pagelinks = intval($bugsys->options['pagelinks']);
68
69 $this->vars = array('p' => $page, 'pp' => $perpage);
70
71 if ($this->page <= 0)
72 {
73 $this->page = 1;
74 }
75
76 if ($this->perpage <= 0)
77 {
78 $this->perpage = $bugsys->options['defaultpp'];
79 }
80 if ($this->perpage > $bugsys->options['maxpp'])
81 {
82 $this->perpage = $bugsys->options['maxpp'];
83 }
84 }
85
86 /**
87 * Takes the variables and splits up the pages
88 *
89 * @access protected
90 */
91 function split_pages()
92 {
93 $this->pagecount = ceil($this->total / $this->perpage);
94 }
95
96 /**
97 * Returns the lower limit of the pages
98 *
99 * @access public
100 *
101 * @param integer Page number
102 *
103 * @return integer Lower result limit
104 */
105 function fetch_limit($page = null)
106 {
107 if ($page === null)
108 {
109 $page = $this->page;
110 }
111
112 if ($page < 1)
113 {
114 $page = 1;
115 }
116 else if ($page > $this->pagecount)
117 {
118 $page = $this->pagecount - 1;
119 }
120
121 $limit = $page * $this->perpage;
122
123 if ($limit < 0)
124 {
125 return 0;
126 }
127 else if ($limit > $this->total)
128 {
129 return $this->total;
130 }
131 else
132 {
133 return $limit;
134 }
135 }
136
137 /**
138 * Constructs the page navigator
139 *
140 * @access public
141 *
142 * @param string Base link path
143 *
144 * @return string Generated HTML page navigator
145 */
146 function construct_page_nav($baselink)
147 {
148 global $bugsys;
149
150 // handle base link
151 if (strpos($baselink, '?') === false)
152 {
153 $baselink .= '?';
154 }
155 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
156 {
157 $baselink .= '&amp;';
158 }
159
160 // first page number in page nav
161 $startpage = $this->page - $this->pagelinks;
162 if ($startpage < 1)
163 {
164 $startpage = 1;
165 }
166
167 // last page number in page nav
168 $endpage = $this->page + $this->pagelinks;
169 if ($endpage > $this->pagecount)
170 {
171 $endpage = $this->pagecount;
172 }
173
174 // prev page in page nav
175 $prevpage = $this->page - 1;
176 if ($prevpage < 1)
177 {
178 $prevpage = 1;
179 }
180
181 // next page in page nav
182 $nextpage = $this->page + 1;
183 if ($nextpage > $this->pagecount)
184 {
185 $nextpage = $this->pagecount;
186 }
187
188 // show the prev page
189 $show['prev'] = true;
190 if ($this->page == $startpage)
191 {
192 $show['prev'] = false;
193 }
194
195 // show the next page
196 $show['next'] = true;
197 if ($this->page == $endpage)
198 {
199 $show['next'] = false;
200 }
201
202 // show the first page
203 $show['first'] = false;
204 if ($startpage > 1)
205 {
206 $show['first'] = true;
207 }
208
209 // show the last page
210 $show['last'] = false;
211 if ($endpage < $this->pagecount)
212 {
213 $show['last'] = true;
214 }
215
216 // construct the page bits
217 for ($i = $startpage; $i <= $endpage; $i++)
218 {
219 if ($i == $this->page)
220 {
221 $nolink = true;
222 }
223 else
224 {
225 $nolink = false;
226 }
227 eval('$pagebits[] .= "' . $bugsys->template->fetch('pagenav_bit') . '";');
228 }
229
230 $pagebits = implode(",\n", $pagebits);
231
232 eval('$pagenav = "' . $bugsys->template->fetch('pagenav') . '";');
233
234 return $pagenav;
235 }
236 }
237
238 /*=====================================================================*\
239 || ###################################################################
240 || # $HeadURL$
241 || # $Id$
242 || ###################################################################
243 \*=====================================================================*/
244 ?>