blob: 600aaa66383b4eb4387f4150d7abe9a4345f97d6 [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
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
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
Eric Barnes9a4902a2011-12-03 23:46:06 -050041 protected $base_url = ''; // The page we are linking to
42 protected $prefix = ''; // A custom prefix added to the path.
43 protected $suffix = ''; // A custom suffix added to the path.
44 protected $total_rows = 0; // Total number of items (database results)
45 protected $per_page = 10; // Max number of items you want shown per page
46 protected $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
47 protected $cur_page = 0; // The current page being viewed
48 protected $use_page_numbers = FALSE; // Use page number for segment instead of offset
49 protected $first_link = '&lsaquo; First';
50 protected $next_link = '&gt;';
51 protected $prev_link = '&lt;';
52 protected $last_link = 'Last &rsaquo;';
53 protected $uri_segment = 3;
54 protected $full_tag_open = '';
55 protected $full_tag_close = '';
56 protected $first_tag_open = '';
57 protected $first_tag_close = '&nbsp;';
58 protected $last_tag_open = '&nbsp;';
59 protected $last_tag_close = '';
60 protected $first_url = ''; // Alternative URL for the First Page.
61 protected $cur_tag_open = '&nbsp;<strong>';
62 protected $cur_tag_close = '</strong>';
63 protected $next_tag_open = '&nbsp;';
64 protected $next_tag_close = '&nbsp;';
65 protected $prev_tag_open = '&nbsp;';
66 protected $prev_tag_close = '';
67 protected $num_tag_open = '&nbsp;';
68 protected $num_tag_close = '';
69 protected $page_query_string = FALSE;
70 protected $query_string_segment = 'per_page';
71 protected $display_pages = TRUE;
72 protected $anchor_class = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000073
74 /**
75 * Constructor
76 *
77 * @access public
78 * @param array initialization parameters
79 */
Greg Akera9263282010-11-10 15:26:43 -060080 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000081 {
82 if (count($params) > 0)
83 {
Derek Allardfaac15e2009-03-07 17:17:58 +000084 $this->initialize($params);
Derek Allard2067d1a2008-11-13 22:59:24 +000085 }
Derek Allardfaac15e2009-03-07 17:17:58 +000086
Derek Allard96bb75c2010-07-05 10:54:30 -040087 if ($this->anchor_class != '')
88 {
89 $this->anchor_class = 'class="'.$this->anchor_class.'" ';
90 }
91
Derek Allard2067d1a2008-11-13 22:59:24 +000092 log_message('debug', "Pagination Class Initialized");
93 }
Derek Allardfaac15e2009-03-07 17:17:58 +000094
Derek Allard2067d1a2008-11-13 22:59:24 +000095 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +000096
Derek Allard2067d1a2008-11-13 22:59:24 +000097 /**
98 * Initialize Preferences
99 *
100 * @access public
101 * @param array initialization parameters
102 * @return void
103 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500104 public function initialize($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
106 if (count($params) > 0)
107 {
108 foreach ($params as $key => $val)
109 {
110 if (isset($this->$key))
111 {
112 $this->$key = $val;
113 }
114 }
115 }
116 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000117
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 /**
121 * Generate the pagination links
122 *
123 * @access public
124 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000125 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500126 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 {
128 // If our item count or per-page total is zero there is no need to continue.
129 if ($this->total_rows == 0 OR $this->per_page == 0)
130 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000131 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133
134 // Calculate the total number of pages
135 $num_pages = ceil($this->total_rows / $this->per_page);
136
137 // Is there only one page? Hm... nothing more to do here then.
138 if ($num_pages == 1)
139 {
140 return '';
141 }
142
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400143 // Set the base page index for starting page number
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400144 $base_page = ($this->use_page_numbers) ? 1 : 0;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400145
Derek Allardfaac15e2009-03-07 17:17:58 +0000146 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
150 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400151 if ($CI->input->get($this->query_string_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
153 $this->cur_page = $CI->input->get($this->query_string_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 // Prep the current page - no funny business!
156 $this->cur_page = (int) $this->cur_page;
157 }
158 }
159 else
160 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400161 if ($CI->uri->segment($this->uri_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 {
163 $this->cur_page = $CI->uri->segment($this->uri_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 // Prep the current page - no funny business!
166 $this->cur_page = (int) $this->cur_page;
167 }
168 }
Eric Barnes9a4902a2011-12-03 23:46:06 -0500169
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400170 // Set current page to 1 if using page numbers instead of offset
171 if ($this->use_page_numbers AND $this->cur_page == 0)
172 {
173 $this->cur_page = $base_page;
174 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000175
176 $this->num_links = (int)$this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000177
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 if ($this->num_links < 1)
179 {
180 show_error('Your number of links must be a positive number.');
181 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000182
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 if ( ! is_numeric($this->cur_page))
184 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400185 $this->cur_page = $base_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // Is the page number beyond the result range?
189 // If so we show the last page
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400190 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400192 if ($this->cur_page > $num_pages)
193 {
194 $this->cur_page = $num_pages;
195 }
196 }
197 else
198 {
199 if ($this->cur_page > $this->total_rows)
200 {
201 $this->cur_page = ($num_pages - 1) * $this->per_page;
202 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500206
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400207 if ( ! $this->use_page_numbers)
208 {
209 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
210 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000211
212 // Calculate the start and end numbers. These determine
213 // which number to start and end the digit links with
214 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
Derek Jones4b9c6292011-07-01 17:40:48 -0500215 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000216
Derek Jones4b9c6292011-07-01 17:40:48 -0500217 // Is pagination being used over GET or POST? If get, add a per_page query
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // string. If post, add a trailing slash to the base URL if needed
219 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
220 {
221 $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
222 }
223 else
224 {
225 $this->base_url = rtrim($this->base_url, '/') .'/';
226 }
227
Barry Mienydd671972010-10-04 16:33:58 +0200228 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 $output = '';
230
231 // Render the "First" link
Derek Jones4b9c6292011-07-01 17:40:48 -0500232 if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400234 $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
Derek Allard96bb75c2010-07-05 10:54:30 -0400235 $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 +0000236 }
237
238 // Render the "previous" link
Derek Jones4b9c6292011-07-01 17:40:48 -0500239 if ($this->prev_link !== FALSE AND $this->cur_page != 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400241 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200242
garthkerr48b23012011-09-21 20:52:50 -0300243 if (($i == 0 OR ($this->use_page_numbers && $i == 1)) AND $this->first_url != '')
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400244 {
Barry Mienydd671972010-10-04 16:33:58 +0200245 $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 -0400246 }
247 else
248 {
249 $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
Derek Allard96bb75c2010-07-05 10:54:30 -0400250 $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 -0400251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254
Derek Allarde01fd0f2010-07-05 11:06:07 -0400255 // Render the pages
256 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400258 // Write the digit links
259 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400261 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400262
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400263 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400265 if ($this->cur_page == $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400266 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400267 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400268 }
269 else
270 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400271 $n = ($i == $base_page) ? '' : $i;
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allarde01fd0f2010-07-05 11:06:07 -0400273 if ($n == '' && $this->first_url != '')
274 {
275 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
276 }
277 else
278 {
279 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
Barry Mienydd671972010-10-04 16:33:58 +0200280
Derek Allarde01fd0f2010-07-05 11:06:07 -0400281 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
282 }
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400283 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 }
285 }
286 }
287
288 // Render the "next" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400289 if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400291 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400292
293 $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 +0000294 }
295
296 // Render the "Last" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400297 if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000298 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400299 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000300
Derek Allard96bb75c2010-07-05 10:54:30 -0400301 $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 +0000302 }
303
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000304 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // in the penultimate link so we'll kill all double slashes.
306 $output = preg_replace("#([^:])//+#", "\\1/", $output);
307
308 // Add the wrapper HTML if exists
309 $output = $this->full_tag_open.$output.$this->full_tag_close;
Derek Allardfaac15e2009-03-07 17:17:58 +0000310
311 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 }
313}
314// END Pagination Class
315
316/* End of file Pagination.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000317/* Location: ./system/libraries/Pagination.php */