source: trunk/lib/PageNumbers.inc.php @ 152

Last change on this file since 152 was 152, checked in by scdev, 18 years ago

Q - In the middle of working on the Prefs and Cache instantiation mode...can't decide to use singleton pattern or global vars. Updated ImageThumb? to allow filenames with path elements such as 01/23/4567_file.jpg.

File size: 13.1 KB
Line 
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
17require_once dirname(__FILE__) . '/Prefs.inc.php';
18
19class 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>&laquo;</strong>';
58        $this->left_dbl_arrow_disabled = '<span style="color: #aaaaaa;"><strong>&laquo;</strong></span>';
59        $this->right_arrow = _("next");
60        $this->right_arrow_disabled = '<span style="color: #aaaaaa;">' . _("next") . '</span>';
61        $this->right_dbl_arrow = '<strong>&raquo;</strong>';
62        $this->right_dbl_arrow_disabled = '<span style="color: #aaaaaa;"><strong>&raquo;</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        $prefs =& Prefs::getInstance($_SERVER['PHP_SELF']);
75        $prefs->setParam(array('persistent' => false));
76   
77        // (1) By provided argument, if valid.
78        // (2) By saved preference, if available.
79        // (3) Set to default value if provided and valid.
80        // (4) Keep as Class default of 25.
81        if (is_numeric($per_page) && $per_page > 0) {
82            $this->_per_page = $per_page;
83            if ($save_value) {
84                $prefs->set('items_per_page', $this->_per_page);
85            }
86        } else if ($save_value && $prefs->exists('items_per_page')) {
87            $this->_per_page = (int)$prefs->get('items_per_page');
88        } else if (is_numeric($default) && $default > 0) {
89            $this->_per_page = $default;
90        }
91        $this->set_per_page_initialized = true;
92    }
93
94    /**
95     * Set the current page number.
96     */
97    function setPageNumber($page_number, $save_value=true)
98    {
99        $prefs =& Prefs::getInstance($_SERVER['PHP_SELF']);
100        $prefs->setParam(array('persistent' => false));
101   
102        // (1) By provided argument, if valid.
103        // (2) By saved preference, if available.
104        // (3) Don't change from what was provided at class instantiation.
105        if (is_numeric($page_number)) {
106            if ($page_number < 1) {
107                // FIXME: How to go back around to the last page? Hmmm. Set to 1 for now.
108                $this->current_page = 1;
109            } else {
110                $this->current_page = $page_number;
111            }
112            if ($save_value) {
113                $prefs->set('page_number', $this->current_page);
114            }
115        } else if ($save_value && $prefs->exists('page_number')) {
116            $this->current_page = (int)$prefs->get('page_number');
117        }
118        $this->set_page_number_initialized = true;
119    }
120
121    /**
122     * Set the total number of items.
123     */
124    function setTotalItems($total_items)
125    {
126        if (is_numeric($total_items) && $total_items > 0) {
127            $this->total_items = $total_items;
128        } else {
129            $this->total_items = 0;
130        }
131        $this->set_total_items_initialized = true;
132    }
133
134    /**
135     * After $total_items or other options are set, this function calculates
136     * all the other numbers needed. If you set any variables manually,
137     * for example if $page_number comes from
138     * some place other than the GET or POST array, you should call this
139     * function manually, otherwise it happens at object instantiation.
140     *
141     * @access public
142     */
143    function calculate()
144    {
145        $app =& App::getInstance();
146
147        if (!$this->set_per_page_initialized) {
148            $app->logMsg(sprintf('set_per_page not initialized'), LOG_ERR, __FILE__, __LINE__);
149        }
150        if (!$this->set_page_number_initialized) {
151            $app->logMsg(sprintf('set_page_number not initialized'), LOG_ERR, __FILE__, __LINE__);
152        }
153        if (!$this->set_total_items_initialized) {
154            $app->logMsg(sprintf('set_total_items not initialized'), LOG_ERR, __FILE__, __LINE__);
155        }
156
157        // If the specified page exceedes total pages or is less than 1, set the page to 1.
158        if ($this->_per_page * $this->current_page >= $this->total_items + $this->_per_page || $this->_per_page * $this->current_page < 1) {
159            $this->current_page = 1;
160        }
161
162        // The first item to be shown on this page.
163        $this->first_item = ($this->current_page - 1) * $this->_per_page;
164
165        // The last item to be shown on this page.
166        if ($this->total_items < $this->current_page * $this->_per_page) {
167            $this->last_item = $this->total_items - 1;
168        } else {
169            $this->last_item = $this->current_page * $this->_per_page - 1;
170        }
171
172        // Zeroing. Just in case. Paranoia. Yeah, negative numbers perturb me.
173        if ($this->first_item < 1) {
174            $this->first_item = 0;
175        }
176        if ($this->last_item < 1) {
177            $this->last_item = 0;
178        }
179        if ($this->total_items < 1) {
180            $this->total_items = 0;
181        }
182
183        // The total number of pages.
184        $this->total_pages = ceil($this->total_items / $this->_per_page);
185
186        // Figure out how many page number links to print.
187        if ($this->total_pages >= $this->max_num_links) {
188            $this->_num_links = $this->max_num_links;
189        } else {
190            $this->_num_links = $this->total_pages;
191        }
192    }
193
194    /**
195     * Returns the SQL code to limit query to items that are on current page.
196     */
197    function getLimitSQL()
198    {
199        $app =& App::getInstance();
200        $db =& DB::getInstance();
201
202        if (is_numeric($this->first_item) && is_numeric($this->_per_page)) {
203            return ' LIMIT ' . $db->escapeString($this->first_item) . ', ' . $db->escapeString($this->_per_page) . ' ';
204        } else {
205            $app->logMsg(sprintf('Could not find SQL to LIMIT by %s %s.', $this->first_item, $this->_per_page), LOG_WARNING, __FILE__, __LINE__);
206            return '';
207        }
208    }
209
210    /**
211     * Prints links to change the number of items shown per page.
212     *
213     * @access public
214     */
215    function printPerPageLinks($query_key='per_page')
216    {
217        $app =& App::getInstance();
218
219        $sp = '';
220        for ($i=0; $i<sizeof($this->per_page_options); $i++) {
221            if ($this->_per_page != $this->per_page_options[$i]) {
222                printf('%s<a href="%s">%s</a>',
223                    $sp,
224                    $app->oHREF($_SERVER['PHP_SELF'] . '?' . $query_key . '=' . $this->per_page_options[$i]),
225                    $this->per_page_options[$i]
226                );
227            } else {
228                echo $sp . '<strong>' . $this->per_page_options[$i] . '</strong>';
229            }
230            $sp = '&nbsp;';
231        }
232    }
233
234    /**
235     * Outputs an $app->oHREF compatible url that goes to the page $page_number.
236     * Depends on $this->base_url to build the url onto. This is used in the
237     * page_number.ihtml template.
238     *
239     * @param  int   $page_number    The page number this page will go to.
240     *
241     * @return string                The URL.
242     *
243     * @access public
244     */
245    function getPageNumURL($page_number, $carry_args=null)
246    {
247        $app =& App::getInstance();
248
249        return $app->oHREF($this->url_base . $page_number, $carry_args);
250    }
251    function printPageNumURL($page_number, $carry_args=null)
252    {
253        echo $this->getPageNumURL($page_number, $carry_args);
254    }
255
256    /**
257     * Returns an array of page number links.
258     *
259     * @access public
260     */
261    function getPageNumbersArray($carry_args=null)
262    {
263        $page_numbers = array();
264
265        for ($i = 1; $i <= $this->total_pages; $i++) {
266            $page_numbers[] = array(
267                'number' => $i,
268                'url' => $this->getPageNumURL($i, $carry_args),
269                'current' => ($this->current_page == $i)
270            );
271        }
272
273        return $page_numbers;
274    }
275
276    /**
277     * Returns a string containing the page number links.
278     *
279     * @access public
280     */
281    function getPageNumbers($carry_args=null)
282    {
283        $page_numbers_string = '';
284
285        if ($this->current_page > $this->total_pages - floor($this->_num_links / 2)) {
286            $high_num = $this->total_pages;
287            $low_num = $high_num - $this->_num_links + 1;
288        } else {
289            $low_num = $this->current_page - floor($this->_num_links / 2);
290            if ($low_num < 1) {
291                $low_num = 1;
292            }
293            $high_num = $low_num + $this->_num_links - 1;
294        }
295
296        if ($this->current_page != 1) {
297            // Print "first" and "previous" page links.
298            if ($this->left_dbl_arrow) {
299                $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a>&nbsp;', $this->getPageNumURL(1, $carry_args), _("Go to the first page"), $this->left_dbl_arrow);
300            }
301            if ($this->left_arrow) {
302                $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a>&nbsp;&nbsp;', $this->getPageNumURL($this->current_page - 1, $carry_args), _("Go back one page"), $this->left_arrow);
303            }
304            // Print links to specific page numbers before the current page.
305            for ($i = $low_num; $i < $this->current_page; $i++) {
306                $page_numbers_string .= sprintf('<a href="%s">%s</a>&nbsp;', $this->getPageNumURL($i, $carry_args), $i);
307            }
308        } else {
309            if ($this->left_dbl_arrow) {
310                $page_numbers_string .= $this->left_dbl_arrow_disabled . '&nbsp;';
311            }
312            if ($this->left_arrow) {
313                $page_numbers_string .= $this->left_arrow_disabled . '&nbsp;';
314            }
315        }
316
317        if ($this->_num_links > 0) {
318            // Print the current page number.
319            $page_numbers_string .= sprintf('<strong>%s</strong>&nbsp;', $this->current_page);
320        }
321
322        if ($this->current_page < $this->total_pages) {
323            // Print links to specific page numbers after the current page.
324            for ($i = $this->current_page + 1; $i <= $high_num; $i++) {
325                $page_numbers_string .= sprintf('<a href="%s">%s</a>&nbsp;', $this->getPageNumURL($i, $carry_args), $i);
326            }
327            // Print "last" and "next" page links.
328            if ($this->right_arrow) {
329                $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a>&nbsp;&nbsp;', $this->getPageNumURL($this->current_page + 1, $carry_args), _("Go forward one page"), $this->right_arrow);
330            }
331            if ($this->right_dbl_arrow) {
332                $page_numbers_string .= sprintf('<a href="%s" title="%s">%s</a>&nbsp;&nbsp;', $this->getPageNumURL($this->total_pages, $carry_args), _("Go to the last page"), $this->right_dbl_arrow);
333            }
334        } else {
335            if ($this->right_arrow_disabled) {
336                $page_numbers_string .= $this->right_arrow_disabled . '&nbsp;';
337            }
338            if ($this->right_dbl_arrow_disabled) {
339                $page_numbers_string .= $this->right_dbl_arrow_disabled;
340            }
341        }
342
343        return $page_numbers_string;
344    }
345
346    function printPageNumbers($carry_args=null)
347    {
348        echo $this->getPageNumbers($carry_args);
349    }
350
351}
352
353?>
Note: See TracBrowser for help on using the repository browser.