Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 8 | * |
Master Yoda | da60e9b | 2016-12-31 08:46:18 -0800 | [diff] [blame] | 9 | * Copyright (c) 2014 - 2017, British Columbia Institute of Technology |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Master Yoda | da60e9b | 2016-12-31 08:46:18 -0800 | [diff] [blame] | 32 | * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
| 41 | * Pagination Class |
| 42 | * |
| 43 | * @package CodeIgniter |
| 44 | * @subpackage Libraries |
| 45 | * @category Pagination |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 46 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 47 | * @link https://codeigniter.com/user_guide/libraries/pagination.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 48 | */ |
| 49 | class CI_Pagination { |
| 50 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 51 | /** |
| 52 | * Base URL |
| 53 | * |
| 54 | * The page that we're linking to |
| 55 | * |
| 56 | * @var string |
| 57 | */ |
| 58 | protected $base_url = ''; |
| 59 | |
| 60 | /** |
| 61 | * Prefix |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 65 | protected $prefix = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * Suffix |
| 69 | * |
| 70 | * @var string |
| 71 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 72 | protected $suffix = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * Total number of items |
| 76 | * |
| 77 | * @var int |
| 78 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 79 | protected $total_rows = 0; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 80 | |
| 81 | /** |
| 82 | * Number of links to show |
| 83 | * |
| 84 | * Relates to "digit" type links shown before/after |
| 85 | * the currently viewed page. |
| 86 | * |
| 87 | * @var int |
| 88 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 89 | protected $num_links = 2; |
| 90 | |
| 91 | /** |
| 92 | * Items per page |
| 93 | * |
| 94 | * @var int |
| 95 | */ |
| 96 | public $per_page = 10; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Current page |
| 100 | * |
| 101 | * @var int |
| 102 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 103 | public $cur_page = 0; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
| 106 | * Use page numbers flag |
| 107 | * |
| 108 | * Whether to use actual page numbers instead of an offset |
| 109 | * |
| 110 | * @var bool |
| 111 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 112 | protected $use_page_numbers = FALSE; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * First link |
| 116 | * |
| 117 | * @var string |
| 118 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 119 | protected $first_link = '‹ First'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * Next link |
| 123 | * |
| 124 | * @var string |
| 125 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 126 | protected $next_link = '>'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Previous link |
| 130 | * |
| 131 | * @var string |
| 132 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 133 | protected $prev_link = '<'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * Last link |
| 137 | * |
| 138 | * @var string |
| 139 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 140 | protected $last_link = 'Last ›'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * URI Segment |
| 144 | * |
| 145 | * @var int |
| 146 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 147 | protected $uri_segment = 0; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 148 | |
| 149 | /** |
| 150 | * Full tag open |
| 151 | * |
| 152 | * @var string |
| 153 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 154 | protected $full_tag_open = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Full tag close |
| 158 | * |
| 159 | * @var string |
| 160 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 161 | protected $full_tag_close = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 162 | |
| 163 | /** |
| 164 | * First tag open |
| 165 | * |
| 166 | * @var string |
| 167 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 168 | protected $first_tag_open = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * First tag close |
| 172 | * |
| 173 | * @var string |
| 174 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 175 | protected $first_tag_close = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 176 | |
| 177 | /** |
| 178 | * Last tag open |
| 179 | * |
| 180 | * @var string |
| 181 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 182 | protected $last_tag_open = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 183 | |
| 184 | /** |
| 185 | * Last tag close |
| 186 | * |
| 187 | * @var string |
| 188 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 189 | protected $last_tag_close = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 190 | |
| 191 | /** |
| 192 | * First URL |
| 193 | * |
| 194 | * An alternative URL for the first page |
| 195 | * |
| 196 | * @var string |
| 197 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 198 | protected $first_url = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 199 | |
| 200 | /** |
| 201 | * Current tag open |
| 202 | * |
| 203 | * @var string |
| 204 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 205 | protected $cur_tag_open = '<strong>'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Current tag close |
| 209 | * |
| 210 | * @var string |
| 211 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 212 | protected $cur_tag_close = '</strong>'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Next tag open |
| 216 | * |
| 217 | * @var string |
| 218 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 219 | protected $next_tag_open = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 220 | |
| 221 | /** |
| 222 | * Next tag close |
| 223 | * |
| 224 | * @var string |
| 225 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 226 | protected $next_tag_close = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * Previous tag open |
| 230 | * |
| 231 | * @var string |
| 232 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 233 | protected $prev_tag_open = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 234 | |
| 235 | /** |
| 236 | * Previous tag close |
| 237 | * |
| 238 | * @var string |
| 239 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 240 | protected $prev_tag_close = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 241 | |
| 242 | /** |
| 243 | * Number tag open |
| 244 | * |
| 245 | * @var string |
| 246 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 247 | protected $num_tag_open = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 248 | |
| 249 | /** |
| 250 | * Number tag close |
| 251 | * |
| 252 | * @var string |
| 253 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 254 | protected $num_tag_close = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 255 | |
| 256 | /** |
| 257 | * Page query string flag |
| 258 | * |
| 259 | * @var bool |
| 260 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 261 | protected $page_query_string = FALSE; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * Query string segment |
| 265 | * |
| 266 | * @var string |
| 267 | */ |
Alex Bilbie | f7e23b3 | 2012-09-07 09:52:32 +0100 | [diff] [blame] | 268 | protected $query_string_segment = 'per_page'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Display pages flag |
| 272 | * |
| 273 | * @var bool |
| 274 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 275 | protected $display_pages = TRUE; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 276 | |
| 277 | /** |
| 278 | * Attributes |
| 279 | * |
| 280 | * @var string |
| 281 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 282 | protected $_attributes = ''; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Link types |
| 286 | * |
| 287 | * "rel" attribute |
| 288 | * |
| 289 | * @see CI_Pagination::_attr_rel() |
| 290 | * @var array |
| 291 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 292 | protected $_link_types = array(); |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * Reuse query string flag |
| 296 | * |
| 297 | * @var bool |
| 298 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 299 | protected $reuse_query_string = FALSE; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 300 | |
| 301 | /** |
Andrey Andreev | 0da5012 | 2015-01-20 13:30:05 +0200 | [diff] [blame] | 302 | * Use global URL suffix flag |
| 303 | * |
| 304 | * @var bool |
| 305 | */ |
| 306 | protected $use_global_url_suffix = FALSE; |
| 307 | |
| 308 | /** |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 309 | * Data page attribute |
| 310 | * |
| 311 | * @var string |
| 312 | */ |
Andrey Andreev | 8382157 | 2014-08-01 12:08:56 +0300 | [diff] [blame] | 313 | protected $data_page_attr = 'data-ci-pagination-page'; |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 314 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 315 | /** |
| 316 | * CI Singleton |
| 317 | * |
| 318 | * @var object |
| 319 | */ |
| 320 | protected $CI; |
| 321 | |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 322 | // -------------------------------------------------------------------- |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | |
| 324 | /** |
| 325 | * Constructor |
| 326 | * |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 327 | * @param array $params Initialization parameters |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 328 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 329 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 330 | public function __construct($params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 331 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 332 | $this->CI =& get_instance(); |
| 333 | $this->CI->load->language('pagination'); |
Andrey Andreev | aef63e5 | 2014-02-13 14:49:55 +0200 | [diff] [blame] | 334 | foreach (array('first_link', 'next_link', 'prev_link', 'last_link') as $key) |
| 335 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 336 | if (($val = $this->CI->lang->line('pagination_'.$key)) !== FALSE) |
Andrey Andreev | aef63e5 | 2014-02-13 14:49:55 +0200 | [diff] [blame] | 337 | { |
| 338 | $this->$key = $val; |
| 339 | } |
| 340 | } |
| 341 | |
Andrey Andreev | 75db0cf | 2017-11-28 13:12:12 +0200 | [diff] [blame^] | 342 | // Enable rel attribute by default |
| 343 | isset($params['attributes']) OR $params['attributes'] = array(); |
Eric Barnes | bce4140 | 2011-12-03 23:47:41 -0500 | [diff] [blame] | 344 | $this->initialize($params); |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 345 | log_message('info', 'Pagination Class Initialized'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 346 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 347 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | // -------------------------------------------------------------------- |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 349 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | /** |
| 351 | * Initialize Preferences |
| 352 | * |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 353 | * @param array $params Initialization parameters |
Andrey Andreev | 6f6102c | 2014-02-08 19:11:40 +0200 | [diff] [blame] | 354 | * @return CI_Pagination |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | */ |
Andrey Andreev | aef63e5 | 2014-02-13 14:49:55 +0200 | [diff] [blame] | 356 | public function initialize(array $params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | { |
Andrey Andreev | 75db0cf | 2017-11-28 13:12:12 +0200 | [diff] [blame^] | 358 | if (isset($params['attributes']) && is_array($params['attributes'])) |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 359 | { |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 360 | $this->_parse_attributes($params['attributes']); |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 361 | unset($params['attributes']); |
| 362 | } |
| 363 | |
| 364 | // Deprecated legacy support for the anchor_class option |
| 365 | // Should be removed in CI 3.1+ |
| 366 | if (isset($params['anchor_class'])) |
| 367 | { |
| 368 | empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class']; |
| 369 | unset($params['anchor_class']); |
| 370 | } |
| 371 | |
Andrey Andreev | aef63e5 | 2014-02-13 14:49:55 +0200 | [diff] [blame] | 372 | foreach ($params as $key => $val) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 373 | { |
Andrey Andreev | e9c8c89 | 2014-02-17 17:29:23 +0200 | [diff] [blame] | 374 | if (property_exists($this, $key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 375 | { |
Andrey Andreev | aef63e5 | 2014-02-13 14:49:55 +0200 | [diff] [blame] | 376 | $this->$key = $val; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 377 | } |
| 378 | } |
Andrey Andreev | 6f6102c | 2014-02-08 19:11:40 +0200 | [diff] [blame] | 379 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 380 | if ($this->CI->config->item('enable_query_strings') === TRUE) |
| 381 | { |
| 382 | $this->page_query_string = TRUE; |
| 383 | } |
| 384 | |
Andrey Andreev | 0da5012 | 2015-01-20 13:30:05 +0200 | [diff] [blame] | 385 | if ($this->use_global_url_suffix === TRUE) |
| 386 | { |
| 387 | $this->suffix = $this->CI->config->item('url_suffix'); |
| 388 | } |
| 389 | |
Andrey Andreev | 6f6102c | 2014-02-08 19:11:40 +0200 | [diff] [blame] | 390 | return $this; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 391 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 392 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 393 | // -------------------------------------------------------------------- |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 394 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 395 | /** |
| 396 | * Generate the pagination links |
| 397 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 398 | * @return string |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 399 | */ |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 400 | public function create_links() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | { |
| 402 | // If our item count or per-page total is zero there is no need to continue. |
Andrey Andreev | b367f7b | 2013-10-14 12:27:00 +0300 | [diff] [blame] | 403 | // Note: DO NOT change the operator to === here! |
| 404 | if ($this->total_rows == 0 OR $this->per_page == 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 405 | { |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 406 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | // Calculate the total number of pages |
Ronald Beilsma | cfb7021 | 2011-12-29 09:57:49 +0100 | [diff] [blame] | 410 | $num_pages = (int) ceil($this->total_rows / $this->per_page); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 411 | |
| 412 | // Is there only one page? Hm... nothing more to do here then. |
Ronald Beilsma | cfb7021 | 2011-12-29 09:57:49 +0100 | [diff] [blame] | 413 | if ($num_pages === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 414 | { |
| 415 | return ''; |
| 416 | } |
| 417 | |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 418 | // Check the user defined number of links. |
Eric Barnes | 3b37659 | 2012-01-04 00:28:27 -0500 | [diff] [blame] | 419 | $this->num_links = (int) $this->num_links; |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 420 | |
Takayuki Sakai | 8bc5903 | 2014-06-13 18:38:05 +0900 | [diff] [blame] | 421 | if ($this->num_links < 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 422 | { |
Takayuki Sakai | 8bc5903 | 2014-06-13 18:38:05 +0900 | [diff] [blame] | 423 | show_error('Your number of links must be a non-negative number.'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 424 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 425 | |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 426 | // Keep any existing query string items. |
| 427 | // Note: Has nothing to do with any other query string option. |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 428 | if ($this->reuse_query_string === TRUE) |
| 429 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 430 | $get = $this->CI->input->get(); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 431 | |
Andrey Andreev | 71d8f72 | 2017-01-17 12:01:00 +0200 | [diff] [blame] | 432 | // Unset the control, method, old-school routing options |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 433 | unset($get['c'], $get['m'], $get[$this->query_string_segment]); |
| 434 | } |
Eric Roberts | ba67f3b | 2013-01-28 18:01:01 -0600 | [diff] [blame] | 435 | else |
| 436 | { |
| 437 | $get = array(); |
| 438 | } |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 439 | |
| 440 | // Put together our base and first URLs. |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 441 | // Note: DO NOT append to the properties as that would break successive calls |
| 442 | $base_url = trim($this->base_url); |
| 443 | $first_url = $this->first_url; |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 444 | |
| 445 | $query_string = ''; |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 446 | $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&'; |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 447 | |
| 448 | // Are we using query strings? |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 449 | if ($this->page_query_string === TRUE) |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 450 | { |
| 451 | // If a custom first_url hasn't been specified, we'll create one from |
| 452 | // the base_url, but without the page item. |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 453 | if ($first_url === '') |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 454 | { |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 455 | $first_url = $base_url; |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 456 | |
| 457 | // If we saved any GET items earlier, make sure they're appended. |
| 458 | if ( ! empty($get)) |
| 459 | { |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 460 | $first_url .= $query_string_sep.http_build_query($get); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | // Add the page segment to the end of the query string, where the |
| 465 | // page number will be appended. |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 466 | $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => ''))); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 467 | } |
| 468 | else |
| 469 | { |
| 470 | // Standard segment mode. |
| 471 | // Generate our saved query string to append later after the page number. |
| 472 | if ( ! empty($get)) |
| 473 | { |
Eric Roberts | 9668d1d | 2013-01-28 17:45:34 -0600 | [diff] [blame] | 474 | $query_string = $query_string_sep.http_build_query($get); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 475 | $this->suffix .= $query_string; |
| 476 | } |
| 477 | |
| 478 | // Does the base_url have the query string in it? |
| 479 | // If we're supposed to save it, remove it so we can append it later. |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 480 | if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE) |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 481 | { |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 482 | $base_url = substr($base_url, 0, $base_query_pos); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 483 | } |
| 484 | |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 485 | if ($first_url === '') |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 486 | { |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 487 | $first_url = $base_url.$query_string; |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 488 | } |
| 489 | |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 490 | $base_url = rtrim($base_url, '/').'/'; |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | // Determine the current page number. |
| 494 | $base_page = ($this->use_page_numbers) ? 1 : 0; |
| 495 | |
| 496 | // Are we using query strings? |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 497 | if ($this->page_query_string === TRUE) |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 498 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 499 | $this->cur_page = $this->CI->input->get($this->query_string_segment); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 500 | } |
jekkos | 4e87bd3 | 2016-01-18 21:14:06 +0100 | [diff] [blame] | 501 | elseif (empty($this->cur_page)) |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 502 | { |
| 503 | // Default to the last segment number if one hasn't been defined. |
| 504 | if ($this->uri_segment === 0) |
| 505 | { |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 506 | $this->uri_segment = count($this->CI->uri->segment_array()); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 507 | } |
| 508 | |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 509 | $this->cur_page = $this->CI->uri->segment($this->uri_segment); |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 510 | |
| 511 | // Remove any specified prefix/suffix from the segment. |
Eric Roberts | 032af98 | 2013-01-28 23:25:52 -0600 | [diff] [blame] | 512 | if ($this->prefix !== '' OR $this->suffix !== '') |
| 513 | { |
| 514 | $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page); |
| 515 | } |
Andrey Andreev | ca26561 | 2016-01-20 19:36:39 +0200 | [diff] [blame] | 516 | } |
jekkos | 4e87bd3 | 2016-01-18 21:14:06 +0100 | [diff] [blame] | 517 | else |
| 518 | { |
| 519 | $this->cur_page = (string) $this->cur_page; |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // If something isn't quite right, back to the default base page. |
Eric Roberts | 032af98 | 2013-01-28 23:25:52 -0600 | [diff] [blame] | 523 | if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0)) |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 524 | { |
| 525 | $this->cur_page = $base_page; |
| 526 | } |
Eric Roberts | 032af98 | 2013-01-28 23:25:52 -0600 | [diff] [blame] | 527 | else |
| 528 | { |
| 529 | // Make sure we're using integers for comparisons later. |
| 530 | $this->cur_page = (int) $this->cur_page; |
| 531 | } |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 532 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 533 | // Is the page number beyond the result range? |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 534 | // If so, we show the last page. |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 535 | if ($this->use_page_numbers) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 536 | { |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 537 | if ($this->cur_page > $num_pages) |
| 538 | { |
| 539 | $this->cur_page = $num_pages; |
| 540 | } |
| 541 | } |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 542 | elseif ($this->cur_page > $this->total_rows) |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 543 | { |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 544 | $this->cur_page = ($num_pages - 1) * $this->per_page; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 545 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 546 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 547 | $uri_page_number = $this->cur_page; |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 548 | |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 549 | // If we're using offset instead of page numbers, convert it |
| 550 | // to a page number, so we can generate the surrounding number links. |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 551 | if ( ! $this->use_page_numbers) |
| 552 | { |
Bo-Yi Wu | 5d50453 | 2012-07-14 00:03:56 +0800 | [diff] [blame] | 553 | $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1); |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 554 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 555 | |
| 556 | // Calculate the start and end numbers. These determine |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 557 | // which number to start and end the digit links with. |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 558 | $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1; |
| 559 | $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 560 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 561 | // And here we go... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 562 | $output = ''; |
Phil Sturgeon | f82b929 | 2012-06-23 15:49:23 +0100 | [diff] [blame] | 563 | |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 564 | // Render the "First" link. |
Takayuki Sakai | 8bc5903 | 2014-06-13 18:38:05 +0900 | [diff] [blame] | 565 | if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 566 | { |
Takayuki Sakai | 1240b6a | 2014-06-13 18:12:02 +0900 | [diff] [blame] | 567 | // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks. |
| 568 | $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1); |
Phil Sturgeon | a44cf57 | 2012-07-12 11:50:46 +0100 | [diff] [blame] | 569 | |
Takayuki Sakai | 1240b6a | 2014-06-13 18:12:02 +0900 | [diff] [blame] | 570 | $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>' |
Ahmad Anbar | 290f587 | 2014-02-18 23:10:38 +0200 | [diff] [blame] | 571 | .$this->first_link.'</a>'.$this->first_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 574 | // Render the "Previous" link. |
Phil Sturgeon | f82b929 | 2012-06-23 15:49:23 +0100 | [diff] [blame] | 575 | if ($this->prev_link !== FALSE && $this->cur_page !== 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 576 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 577 | $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 578 | |
Andrey Andreev | 135b64a | 2015-09-16 14:20:50 +0300 | [diff] [blame] | 579 | $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1)); |
Phil Sturgeon | a44cf57 | 2012-07-12 11:50:46 +0100 | [diff] [blame] | 580 | |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 581 | if ($i === $base_page) |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 582 | { |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 583 | // First page |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 584 | $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 585 | .$this->prev_link.'</a>'.$this->prev_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 586 | } |
| 587 | else |
| 588 | { |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 589 | $append = $this->prefix.$i.$this->suffix; |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 590 | $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 591 | .$this->prev_link.'</a>'.$this->prev_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 592 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 593 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 594 | } |
| 595 | |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 596 | // Render the pages |
| 597 | if ($this->display_pages !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 598 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 599 | // Write the digit links |
Andrey Andreev | 135b64a | 2015-09-16 14:20:50 +0300 | [diff] [blame] | 600 | for ($loop = $start - 1; $loop <= $end; $loop++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 601 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 602 | $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page; |
Phil Sturgeon | a44cf57 | 2012-07-12 11:50:46 +0100 | [diff] [blame] | 603 | |
Andrey Andreev | 135b64a | 2015-09-16 14:20:50 +0300 | [diff] [blame] | 604 | $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop); |
Phil Sturgeon | a44cf57 | 2012-07-12 11:50:46 +0100 | [diff] [blame] | 605 | |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 606 | if ($i >= $base_page) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 607 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 608 | if ($this->cur_page === $loop) |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 609 | { |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 610 | // Current page |
| 611 | $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; |
| 612 | } |
| 613 | elseif ($i === $base_page) |
| 614 | { |
| 615 | // First page |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 616 | $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>' |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 617 | .$loop.'</a>'.$this->num_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 618 | } |
| 619 | else |
| 620 | { |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 621 | $append = $this->prefix.$i.$this->suffix; |
Andrey Andreev | 135b64a | 2015-09-16 14:20:50 +0300 | [diff] [blame] | 622 | $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>' |
Eric Roberts | 9f3a590 | 2013-01-28 04:29:06 -0600 | [diff] [blame] | 623 | .$loop.'</a>'.$this->num_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 624 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | // Render the "next" link |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 630 | if ($this->next_link !== FALSE && $this->cur_page < $num_pages) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 631 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 632 | $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page; |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 633 | |
Andrey Andreev | 135b64a | 2015-09-16 14:20:50 +0300 | [diff] [blame] | 634 | $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1); |
Phil Sturgeon | a44cf57 | 2012-07-12 11:50:46 +0100 | [diff] [blame] | 635 | |
Andrey Andreev | b1616b8 | 2014-02-13 15:12:43 +0200 | [diff] [blame] | 636 | $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 637 | .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | // Render the "Last" link |
Takayuki Sakai | 8bc5903 | 2014-06-13 18:38:05 +0900 | [diff] [blame] | 641 | if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 642 | { |
Takayuki Sakai | 1240b6a | 2014-06-13 18:12:02 +0900 | [diff] [blame] | 643 | $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page; |
Phil Sturgeon | c00a5a0 | 2011-11-22 15:25:32 +0000 | [diff] [blame] | 644 | |
Andrey Andreev | 135b64a | 2015-09-16 14:20:50 +0300 | [diff] [blame] | 645 | $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages); |
Phil Sturgeon | a44cf57 | 2012-07-12 11:50:46 +0100 | [diff] [blame] | 646 | |
Takayuki Sakai | 1240b6a | 2014-06-13 18:12:02 +0900 | [diff] [blame] | 647 | $output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>' |
| 648 | .$this->last_link.'</a>'.$this->last_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Phil Sturgeon | c00a5a0 | 2011-11-22 15:25:32 +0000 | [diff] [blame] | 651 | // Kill double slashes. Note: Sometimes we can end up with a double slash |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 652 | // in the penultimate link so we'll kill all double slashes. |
Miguel González | 361750a | 2015-03-30 05:00:21 +0200 | [diff] [blame] | 653 | $output = preg_replace('#([^:"])//+#', '\\1/', $output); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 654 | |
| 655 | // Add the wrapper HTML if exists |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 656 | return $this->full_tag_open.$output.$this->full_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 657 | } |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 658 | |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 659 | // -------------------------------------------------------------------- |
| 660 | |
| 661 | /** |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 662 | * Parse attributes |
| 663 | * |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 664 | * @param array $attributes |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 665 | * @return void |
| 666 | */ |
| 667 | protected function _parse_attributes($attributes) |
| 668 | { |
| 669 | isset($attributes['rel']) OR $attributes['rel'] = TRUE; |
| 670 | $this->_link_types = ($attributes['rel']) |
Andrey Andreev | ffe8ade | 2014-02-17 18:51:48 +0200 | [diff] [blame] | 671 | ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next') |
| 672 | : array(); |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 673 | unset($attributes['rel']); |
| 674 | |
| 675 | $this->_attributes = ''; |
| 676 | foreach ($attributes as $key => $value) |
| 677 | { |
| 678 | $this->_attributes .= ' '.$key.'="'.$value.'"'; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | // -------------------------------------------------------------------- |
| 683 | |
| 684 | /** |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 685 | * Add "rel" attribute |
| 686 | * |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 687 | * @link http://www.w3.org/TR/html5/links.html#linkTypes |
Andrey Andreev | 0fa95bd | 2012-11-01 23:33:14 +0200 | [diff] [blame] | 688 | * @param string $type |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 689 | * @return string |
| 690 | */ |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 691 | protected function _attr_rel($type) |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 692 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 693 | if (isset($this->_link_types[$type])) |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 694 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 695 | unset($this->_link_types[$type]); |
| 696 | return ' rel="'.$type.'"'; |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 697 | } |
| 698 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 699 | return ''; |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 700 | } |
| 701 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 702 | } |