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