blob: 5d501a966bafd18fe8724e3fc2ebc673011c3ffb [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 *
Instructor, BCIT0e59db62019-01-01 08:34:36 -08009 * Copyright (c) 2014 - 2019, 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/)
Instructor, BCIT0e59db62019-01-01 08:34:36 -080032 * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33 * @license https://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 Andreev51dbfde2017-11-28 13:14:44 +0200342 // _parse_attributes(), called by initialize(), needs to run at least once
343 // in order to enable "rel" attributes, and this triggers it.
Andrey Andreev75db0cf2017-11-28 13:12:12 +0200344 isset($params['attributes']) OR $params['attributes'] = array();
Andrey Andreev51dbfde2017-11-28 13:14:44 +0200345
Eric Barnesbce41402011-12-03 23:47:41 -0500346 $this->initialize($params);
Andrey Andreev90726b82015-01-20 12:39:22 +0200347 log_message('info', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 /**
353 * Initialize Preferences
354 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200355 * @param array $params Initialization parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200356 * @return CI_Pagination
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 */
Andrey Andreevaef63e52014-02-13 14:49:55 +0200358 public function initialize(array $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
Andrey Andreev75db0cf2017-11-28 13:12:12 +0200360 if (isset($params['attributes']) && is_array($params['attributes']))
Andrey Andreev88c47272012-06-17 02:32:31 +0300361 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600362 $this->_parse_attributes($params['attributes']);
Andrey Andreev88c47272012-06-17 02:32:31 +0300363 unset($params['attributes']);
364 }
365
366 // Deprecated legacy support for the anchor_class option
367 // Should be removed in CI 3.1+
368 if (isset($params['anchor_class']))
369 {
370 empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
371 unset($params['anchor_class']);
372 }
373
Andrey Andreevaef63e52014-02-13 14:49:55 +0200374 foreach ($params as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
Andrey Andreeve9c8c892014-02-17 17:29:23 +0200376 if (property_exists($this, $key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200378 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
380 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200381
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200382 if ($this->CI->config->item('enable_query_strings') === TRUE)
383 {
384 $this->page_query_string = TRUE;
385 }
386
Andrey Andreev0da50122015-01-20 13:30:05 +0200387 if ($this->use_global_url_suffix === TRUE)
388 {
389 $this->suffix = $this->CI->config->item('url_suffix');
390 }
391
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200392 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 /**
398 * Generate the pagination links
399 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000401 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500402 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 {
404 // If our item count or per-page total is zero there is no need to continue.
Andrey Andreevb367f7b2013-10-14 12:27:00 +0300405 // Note: DO NOT change the operator to === here!
406 if ($this->total_rows == 0 OR $this->per_page == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000408 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 }
410
411 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100412 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000413
414 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100415 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 return '';
418 }
419
Eric Roberts9f3a5902013-01-28 04:29:06 -0600420 // Check the user defined number of links.
Eric Barnes3b376592012-01-04 00:28:27 -0500421 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000422
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900423 if ($this->num_links < 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 {
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900425 show_error('Your number of links must be a non-negative number.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000427
Eric Roberts9f3a5902013-01-28 04:29:06 -0600428 // Keep any existing query string items.
429 // Note: Has nothing to do with any other query string option.
Eric Roberts9f3a5902013-01-28 04:29:06 -0600430 if ($this->reuse_query_string === TRUE)
431 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200432 $get = $this->CI->input->get();
Eric Roberts9f3a5902013-01-28 04:29:06 -0600433
Andrey Andreev71d8f722017-01-17 12:01:00 +0200434 // Unset the control, method, old-school routing options
Eric Roberts9f3a5902013-01-28 04:29:06 -0600435 unset($get['c'], $get['m'], $get[$this->query_string_segment]);
436 }
Eric Robertsba67f3b2013-01-28 18:01:01 -0600437 else
438 {
439 $get = array();
440 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600441
442 // Put together our base and first URLs.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200443 // Note: DO NOT append to the properties as that would break successive calls
444 $base_url = trim($this->base_url);
445 $first_url = $this->first_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600446
447 $query_string = '';
Andrey Andreevb1616b82014-02-13 15:12:43 +0200448 $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600449
450 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200451 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600452 {
453 // If a custom first_url hasn't been specified, we'll create one from
454 // the base_url, but without the page item.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200455 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600456 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200457 $first_url = $base_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600458
459 // If we saved any GET items earlier, make sure they're appended.
460 if ( ! empty($get))
461 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200462 $first_url .= $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600463 }
464 }
465
466 // Add the page segment to the end of the query string, where the
467 // page number will be appended.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200468 $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
Eric Roberts9f3a5902013-01-28 04:29:06 -0600469 }
470 else
471 {
472 // Standard segment mode.
473 // Generate our saved query string to append later after the page number.
474 if ( ! empty($get))
475 {
Eric Roberts9668d1d2013-01-28 17:45:34 -0600476 $query_string = $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600477 $this->suffix .= $query_string;
478 }
479
480 // Does the base_url have the query string in it?
481 // If we're supposed to save it, remove it so we can append it later.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200482 if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600483 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200484 $base_url = substr($base_url, 0, $base_query_pos);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600485 }
486
Andrey Andreevb1616b82014-02-13 15:12:43 +0200487 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600488 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200489 $first_url = $base_url.$query_string;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600490 }
491
Andrey Andreevb1616b82014-02-13 15:12:43 +0200492 $base_url = rtrim($base_url, '/').'/';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600493 }
494
495 // Determine the current page number.
496 $base_page = ($this->use_page_numbers) ? 1 : 0;
497
498 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200499 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600500 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200501 $this->cur_page = $this->CI->input->get($this->query_string_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600502 }
jekkos4e87bd32016-01-18 21:14:06 +0100503 elseif (empty($this->cur_page))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600504 {
505 // Default to the last segment number if one hasn't been defined.
506 if ($this->uri_segment === 0)
507 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200508 $this->uri_segment = count($this->CI->uri->segment_array());
Eric Roberts9f3a5902013-01-28 04:29:06 -0600509 }
510
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200511 $this->cur_page = $this->CI->uri->segment($this->uri_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600512
513 // Remove any specified prefix/suffix from the segment.
Eric Roberts032af982013-01-28 23:25:52 -0600514 if ($this->prefix !== '' OR $this->suffix !== '')
515 {
516 $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
517 }
Andrey Andreevca265612016-01-20 19:36:39 +0200518 }
jekkos4e87bd32016-01-18 21:14:06 +0100519 else
520 {
521 $this->cur_page = (string) $this->cur_page;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600522 }
523
524 // If something isn't quite right, back to the default base page.
Eric Roberts032af982013-01-28 23:25:52 -0600525 if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600526 {
527 $this->cur_page = $base_page;
528 }
Eric Roberts032af982013-01-28 23:25:52 -0600529 else
530 {
531 // Make sure we're using integers for comparisons later.
532 $this->cur_page = (int) $this->cur_page;
533 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 // Is the page number beyond the result range?
Eric Roberts9f3a5902013-01-28 04:29:06 -0600536 // If so, we show the last page.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400537 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400539 if ($this->cur_page > $num_pages)
540 {
541 $this->cur_page = $num_pages;
542 }
543 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300544 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400545 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300546 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000548
Derek Allard2067d1a2008-11-13 22:59:24 +0000549 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500550
Eric Roberts9f3a5902013-01-28 04:29:06 -0600551 // If we're using offset instead of page numbers, convert it
552 // to a page number, so we can generate the surrounding number links.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400553 if ( ! $this->use_page_numbers)
554 {
Bo-Yi Wu5d504532012-07-14 00:03:56 +0800555 $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400556 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000557
558 // Calculate the start and end numbers. These determine
Eric Roberts9f3a5902013-01-28 04:29:06 -0600559 // which number to start and end the digit links with.
Andrey Andreev796bc952012-03-26 19:40:40 +0300560 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
561 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000562
Barry Mienydd671972010-10-04 16:33:58 +0200563 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 $output = '';
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100565
Eric Roberts9f3a5902013-01-28 04:29:06 -0600566 // Render the "First" link.
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900567 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links))
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900569 // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
570 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100571
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900572 $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Ahmad Anbar290f5872014-02-18 23:10:38 +0200573 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 }
575
Eric Roberts9f3a5902013-01-28 04:29:06 -0600576 // Render the "Previous" link.
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100577 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400579 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200580
Andrey Andreev135b64a2015-09-16 14:20:50 +0300581 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, ($this->cur_page - 1));
Phil Sturgeona44cf572012-07-12 11:50:46 +0100582
Eric Roberts9f3a5902013-01-28 04:29:06 -0600583 if ($i === $base_page)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400584 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600585 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200586 $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300587 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400588 }
589 else
590 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600591 $append = $this->prefix.$i.$this->suffix;
Andrey Andreevb1616b82014-02-13 15:12:43 +0200592 $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300593 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400594 }
Barry Mienydd671972010-10-04 16:33:58 +0200595
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 }
597
Derek Allarde01fd0f2010-07-05 11:06:07 -0400598 // Render the pages
599 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400601 // Write the digit links
Andrey Andreev135b64a2015-09-16 14:20:50 +0300602 for ($loop = $start - 1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000603 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400604 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100605
Andrey Andreev135b64a2015-09-16 14:20:50 +0300606 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $loop);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100607
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400608 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000609 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100610 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400611 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600612 // Current page
613 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
614 }
615 elseif ($i === $base_page)
616 {
617 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200618 $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600619 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400620 }
621 else
622 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600623 $append = $this->prefix.$i.$this->suffix;
Andrey Andreev135b64a2015-09-16 14:20:50 +0300624 $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600625 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400626 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000627 }
628 }
629 }
630
631 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300632 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400634 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400635
Andrey Andreev135b64a2015-09-16 14:20:50 +0300636 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $this->cur_page + 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100637
Andrey Andreevb1616b82014-02-13 15:12:43 +0200638 $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300639 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000640 }
641
642 // Render the "Last" link
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900643 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900645 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000646
Andrey Andreev135b64a2015-09-16 14:20:50 +0300647 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, $num_pages);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100648
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900649 $output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
650 .$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000651 }
652
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000653 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000654 // in the penultimate link so we'll kill all double slashes.
Miguel González361750a2015-03-30 05:00:21 +0200655 $output = preg_replace('#([^:"])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000656
657 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300658 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300660
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300661 // --------------------------------------------------------------------
662
663 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300664 * Parse attributes
665 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200666 * @param array $attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300667 * @return void
668 */
669 protected function _parse_attributes($attributes)
670 {
671 isset($attributes['rel']) OR $attributes['rel'] = TRUE;
672 $this->_link_types = ($attributes['rel'])
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200673 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
674 : array();
Andrey Andreev88c47272012-06-17 02:32:31 +0300675 unset($attributes['rel']);
676
677 $this->_attributes = '';
678 foreach ($attributes as $key => $value)
679 {
680 $this->_attributes .= ' '.$key.'="'.$value.'"';
681 }
682 }
683
684 // --------------------------------------------------------------------
685
686 /**
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300687 * Add "rel" attribute
688 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300689 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200690 * @param string $type
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300691 * @return string
692 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300693 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300694 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300695 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300696 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300697 unset($this->_link_types[$type]);
698 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300699 }
700
Andrey Andreev88c47272012-06-17 02:32:31 +0300701 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300702 }
703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704}