blob: 7398c292d2108993976a2099e40cdf86ab3c7032 [file] [log] [blame]
Derek Jones4b9c6292011-07-01 17:40:48 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * 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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
28// ------------------------------------------------------------------------
29
30/**
31 * Pagination Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Pagination
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/pagination.html
38 */
39class CI_Pagination {
40
41 var $base_url = ''; // The page we are linking to
Robin Sowell2a6c1da2010-05-24 12:20:03 -040042 var $prefix = ''; // A custom prefix added to the path.
43 var $suffix = ''; // A custom suffix added to the path.
44
Derek Jones4b9c6292011-07-01 17:40:48 -050045 var $total_rows = 0; // Total number of items (database results)
Barry Mienydd671972010-10-04 16:33:58 +020046 var $per_page = 10; // Max number of items you want shown per page
Derek Jones4b9c6292011-07-01 17:40:48 -050047 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
48 var $cur_page = 0; // The current page being viewed
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -040049 var $use_page_numbers = FALSE; // Use page number for segment instead of offset
Barry Mienydd671972010-10-04 16:33:58 +020050 var $first_link = '&lsaquo; First';
Derek Allard2067d1a2008-11-13 22:59:24 +000051 var $next_link = '&gt;';
52 var $prev_link = '&lt;';
53 var $last_link = 'Last &rsaquo;';
54 var $uri_segment = 3;
55 var $full_tag_open = '';
56 var $full_tag_close = '';
57 var $first_tag_open = '';
58 var $first_tag_close = '&nbsp;';
59 var $last_tag_open = '&nbsp;';
60 var $last_tag_close = '';
Robin Sowell2a6c1da2010-05-24 12:20:03 -040061 var $first_url = ''; // Alternative URL for the First Page.
Derek Allard2067d1a2008-11-13 22:59:24 +000062 var $cur_tag_open = '&nbsp;<strong>';
63 var $cur_tag_close = '</strong>';
64 var $next_tag_open = '&nbsp;';
65 var $next_tag_close = '&nbsp;';
66 var $prev_tag_open = '&nbsp;';
67 var $prev_tag_close = '';
68 var $num_tag_open = '&nbsp;';
69 var $num_tag_close = '';
70 var $page_query_string = FALSE;
71 var $query_string_segment = 'per_page';
Derek Allarde01fd0f2010-07-05 11:06:07 -040072 var $display_pages = TRUE;
Derek Allard96bb75c2010-07-05 10:54:30 -040073 var $anchor_class = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000074
75 /**
76 * Constructor
77 *
78 * @access public
79 * @param array initialization parameters
80 */
Greg Akera9263282010-11-10 15:26:43 -060081 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000082 {
83 if (count($params) > 0)
84 {
Derek Allardfaac15e2009-03-07 17:17:58 +000085 $this->initialize($params);
Derek Allard2067d1a2008-11-13 22:59:24 +000086 }
Derek Allardfaac15e2009-03-07 17:17:58 +000087
Derek Allard96bb75c2010-07-05 10:54:30 -040088 if ($this->anchor_class != '')
89 {
90 $this->anchor_class = 'class="'.$this->anchor_class.'" ';
91 }
92
Derek Allard2067d1a2008-11-13 22:59:24 +000093 log_message('debug', "Pagination Class Initialized");
94 }
Derek Allardfaac15e2009-03-07 17:17:58 +000095
Derek Allard2067d1a2008-11-13 22:59:24 +000096 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +000097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 /**
99 * Initialize Preferences
100 *
101 * @access public
102 * @param array initialization parameters
103 * @return void
104 */
105 function initialize($params = array())
106 {
107 if (count($params) > 0)
108 {
109 foreach ($params as $key => $val)
110 {
111 if (isset($this->$key))
112 {
113 $this->$key = $val;
114 }
115 }
116 }
117 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 /**
122 * Generate the pagination links
123 *
124 * @access public
125 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000126 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 function create_links()
128 {
129 // If our item count or per-page total is zero there is no need to continue.
130 if ($this->total_rows == 0 OR $this->per_page == 0)
131 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000132 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
134
135 // Calculate the total number of pages
136 $num_pages = ceil($this->total_rows / $this->per_page);
137
138 // Is there only one page? Hm... nothing more to do here then.
139 if ($num_pages == 1)
140 {
141 return '';
142 }
143
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400144 // Set the base page index for starting page number
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400145 $base_page = ($this->use_page_numbers) ? 1 : 0;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400146
Derek Allardfaac15e2009-03-07 17:17:58 +0000147 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
151 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400152 if ($CI->input->get($this->query_string_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 {
154 $this->cur_page = $CI->input->get($this->query_string_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 // Prep the current page - no funny business!
157 $this->cur_page = (int) $this->cur_page;
158 }
159 }
160 else
161 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400162 if ($CI->uri->segment($this->uri_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 $this->cur_page = $CI->uri->segment($this->uri_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // Prep the current page - no funny business!
167 $this->cur_page = (int) $this->cur_page;
168 }
169 }
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400170
171 // Set current page to 1 if using page numbers instead of offset
172 if ($this->use_page_numbers AND $this->cur_page == 0)
173 {
174 $this->cur_page = $base_page;
175 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000176
177 $this->num_links = (int)$this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 if ($this->num_links < 1)
180 {
181 show_error('Your number of links must be a positive number.');
182 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 if ( ! is_numeric($this->cur_page))
185 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400186 $this->cur_page = $base_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000188
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 // Is the page number beyond the result range?
190 // If so we show the last page
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400191 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400193 if ($this->cur_page > $num_pages)
194 {
195 $this->cur_page = $num_pages;
196 }
197 }
198 else
199 {
200 if ($this->cur_page > $this->total_rows)
201 {
202 $this->cur_page = ($num_pages - 1) * $this->per_page;
203 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000205
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 $uri_page_number = $this->cur_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400207
208 if ( ! $this->use_page_numbers)
209 {
210 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
211 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000212
213 // Calculate the start and end numbers. These determine
214 // which number to start and end the digit links with
215 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
Derek Jones4b9c6292011-07-01 17:40:48 -0500216 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000217
Derek Jones4b9c6292011-07-01 17:40:48 -0500218 // Is pagination being used over GET or POST? If get, add a per_page query
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 // string. If post, add a trailing slash to the base URL if needed
220 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
221 {
222 $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
223 }
224 else
225 {
226 $this->base_url = rtrim($this->base_url, '/') .'/';
227 }
228
Barry Mienydd671972010-10-04 16:33:58 +0200229 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 $output = '';
231
232 // Render the "First" link
Derek Jones4b9c6292011-07-01 17:40:48 -0500233 if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400235 $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
Derek Allard96bb75c2010-07-05 10:54:30 -0400236 $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
238
239 // Render the "previous" link
Derek Jones4b9c6292011-07-01 17:40:48 -0500240 if ($this->prev_link !== FALSE AND $this->cur_page != 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400242 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200243
garthkerr48b23012011-09-21 20:52:50 -0300244 if (($i == 0 OR ($this->use_page_numbers && $i == 1)) AND $this->first_url != '')
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400245 {
Barry Mienydd671972010-10-04 16:33:58 +0200246 $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400247 }
248 else
249 {
250 $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
Derek Allard96bb75c2010-07-05 10:54:30 -0400251 $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400252 }
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 }
255
Derek Allarde01fd0f2010-07-05 11:06:07 -0400256 // Render the pages
257 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400259 // Write the digit links
260 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400262 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400263
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400264 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400266 if ($this->cur_page == $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400267 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400268 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400269 }
270 else
271 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400272 $n = ($i == $base_page) ? '' : $i;
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allarde01fd0f2010-07-05 11:06:07 -0400274 if ($n == '' && $this->first_url != '')
275 {
276 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
277 }
278 else
279 {
280 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allarde01fd0f2010-07-05 11:06:07 -0400282 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
283 }
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400284 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 }
286 }
287 }
288
289 // Render the "next" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400290 if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400292 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400293
294 $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 }
296
297 // Render the "Last" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400298 if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400300 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
301
Derek Allard96bb75c2010-07-05 10:54:30 -0400302 $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 }
304
Derek Jones4b9c6292011-07-01 17:40:48 -0500305 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // in the penultimate link so we'll kill all double slashes.
307 $output = preg_replace("#([^:])//+#", "\\1/", $output);
308
309 // Add the wrapper HTML if exists
310 $output = $this->full_tag_open.$output.$this->full_tag_close;
Derek Allardfaac15e2009-03-07 17:17:58 +0000311
312 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 }
314}
315// END Pagination Class
316
317/* End of file Pagination.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000318/* Location: ./system/libraries/Pagination.php */