blob: 2f1bcfbadcb6cd638ffe93a0aeadbf6c35550dd4 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Eric Barnes9a4902a2011-12-03 23:46:06 -05008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Eric Barnes9a4902a2011-12-03 23:46:06 -050010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Pagination Class
31 *
32 * @package CodeIgniter
33 * @subpackage Libraries
34 * @category Pagination
Derek Jonesf4a4bd82011-10-20 12:18:42 -050035 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @link http://codeigniter.com/user_guide/libraries/pagination.html
37 */
38class CI_Pagination {
39
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020040 /**
41 * Base URL
42 *
43 * The page that we're linking to
44 *
45 * @var string
46 */
47 protected $base_url = '';
48
49 /**
50 * Prefix
51 *
52 * @var string
53 */
54 protected $prefix = '';
55
56 /**
57 * Suffix
58 *
59 * @var string
60 */
61 protected $suffix = '';
62
63 /**
64 * Total number of items
65 *
66 * @var int
67 */
68 protected $total_rows = 0;
69
70 /**
71 * Items per page
72 *
73 * @var int
74 */
75 protected $per_page = 10;
76
77 /**
78 * Number of links to show
79 *
80 * Relates to "digit" type links shown before/after
81 * the currently viewed page.
82 *
83 * @var int
84 */
85 protected $num_links = 2;
86
87 /**
88 * Current page
89 *
90 * @var int
91 */
92 protected $cur_page = 0;
93
94 /**
95 * Use page numbers flag
96 *
97 * Whether to use actual page numbers instead of an offset
98 *
99 * @var bool
100 */
101 protected $use_page_numbers = FALSE;
102
103 /**
104 * First link
105 *
106 * @var string
107 */
108 protected $first_link = '&lsaquo; First';
109
110 /**
111 * Next link
112 *
113 * @var string
114 */
115 protected $next_link = '&gt;';
116
117 /**
118 * Previous link
119 *
120 * @var string
121 */
122 protected $prev_link = '&lt;';
123
124 /**
125 * Last link
126 *
127 * @var string
128 */
129 protected $last_link = 'Last &rsaquo;';
130
131 /**
132 * URI Segment
133 *
134 * @var int
135 */
Eric Roberts9f3a5902013-01-28 04:29:06 -0600136 protected $uri_segment = 0;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200137
138 /**
139 * Full tag open
140 *
141 * @var string
142 */
143 protected $full_tag_open = '';
144
145 /**
146 * Full tag close
147 *
148 * @var string
149 */
150 protected $full_tag_close = '';
151
152 /**
153 * First tag open
154 *
155 * @var string
156 */
157 protected $first_tag_open = '';
158
159 /**
160 * First tag close
161 *
162 * @var string
163 */
164 protected $first_tag_close = '';
165
166 /**
167 * Last tag open
168 *
169 * @var string
170 */
171 protected $last_tag_open = '';
172
173 /**
174 * Last tag close
175 *
176 * @var string
177 */
178 protected $last_tag_close = '';
179
180 /**
181 * First URL
182 *
183 * An alternative URL for the first page
184 *
185 * @var string
186 */
187 protected $first_url = '';
188
189 /**
190 * Current tag open
191 *
192 * @var string
193 */
194 protected $cur_tag_open = '<strong>';
195
196 /**
197 * Current tag close
198 *
199 * @var string
200 */
201 protected $cur_tag_close = '</strong>';
202
203 /**
204 * Next tag open
205 *
206 * @var string
207 */
208 protected $next_tag_open = '';
209
210 /**
211 * Next tag close
212 *
213 * @var string
214 */
215 protected $next_tag_close = '';
216
217 /**
218 * Previous tag open
219 *
220 * @var string
221 */
222 protected $prev_tag_open = '';
223
224 /**
225 * Previous tag close
226 *
227 * @var string
228 */
229 protected $prev_tag_close = '';
230
231 /**
232 * Number tag open
233 *
234 * @var string
235 */
236 protected $num_tag_open = '';
237
238 /**
239 * Number tag close
240 *
241 * @var string
242 */
243 protected $num_tag_close = '';
244
245 /**
246 * Page query string flag
247 *
248 * @var bool
249 */
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100250 protected $page_query_string = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200251
252 /**
253 * Query string segment
254 *
255 * @var string
256 */
Alex Bilbief7e23b32012-09-07 09:52:32 +0100257 protected $query_string_segment = 'per_page';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200258
259 /**
260 * Display pages flag
261 *
262 * @var bool
263 */
264 protected $display_pages = TRUE;
265
266 /**
267 * Attributes
268 *
269 * @var string
270 */
271 protected $_attributes = '';
272
273 /**
274 * Link types
275 *
276 * "rel" attribute
277 *
278 * @see CI_Pagination::_attr_rel()
279 * @var array
280 */
281 protected $_link_types = array();
282
283 /**
284 * Reuse query string flag
285 *
286 * @var bool
287 */
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100288 protected $reuse_query_string = FALSE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200289
290 /**
291 * Data page attribute
292 *
293 * @var string
294 */
295 protected $data_page_attr = 'data-ci-pagination-page';
296
297 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000298
299 /**
300 * Constructor
301 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200302 * @param array $params Initialization parameters
Andrey Andreev56454792012-05-17 14:32:19 +0300303 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 */
Greg Akera9263282010-11-10 15:26:43 -0600305 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200307 $CI =& get_instance();
308 $CI->load->language('paginaton');
309 foreach (array('first_link', 'next_link', 'prev_link', 'last_link') as $key)
310 {
311 if (($val = $CI->lang->line('pagination_'.$key)) !== FALSE)
312 {
313 $this->$key = $val;
314 }
315 }
316
Eric Barnesbce41402011-12-03 23:47:41 -0500317 $this->initialize($params);
Andrey Andreev796bc952012-03-26 19:40:40 +0300318 log_message('debug', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000320
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 /**
324 * Initialize Preferences
325 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200326 * @param array $params Initialization parameters
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200327 * @return CI_Pagination
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 */
Andrey Andreevaef63e52014-02-13 14:49:55 +0200329 public function initialize(array $params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300331 if (isset($params['attributes']) && is_array($params['attributes']))
332 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600333 $this->_parse_attributes($params['attributes']);
Andrey Andreev88c47272012-06-17 02:32:31 +0300334 unset($params['attributes']);
335 }
336
337 // Deprecated legacy support for the anchor_class option
338 // Should be removed in CI 3.1+
339 if (isset($params['anchor_class']))
340 {
341 empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
342 unset($params['anchor_class']);
343 }
344
Andrey Andreevaef63e52014-02-13 14:49:55 +0200345 foreach ($params as $key => $val)
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200347 if (property_exists($this, $this->$key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 {
Andrey Andreevaef63e52014-02-13 14:49:55 +0200349 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 }
351 }
Andrey Andreev6f6102c2014-02-08 19:11:40 +0200352
353 return $this;
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000355
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 /**
359 * Generate the pagination links
360 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000362 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500363 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
365 // If our item count or per-page total is zero there is no need to continue.
Andrey Andreevb367f7b2013-10-14 12:27:00 +0300366 // Note: DO NOT change the operator to === here!
367 if ($this->total_rows == 0 OR $this->per_page == 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000369 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 }
371
372 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100373 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000374
375 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100376 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 return '';
379 }
380
Eric Roberts9f3a5902013-01-28 04:29:06 -0600381 // Check the user defined number of links.
Eric Barnes3b376592012-01-04 00:28:27 -0500382 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 if ($this->num_links < 1)
385 {
386 show_error('Your number of links must be a positive number.');
387 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000388
Eric Roberts9f3a5902013-01-28 04:29:06 -0600389 $CI =& get_instance();
390
391 // Keep any existing query string items.
392 // Note: Has nothing to do with any other query string option.
Eric Roberts9f3a5902013-01-28 04:29:06 -0600393 if ($this->reuse_query_string === TRUE)
394 {
395 $get = $CI->input->get();
396
397 // Unset the controll, method, old-school routing options
398 unset($get['c'], $get['m'], $get[$this->query_string_segment]);
399 }
Eric Robertsba67f3b2013-01-28 18:01:01 -0600400 else
401 {
402 $get = array();
403 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600404
405 // Put together our base and first URLs.
406 $this->base_url = trim($this->base_url);
407
408 $query_string = '';
409 $query_string_sep = (strpos($this->base_url, '?') === FALSE) ? '?' : '&amp;';
410
411 // Are we using query strings?
412 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
413 {
414 // If a custom first_url hasn't been specified, we'll create one from
415 // the base_url, but without the page item.
416 if ($this->first_url === '')
417 {
418 $this->first_url = $this->base_url;
419
420 // If we saved any GET items earlier, make sure they're appended.
421 if ( ! empty($get))
422 {
Eric Roberts9668d1d2013-01-28 17:45:34 -0600423 $this->first_url .= $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600424 }
425 }
426
427 // Add the page segment to the end of the query string, where the
428 // page number will be appended.
Eric Roberts9668d1d2013-01-28 17:45:34 -0600429 $this->base_url .= $query_string_sep.http_build_query(array_merge($get, array($this->query_string_segment => '')));
Eric Roberts9f3a5902013-01-28 04:29:06 -0600430 }
431 else
432 {
433 // Standard segment mode.
434 // Generate our saved query string to append later after the page number.
435 if ( ! empty($get))
436 {
Eric Roberts9668d1d2013-01-28 17:45:34 -0600437 $query_string = $query_string_sep.http_build_query($get);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600438 $this->suffix .= $query_string;
439 }
440
441 // Does the base_url have the query string in it?
442 // If we're supposed to save it, remove it so we can append it later.
443 if ($this->reuse_query_string === TRUE && ($base_query_pos = strpos($this->base_url, '?')) !== FALSE)
444 {
445 $this->base_url = substr($this->base_url, 0, $base_query_pos);
446 }
447
448 if ($this->first_url === '')
449 {
Eric Roberts9668d1d2013-01-28 17:45:34 -0600450 $this->first_url = $this->base_url.$query_string;
Eric Roberts9f3a5902013-01-28 04:29:06 -0600451 }
452
Eric Roberts9668d1d2013-01-28 17:45:34 -0600453 $this->base_url = rtrim($this->base_url, '/').'/';
Eric Roberts9f3a5902013-01-28 04:29:06 -0600454 }
455
456 // Determine the current page number.
457 $base_page = ($this->use_page_numbers) ? 1 : 0;
458
459 // Are we using query strings?
460 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
461 {
Andrey Andreev870f1132013-02-08 22:06:00 +0200462 $this->cur_page = $CI->input->get($this->query_string_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600463 }
464 else
465 {
466 // Default to the last segment number if one hasn't been defined.
467 if ($this->uri_segment === 0)
468 {
469 $this->uri_segment = count($CI->uri->segment_array());
470 }
471
Andrey Andreev870f1132013-02-08 22:06:00 +0200472 $this->cur_page = $CI->uri->segment($this->uri_segment);
Eric Roberts9f3a5902013-01-28 04:29:06 -0600473
474 // Remove any specified prefix/suffix from the segment.
Eric Roberts032af982013-01-28 23:25:52 -0600475 if ($this->prefix !== '' OR $this->suffix !== '')
476 {
477 $this->cur_page = str_replace(array($this->prefix, $this->suffix), '', $this->cur_page);
478 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600479 }
480
481 // If something isn't quite right, back to the default base page.
Eric Roberts032af982013-01-28 23:25:52 -0600482 if ( ! ctype_digit($this->cur_page) OR ($this->use_page_numbers && (int) $this->cur_page === 0))
Eric Roberts9f3a5902013-01-28 04:29:06 -0600483 {
484 $this->cur_page = $base_page;
485 }
Eric Roberts032af982013-01-28 23:25:52 -0600486 else
487 {
488 // Make sure we're using integers for comparisons later.
489 $this->cur_page = (int) $this->cur_page;
490 }
Eric Roberts9f3a5902013-01-28 04:29:06 -0600491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 // Is the page number beyond the result range?
Eric Roberts9f3a5902013-01-28 04:29:06 -0600493 // If so, we show the last page.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400494 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400496 if ($this->cur_page > $num_pages)
497 {
498 $this->cur_page = $num_pages;
499 }
500 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300501 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400502 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300503 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000505
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500507
Eric Roberts9f3a5902013-01-28 04:29:06 -0600508 // If we're using offset instead of page numbers, convert it
509 // to a page number, so we can generate the surrounding number links.
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400510 if ( ! $this->use_page_numbers)
511 {
Bo-Yi Wu5d504532012-07-14 00:03:56 +0800512 $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400513 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000514
515 // Calculate the start and end numbers. These determine
Eric Roberts9f3a5902013-01-28 04:29:06 -0600516 // which number to start and end the digit links with.
Andrey Andreev796bc952012-03-26 19:40:40 +0300517 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
518 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000519
Barry Mienydd671972010-10-04 16:33:58 +0200520 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $output = '';
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100522
Eric Roberts9f3a5902013-01-28 04:29:06 -0600523 // Render the "First" link.
Andrey Andreev796bc952012-03-26 19:40:40 +0300524 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600526 // Take the general parameters, and squeeze this pagination-page attr in for JS frameworks.
Andrey Andreev685cdd72012-07-13 20:11:13 +0300527 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100528
Eric Roberts9f3a5902013-01-28 04:29:06 -0600529 $output .= $this->first_tag_open.'<a href="'.$this->first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300530 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532
Eric Roberts9f3a5902013-01-28 04:29:06 -0600533 // Render the "Previous" link.
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100534 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400536 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200537
Phil Sturgeona44cf572012-07-12 11:50:46 +0100538 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
539
Eric Roberts9f3a5902013-01-28 04:29:06 -0600540 if ($i === $base_page)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400541 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600542 // First page
543 $output .= $this->prev_tag_open.'<a href="'.$this->first_url.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300544 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400545 }
546 else
547 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600548 $append = $this->prefix.$i.$this->suffix;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100549 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300550 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400551 }
Barry Mienydd671972010-10-04 16:33:58 +0200552
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 }
554
Derek Allarde01fd0f2010-07-05 11:06:07 -0400555 // Render the pages
556 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400558 // Write the digit links
559 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400561 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100562
Phil Sturgeona44cf572012-07-12 11:50:46 +0100563 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
564
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400565 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100567 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400568 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600569 // Current page
570 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close;
571 }
572 elseif ($i === $base_page)
573 {
574 // First page
575 $output .= $this->num_tag_open.'<a href="'.$this->first_url.'"'.$attributes.$this->_attr_rel('start').'>'
576 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400577 }
578 else
579 {
Eric Roberts9f3a5902013-01-28 04:29:06 -0600580 $append = $this->prefix.$i.$this->suffix;
581 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$append.'"'.$attributes.$this->_attr_rel('start').'>'
582 .$loop.'</a>'.$this->num_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400583 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 }
585 }
586 }
587
588 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300589 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400591 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400592
Phil Sturgeona44cf572012-07-12 11:50:46 +0100593 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
594
595 $output .= $this->next_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300596 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 }
598
599 // Render the "Last" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300600 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400602 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000603
Phil Sturgeona44cf572012-07-12 11:50:46 +0100604 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
605
606 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300607 .$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 }
609
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000610 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 // in the penultimate link so we'll kill all double slashes.
Andrey Andreev796bc952012-03-26 19:40:40 +0300612 $output = preg_replace('#([^:])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000613
614 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300615 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000616 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300617
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300618 // --------------------------------------------------------------------
619
620 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300621 * Parse attributes
622 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200623 * @param array $attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300624 * @return void
625 */
626 protected function _parse_attributes($attributes)
627 {
628 isset($attributes['rel']) OR $attributes['rel'] = TRUE;
629 $this->_link_types = ($attributes['rel'])
630 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
631 : array();
632 unset($attributes['rel']);
633
634 $this->_attributes = '';
635 foreach ($attributes as $key => $value)
636 {
637 $this->_attributes .= ' '.$key.'="'.$value.'"';
638 }
639 }
640
641 // --------------------------------------------------------------------
642
643 /**
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300644 * Add "rel" attribute
645 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300646 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200647 * @param string $type
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300648 * @return string
649 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300650 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300651 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300652 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300653 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300654 unset($this->_link_types[$type]);
655 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300656 }
657
Andrey Andreev88c47272012-06-17 02:32:31 +0300658 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300659 }
660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661}
Derek Allard2067d1a2008-11-13 22:59:24 +0000662
663/* End of file Pagination.php */
Andrey Andreev685cdd72012-07-13 20:11:13 +0300664/* Location: ./system/libraries/Pagination.php */