blob: 1081fbec7d4594010a9a9264c2eb0b12acf21f5b [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 Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, 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
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @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
Derek Allard2067d1a2008-11-13 22:59:24 +000047 * @link http://codeigniter.com/user_guide/libraries/pagination.html
48 */
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 /**
302 * Data page attribute
303 *
304 * @var string
305 */
Andrey Andreev83821572014-08-01 12:08:56 +0300306 protected $data_page_attr = 'data-ci-pagination-page';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200307
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200308 /**
309 * CI Singleton
310 *
311 * @var object
312 */
313 protected $CI;
314
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200315 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000316
317 /**
318 * Constructor
319 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200320 * @param array $params Initialization parameters
Andrey Andreev56454792012-05-17 14:32:19 +0300321 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 */
Greg Akera9263282010-11-10 15:26:43 -0600323 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200325 $this->CI =& get_instance();
326 $this->CI->load->language('pagination');
Andrey Andreevaef63e52014-02-13 14:49:55 +0200327 foreach (array('first_link', 'next_link', 'prev_link', 'last_link') as $key)
328 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200329 if (($val = $this->CI->lang->line('pagination_'.$key)) !== FALSE)
Andrey Andreevaef63e52014-02-13 14:49:55 +0200330 {
331 $this->$key = $val;
332 }
333 }
334
Eric Barnesbce41402011-12-03 23:47:41 -0500335 $this->initialize($params);
Andrey Andreev796bc952012-03-26 19:40:40 +0300336 log_message('debug', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 /**
342 * Initialize Preferences
343 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200344 * @param array $params Initialization parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200345 * @return CI_Pagination
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 */
Andrey Andreevaef63e52014-02-13 14:49:55 +0200347 public function initialize(array $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300349 if (isset($params['attributes']) && is_array($params['attributes']))
350 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600351 $this->_parse_attributes($params['attributes']);
Andrey Andreev88c47272012-06-17 02:32:31 +0300352 unset($params['attributes']);
353 }
354
355 // Deprecated legacy support for the anchor_class option
356 // Should be removed in CI 3.1+
357 if (isset($params['anchor_class']))
358 {
359 empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
360 unset($params['anchor_class']);
361 }
362
Andrey Andreevaef63e52014-02-13 14:49:55 +0200363 foreach ($params as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Andrey Andreeve9c8c892014-02-17 17:29:23 +0200365 if (property_exists($this, $key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200367 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 }
369 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200370
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200371 if ($this->CI->config->item('enable_query_strings') === TRUE)
372 {
373 $this->page_query_string = TRUE;
374 }
375
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200376 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 /**
382 * Generate the pagination links
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000385 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500386 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 {
388 // If our item count or per-page total is zero there is no need to continue.
Andrey Andreevb367f7b2013-10-14 12:27:00 +0300389 // Note: DO NOT change the operator to === here!
390 if ($this->total_rows == 0 OR $this->per_page == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000392 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394
395 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100396 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000397
398 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100399 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 return '';
402 }
403
Eric Roberts9f3a5902013-01-28 04:29:06 -0600404 // Check the user defined number of links.
Eric Barnes3b376592012-01-04 00:28:27 -0500405 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000406
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900407 if ($this->num_links < 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900409 show_error('Your number of links must be a non-negative number.');
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000411
Eric Roberts9f3a5902013-01-28 04:29:06 -0600412 // Keep any existing query string items.
413 // Note: Has nothing to do with any other query string option.
Eric Roberts9f3a5902013-01-28 04:29:06 -0600414 if ($this->reuse_query_string === TRUE)
415 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200416 $get = $this->CI->input->get();
Eric Roberts9f3a5902013-01-28 04:29:06 -0600417
418 // Unset the controll, method, old-school routing options
419 unset($get['c'], $get['m'], $get[$this->query_string_segment]);
420 }
Eric Robertsba67f3b2013-01-28 18:01:01 -0600421 else
422 {
423 $get = array();
424 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600425
426 // Put together our base and first URLs.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200427 // Note: DO NOT append to the properties as that would break successive calls
428 $base_url = trim($this->base_url);
429 $first_url = $this->first_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600430
431 $query_string = '';
Andrey Andreevb1616b82014-02-13 15:12:43 +0200432 $query_string_sep = (strpos($base_url, '?') === FALSE) ? '?' : '&amp;';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600433
434 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200435 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600436 {
437 // If a custom first_url hasn't been specified, we'll create one from
438 // the base_url, but without the page item.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200439 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600440 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200441 $first_url = $base_url;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600442
443 // If we saved any GET items earlier, make sure they're appended.
444 if ( ! empty($get))
445 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200446 $first_url .= $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600447 }
448 }
449
450 // Add the page segment to the end of the query string, where the
451 // page number will be appended.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200452 $base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
Eric Roberts9f3a5902013-01-28 04:29:06 -0600453 }
454 else
455 {
456 // Standard segment mode.
457 // Generate our saved query string to append later after the page number.
458 if ( ! empty($get))
459 {
Eric Roberts9668d1d2013-01-28 17:45:34 -0600460 $query_string = $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600461 $this->suffix .= $query_string;
462 }
463
464 // Does the base_url have the query string in it?
465 // If we're supposed to save it, remove it so we can append it later.
Andrey Andreevb1616b82014-02-13 15:12:43 +0200466 if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($base_url, '?')) !== FALSE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600467 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200468 $base_url = substr($base_url, 0, $base_query_pos);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600469 }
470
Andrey Andreevb1616b82014-02-13 15:12:43 +0200471 if ($first_url === '')
Eric Roberts9f3a5902013-01-28 04:29:06 -0600472 {
Andrey Andreevb1616b82014-02-13 15:12:43 +0200473 $first_url = $base_url.$query_string;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600474 }
475
Andrey Andreevb1616b82014-02-13 15:12:43 +0200476 $base_url = rtrim($base_url, '/').'/';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600477 }
478
479 // Determine the current page number.
480 $base_page = ($this->use_page_numbers) ? 1 : 0;
481
482 // Are we using query strings?
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200483 if ($this->page_query_string === TRUE)
Eric Roberts9f3a5902013-01-28 04:29:06 -0600484 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200485 $this->cur_page = $this->CI->input->get($this->query_string_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600486 }
487 else
488 {
489 // Default to the last segment number if one hasn't been defined.
490 if ($this->uri_segment === 0)
491 {
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200492 $this->uri_segment = count($this->CI->uri->segment_array());
Eric Roberts9f3a5902013-01-28 04:29:06 -0600493 }
494
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200495 $this->cur_page = $this->CI->uri->segment($this->uri_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600496
497 // Remove any specified prefix/suffix from the segment.
Eric Roberts032af982013-01-28 23:25:52 -0600498 if ($this->prefix !== '' OR $this->suffix !== '')
499 {
500 $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
501 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600502 }
503
504 // If something isn't quite right, back to the default base page.
Eric Roberts032af982013-01-28 23:25:52 -0600505 if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600506 {
507 $this->cur_page = $base_page;
508 }
Eric Roberts032af982013-01-28 23:25:52 -0600509 else
510 {
511 // Make sure we're using integers for comparisons later.
512 $this->cur_page = (int) $this->cur_page;
513 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 // Is the page number beyond the result range?
Eric Roberts9f3a5902013-01-28 04:29:06 -0600516 // If so, we show the last page.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400517 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400519 if ($this->cur_page > $num_pages)
520 {
521 $this->cur_page = $num_pages;
522 }
523 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300524 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400525 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300526 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500530
Eric Roberts9f3a5902013-01-28 04:29:06 -0600531 // If we're using offset instead of page numbers, convert it
532 // to a page number, so we can generate the surrounding number links.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400533 if ( ! $this->use_page_numbers)
534 {
Bo-Yi Wu5d504532012-07-14 00:03:56 +0800535 $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400536 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000537
538 // Calculate the start and end numbers. These determine
Eric Roberts9f3a5902013-01-28 04:29:06 -0600539 // which number to start and end the digit links with.
Andrey Andreev796bc952012-03-26 19:40:40 +0300540 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
541 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000542
Barry Mienydd671972010-10-04 16:33:58 +0200543 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 $output = '';
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100545
Eric Roberts9f3a5902013-01-28 04:29:06 -0600546 // Render the "First" link.
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900547 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1 + ! $this->num_links))
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900549 // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
550 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100551
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900552 $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Ahmad Anbar290f5872014-02-18 23:10:38 +0200553 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555
Eric Roberts9f3a5902013-01-28 04:29:06 -0600556 // Render the "Previous" link.
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100557 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400559 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200560
Phil Sturgeona44cf572012-07-12 11:50:46 +0100561 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
562
Eric Roberts9f3a5902013-01-28 04:29:06 -0600563 if ($i === $base_page)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400564 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600565 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200566 $output .= $this->prev_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300567 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400568 }
569 else
570 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600571 $append = $this->prefix.$i.$this->suffix;
Andrey Andreevb1616b82014-02-13 15:12:43 +0200572 $output .= $this->prev_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300573 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400574 }
Barry Mienydd671972010-10-04 16:33:58 +0200575
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 }
577
Derek Allarde01fd0f2010-07-05 11:06:07 -0400578 // Render the pages
579 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400581 // Write the digit links
582 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000583 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400584 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100585
Phil Sturgeona44cf572012-07-12 11:50:46 +0100586 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
587
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400588 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100590 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400591 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600592 // Current page
593 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
594 }
595 elseif ($i === $base_page)
596 {
597 // First page
Andrey Andreevb1616b82014-02-13 15:12:43 +0200598 $output .= $this->num_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600599 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400600 }
601 else
602 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600603 $append = $this->prefix.$i.$this->suffix;
Andrey Andreevb1616b82014-02-13 15:12:43 +0200604 $output .= $this->num_tag_open.'<a href="'.$base_url.$append.'"'.$attributes.$this->_attr_rel('start').'>'
Eric Roberts9f3a5902013-01-28 04:29:06 -0600605 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400606 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608 }
609 }
610
611 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300612 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000613 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400614 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400615
Phil Sturgeona44cf572012-07-12 11:50:46 +0100616 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
617
Andrey Andreevb1616b82014-02-13 15:12:43 +0200618 $output .= $this->next_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300619 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 }
621
622 // Render the "Last" link
Takayuki Sakai8bc59032014-06-13 18:38:05 +0900623 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links + ! $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000624 {
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900625 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000626
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900627 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100628
Takayuki Sakai1240b6a2014-06-13 18:12:02 +0900629 $output .= $this->last_tag_open.'<a href="'.$base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
630 .$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000631 }
632
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000633 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000634 // in the penultimate link so we'll kill all double slashes.
Andrey Andreev796bc952012-03-26 19:40:40 +0300635 $output = preg_replace('#([^:])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000636
637 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300638 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300640
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300641 // --------------------------------------------------------------------
642
643 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300644 * Parse attributes
645 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200646 * @param array $attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300647 * @return void
648 */
649 protected function _parse_attributes($attributes)
650 {
651 isset($attributes['rel']) OR $attributes['rel'] = TRUE;
652 $this->_link_types = ($attributes['rel'])
Andrey Andreevffe8ade2014-02-17 18:51:48 +0200653 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
654 : array();
Andrey Andreev88c47272012-06-17 02:32:31 +0300655 unset($attributes['rel']);
656
657 $this->_attributes = '';
658 foreach ($attributes as $key => $value)
659 {
660 $this->_attributes .= ' '.$key.'="'.$value.'"';
661 }
662 }
663
664 // --------------------------------------------------------------------
665
666 /**
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300667 * Add "rel" attribute
668 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300669 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200670 * @param string $type
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300671 * @return string
672 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300673 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300674 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300675 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300676 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300677 unset($this->_link_types[$type]);
678 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300679 }
680
Andrey Andreev88c47272012-06-17 02:32:31 +0300681 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300682 }
683
Derek Allard2067d1a2008-11-13 22:59:24 +0000684}
Derek Allard2067d1a2008-11-13 22:59:24 +0000685
686/* End of file Pagination.php */
Andrey Andreev685cdd72012-07-13 20:11:13 +0300687/* Location: ./system/libraries/Pagination.php */