blob: ae8dba07222e7c8e82709f426d97b71b6a664bf6 [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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
136 protected $uri_segment = 3;
137
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 {
Eric Barnesbce41402011-12-03 23:47:41 -0500307 $this->initialize($params);
Andrey Andreev796bc952012-03-26 19:40:40 +0300308 log_message('debug', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000310
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000312
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 /**
314 * Initialize Preferences
315 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200316 * @param array $params Initialization parameters
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @return void
318 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500319 public function initialize($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300321 $attributes = array();
322
323 if (isset($params['attributes']) && is_array($params['attributes']))
324 {
325 $attributes = $params['attributes'];
326 unset($params['attributes']);
327 }
328
329 // Deprecated legacy support for the anchor_class option
330 // Should be removed in CI 3.1+
331 if (isset($params['anchor_class']))
332 {
333 empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
334 unset($params['anchor_class']);
335 }
336
337 $this->_parse_attributes($attributes);
338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 if (count($params) > 0)
340 {
341 foreach ($params as $key => $val)
342 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300343 if (isset($this->$key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 {
345 $this->$key = $val;
346 }
347 }
348 }
349 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000350
Derek Allard2067d1a2008-11-13 22:59:24 +0000351 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000352
Derek Allard2067d1a2008-11-13 22:59:24 +0000353 /**
354 * Generate the pagination links
355 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000357 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500358 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 // If our item count or per-page total is zero there is no need to continue.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100361 if ($this->total_rows === 0 OR $this->per_page === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000363 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
365
366 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100367 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000368
369 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100370 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 {
372 return '';
373 }
374
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400375 // Set the base page index for starting page number
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400376 $base_page = ($this->use_page_numbers) ? 1 : 0;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400377
Derek Allardfaac15e2009-03-07 17:17:58 +0000378 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000380
Eric Barnes3b376592012-01-04 00:28:27 -0500381 // See if we are using a prefix or suffix on links
Alex Bilbied261b1e2012-06-02 11:12:16 +0100382 if ($this->prefix !== '' OR $this->suffix !== '')
Eric Barnes8d727f12012-01-04 00:06:36 -0500383 {
Andrey Andreeve66d6242012-10-22 16:39:12 +0300384 $this->cur_page = (int) str_replace(array($this->prefix, $this->suffix), '', $CI->uri->rsegment($this->uri_segment));
Eric Barnes8d727f12012-01-04 00:06:36 -0500385 }
386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
388 {
Andrey Andreev7eb7ebf2012-06-12 10:26:27 +0300389 if ($CI->input->get($this->query_string_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 {
Andrey Andreev33987e62011-12-24 19:48:45 +0200391 $this->cur_page = (int) $CI->input->get($this->query_string_segment);
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 }
393 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100394 elseif ( ! $this->cur_page && $CI->uri->segment($this->uri_segment) !== $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
Andrey Andreeve66d6242012-10-22 16:39:12 +0300396 $this->cur_page = (int) $CI->uri->rsegment($this->uri_segment);
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
Eric Barnes9a4902a2011-12-03 23:46:06 -0500398
Andrey Andreev33987e62011-12-24 19:48:45 +0200399 // Set current page to 1 if it's not valid or if using page numbers instead of offset
Alex Bilbied261b1e2012-06-02 11:12:16 +0100400 if ( ! is_numeric($this->cur_page) OR ($this->use_page_numbers && $this->cur_page === 0))
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400401 {
402 $this->cur_page = $base_page;
403 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000404
Eric Barnes3b376592012-01-04 00:28:27 -0500405 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 if ($this->num_links < 1)
408 {
409 show_error('Your number of links must be a positive number.');
410 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000411
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 // Is the page number beyond the result range?
413 // If so we show the last page
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400414 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400416 if ($this->cur_page > $num_pages)
417 {
418 $this->cur_page = $num_pages;
419 }
420 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300421 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400422 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300423 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500427
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400428 if ( ! $this->use_page_numbers)
429 {
Bo-Yi Wu5d504532012-07-14 00:03:56 +0800430 $this->cur_page = (int) floor(($this->cur_page/$this->per_page) + 1);
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400431 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000432
433 // Calculate the start and end numbers. These determine
434 // which number to start and end the digit links with
Andrey Andreev796bc952012-03-26 19:40:40 +0300435 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
436 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000437
Andrey Andreev796bc952012-03-26 19:40:40 +0300438 // Is pagination being used over GET or POST? If get, add a per_page query
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 // string. If post, add a trailing slash to the base URL if needed
440 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
441 {
Bo-Yi Wu6b4e3622012-09-21 13:57:24 +0800442 $segment = (strpos($this->base_url, '?')) ? '&amp;' : '?';
443 $this->base_url = rtrim($this->base_url).$segment.$this->query_string_segment.'=';
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 }
445 else
446 {
447 $this->base_url = rtrim($this->base_url, '/') .'/';
448 }
449
Barry Mienydd671972010-10-04 16:33:58 +0200450 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 $output = '';
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100452 $query_string = '';
453
454 // Add anything in the query string back to the links
455 // Note: Nothing to do with query_string_segment or any other query string options
456 if ($this->reuse_query_string === TRUE)
457 {
458 $get = $CI->input->get();
Andrey Andreev685cdd72012-07-13 20:11:13 +0300459
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100460 // Unset the controll, method, old-school routing options
461 unset($get['c'], $get['m'], $get[$this->query_string_segment]);
462
Phil Sturgeona44cf572012-07-12 11:50:46 +0100463 if ( ! $get) $get = array();
464
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100465 // Put everything else onto the end
466 $query_string = (strpos($this->base_url, '&amp;') !== FALSE ? '&amp;' : '?') . http_build_query($get, '', '&amp;');
467
468 // Add this after the suffix to put it into more links easily
469 $this->suffix .= $query_string;
470 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000471
472 // Render the "First" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300473 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100475 $first_url = ($this->first_url === '') ? $this->base_url : $this->first_url;
Andrey Andreev685cdd72012-07-13 20:11:13 +0300476
Phil Sturgeona44cf572012-07-12 11:50:46 +0100477 // Take the general parameters, and squeeze this pagination-page attr in there for JS fw's
Andrey Andreev685cdd72012-07-13 20:11:13 +0300478 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, 1);
Phil Sturgeona44cf572012-07-12 11:50:46 +0100479
480 $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300481 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
483
484 // Render the "previous" link
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100485 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400487 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200488
Phil Sturgeona44cf572012-07-12 11:50:46 +0100489 // Take the general parameters, and squeeze this pagination-page attr in there for JS fw's
490 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
491
Alex Bilbied261b1e2012-06-02 11:12:16 +0100492 if ($i === $base_page && $this->first_url !== '')
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400493 {
Phil Sturgeona44cf572012-07-12 11:50:46 +0100494 $output .= $this->prev_tag_open.'<a href="'.$this->first_url.$query_string.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300495 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400496 }
497 else
498 {
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100499 $append = ($i === $base_page) ? $query_string : $this->prefix.$i.$this->suffix;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100500 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$append.'"'.$attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300501 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 }
505
Derek Allarde01fd0f2010-07-05 11:06:07 -0400506 // Render the pages
507 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400509 // Write the digit links
510 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400512 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100513
514 // Take the general parameters, and squeeze this pagination-page attr in there for JS fw's
515 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
516
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400517 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100519 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400520 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400521 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400522 }
523 else
524 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100525 $n = ($i === $base_page) ? '' : $i;
Andrey Andreev88c47272012-06-17 02:32:31 +0300526 if ($n === '' && ! empty($this->first_url))
Derek Allarde01fd0f2010-07-05 11:06:07 -0400527 {
Phil Sturgeona44cf572012-07-12 11:50:46 +0100528 $output .= $this->num_tag_open.'<a href="'.$this->first_url.$query_string.'"'.$attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300529 .$loop.'</a>'.$this->num_tag_close;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400530 }
531 else
532 {
Phil Sturgeonf82b9292012-06-23 15:49:23 +0100533 $append = ($n === '') ? $query_string : $this->prefix.$n.$this->suffix;
Phil Sturgeona44cf572012-07-12 11:50:46 +0100534 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$append.'"'.$attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300535 .$loop.'</a>'.$this->num_tag_close;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400536 }
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400537 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539 }
540 }
541
542 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300543 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400545 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400546
Phil Sturgeona44cf572012-07-12 11:50:46 +0100547 // Take the general parameters, and squeeze this pagination-page attr in there for JS fw's
548 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
549
550 $output .= $this->next_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300551 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 }
553
554 // Render the "Last" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300555 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400557 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000558
Phil Sturgeona44cf572012-07-12 11:50:46 +0100559 // Take the general parameters, and squeeze this pagination-page attr in there for JS fw's
560 $attributes = sprintf('%s %s="%d"', $this->_attributes, $this->data_page_attr, (int) $i);
561
562 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$attributes.'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300563 .$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 }
565
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000566 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 // in the penultimate link so we'll kill all double slashes.
Andrey Andreev796bc952012-03-26 19:40:40 +0300568 $output = preg_replace('#([^:])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000569
570 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300571 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300573
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300574 // --------------------------------------------------------------------
575
576 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300577 * Parse attributes
578 *
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200579 * @param array $attributes
Andrey Andreev88c47272012-06-17 02:32:31 +0300580 * @return void
581 */
582 protected function _parse_attributes($attributes)
583 {
584 isset($attributes['rel']) OR $attributes['rel'] = TRUE;
585 $this->_link_types = ($attributes['rel'])
586 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
587 : array();
588 unset($attributes['rel']);
589
590 $this->_attributes = '';
591 foreach ($attributes as $key => $value)
592 {
593 $this->_attributes .= ' '.$key.'="'.$value.'"';
594 }
595 }
596
597 // --------------------------------------------------------------------
598
599 /**
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300600 * Add "rel" attribute
601 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300602 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev0fa95bd2012-11-01 23:33:14 +0200603 * @param string $type
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300604 * @return string
605 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300606 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300607 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300608 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300609 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300610 unset($this->_link_types[$type]);
611 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300612 }
613
Andrey Andreev88c47272012-06-17 02:32:31 +0300614 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300615 }
616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617}
Derek Allard2067d1a2008-11-13 22:59:24 +0000618
619/* End of file Pagination.php */
Andrey Andreev685cdd72012-07-13 20:11:13 +0300620/* Location: ./system/libraries/Pagination.php */