blob: ccd3e8261156b8340d4bc61bb60def8f50063cba [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
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
Derek Allard2067d1a2008-11-13 22:59:24 +000033 var $total_rows = ''; // Total number of items (database results)
34 var $per_page = 10; // Max number of items you want shown per page
35 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
36 var $cur_page = 0; // The current page being viewed
37 var $first_link = '&lsaquo; First';
38 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';
59
60 /**
61 * Constructor
62 *
63 * @access public
64 * @param array initialization parameters
65 */
66 function CI_Pagination($params = array())
67 {
68 if (count($params) > 0)
69 {
Derek Allardfaac15e2009-03-07 17:17:58 +000070 $this->initialize($params);
Derek Allard2067d1a2008-11-13 22:59:24 +000071 }
Derek Allardfaac15e2009-03-07 17:17:58 +000072
Derek Allard2067d1a2008-11-13 22:59:24 +000073 log_message('debug', "Pagination Class Initialized");
74 }
Derek Allardfaac15e2009-03-07 17:17:58 +000075
Derek Allard2067d1a2008-11-13 22:59:24 +000076 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +000077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 /**
79 * Initialize Preferences
80 *
81 * @access public
82 * @param array initialization parameters
83 * @return void
84 */
85 function initialize($params = array())
86 {
87 if (count($params) > 0)
88 {
89 foreach ($params as $key => $val)
90 {
91 if (isset($this->$key))
92 {
93 $this->$key = $val;
94 }
95 }
96 }
97 }
Derek Allardfaac15e2009-03-07 17:17:58 +000098
Derek Allard2067d1a2008-11-13 22:59:24 +000099 // --------------------------------------------------------------------
Derek Allardfaac15e2009-03-07 17:17:58 +0000100
Derek Allard2067d1a2008-11-13 22:59:24 +0000101 /**
102 * Generate the pagination links
103 *
104 * @access public
105 * @return string
Derek Allardfaac15e2009-03-07 17:17:58 +0000106 */
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 function create_links()
108 {
109 // If our item count or per-page total is zero there is no need to continue.
110 if ($this->total_rows == 0 OR $this->per_page == 0)
111 {
Derek Allardfaac15e2009-03-07 17:17:58 +0000112 return '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 }
114
115 // Calculate the total number of pages
116 $num_pages = ceil($this->total_rows / $this->per_page);
117
118 // Is there only one page? Hm... nothing more to do here then.
119 if ($num_pages == 1)
120 {
121 return '';
122 }
123
Derek Allardfaac15e2009-03-07 17:17:58 +0000124 // Determine the current page number.
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 $CI =& get_instance();
Derek Allardfaac15e2009-03-07 17:17:58 +0000126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
128 {
129 if ($CI->input->get($this->query_string_segment) != 0)
130 {
131 $this->cur_page = $CI->input->get($this->query_string_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000132
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 // Prep the current page - no funny business!
134 $this->cur_page = (int) $this->cur_page;
135 }
136 }
137 else
138 {
139 if ($CI->uri->segment($this->uri_segment) != 0)
140 {
141 $this->cur_page = $CI->uri->segment($this->uri_segment);
Derek Allardfaac15e2009-03-07 17:17:58 +0000142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 // Prep the current page - no funny business!
144 $this->cur_page = (int) $this->cur_page;
145 }
146 }
147
148 $this->num_links = (int)$this->num_links;
Derek Allardfaac15e2009-03-07 17:17:58 +0000149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 if ($this->num_links < 1)
151 {
152 show_error('Your number of links must be a positive number.');
153 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 if ( ! is_numeric($this->cur_page))
156 {
157 $this->cur_page = 0;
158 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 // Is the page number beyond the result range?
161 // If so we show the last page
162 if ($this->cur_page > $this->total_rows)
163 {
164 $this->cur_page = ($num_pages - 1) * $this->per_page;
165 }
Derek Allardfaac15e2009-03-07 17:17:58 +0000166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $uri_page_number = $this->cur_page;
168 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
169
170 // Calculate the start and end numbers. These determine
171 // which number to start and end the digit links with
172 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
173 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
174
175 // Is pagination being used over GET or POST? If get, add a per_page query
176 // string. If post, add a trailing slash to the base URL if needed
177 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
178 {
179 $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
180 }
181 else
182 {
183 $this->base_url = rtrim($this->base_url, '/') .'/';
184 }
185
186 // And here we go...
187 $output = '';
188
189 // Render the "First" link
190 if ($this->cur_page > ($this->num_links + 1))
191 {
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400192 $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
193 $output .= $this->first_tag_open.'<a href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 }
195
196 // Render the "previous" link
197 if ($this->cur_page != 1)
198 {
199 $i = $uri_page_number - $this->per_page;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400200
201 if ($i == 0 && $this->first_url != '')
202 {
203 $output .= $this->prev_tag_open.'<a href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
204 }
205 else
206 {
207 $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
208 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
209 }
210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213 // Write the digit links
214 for ($loop = $start -1; $loop <= $end; $loop++)
215 {
216 $i = ($loop * $this->per_page) - $this->per_page;
Derek Allardfaac15e2009-03-07 17:17:58 +0000217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 if ($i >= 0)
219 {
220 if ($this->cur_page == $loop)
221 {
222 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
223 }
224 else
225 {
226 $n = ($i == 0) ? '' : $i;
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400227
228 if ($n == '' && $this->first_url != '')
229 {
230 $output .= $this->num_tag_open.'<a href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
231 }
232 else
233 {
234 $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
235
236 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
237 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000238 }
239 }
240 }
241
242 // Render the "next" link
243 if ($this->cur_page < $num_pages)
244 {
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400245 $output .= $this->next_tag_open.'<a 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 +0000246 }
247
248 // Render the "Last" link
249 if (($this->cur_page + $this->num_links) < $num_pages)
250 {
251 $i = (($num_pages * $this->per_page) - $this->per_page);
Robin Sowell2a6c1da2010-05-24 12:20:03 -0400252 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 }
254
255 // Kill double slashes. Note: Sometimes we can end up with a double slash
256 // in the penultimate link so we'll kill all double slashes.
257 $output = preg_replace("#([^:])//+#", "\\1/", $output);
258
259 // Add the wrapper HTML if exists
260 $output = $this->full_tag_open.$output.$this->full_tag_close;
Derek Allardfaac15e2009-03-07 17:17:58 +0000261
262 return $output;
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 }
264}
265// END Pagination Class
266
267/* End of file Pagination.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000268/* Location: ./system/libraries/Pagination.php */