blob: 47b718bd25e1b2cfe7f06cd66e2e68415cbd1ea3 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Pagination Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Pagination
24 * @author ExpressionEngine Dev Team
25 * @link http://codeigniter.com/user_guide/libraries/pagination.html
26 */
27class CI_Pagination {
28
29 var $base_url = ''; // The page we are linking to
Robin Sowell2a6c1da2010-05-24 12:20:03 -040030 var $prefix = ''; // A custom prefix added to the path.
31 var $suffix = ''; // A custom suffix added to the path.
32
Barry Mienydd671972010-10-04 16:33:58 +020033 var $total_rows = ''; // Total number of items (database results)
34 var $per_page = 10; // Max number of items you want shown per page
Derek Allard2067d1a2008-11-13 22:59:24 +000035 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
Barry Mienydd671972010-10-04 16:33:58 +020036 var $cur_page = 0; // The current page being viewed
37 var $first_link = '&lsaquo; First';
Derek Allard2067d1a2008-11-13 22:59:24 +000038 var $next_link = '&gt;';
39 var $prev_link = '&lt;';
40 var $last_link = 'Last &rsaquo;';
41 var $uri_segment = 3;
42 var $full_tag_open = '';
43 var $full_tag_close = '';
44 var $first_tag_open = '';
45 var $first_tag_close = '&nbsp;';
46 var $last_tag_open = '&nbsp;';
47 var $last_tag_close = '';
Robin Sowell2a6c1da2010-05-24 12:20:03 -040048 var $first_url = ''; // Alternative URL for the First Page.
Derek Allard2067d1a2008-11-13 22:59:24 +000049 var $cur_tag_open = '&nbsp;<strong>';
50 var $cur_tag_close = '</strong>';
51 var $next_tag_open = '&nbsp;';
52 var $next_tag_close = '&nbsp;';
53 var $prev_tag_open = '&nbsp;';
54 var $prev_tag_close = '';
55 var $num_tag_open = '&nbsp;';
56 var $num_tag_close = '';
57 var $page_query_string = FALSE;
58 var $query_string_segment = 'per_page';
Derek Allarde01fd0f2010-07-05 11:06:07 -040059 var $display_pages = TRUE;
Derek Allard96bb75c2010-07-05 10:54:30 -040060 var $anchor_class = '';
Derek Allard2067d1a2008-11-13 22:59:24 +000061
62 /**
63 * Constructor
64 *
65 * @access public
66 * @param array initialization parameters
67 */
Greg Akera9263282010-11-10 15:26:43 -060068 public function __construct($params = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000069 {
70 if (count($params) > 0)
71 {
Derek Allardfaac15e2009-03-07 17:17:58 +000072 $this->initialize($params);
Derek Allard2067d1a2008-11-13 22:59:24 +000073 }
Derek Allardfaac15e2009-03-07 17:17:58 +000074
Derek Allard96bb75c2010-07-05 10:54:30 -040075 if ($this->anchor_class != '')
76 {
77 $this->anchor_class = 'class="'.$this->anchor_class.'" ';
78 }
79
Derek Allard2067d1a2008-11-13 22:59:24 +000080 log_message('debug', "Pagination Class Initialized");
81 }
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 *
88 * @access public
89 * @param array initialization parameters
90 * @return void
91 */
92 function initialize($params = array())
93 {
94 if (count($params) > 0)
95 {
96 foreach ($params as $key => $val)
97 {
98 if (isset($this->$key))
99 {
100 $this->$key = $val;
101 }
102 }
103 }
104 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 /**
109 * Generate the pagination links
110 *
111 * @access public
112 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000113 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 function create_links()
115 {
116 // If our item count or per-page total is zero there is no need to continue.
117 if ($this->total_rows == 0 OR $this->per_page == 0)
118 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000119 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121
122 // Calculate the total number of pages
123 $num_pages = ceil($this->total_rows / $this->per_page);
124
125 // Is there only one page? Hm... nothing more to do here then.
126 if ($num_pages == 1)
127 {
128 return '';
129 }
130
Derek Allardfaac15e2009-03-07 17:17:58 +0000131 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
135 {
136 if ($CI->input->get($this->query_string_segment) != 0)
137 {
138 $this->cur_page = $CI->input->get($this->query_string_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000139
Derek Allard2067d1a2008-11-13 22:59:24 +0000140 // Prep the current page - no funny business!
141 $this->cur_page = (int) $this->cur_page;
142 }
143 }
144 else
145 {
146 if ($CI->uri->segment($this->uri_segment) != 0)
147 {
148 $this->cur_page = $CI->uri->segment($this->uri_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 // Prep the current page - no funny business!
151 $this->cur_page = (int) $this->cur_page;
152 }
153 }
154
155 $this->num_links = (int)$this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000156
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 if ($this->num_links < 1)
158 {
159 show_error('Your number of links must be a positive number.');
160 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if ( ! is_numeric($this->cur_page))
163 {
164 $this->cur_page = 0;
165 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // Is the page number beyond the result range?
168 // If so we show the last page
169 if ($this->cur_page > $this->total_rows)
170 {
171 $this->cur_page = ($num_pages - 1) * $this->per_page;
172 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 $uri_page_number = $this->cur_page;
175 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
176
177 // Calculate the start and end numbers. These determine
178 // which number to start and end the digit links with
179 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
180 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
181
182 // Is pagination being used over GET or POST? If get, add a per_page query
183 // string. If post, add a trailing slash to the base URL if needed
184 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
185 {
186 $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
187 }
188 else
189 {
190 $this->base_url = rtrim($this->base_url, '/') .'/';
191 }
192
Barry Mienydd671972010-10-04 16:33:58 +0200193 // And here we go...
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $output = '';
195
196 // Render the "First" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400197 if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400199 $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
Derek Allard96bb75c2010-07-05 10:54:30 -0400200 $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 +0000201 }
202
203 // Render the "previous" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400204 if ($this->prev_link !== FALSE AND $this->cur_page != 1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 $i = $uri_page_number - $this->per_page;
Barry Mienydd671972010-10-04 16:33:58 +0200207
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400208 if ($i == 0 && $this->first_url != '')
209 {
Barry Mienydd671972010-10-04 16:33:58 +0200210 $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 -0400211 }
212 else
213 {
214 $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
Derek Allard96bb75c2010-07-05 10:54:30 -0400215 $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 -0400216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 }
219
Derek Allarde01fd0f2010-07-05 11:06:07 -0400220 // Render the pages
221 if ($this->display_pages !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400223 // Write the digit links
224 for ($loop = $start -1; $loop <= $end; $loop++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400226 $i = ($loop * $this->per_page) - $this->per_page;
227
228 if ($i >= 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400230 if ($this->cur_page == $loop)
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400231 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400232 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400233 }
234 else
235 {
Derek Allarde01fd0f2010-07-05 11:06:07 -0400236 $n = ($i == 0) ? '' : $i;
Barry Mienydd671972010-10-04 16:33:58 +0200237
Derek Allarde01fd0f2010-07-05 11:06:07 -0400238 if ($n == '' && $this->first_url != '')
239 {
240 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
241 }
242 else
243 {
244 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allarde01fd0f2010-07-05 11:06:07 -0400246 $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
247 }
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400248 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 }
250 }
251 }
252
253 // Render the "next" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400254 if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
Derek Allard96bb75c2010-07-05 10:54:30 -0400256 $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 }
258
259 // Render the "Last" link
Derek Allard96bb75c2010-07-05 10:54:30 -0400260 if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 {
262 $i = (($num_pages * $this->per_page) - $this->per_page);
Derek Allard96bb75c2010-07-05 10:54:30 -0400263 $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 +0000264 }
265
266 // Kill double slashes. Note: Sometimes we can end up with a double slash
267 // in the penultimate link so we'll kill all double slashes.
268 $output = preg_replace("#([^:])//+#", "\\1/", $output);
269
270 // Add the wrapper HTML if exists
271 $output = $this->full_tag_open.$output.$this->full_tag_close;
Derek Allardfaac15e2009-03-07 17:17:58 +0000272
273 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 }
275}
276// END Pagination Class
277
278/* End of file Pagination.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000279/* Location: ./system/libraries/Pagination.php */