blob: 9a5a0bf62764b3d47c7effbbab6eb85a35435064 [file] [log] [blame]
Andrey Andreev33987e62011-12-24 19:48:45 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Pagination Class
30 *
31 * @package CodeIgniter
32 * @subpackage Libraries
33 * @category Pagination
Derek Jonesf4a4bd82011-10-20 12:18:42 -050034 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000035 * @link http://codeigniter.com/user_guide/libraries/pagination.html
36 */
37class CI_Pagination {
38
Andrey Andreev7eb7ebf2012-06-12 10:26:27 +030039 public $base_url = ''; // The page we are linking to
40 public $prefix = ''; // A custom prefix added to the path.
41 public $suffix = ''; // A custom suffix added to the path.
42 public $total_rows = 0; // Total number of items (database results)
43 public $per_page = 10; // Max number of items you want shown per page
44 public $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
45 public $cur_page = 0; // The current page being viewed
46 public $use_page_numbers = FALSE; // Use page number for segment instead of offset
47 public $first_link = '&lsaquo; First';
48 public $next_link = '&gt;';
49 public $prev_link = '&lt;';
50 public $last_link = 'Last &rsaquo;';
51 public $uri_segment = 3;
52 public $full_tag_open = '';
53 public $full_tag_close = '';
54 public $first_tag_open = '';
55 public $first_tag_close = '&nbsp;';
56 public $last_tag_open = '&nbsp;';
57 public $last_tag_close = '';
58 public $first_url = ''; // Alternative URL for the First Page.
59 public $cur_tag_open = '&nbsp;<strong>';
60 public $cur_tag_close = '</strong>';
61 public $next_tag_open = '&nbsp;';
62 public $next_tag_close = '&nbsp;';
63 public $prev_tag_open = '&nbsp;';
64 public $prev_tag_close = '';
65 public $num_tag_open = '&nbsp;';
66 public $num_tag_close = '';
67 public $page_query_string = FALSE;
68 public $query_string_segment = 'per_page';
69 public $display_pages = TRUE;
Andrey Andreev88c47272012-06-17 02:32:31 +030070 protected $_attributes = '';
71 protected $_link_types = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000072
73 /**
74 * Constructor
75 *
Derek Allard2067d1a2008-11-13 22:59:24 +000076 * @param array initialization parameters
Andrey Andreev56454792012-05-17 14:32:19 +030077 * @return void
Derek Allard2067d1a2008-11-13 22:59:24 +000078 */
Greg Akera9263282010-11-10 15:26:43 -060079 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000080 {
Eric Barnesbce41402011-12-03 23:47:41 -050081 $this->initialize($params);
Andrey Andreev796bc952012-03-26 19:40:40 +030082 log_message('debug', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000083 }
Derek Allardfaac15e2009-03-07 17:17:58 +000084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +000086
Derek Allard2067d1a2008-11-13 22:59:24 +000087 /**
88 * Initialize Preferences
89 *
Derek Allard2067d1a2008-11-13 22:59:24 +000090 * @param array initialization parameters
91 * @return void
92 */
Eric Barnes9a4902a2011-12-03 23:46:06 -050093 public function initialize($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Andrey Andreev88c47272012-06-17 02:32:31 +030095 $attributes = array();
96
97 if (isset($params['attributes']) && is_array($params['attributes']))
98 {
99 $attributes = $params['attributes'];
100 unset($params['attributes']);
101 }
102
103 // Deprecated legacy support for the anchor_class option
104 // Should be removed in CI 3.1+
105 if (isset($params['anchor_class']))
106 {
107 empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class'];
108 unset($params['anchor_class']);
109 }
110
111 $this->_parse_attributes($attributes);
112
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 if (count($params) > 0)
114 {
115 foreach ($params as $key => $val)
116 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300117 if (isset($this->$key))
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 $this->$key = $val;
120 }
121 }
122 }
123 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 /**
128 * Generate the pagination links
129 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000131 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500132 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 {
134 // If our item count or per-page total is zero there is no need to continue.
Alex Bilbied261b1e2012-06-02 11:12:16 +0100135 if ($this->total_rows === 0 OR $this->per_page === 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000137 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 }
139
140 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100141 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000142
143 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100144 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 {
146 return '';
147 }
148
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400149 // Set the base page index for starting page number
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400150 $base_page = ($this->use_page_numbers) ? 1 : 0;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400151
Derek Allardfaac15e2009-03-07 17:17:58 +0000152 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000154
Eric Barnes3b376592012-01-04 00:28:27 -0500155 // See if we are using a prefix or suffix on links
Alex Bilbied261b1e2012-06-02 11:12:16 +0100156 if ($this->prefix !== '' OR $this->suffix !== '')
Eric Barnes8d727f12012-01-04 00:06:36 -0500157 {
Eric Barnes3b376592012-01-04 00:28:27 -0500158 $this->cur_page = (int) str_replace(array($this->prefix, $this->suffix), '', $CI->uri->segment($this->uri_segment));
Eric Barnes8d727f12012-01-04 00:06:36 -0500159 }
160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
162 {
Andrey Andreev7eb7ebf2012-06-12 10:26:27 +0300163 if ($CI->input->get($this->query_string_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 {
Andrey Andreev33987e62011-12-24 19:48:45 +0200165 $this->cur_page = (int) $CI->input->get($this->query_string_segment);
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 }
167 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100168 elseif ( ! $this->cur_page && $CI->uri->segment($this->uri_segment) !== $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 {
Andrey Andreev33987e62011-12-24 19:48:45 +0200170 $this->cur_page = (int) $CI->uri->segment($this->uri_segment);
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 }
Eric Barnes9a4902a2011-12-03 23:46:06 -0500172
Andrey Andreev33987e62011-12-24 19:48:45 +0200173 // 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 +0100174 if ( ! is_numeric($this->cur_page) OR ($this->use_page_numbers && $this->cur_page === 0))
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400175 {
176 $this->cur_page = $base_page;
177 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000178
Eric Barnes3b376592012-01-04 00:28:27 -0500179 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 if ($this->num_links < 1)
182 {
183 show_error('Your number of links must be a positive number.');
184 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 // Is the page number beyond the result range?
187 // If so we show the last page
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400188 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400190 if ($this->cur_page > $num_pages)
191 {
192 $this->cur_page = $num_pages;
193 }
194 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300195 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400196 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300197 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500201
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400202 if ( ! $this->use_page_numbers)
203 {
204 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
205 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000206
207 // Calculate the start and end numbers. These determine
208 // which number to start and end the digit links with
Andrey Andreev796bc952012-03-26 19:40:40 +0300209 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
210 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
Andrey Andreev796bc952012-03-26 19:40:40 +0300212 // Is pagination being used over GET or POST? If get, add a per_page query
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 // string. If post, add a trailing slash to the base URL if needed
214 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
215 {
216 $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
217 }
218 else
219 {
220 $this->base_url = rtrim($this->base_url, '/') .'/';
221 }
222
Barry Mienydd671972010-10-04 16:33:58 +0200223 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 $output = '';
225
226 // Render the "First" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300227 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100229 $first_url = ($this->first_url === '') ? $this->base_url : $this->first_url;
Andrey Andreev88c47272012-06-17 02:32:31 +0300230 $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$this->_attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300231 .$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
233
234 // Render the "previous" link
Alex Bilbied261b1e2012-06-02 11:12:16 +0100235 if ($this->prev_link !== FALSE && $this->cur_page !== 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400237 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200238
Alex Bilbied261b1e2012-06-02 11:12:16 +0100239 if ($i === $base_page && $this->first_url !== '')
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400240 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300241 $output .= $this->prev_tag_open.'<a href="'.$this->first_url.'"'.$this->_attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300242 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400243 }
244 else
245 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100246 $i = ($i === $base_page) ? '' : $this->prefix.$i.$this->suffix;
Andrey Andreev88c47272012-06-17 02:32:31 +0300247 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'"'.$this->_attributes.$this->_attr_rel('prev').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300248 .$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400249 }
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 }
252
Derek Allarde01fd0f2010-07-05 11:06:07 -0400253 // Render the pages
254 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400256 // Write the digit links
257 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400259 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400260 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100262 if ($this->cur_page === $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400263 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400264 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400265 }
266 else
267 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100268 $n = ($i === $base_page) ? '' : $i;
Andrey Andreev88c47272012-06-17 02:32:31 +0300269 if ($n === '' && ! empty($this->first_url))
Derek Allarde01fd0f2010-07-05 11:06:07 -0400270 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300271 $output .= $this->num_tag_open.'<a href="'.$this->first_url.'"'.$this->_attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300272 .$loop.'</a>'.$this->num_tag_close;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400273 }
274 else
275 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100276 $n = ($n === '') ? '' : $this->prefix.$n.$this->suffix;
Andrey Andreev88c47272012-06-17 02:32:31 +0300277 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'"'.$this->_attributes.$this->_attr_rel('start').'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300278 .$loop.'</a>'.$this->num_tag_close;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400279 }
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400280 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 }
282 }
283 }
284
285 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300286 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400288 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400289
Andrey Andreev88c47272012-06-17 02:32:31 +0300290 $output .= $this->next_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$this->_attributes
291 .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 }
293
294 // Render the "Last" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300295 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400297 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000298
Andrey Andreev88c47272012-06-17 02:32:31 +0300299 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$this->_attributes.'>'
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300300 .$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 }
302
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000303 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 // in the penultimate link so we'll kill all double slashes.
Andrey Andreev796bc952012-03-26 19:40:40 +0300305 $output = preg_replace('#([^:])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000306
307 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300308 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300310
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300311 // --------------------------------------------------------------------
312
313 /**
Andrey Andreev88c47272012-06-17 02:32:31 +0300314 * Parse attributes
315 *
316 * @param array
317 * @return void
318 */
319 protected function _parse_attributes($attributes)
320 {
321 isset($attributes['rel']) OR $attributes['rel'] = TRUE;
322 $this->_link_types = ($attributes['rel'])
323 ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next')
324 : array();
325 unset($attributes['rel']);
326
327 $this->_attributes = '';
328 foreach ($attributes as $key => $value)
329 {
330 $this->_attributes .= ' '.$key.'="'.$value.'"';
331 }
332 }
333
334 // --------------------------------------------------------------------
335
336 /**
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300337 * Add "rel" attribute
338 *
Andrey Andreev88c47272012-06-17 02:32:31 +0300339 * @link http://www.w3.org/TR/html5/links.html#linkTypes
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300340 * @param string
341 * @return string
342 */
Andrey Andreev88c47272012-06-17 02:32:31 +0300343 protected function _attr_rel($type)
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300344 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300345 if (isset($this->_link_types[$type]))
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300346 {
Andrey Andreev88c47272012-06-17 02:32:31 +0300347 unset($this->_link_types[$type]);
348 return ' rel="'.$type.'"';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300349 }
350
Andrey Andreev88c47272012-06-17 02:32:31 +0300351 return '';
Andrey Andreev5a1e5e32012-06-12 11:28:26 +0300352 }
353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354}
Derek Allard2067d1a2008-11-13 22:59:24 +0000355
356/* End of file Pagination.php */
Andrey Andreev796bc952012-03-26 19:40:40 +0300357/* Location: ./system/libraries/Pagination.php */