blob: 44f848fe035e8ae56ee0171e46e85fab731109ff [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 *
Andrey Andreev125ef472016-01-11 12:33:00 +02009 * Copyright (c) 2014 - 2016, 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/)
Andrey Andreev125ef472016-01-11 12:33:00 +020032 * @copyright Copyright (c) 2014 - 2016, 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
Eric Barnesbce41402011-12-03 23:47:41 -0500342 $this->initialize($params);
Andrey Andreev90726b82015-01-20 12:39:22 +0200343 log_message('info', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 /**
349 * Initialize Preferences
350 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200351 * @param array $params Initialization parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200352 * @return CI_Pagination
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 */
Andrey Andreevaef63e52014-02-13 14:49:55 +0200354 public function initialize(array $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
Andrey Andreevd91ed262015-07-26 23:12:16 +0300356 isset($params['attributes']) OR $params['attributes'] = array();
357 if (is_array($params['attributes']))
Andrey Andreev88c47272012-06-17 02:32:31 +0300358 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600359 $this->_parse_attributes($params['attributes']);
Andrey Andreev88c47272012-06-17 02:32:31 +0300360 unset($params['attributes']);
361 }
362
363 // Deprecated legacy support for the anchor_class option
364 // Should be removed in CI 3.1+
365 if (isset($params['anchor_class']))
366 {
367 empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
368 unset($params['anchor_class']);
369 }
370
Andrey Andreevaef63e52014-02-13 14:49:55 +0200371 foreach ($params as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
Andrey Andreeve9c8c892014-02-17 17:29:23 +0200373 if (property_exists($this, $key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200375 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200378
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200379 if ($this->CI->config->item('enable_query_strings') === TRUE)
380 {
381 $this->page_query_string = TRUE;
382 }
383
Andrey Andreev0da50122015-01-20 13:30:05 +0200384 if ($this->use_global_url_suffix === TRUE)
385 {
386 $this->suffix = $this->CI->config->item('url_suffix');
387 }
388
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200389 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000393
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 /**
395 * Generate the pagination links
396 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000398 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500399 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 // If our item count or per-page total is zero there is no need to continue.
Andrey Andreevb367f7b2013-10-14 12:27:00 +0300402 // Note: DO NOT change the operator to === here!
403 if ($this->total_rows == 0 OR $this->per_page == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000405 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 }
407
408 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100409 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000410
411 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100412 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 return '';
415 }
416
Eric Roberts9f3a5902013-01-28 04:29:06 -0600417 // Check the user defined number of links.
Eric Barnes3b376592012-01-04 00:28:27 -0500418 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000419
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900420 if ($this->num_links < 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900422 show_error('Your number of links must be a non-negative number.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000424
Eric Roberts9f3a5902013-01-28 04:29:06 -0600425 // Keep any existing query string items.
426 // Note: Has nothing to do with any other query string option.
Eric Roberts9f3a5902013-01-28 04:29:06 -0600427 if ($this->reuse_query_string === TRUE)
428 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200429 $get = $this->CI->input->get();
Eric Roberts9f3a5902013-01-28 04:29:06 -0600430
431 // Unset the controll, method, old-school routing options
432 unset($get['c'], $get['m'], $get[$this->query_string_segment]);
433 }
Eric Robertsba67f3b2013-01-28 18:01:01 -0600434 else
435 {
436 $get = array();
437 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600438
439 // Put together our base and first URLs.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200440 // Note: DO NOT append to the properties as that would break successive calls
441 $base_url = trim($this->base_url);
442 $first_url = $this->first_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600443
444 $query_string = '';
Andrey Andreevb1616b82014-02-13 15:12:43 +0200445 $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600446
447 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200448 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600449 {
450 // If a custom first_url hasn't been specified, we'll create one from
451 // the base_url, but without the page item.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200452 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600453 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200454 $first_url = $base_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600455
456 // If we saved any GET items earlier, make sure they're appended.
457 if ( ! empty($get))
458 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200459 $first_url .= $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600460 }
461 }
462
463 // Add the page segment to the end of the query string, where the
464 // page number will be appended.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200465 $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
Eric Roberts9f3a5902013-01-28 04:29:06 -0600466 }
467 else
468 {
469 // Standard segment mode.
470 // Generate our saved query string to append later after the page number.
471 if ( ! empty($get))
472 {
Eric Roberts9668d1d2013-01-28 17:45:34 -0600473 $query_string = $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600474 $this->suffix .= $query_string;
475 }
476
477 // Does the base_url have the query string in it?
478 // If we're supposed to save it, remove it so we can append it later.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200479 if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600480 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200481 $base_url = substr($base_url, 0, $base_query_pos);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600482 }
483
Andrey Andreevb1616b82014-02-13 15:12:43 +0200484 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600485 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200486 $first_url = $base_url.$query_string;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600487 }
488
Andrey Andreevb1616b82014-02-13 15:12:43 +0200489 $base_url = rtrim($base_url, '/').'/';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600490 }
491
492 // Determine the current page number.
493 $base_page = ($this->use_page_numbers) ? 1 : 0;
494
495 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200496 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600497 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200498 $this->cur_page = $this->CI->input->get($this->query_string_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600499 }
jekkos4e87bd32016-01-18 21:14:06 +0100500 elseif (empty($this->cur_page))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600501 {
502 // Default to the last segment number if one hasn't been defined.
503 if ($this->uri_segment === 0)
504 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200505 $this->uri_segment = count($this->CI->uri->segment_array());
Eric Roberts9f3a5902013-01-28 04:29:06 -0600506 }
507
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200508 $this->cur_page = $this->CI->uri->segment($this->uri_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600509
510 // Remove any specified prefix/suffix from the segment.
Eric Roberts032af982013-01-28 23:25:52 -0600511 if ($this->prefix !== '' OR $this->suffix !== '')
512 {
513 $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
514 }
Andrey Andreevca265612016-01-20 19:36:39 +0200515 }
jekkos4e87bd32016-01-18 21:14:06 +0100516 else
517 {
518 $this->cur_page = (string) $this->cur_page;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600519 }
520
521 // If something isn't quite right, back to the default base page.
Eric Roberts032af982013-01-28 23:25:52 -0600522 if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600523 {
524 $this->cur_page = $base_page;
525 }
Eric Roberts032af982013-01-28 23:25:52 -0600526 else
527 {
528 // Make sure we're using integers for comparisons later.
529 $this->cur_page = (int) $this->cur_page;
530 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600531
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 // Is the page number beyond the result range?
Eric Roberts9f3a5902013-01-28 04:29:06 -0600533 // If so, we show the last page.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400534 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400536 if ($this->cur_page > $num_pages)
537 {
538 $this->cur_page = $num_pages;
539 }
540 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300541 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400542 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300543 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000545
Derek Allard2067d1a2008-11-13 22:59:24 +0000546 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500547
Eric Roberts9f3a5902013-01-28 04:29:06 -0600548 // If we're using offset instead of page numbers, convert it
549 // to a page number, so we can generate the surrounding number links.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400550 if ( ! $this->use_page_numbers)
551 {
Bo-Yi Wu5d504532012-07-14 00:03:56 +0800552 $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400553 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000554
555 // Calculate the start and end numbers. These determine
Eric Roberts9f3a5902013-01-28 04:29:06 -0600556 // which number to start and end the digit links with.
Andrey Andreev796bc952012-03-26 19:40:40 +0300557 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
558 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000559
Barry Mienydd671972010-10-04 16:33:58 +0200560 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000561 $output = '';
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100562
Eric Roberts9f3a5902013-01-28 04:29:06 -0600563 // Render the "First" link.
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900564 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links))
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900566 // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
567 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100568
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900569 $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Ahmad Anbar290f5872014-02-18 23:10:38 +0200570 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 }
572
Eric Roberts9f3a5902013-01-28 04:29:06 -0600573 // Render the "Previous" link.
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100574 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400576 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200577
Andrey Andreev135b64a2015-09-16 14:20:50 +0300578 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
Phil Sturgeona44cf572012-07-12 11:50:46 +0100579
Eric Roberts9f3a5902013-01-28 04:29:06 -0600580 if ($i === $base_page)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400581 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600582 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200583 $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300584 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400585 }
586 else
587 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600588 $append = $this->prefix.$i.$this->suffix;
Andrey Andreevb1616b82014-02-13 15:12:43 +0200589 $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300590 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400591 }
Barry Mienydd671972010-10-04 16:33:58 +0200592
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 }
594
Derek Allarde01fd0f2010-07-05 11:06:07 -0400595 // Render the pages
596 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400598 // Write the digit links
Andrey Andreev135b64a2015-09-16 14:20:50 +0300599 for ($loop = $start - 1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400601 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100602
Andrey Andreev135b64a2015-09-16 14:20:50 +0300603 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100604
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400605 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000606 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100607 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400608 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600609 // Current page
610 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
611 }
612 elseif ($i === $base_page)
613 {
614 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200615 $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600616 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400617 }
618 else
619 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600620 $append = $this->prefix.$i.$this->suffix;
Andrey Andreev135b64a2015-09-16 14:20:50 +0300621 $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600622 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400623 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 }
625 }
626 }
627
628 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300629 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400631 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400632
Andrey Andreev135b64a2015-09-16 14:20:50 +0300633 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100634
Andrey Andreevb1616b82014-02-13 15:12:43 +0200635 $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300636 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 }
638
639 // Render the "Last" link
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900640 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000641 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900642 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000643
Andrey Andreev135b64a2015-09-16 14:20:50 +0300644 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100645
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900646 $output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
647 .$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 }
649
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000650 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 // in the penultimate link so we'll kill all double slashes.
Miguel González361750a2015-03-30 05:00:21 +0200652 $output = preg_replace('#([^:"])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000653
654 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300655 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300657
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300658 // --------------------------------------------------------------------
659
660 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300661 * Parse attributes
662 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200663 * @param array $attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300664 * @return void
665 */
666 protected function _parse_attributes($attributes)
667 {
668 isset($attributes['rel']) OR $attributes['rel'] = TRUE;
669 $this->_link_types = ($attributes['rel'])
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200670 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
671 : array();
Andrey Andreev88c47272012-06-17 02:32:31 +0300672 unset($attributes['rel']);
673
674 $this->_attributes = '';
675 foreach ($attributes as $key => $value)
676 {
677 $this->_attributes .= ' '.$key.'="'.$value.'"';
678 }
679 }
680
681 // --------------------------------------------------------------------
682
683 /**
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300684 * Add "rel" attribute
685 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300686 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200687 * @param string $type
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300688 * @return string
689 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300690 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300691 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300692 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300693 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300694 unset($this->_link_types[$type]);
695 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300696 }
697
Andrey Andreev88c47272012-06-17 02:32:31 +0300698 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300699 }
700
Derek Allard2067d1a2008-11-13 22:59:24 +0000701}