blob: 0fe73d69f342ba500fbfb298647a641f3cd8b4ec [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
Eric Barnes9a4902a2011-12-03 23:46:06 -050039 protected $base_url = ''; // The page we are linking to
40 protected $prefix = ''; // A custom prefix added to the path.
41 protected $suffix = ''; // A custom suffix added to the path.
42 protected $total_rows = 0; // Total number of items (database results)
43 protected $per_page = 10; // Max number of items you want shown per page
44 protected $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
45 protected $cur_page = 0; // The current page being viewed
46 protected $use_page_numbers = FALSE; // Use page number for segment instead of offset
47 protected $first_link = '&lsaquo; First';
48 protected $next_link = '&gt;';
49 protected $prev_link = '&lt;';
50 protected $last_link = 'Last &rsaquo;';
51 protected $uri_segment = 3;
52 protected $full_tag_open = '';
53 protected $full_tag_close = '';
54 protected $first_tag_open = '';
55 protected $first_tag_close = '&nbsp;';
56 protected $last_tag_open = '&nbsp;';
57 protected $last_tag_close = '';
58 protected $first_url = ''; // Alternative URL for the First Page.
59 protected $cur_tag_open = '&nbsp;<strong>';
60 protected $cur_tag_close = '</strong>';
61 protected $next_tag_open = '&nbsp;';
62 protected $next_tag_close = '&nbsp;';
63 protected $prev_tag_open = '&nbsp;';
64 protected $prev_tag_close = '';
65 protected $num_tag_open = '&nbsp;';
66 protected $num_tag_close = '';
67 protected $page_query_string = FALSE;
68 protected $query_string_segment = 'per_page';
69 protected $display_pages = TRUE;
70 protected $anchor_class = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000071
72 /**
73 * Constructor
74 *
Derek Allard2067d1a2008-11-13 22:59:24 +000075 * @param array initialization parameters
76 */
Greg Akera9263282010-11-10 15:26:43 -060077 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000078 {
Eric Barnesbce41402011-12-03 23:47:41 -050079 $this->initialize($params);
Andrey Andreev796bc952012-03-26 19:40:40 +030080 log_message('debug', 'Pagination Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000081 }
Derek Allardfaac15e2009-03-07 17:17:58 +000082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +000084
Derek Allard2067d1a2008-11-13 22:59:24 +000085 /**
86 * Initialize Preferences
87 *
Derek Allard2067d1a2008-11-13 22:59:24 +000088 * @param array initialization parameters
89 * @return void
90 */
Eric Barnes9a4902a2011-12-03 23:46:06 -050091 public function initialize($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
93 if (count($params) > 0)
94 {
95 foreach ($params as $key => $val)
96 {
97 if (isset($this->$key))
98 {
99 $this->$key = $val;
100 }
101 }
102 }
Eric Barnesbce41402011-12-03 23:47:41 -0500103
104 if ($this->anchor_class != '')
105 {
106 $this->anchor_class = 'class="'.$this->anchor_class.'" ';
107 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000111
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 /**
113 * Generate the pagination links
114 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000116 */
Eric Barnes9a4902a2011-12-03 23:46:06 -0500117 public function create_links()
Derek Allard2067d1a2008-11-13 22:59:24 +0000118 {
119 // If our item count or per-page total is zero there is no need to continue.
120 if ($this->total_rows == 0 OR $this->per_page == 0)
121 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000122 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 }
124
125 // Calculate the total number of pages
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100126 $num_pages = (int) ceil($this->total_rows / $this->per_page);
Derek Allard2067d1a2008-11-13 22:59:24 +0000127
128 // Is there only one page? Hm... nothing more to do here then.
Ronald Beilsmacfb70212011-12-29 09:57:49 +0100129 if ($num_pages === 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 {
131 return '';
132 }
133
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400134 // Set the base page index for starting page number
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400135 $base_page = ($this->use_page_numbers) ? 1 : 0;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400136
Derek Allardfaac15e2009-03-07 17:17:58 +0000137 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000139
Eric Barnes3b376592012-01-04 00:28:27 -0500140 // See if we are using a prefix or suffix on links
Eric Barnes8d727f12012-01-04 00:06:36 -0500141 if ($this->prefix != '' OR $this->suffix != '')
142 {
Eric Barnes3b376592012-01-04 00:28:27 -0500143 $this->cur_page = (int) str_replace(array($this->prefix, $this->suffix), '', $CI->uri->segment($this->uri_segment));
Eric Barnes8d727f12012-01-04 00:06:36 -0500144 }
145
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
147 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400148 if ($CI->input->get($this->query_string_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
Andrey Andreev33987e62011-12-24 19:48:45 +0200150 $this->cur_page = (int) $CI->input->get($this->query_string_segment);
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 }
152 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300153 elseif ( ! $this->cur_page && $CI->uri->segment($this->uri_segment) != $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 {
Andrey Andreev33987e62011-12-24 19:48:45 +0200155 $this->cur_page = (int) $CI->uri->segment($this->uri_segment);
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
Eric Barnes9a4902a2011-12-03 23:46:06 -0500157
Andrey Andreev33987e62011-12-24 19:48:45 +0200158 // Set current page to 1 if it's not valid or if using page numbers instead of offset
Andrey Andreev796bc952012-03-26 19:40:40 +0300159 if ( ! is_numeric($this->cur_page) OR ($this->use_page_numbers && $this->cur_page == 0))
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400160 {
161 $this->cur_page = $base_page;
162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163
Eric Barnes3b376592012-01-04 00:28:27 -0500164 $this->num_links = (int) $this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 if ($this->num_links < 1)
167 {
168 show_error('Your number of links must be a positive number.');
169 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 // Is the page number beyond the result range?
172 // If so we show the last page
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400173 if ($this->use_page_numbers)
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400175 if ($this->cur_page > $num_pages)
176 {
177 $this->cur_page = $num_pages;
178 }
179 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300180 elseif ($this->cur_page > $this->total_rows)
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400181 {
Andrey Andreev796bc952012-03-26 19:40:40 +0300182 $this->cur_page = ($num_pages - 1) * $this->per_page;
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 $uri_page_number = $this->cur_page;
Eric Barnes9a4902a2011-12-03 23:46:06 -0500186
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400187 if ( ! $this->use_page_numbers)
188 {
189 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
190 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000191
192 // Calculate the start and end numbers. These determine
193 // which number to start and end the digit links with
Andrey Andreev796bc952012-03-26 19:40:40 +0300194 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
195 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
Derek Allard2067d1a2008-11-13 22:59:24 +0000196
Andrey Andreev796bc952012-03-26 19:40:40 +0300197 // Is pagination being used over GET or POST? If get, add a per_page query
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 // string. If post, add a trailing slash to the base URL if needed
199 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
200 {
201 $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
202 }
203 else
204 {
205 $this->base_url = rtrim($this->base_url, '/') .'/';
206 }
207
Barry Mienydd671972010-10-04 16:33:58 +0200208 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 $output = '';
210
211 // Render the "First" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300212 if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400214 $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
Derek Allard96bb75c2010-07-05 10:54:30 -0400215 $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 +0000216 }
217
218 // Render the "previous" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300219 if ($this->prev_link !== FALSE && $this->cur_page != 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400221 $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200222
Andrey Andreev796bc952012-03-26 19:40:40 +0300223 if ($i == $base_page && $this->first_url != '')
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400224 {
Barry Mienydd671972010-10-04 16:33:58 +0200225 $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 -0400226 }
227 else
228 {
dixy8bd8f682011-12-19 22:26:12 +0100229 $i = ($i == $base_page) ? '' : $this->prefix.$i.$this->suffix;
Derek Allard96bb75c2010-07-05 10:54:30 -0400230 $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 -0400231 }
Barry Mienydd671972010-10-04 16:33:58 +0200232
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 }
234
Derek Allarde01fd0f2010-07-05 11:06:07 -0400235 // Render the pages
236 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400238 // Write the digit links
239 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400241 $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page;
Derek Allarde01fd0f2010-07-05 11:06:07 -0400242
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400243 if ($i >= $base_page)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400245 if ($this->cur_page == $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400246 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400247 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400248 }
249 else
250 {
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400251 $n = ($i == $base_page) ? '' : $i;
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allarde01fd0f2010-07-05 11:06:07 -0400253 if ($n == '' && $this->first_url != '')
254 {
255 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
256 }
257 else
258 {
259 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allarde01fd0f2010-07-05 11:06:07 -0400261 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
262 }
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400263 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 }
265 }
266 }
267
268 // Render the "next" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300269 if ($this->next_link !== FALSE && $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400271 $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page;
Aaron Kuzemchak11c5f162011-09-03 20:59:07 -0400272
273 $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 +0000274 }
275
276 // Render the "Last" link
Andrey Andreev796bc952012-03-26 19:40:40 +0300277 if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 {
Aaron Kuzemchaka5e13f92011-09-04 16:39:47 -0400279 $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page;
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000280
Derek Allard96bb75c2010-07-05 10:54:30 -0400281 $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 +0000282 }
283
Phil Sturgeonc00a5a02011-11-22 15:25:32 +0000284 // Kill double slashes. Note: Sometimes we can end up with a double slash
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // in the penultimate link so we'll kill all double slashes.
Andrey Andreev796bc952012-03-26 19:40:40 +0300286 $output = preg_replace('#([^:])//+#', '\\1/', $output);
Derek Allard2067d1a2008-11-13 22:59:24 +0000287
288 // Add the wrapper HTML if exists
Andrey Andreev796bc952012-03-26 19:40:40 +0300289 return $this->full_tag_open.$output.$this->full_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 }
Andrey Andreev796bc952012-03-26 19:40:40 +0300291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292}
Derek Allard2067d1a2008-11-13 22:59:24 +0000293
294/* End of file Pagination.php */
Andrey Andreev796bc952012-03-26 19:40:40 +0300295/* Location: ./system/libraries/Pagination.php */