blob: ece4edac94d90d2ce805a9c6eb99944078e6269c [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Eric Barnes9a4902a2011-12-03 23:46:06 -05008 *
Master Yodada60e9b2016-12-31 08:46:18 -08009 * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
Eric Barnes9a4902a2011-12-03 23:46:06 -050010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * 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 Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * 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 Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Master Yodada60e9b2016-12-31 08:46:18 -080032 * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Pagination Class
42 *
43 * @package CodeIgniter
44 * @subpackage Libraries
45 * @category Pagination
Derek Jonesf4a4bd82011-10-20 12:18:42 -050046 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020047 * @link https://codeigniter.com/user_guide/libraries/pagination.html
Derek Allard2067d1a2008-11-13 22:59:24 +000048 */
49class CI_Pagination {
50
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020051 /**
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 Andreev83821572014-08-01 12:08:56 +030065 protected $prefix = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020066
67 /**
68 * Suffix
69 *
70 * @var string
71 */
Andrey Andreev83821572014-08-01 12:08:56 +030072 protected $suffix = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020073
74 /**
75 * Total number of items
76 *
77 * @var int
78 */
Andrey Andreev83821572014-08-01 12:08:56 +030079 protected $total_rows = 0;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020080
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 Andreev83821572014-08-01 12:08:56 +030089 protected $num_links = 2;
90
91 /**
92 * Items per page
93 *
94 * @var int
95 */
96 public $per_page = 10;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020097
98 /**
99 * Current page
100 *
101 * @var int
102 */
Andrey Andreev83821572014-08-01 12:08:56 +0300103 public $cur_page = 0;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200104
105 /**
106 * Use page numbers flag
107 *
108 * Whether to use actual page numbers instead of an offset
109 *
110 * @var bool
111 */
Andrey Andreev83821572014-08-01 12:08:56 +0300112 protected $use_page_numbers = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200113
114 /**
115 * First link
116 *
117 * @var string
118 */
Andrey Andreev83821572014-08-01 12:08:56 +0300119 protected $first_link = '&lsaquo; First';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200120
121 /**
122 * Next link
123 *
124 * @var string
125 */
Andrey Andreev83821572014-08-01 12:08:56 +0300126 protected $next_link = '&gt;';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200127
128 /**
129 * Previous link
130 *
131 * @var string
132 */
Andrey Andreev83821572014-08-01 12:08:56 +0300133 protected $prev_link = '&lt;';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200134
135 /**
136 * Last link
137 *
138 * @var string
139 */
Andrey Andreev83821572014-08-01 12:08:56 +0300140 protected $last_link = 'Last &rsaquo;';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200141
142 /**
143 * URI Segment
144 *
145 * @var int
146 */
Andrey Andreev83821572014-08-01 12:08:56 +0300147 protected $uri_segment = 0;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200148
149 /**
150 * Full tag open
151 *
152 * @var string
153 */
Andrey Andreev83821572014-08-01 12:08:56 +0300154 protected $full_tag_open = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200155
156 /**
157 * Full tag close
158 *
159 * @var string
160 */
Andrey Andreev83821572014-08-01 12:08:56 +0300161 protected $full_tag_close = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200162
163 /**
164 * First tag open
165 *
166 * @var string
167 */
Andrey Andreev83821572014-08-01 12:08:56 +0300168 protected $first_tag_open = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200169
170 /**
171 * First tag close
172 *
173 * @var string
174 */
Andrey Andreev83821572014-08-01 12:08:56 +0300175 protected $first_tag_close = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200176
177 /**
178 * Last tag open
179 *
180 * @var string
181 */
Andrey Andreev83821572014-08-01 12:08:56 +0300182 protected $last_tag_open = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200183
184 /**
185 * Last tag close
186 *
187 * @var string
188 */
Andrey Andreev83821572014-08-01 12:08:56 +0300189 protected $last_tag_close = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200190
191 /**
192 * First URL
193 *
194 * An alternative URL for the first page
195 *
196 * @var string
197 */
Andrey Andreev83821572014-08-01 12:08:56 +0300198 protected $first_url = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200199
200 /**
201 * Current tag open
202 *
203 * @var string
204 */
Andrey Andreev83821572014-08-01 12:08:56 +0300205 protected $cur_tag_open = '<strong>';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200206
207 /**
208 * Current tag close
209 *
210 * @var string
211 */
Andrey Andreev83821572014-08-01 12:08:56 +0300212 protected $cur_tag_close = '</strong>';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200213
214 /**
215 * Next tag open
216 *
217 * @var string
218 */
Andrey Andreev83821572014-08-01 12:08:56 +0300219 protected $next_tag_open = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200220
221 /**
222 * Next tag close
223 *
224 * @var string
225 */
Andrey Andreev83821572014-08-01 12:08:56 +0300226 protected $next_tag_close = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200227
228 /**
229 * Previous tag open
230 *
231 * @var string
232 */
Andrey Andreev83821572014-08-01 12:08:56 +0300233 protected $prev_tag_open = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200234
235 /**
236 * Previous tag close
237 *
238 * @var string
239 */
Andrey Andreev83821572014-08-01 12:08:56 +0300240 protected $prev_tag_close = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200241
242 /**
243 * Number tag open
244 *
245 * @var string
246 */
Andrey Andreev83821572014-08-01 12:08:56 +0300247 protected $num_tag_open = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200248
249 /**
250 * Number tag close
251 *
252 * @var string
253 */
Andrey Andreev83821572014-08-01 12:08:56 +0300254 protected $num_tag_close = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200255
256 /**
257 * Page query string flag
258 *
259 * @var bool
260 */
Andrey Andreev83821572014-08-01 12:08:56 +0300261 protected $page_query_string = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200262
263 /**
264 * Query string segment
265 *
266 * @var string
267 */
Alex Bilbief7e23b32012-09-07 09:52:32 +0100268 protected $query_string_segment = 'per_page';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200269
270 /**
271 * Display pages flag
272 *
273 * @var bool
274 */
Andrey Andreev83821572014-08-01 12:08:56 +0300275 protected $display_pages = TRUE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200276
277 /**
278 * Attributes
279 *
280 * @var string
281 */
Andrey Andreev83821572014-08-01 12:08:56 +0300282 protected $_attributes = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200283
284 /**
285 * Link types
286 *
287 * "rel" attribute
288 *
289 * @see CI_Pagination::_attr_rel()
290 * @var array
291 */
Andrey Andreev83821572014-08-01 12:08:56 +0300292 protected $_link_types = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200293
294 /**
295 * Reuse query string flag
296 *
297 * @var bool
298 */
Andrey Andreev83821572014-08-01 12:08:56 +0300299 protected $reuse_query_string = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200300
301 /**
Andrey Andreev0da50122015-01-20 13:30:05 +0200302 * Use global URL suffix flag
303 *
304 * @var bool
305 */
306 protected $use_global_url_suffix = FALSE;
307
308 /**
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200309 * Data page attribute
310 *
311 * @var string
312 */
Andrey Andreev83821572014-08-01 12:08:56 +0300313 protected $data_page_attr = 'data-ci-pagination-page';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200314
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200315 /**
316 * CI Singleton
317 *
318 * @var object
319 */
320 protected $CI;
321
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200322 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000323
324 /**
325 * Constructor
326 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200327 * @param array $params Initialization parameters
Andrey Andreev56454792012-05-17 14:32:19 +0300328 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 */
Greg Akera9263282010-11-10 15:26:43 -0600330 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200332 $this->CI =& get_instance();
333 $this->CI->load->language('pagination');
Andrey Andreevaef63e52014-02-13 14:49:55 +0200334 foreach (array('first_link', 'next_link', 'prev_link', 'last_link') as $key)
335 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200336 if (($val = $this->CI->lang->line('pagination_'.$key)) !== FALSE)
Andrey Andreevaef63e52014-02-13 14:49:55 +0200337 {
338 $this->$key = $val;
339 }
340 }
341
Andrey Andreev75db0cf2017-11-28 13:12:12 +0200342 // Enable rel attribute by default
343 isset($params['attributes']) OR $params['attributes'] = array();
Eric Barnesbce41402011-12-03 23:47:41 -0500344 $this->initialize($params);
Andrey Andreev90726b82015-01-20 12:39:22 +0200345 log_message('info', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 /**
351 * Initialize Preferences
352 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200353 * @param array $params Initialization parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200354 * @return CI_Pagination
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 */
Andrey Andreevaef63e52014-02-13 14:49:55 +0200356 public function initialize(array $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 {
Andrey Andreev75db0cf2017-11-28 13:12:12 +0200358 if (isset($params['attributes']) && is_array($params['attributes']))
Andrey Andreev88c47272012-06-17 02:32:31 +0300359 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600360 $this->_parse_attributes($params['attributes']);
Andrey Andreev88c47272012-06-17 02:32:31 +0300361 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 Andreevaef63e52014-02-13 14:49:55 +0200372 foreach ($params as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 {
Andrey Andreeve9c8c892014-02-17 17:29:23 +0200374 if (property_exists($this, $key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200376 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
378 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200379
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200380 if ($this->CI->config->item('enable_query_strings') === TRUE)
381 {
382 $this->page_query_string = TRUE;
383 }
384
Andrey Andreev0da50122015-01-20 13:30:05 +0200385 if ($this->use_global_url_suffix === TRUE)
386 {
387 $this->suffix = $this->CI->config->item('url_suffix');
388 }
389
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200390 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000392
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 /**
396 * Generate the pagination links
397 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000399 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500400 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 // If our item count or per-page total is zero there is no need to continue.
Andrey Andreevb367f7b2013-10-14 12:27:00 +0300403 // Note: DO NOT change the operator to === here!
404 if ($this->total_rows == 0 OR $this->per_page == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000406 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
408
409 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100410 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000411
412 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100413 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 {
415 return '';
416 }
417
Eric Roberts9f3a5902013-01-28 04:29:06 -0600418 // Check the user defined number of links.
Eric Barnes3b376592012-01-04 00:28:27 -0500419 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000420
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900421 if ($this->num_links < 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 {
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900423 show_error('Your number of links must be a non-negative number.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000425
Eric Roberts9f3a5902013-01-28 04:29:06 -0600426 // Keep any existing query string items.
427 // Note: Has nothing to do with any other query string option.
Eric Roberts9f3a5902013-01-28 04:29:06 -0600428 if ($this->reuse_query_string === TRUE)
429 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200430 $get = $this->CI->input->get();
Eric Roberts9f3a5902013-01-28 04:29:06 -0600431
Andrey Andreev71d8f722017-01-17 12:01:00 +0200432 // Unset the control, method, old-school routing options
Eric Roberts9f3a5902013-01-28 04:29:06 -0600433 unset($get['c'], $get['m'], $get[$this->query_string_segment]);
434 }
Eric Robertsba67f3b2013-01-28 18:01:01 -0600435 else
436 {
437 $get = array();
438 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600439
440 // Put together our base and first URLs.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200441 // 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 Roberts9f3a5902013-01-28 04:29:06 -0600444
445 $query_string = '';
Andrey Andreevb1616b82014-02-13 15:12:43 +0200446 $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600447
448 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200449 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600450 {
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 Andreevb1616b82014-02-13 15:12:43 +0200453 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600454 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200455 $first_url = $base_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600456
457 // If we saved any GET items earlier, make sure they're appended.
458 if ( ! empty($get))
459 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200460 $first_url .= $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600461 }
462 }
463
464 // Add the page segment to the end of the query string, where the
465 // page number will be appended.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200466 $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
Eric Roberts9f3a5902013-01-28 04:29:06 -0600467 }
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 Roberts9668d1d2013-01-28 17:45:34 -0600474 $query_string = $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600475 $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 Andreevb1616b82014-02-13 15:12:43 +0200480 if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600481 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200482 $base_url = substr($base_url, 0, $base_query_pos);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600483 }
484
Andrey Andreevb1616b82014-02-13 15:12:43 +0200485 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600486 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200487 $first_url = $base_url.$query_string;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600488 }
489
Andrey Andreevb1616b82014-02-13 15:12:43 +0200490 $base_url = rtrim($base_url, '/').'/';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600491 }
492
493 // Determine the current page number.
494 $base_page = ($this->use_page_numbers) ? 1 : 0;
495
496 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200497 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600498 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200499 $this->cur_page = $this->CI->input->get($this->query_string_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600500 }
jekkos4e87bd32016-01-18 21:14:06 +0100501 elseif (empty($this->cur_page))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600502 {
503 // Default to the last segment number if one hasn't been defined.
504 if ($this->uri_segment === 0)
505 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200506 $this->uri_segment = count($this->CI->uri->segment_array());
Eric Roberts9f3a5902013-01-28 04:29:06 -0600507 }
508
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200509 $this->cur_page = $this->CI->uri->segment($this->uri_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600510
511 // Remove any specified prefix/suffix from the segment.
Eric Roberts032af982013-01-28 23:25:52 -0600512 if ($this->prefix !== '' OR $this->suffix !== '')
513 {
514 $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
515 }
Andrey Andreevca265612016-01-20 19:36:39 +0200516 }
jekkos4e87bd32016-01-18 21:14:06 +0100517 else
518 {
519 $this->cur_page = (string) $this->cur_page;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600520 }
521
522 // If something isn't quite right, back to the default base page.
Eric Roberts032af982013-01-28 23:25:52 -0600523 if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600524 {
525 $this->cur_page = $base_page;
526 }
Eric Roberts032af982013-01-28 23:25:52 -0600527 else
528 {
529 // Make sure we're using integers for comparisons later.
530 $this->cur_page = (int) $this->cur_page;
531 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 // Is the page number beyond the result range?
Eric Roberts9f3a5902013-01-28 04:29:06 -0600534 // If so, we show the last page.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400535 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400537 if ($this->cur_page > $num_pages)
538 {
539 $this->cur_page = $num_pages;
540 }
541 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300542 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400543 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300544 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000545 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000546
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500548
Eric Roberts9f3a5902013-01-28 04:29:06 -0600549 // 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 Kuzemchak11c5f162011-09-03 20:59:07 -0400551 if ( ! $this->use_page_numbers)
552 {
Bo-Yi Wu5d504532012-07-14 00:03:56 +0800553 $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400554 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000555
556 // Calculate the start and end numbers. These determine
Eric Roberts9f3a5902013-01-28 04:29:06 -0600557 // which number to start and end the digit links with.
Andrey Andreev796bc952012-03-26 19:40:40 +0300558 $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 Allard2067d1a2008-11-13 22:59:24 +0000560
Barry Mienydd671972010-10-04 16:33:58 +0200561 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 $output = '';
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100563
Eric Roberts9f3a5902013-01-28 04:29:06 -0600564 // Render the "First" link.
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900565 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links))
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900567 // 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 Sturgeona44cf572012-07-12 11:50:46 +0100569
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900570 $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Ahmad Anbar290f5872014-02-18 23:10:38 +0200571 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
573
Eric Roberts9f3a5902013-01-28 04:29:06 -0600574 // Render the "Previous" link.
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100575 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400577 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200578
Andrey Andreev135b64a2015-09-16 14:20:50 +0300579 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
Phil Sturgeona44cf572012-07-12 11:50:46 +0100580
Eric Roberts9f3a5902013-01-28 04:29:06 -0600581 if ($i === $base_page)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400582 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600583 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200584 $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300585 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400586 }
587 else
588 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600589 $append = $this->prefix.$i.$this->suffix;
Andrey Andreevb1616b82014-02-13 15:12:43 +0200590 $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300591 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 }
595
Derek Allarde01fd0f2010-07-05 11:06:07 -0400596 // Render the pages
597 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400599 // Write the digit links
Andrey Andreev135b64a2015-09-16 14:20:50 +0300600 for ($loop = $start - 1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400602 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100603
Andrey Andreev135b64a2015-09-16 14:20:50 +0300604 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100605
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400606 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100608 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400609 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600610 // Current page
611 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
612 }
613 elseif ($i === $base_page)
614 {
615 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200616 $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600617 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400618 }
619 else
620 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600621 $append = $this->prefix.$i.$this->suffix;
Andrey Andreev135b64a2015-09-16 14:20:50 +0300622 $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600623 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400624 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 }
626 }
627 }
628
629 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300630 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400632 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400633
Andrey Andreev135b64a2015-09-16 14:20:50 +0300634 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100635
Andrey Andreevb1616b82014-02-13 15:12:43 +0200636 $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300637 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000638 }
639
640 // Render the "Last" link
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900641 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000642 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900643 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000644
Andrey Andreev135b64a2015-09-16 14:20:50 +0300645 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100646
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900647 $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 Allard2067d1a2008-11-13 22:59:24 +0000649 }
650
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000651 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000652 // in the penultimate link so we'll kill all double slashes.
Miguel González361750a2015-03-30 05:00:21 +0200653 $output = preg_replace('#([^:"])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000654
655 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300656 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000657 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300658
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300659 // --------------------------------------------------------------------
660
661 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300662 * Parse attributes
663 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200664 * @param array $attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300665 * @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 Andreevffe8ade2014-02-17 18:51:48 +0200671 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
672 : array();
Andrey Andreev88c47272012-06-17 02:32:31 +0300673 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 Andreev5a1e5e32012-06-12 11:28:26 +0300685 * Add "rel" attribute
686 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300687 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200688 * @param string $type
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300689 * @return string
690 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300691 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300692 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300693 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300694 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300695 unset($this->_link_types[$type]);
696 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300697 }
698
Andrey Andreev88c47272012-06-17 02:32:31 +0300699 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300700 }
701
Derek Allard2067d1a2008-11-13 22:59:24 +0000702}