1 | <?php |
---|
2 | /** |
---|
3 | * PageNumbers.inc.php |
---|
4 | * Code by Strangecode :: www.strangecode.com :: This document contains copyrighted information |
---|
5 | * |
---|
6 | * The PageNumbers:: class provides a common abstracted interface to the |
---|
7 | * multiple pages features. It sets the various numbers needed to display items |
---|
8 | * on a page, and includes functions for working with these numbers. |
---|
9 | * You must call set setTotalItems(), setPerPage() and setPageNumber() before calling calculate() |
---|
10 | * this the other various values will be set automatically. Then you can call printPerPageLinks, |
---|
11 | * printPageNumbers, etc, and use the various page object properties in your page templates. |
---|
12 | * |
---|
13 | * @author Quinn Comendant <quinn@strangecode.com> |
---|
14 | * @version 1.61 |
---|
15 | */ |
---|
16 | |
---|
17 | require_once dirname(__FILE__) . '/Prefs.inc.php'; |
---|
18 | |
---|
19 | class PageNumbers { |
---|
20 | |
---|
21 | var $total_items; // Total quantity of items. |
---|
22 | var $total_pages; // The total number of pages. |
---|
23 | var $current_page = 1; // Current page number. |
---|
24 | var $first_item; // The counter for the first item on this page (zero index). |
---|
25 | var $last_item; // The counter for the last item on this page (zero index). |
---|
26 | var $max_num_links = 9; // The max number of links to show on page (odd numbers look best). |
---|
27 | var $_num_links; // The number of links to show on page. |
---|
28 | var $_per_page = 25; // Items per page. |
---|
29 | |
---|
30 | // Flags to ensure all necessary values have been set before calling calculate(). |
---|
31 | var $set_per_page_initialized = false; |
---|
32 | var $set_page_number_initialized = false; |
---|
33 | var $set_total_items_initialized = false; |
---|
34 | |
---|
35 | // These are initialized in the constructor. |
---|
36 | var $per_page_options; |
---|
37 | var $left_arrow; |
---|
38 | var $left_arrow_disabled; |
---|
39 | var $left_dbl_arrow; |
---|
40 | var $left_dbl_arrow_disabled; |
---|
41 | var $right_arrow; |
---|
42 | var $right_arrow_disabled; |
---|
43 | var $right_dbl_arrow; |
---|
44 | var $right_dbl_arrow_disabled; |
---|
45 | |
---|
46 | /** |
---|
47 | * PageNumbers constructor. All arguments are depreciated. Use set* functions instead. |
---|
48 | */ |
---|
49 | function PageNumbers() |
---|
50 | { |
---|
51 | // Default options for the quantity per page links. |
---|
52 | $this->per_page_options = array(25, 50, 100, 200); |
---|
53 | |
---|
54 | // Default options for the page number links. |
---|
55 | $this->left_arrow = _("back"); |
---|
56 | $this->left_arrow_disabled = '<span style="color: #aaaaaa;">' . _("back") . '</span>'; |
---|
57 | $this->left_dbl_arrow = '<strong>«</strong>'; |
---|
58 | $this->left_dbl_arrow_disabled = '<span style="color: #aaaaaa;"><strong>«</strong></span>'; |
---|
59 | $this->right_arrow = _("next"); |
---|
60 | $this->right_arrow_disabled = '<span style="color: #aaaaaa;">' . _("next") . '</span>'; |
---|
61 | $this->right_dbl_arrow = '<strong>»</strong>'; |
---|
62 | $this->right_dbl_arrow_disabled = '<span style="color: #aaaaaa;"><strong>»</strong></span>'; |
---|
63 | |
---|
64 | // Default url base. This will be set manually after instantiation |
---|
65 | // in special cases like using a /my/page/# scheme. |
---|
66 | $this->url_base = $_SERVER['PHP_SELF'] . '?page_number='; |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * Set the number of items per page. |
---|
71 | */ |
---|
72 | function setPerPage($per_page, $default=25, $save_value=true) |
---|
73 | { |
---|
74 | // (1) By provided argument, if valid. |
---|
75 | // (2) By saved preference, if available. |
---|
76 | // (3) Set to default value if provided and valid. |
---|
77 | // (4) Keep as Class default of 25. |
---|
78 | if (is_numeric($per_page) && $per_page > 0) { |
---|
79 | $this->_per_page = $per_page; |
---|
80 | if ($save_value) { |
---|
81 | Prefs::setValue('items_per_page', $this->_per_page, $_SERVER['PHP_SELF']); |
---|
82 | } |
---|
83 | } else if ($save_value && Prefs::exists('items_per_page', $_SERVER['PHP_SELF'])) { |
---|
84 | $this->_per_page = (int)Prefs::getValue('items_per_page', $_SERVER['PHP_SELF']); |
---|
85 | } else if (is_numeric($default) && $default > 0) { |
---|
86 | $this->_per_page = $default; |
---|
87 | } |
---|
88 | $this->set_per_page_initialized = true; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Set the current page number. |
---|
93 | */ |
---|
94 | function setPageNumber($page_number, $save_value=true) |
---|
95 | { |
---|
96 | // (1) By provided argument, if valid. |
---|
97 | // (2) By saved preference, if available. |
---|
98 | // (3) Don't change from what was provided at class instantiation. |
---|
99 | if (is_numeric($page_number)) { |
---|
100 | if ($page_number < 1) { |
---|
101 | // FIXME: How to go back around to the last page? Hmmm. Set to 1 for now. |
---|
102 | $this->current_page = 1; |
---|
103 | } else { |
---|
104 | $this->current_page = $page_number; |
---|
105 | } |
---|
106 | if ($save_value) { |
---|
107 | Prefs::setValue('page_number', $this->current_page, $_SERVER['PHP_SELF']); |
---|
108 | } |
---|
109 | } else if ($save_value && Prefs::exists('page_number', $_SERVER['PHP_SELF'])) { |
---|
110 | $this->current_page = (int)Prefs::getValue('page_number', $_SERVER['PHP_SELF']); |
---|
111 | } |
---|
112 | $this->set_page_number_initialized = true; |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Set the total number of items. |
---|
117 | */ |
---|
118 | function setTotalItems($total_items) |
---|
119 | { |
---|
120 | if (is_numeric($total_items) && $total_items > 0) { |
---|
121 | $this->total_items = $total_items; |
---|
122 | } else { |
---|
123 | $this->total_items = 0; |
---|
124 | } |
---|
125 | $this->set_total_items_initialized = true; |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * After $total_items or other options are set, this function calculates |
---|
130 | * all the other numbers needed. If you set any variables manually, |
---|
131 | * for example if $page_number comes from |
---|
132 | * some place other than the GET or POST array, you should call this |
---|
133 | * function manually, otherwise it happens at object instantiation. |
---|
134 | * |
---|
135 | * @access public |
---|
136 | */ |
---|
137 | function calculate() |
---|
138 | { |
---|
139 | if (!$this->set_per_page_initialized) { |
---|
140 | App::logMsg(sprintf('set_per_page not initialized'), LOG_ERR, __FILE__, __LINE__); |
---|
141 | } |
---|
142 | if (!$this->set_page_number_initialized) { |
---|
143 | App::logMsg(sprintf('set_page_number not initialized'), LOG_ERR, __FILE__, __LINE__); |
---|
144 | } |
---|
145 | if (!$this->set_total_items_initialized) { |
---|
146 | App::logMsg(sprintf('set_total_items not initialized'), LOG_ERR, __FILE__, __LINE__); |
---|
147 | } |
---|
148 | |
---|
149 | // If the specified page exceedes total pages or is less than 1, set the page to 1. |
---|
150 | if ($this->_per_page * $this->current_page >= $this->total_items + $this->_per_page || $this->_per_page * $this->current_page < 1) { |
---|
151 | $this->current_page = 1; |
---|
152 | } |
---|
153 | |
---|
154 | // The first item to be shown on this page. |
---|
155 | $this->first_item = ($this->current_page - 1) * $this->_per_page; |
---|
156 | |
---|
157 | // The last item to be shown on this page. |
---|
158 | if ($this->total_items < $this->current_page * $this->_per_page) { |
---|
159 | $this->last_item = $this->total_items - 1; |
---|
160 | } else { |
---|
161 | $this->last_item = $this->current_page * $this->_per_page - 1; |
---|
162 | } |
---|
163 | |
---|
164 | // Zeroing. Just in case. Paranoia. Yeah, negative numbers perturb me. |
---|
165 | if ($this->first_item < 1) { |
---|
166 | $this->first_item = 0; |
---|
167 | } |
---|
168 | if ($this->last_item < 1) { |
---|
169 | $this->last_item = 0; |
---|
170 | } |
---|
171 | if ($this->total_items < 1) { |
---|
172 | $this->total_items = 0; |
---|
173 | } |
---|
174 | |
---|
175 | // The total number of pages. |
---|
176 | $this->total_pages = ceil($this->total_items / $this->_per_page); |
---|
177 | |
---|
178 | // Figure out how many page number links to print. |
---|
179 | if ($this->total_pages >= $this->max_num_links) { |
---|
180 | $this->_num_links = $this->max_num_links; |
---|
181 | } else { |
---|
182 | $this->_num_links = $this->total_pages; |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * Returns the SQL code to limit query to items that are on current page. |
---|
188 | */ |
---|
189 | function getLimitSQL() |
---|
190 | { |
---|
191 | if (is_numeric($this->first_item) && is_numeric($this->_per_page)) { |
---|
192 | return ' LIMIT ' . addslashes($this->first_item) . ', ' . addslashes($this->_per_page) . ' '; |
---|
193 | } else { |
---|
194 | App::logMsg(sprintf('Could not find SQL to LIMIT by %s %s.', $this->first_item, $this->_per_page), LOG_WARNING, __FILE__, __LINE__); |
---|
195 | return ''; |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Prints links to change the number of items shown per page. |
---|
201 | * |
---|
202 | * @access public |
---|
203 | */ |
---|
204 | function printPerPageLinks($query_key='per_page') |
---|
205 | { |
---|
206 | $sp = ''; |
---|
207 | for ($i=0; $i<sizeof($this->per_page_options); $i++) { |
---|
208 | if ($this->_per_page != $this->per_page_options[$i]) { |
---|
209 | printf('%s<a href="%s">%s</a>', |
---|
210 | $sp, |
---|
211 | App::oHREF($_SERVER['PHP_SELF'] . '?' . $query_key . '=' . $this->per_page_options[$i]), |
---|
212 | $this->per_page_options[$i] |
---|
213 | ); |
---|
214 | } else { |
---|
215 | echo $sp . '<strong>' . $this->per_page_options[$i] . '</strong>'; |
---|
216 | } |
---|
217 | $sp = ' '; |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Outputs an App::oHREF compatible url that goes to the page $page_number. |
---|
223 | * Depends on $this->base_url to build the url onto. This is used in the |
---|
224 | * page_number.ihtml template. |
---|
225 | * |
---|
226 | * @param int $page_number The page number this page will go to. |
---|
227 | * |
---|
228 | * @return string The URL. |
---|
229 | * |
---|
230 | * @access public |
---|
231 | */ |
---|
232 | function getPageNumURL($page_number, $carry_args=null) |
---|
233 | { |
---|
234 | return App::oHREF($this->url_base . $page_number, $carry_args); |
---|
235 | } |
---|
236 | function printPageNumURL($page_number, $carry_args=null) |
---|
237 | { |
---|
238 | echo $this->getPageNumURL($page_number, $carry_args); |
---|
239 | } |
---|
240 | |
---|
241 | /** |
---|
242 | * Returns an array of page number links. |
---|
243 | * |
---|
244 | * @access public |
---|
245 | */ |
---|
246 | function getPageNumbersArray($carry_args=null) |
---|
247 | { |
---|
248 | $page_numbers = array(); |
---|
249 | |
---|
250 | for ($i = 1; $i <= $this->total_pages; $i++) { |
---|
251 | $page_numbers[] = array( |
---|
252 | 'number' => $i, |
---|
253 | 'url' => $this->getPageNumURL($i, $carry_args), |
---|
254 | 'current' => ($this->current_page == $i) |
---|
255 | ); |
---|
256 | } |
---|
257 | |
---|
258 | return $page_numbers; |
---|
259 | } |
---|
260 | |
---|
261 | /** |
---|
262 | * Returns a string containing the page number links. |
---|
263 | * |
---|
264 | * @access public |
---|
265 | */ |
---|
266 | function getPageNumbers($carry_args=null) |
---|
267 | { |
---|
268 | $page_numbers_string = ''; |
---|
269 | |
---|
270 | if ($this->current_page > $this->total_pages - floor($this->_num_links / 2)) { |
---|
271 | $high_num = $this->total_pages; |
---|
272 | $low_num = $high_num - $this->_num_links + 1; |
---|
273 | } else { |
---|
274 | $low_num = $this->current_page - floor($this->_num_links / 2); |
---|
275 | if ($low_num < 1) { |
---|
276 | $low_num = 1; |
---|
277 | } |
---|
278 | $high_num = $low_num + $this->_num_links - 1; |
---|
279 | } |
---|
280 | |
---|
281 | if ($this->current_page != 1) { |
---|
282 | // Print "first" and "previous" page links. |
---|
283 | if ($this->left_dbl_arrow) { |
---|
284 | $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a> ', $this->getPageNumURL(1, $carry_args), _("Go to the first page"), $this->left_dbl_arrow); |
---|
285 | } |
---|
286 | if ($this->left_arrow) { |
---|
287 | $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a> ', $this->getPageNumURL($this->current_page - 1, $carry_args), _("Go back one page"), $this->left_arrow); |
---|
288 | } |
---|
289 | // Print links to specific page numbers before the current page. |
---|
290 | for ($i = $low_num; $i < $this->current_page; $i++) { |
---|
291 | $page_numbers_string .= sprintf('<a href="%s">%s</a> ', $this->getPageNumURL($i, $carry_args), $i); |
---|
292 | } |
---|
293 | } else { |
---|
294 | if ($this->left_dbl_arrow) { |
---|
295 | $page_numbers_string .= $this->left_dbl_arrow_disabled . ' '; |
---|
296 | } |
---|
297 | if ($this->left_arrow) { |
---|
298 | $page_numbers_string .= $this->left_arrow_disabled . ' '; |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | if ($this->_num_links > 0) { |
---|
303 | // Print the current page number. |
---|
304 | $page_numbers_string .= sprintf('<strong>%s</strong> ', $this->current_page); |
---|
305 | } |
---|
306 | |
---|
307 | if ($this->current_page < $this->total_pages) { |
---|
308 | // Print links to specific page numbers after the current page. |
---|
309 | for ($i = $this->current_page + 1; $i <= $high_num; $i++) { |
---|
310 | $page_numbers_string .= sprintf('<a href="%s">%s</a> ', $this->getPageNumURL($i, $carry_args), $i); |
---|
311 | } |
---|
312 | // Print "last" and "next" page links. |
---|
313 | if ($this->right_arrow) { |
---|
314 | $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a> ', $this->getPageNumURL($this->current_page + 1, $carry_args), _("Go forward one page"), $this->right_arrow); |
---|
315 | } |
---|
316 | if ($this->right_dbl_arrow) { |
---|
317 | $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a> ', $this->getPageNumURL($this->total_pages, $carry_args), _("Go to the last page"), $this->right_dbl_arrow); |
---|
318 | } |
---|
319 | } else { |
---|
320 | if ($this->right_arrow_disabled) { |
---|
321 | $page_numbers_string .= $this->right_arrow_disabled . ' '; |
---|
322 | } |
---|
323 | if ($this->right_dbl_arrow_disabled) { |
---|
324 | $page_numbers_string .= $this->right_dbl_arrow_disabled; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | return $page_numbers_string; |
---|
329 | } |
---|
330 | |
---|
331 | function printPageNumbers($carry_args=null) |
---|
332 | { |
---|
333 | echo $this->getPageNumbers($carry_args); |
---|
334 | } |
---|
335 | |
---|
336 | } |
---|
337 | |
---|
338 | ?> |
---|