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