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