r665: Renaming product from "BugStrike" to "Bugdar"
[bugdar.git] / includes / class_pagination.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Bugdar [#]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 $this->perpage = intval($this->perpage);
86 }
87
88 /**
89 * Takes the variables and splits up the pages
90 *
91 * @access protected
92 */
93 function split_pages()
94 {
95 $this->pagecount = ceil($this->total / $this->perpage);
96 }
97
98 /**
99 * Returns the lower limit of the pages
100 *
101 * @access public
102 *
103 * @param integer Page number
104 *
105 * @return integer Lower result limit
106 */
107 function fetch_limit($page = null)
108 {
109 if ($page === null)
110 {
111 $page = $this->page;
112 }
113
114 $limit = $page * $this->perpage;
115
116 if ($page < 1)
117 {
118 $page = 1;
119 $limit = 0;
120 }
121 else if ($page > $this->pagecount)
122 {
123 $page = $this->pagecount - 1;
124 $limit = $this->total;
125 }
126
127 if ($limit < 0)
128 {
129 return 0;
130 }
131 else if ($limit > $this->total)
132 {
133 return $this->total;
134 }
135 else
136 {
137 return $limit;
138 }
139 }
140
141 /**
142 * Constructs the page navigator
143 *
144 * @access public
145 *
146 * @param string Base link path
147 *
148 * @return string Generated HTML page navigator
149 */
150 function construct_page_nav($baselink)
151 {
152 global $bugsys;
153
154 // handle base link
155 if (strpos($baselink, '?') === false)
156 {
157 $baselink .= '?';
158 }
159 else if (!preg_match('#\?$#', $baselink) AND !preg_match('#(&|&amp;)$#', $baselink))
160 {
161 $baselink .= '&amp;';
162 }
163
164 // first page number in page nav
165 $startpage = $this->page - $this->pagelinks;
166 if ($startpage < 1)
167 {
168 $startpage = 1;
169 }
170
171 // last page number in page nav
172 $endpage = $this->page + $this->pagelinks;
173 if ($endpage > $this->pagecount)
174 {
175 $endpage = $this->pagecount;
176 }
177
178 // prev page in page nav
179 $prevpage = $this->page - 1;
180 if ($prevpage < 1)
181 {
182 $prevpage = 1;
183 }
184
185 // next page in page nav
186 $nextpage = $this->page + 1;
187 if ($nextpage > $this->pagecount)
188 {
189 $nextpage = $this->pagecount;
190 }
191
192 // show the prev page
193 $show['prev'] = true;
194 if ($this->page == $startpage)
195 {
196 $show['prev'] = false;
197 }
198
199 // show the next page
200 $show['next'] = true;
201 if ($this->page == $endpage)
202 {
203 $show['next'] = false;
204 }
205
206 // show the first page
207 $show['first'] = false;
208 if ($startpage > 1)
209 {
210 $show['first'] = true;
211 }
212
213 // show the last page
214 $show['last'] = false;
215 if ($endpage < $this->pagecount)
216 {
217 $show['last'] = true;
218 }
219
220 // construct the page bits
221 for ($i = $startpage; $i <= $endpage; $i++)
222 {
223 if ($i == $this->page)
224 {
225 $nolink = true;
226 }
227 else
228 {
229 $nolink = false;
230 }
231 eval('$pagebits[] .= "' . $bugsys->template->fetch('pagenav_bit') . '";');
232 }
233
234 $pagebits = implode(",\n", $pagebits);
235
236 eval('$pagenav = "' . $bugsys->template->fetch('pagenav') . '";');
237
238 return $pagenav;
239 }
240 }
241
242 /*=====================================================================*\
243 || ###################################################################
244 || # $HeadURL$
245 || # $Id$
246 || ###################################################################
247 \*=====================================================================*/
248 ?>