blob: 000af10d2313e54e379fd8aacf62c4efcb1a2470 [file] [log] [blame]
Derek Jones0b59f272008-05-13 04:22:33 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allardd2df9bc2007-04-15 17:41:17 +00002/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
Derek Allard3d879d52008-01-18 19:41:32 +00008 * @author ExpressionEngine Dev Team
Derek Allardd2df9bc2007-04-15 17:41:17 +00009 * @copyright Copyright (c) 2006, EllisLab, Inc.
Derek Jones7a9193a2008-01-21 18:39:20 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
Derek Allardd2df9bc2007-04-15 17:41:17 +000012 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Pagination Class
20 *
21 * @package CodeIgniter
22 * @subpackage Libraries
23 * @category Pagination
Derek Allard3d879d52008-01-18 19:41:32 +000024 * @author ExpressionEngine Dev Team
Derek Jones7a9193a2008-01-21 18:39:20 +000025 * @link http://codeigniter.com/user_guide/libraries/pagination.html
Derek Allardd2df9bc2007-04-15 17:41:17 +000026 */
27class CI_Pagination {
28
29 var $base_url = ''; // The page we are linking to
30 var $total_rows = ''; // Total number of items (database results)
31 var $per_page = 10; // Max number of items you want shown per page
32 var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
33 var $cur_page = 0; // The current page being viewed
34 var $first_link = '&lsaquo; First';
35 var $next_link = '&gt;';
36 var $prev_link = '&lt;';
37 var $last_link = 'Last &rsaquo;';
38 var $uri_segment = 3;
39 var $full_tag_open = '';
40 var $full_tag_close = '';
41 var $first_tag_open = '';
42 var $first_tag_close = '&nbsp;';
43 var $last_tag_open = '&nbsp;';
44 var $last_tag_close = '';
45 var $cur_tag_open = '&nbsp;<b>';
46 var $cur_tag_close = '</b>';
47 var $next_tag_open = '&nbsp;';
48 var $next_tag_close = '&nbsp;';
49 var $prev_tag_open = '&nbsp;';
50 var $prev_tag_close = '';
51 var $num_tag_open = '&nbsp;';
52 var $num_tag_close = '';
Derek Allard8ddc0db2008-05-21 04:54:39 +000053 var $page_query_string = FALSE;
Derek Allardc6238e92008-05-21 05:04:26 +000054 var $query_string_segment = 'per_page';
Derek Allardd2df9bc2007-04-15 17:41:17 +000055
56 /**
57 * Constructor
58 *
59 * @access public
60 * @param array initialization parameters
61 */
62 function CI_Pagination($params = array())
63 {
64 if (count($params) > 0)
65 {
66 $this->initialize($params);
67 }
68
69 log_message('debug', "Pagination Class Initialized");
70 }
71
72 // --------------------------------------------------------------------
73
74 /**
75 * Initialize Preferences
76 *
77 * @access public
78 * @param array initialization parameters
79 * @return void
80 */
81 function initialize($params = array())
82 {
83 if (count($params) > 0)
84 {
85 foreach ($params as $key => $val)
86 {
87 if (isset($this->$key))
88 {
89 $this->$key = $val;
90 }
91 }
92 }
93 }
94
95 // --------------------------------------------------------------------
96
97 /**
98 * Generate the pagination links
99 *
100 * @access public
101 * @return string
102 */
103 function create_links()
104 {
105 // If our item count or per-page total is zero there is no need to continue.
106 if ($this->total_rows == 0 OR $this->per_page == 0)
107 {
108 return '';
109 }
110
111 // Calculate the total number of pages
112 $num_pages = ceil($this->total_rows / $this->per_page);
113
114 // Is there only one page? Hm... nothing more to do here then.
115 if ($num_pages == 1)
116 {
117 return '';
118 }
119
120 // Determine the current page number.
121 $CI =& get_instance();
Derek Allardc6238e92008-05-21 05:04:26 +0000122
123 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000124 {
Derek Allardc6238e92008-05-21 05:04:26 +0000125 if ($CI->input->get($this->query_string_segment) != 0)
126 {
127 $this->cur_page = $CI->input->get($this->query_string_segment);
128
129 // Prep the current page - no funny business!
130 $this->cur_page = (int) $this->cur_page;
131 }
132 }
133 else
134 {
135 if ($CI->uri->segment($this->uri_segment) != 0)
136 {
137 $this->cur_page = $CI->uri->segment($this->uri_segment);
138
139 // Prep the current page - no funny business!
140 $this->cur_page = (int) $this->cur_page;
141 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000142 }
Derek Allard825bc9b2008-01-16 23:37:42 +0000143
144 $this->num_links = (int)$this->num_links;
145
146 if ($this->num_links < 1)
147 {
148 show_error('Your number of links must be a positive number.');
149 }
Derek Allardd2df9bc2007-04-15 17:41:17 +0000150
Derek Jones0b59f272008-05-13 04:22:33 +0000151 if ( ! is_numeric($this->cur_page))
Derek Allardd2df9bc2007-04-15 17:41:17 +0000152 {
153 $this->cur_page = 0;
154 }
155
156 // Is the page number beyond the result range?
157 // If so we show the last page
158 if ($this->cur_page > $this->total_rows)
159 {
160 $this->cur_page = ($num_pages - 1) * $this->per_page;
161 }
162
163 $uri_page_number = $this->cur_page;
164 $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
165
166 // Calculate the start and end numbers. These determine
167 // which number to start and end the digit links with
168 $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
169 $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
170
Derek Allard8ddc0db2008-05-21 04:54:39 +0000171 // Is pagination being used over GET or POST? If get, add a per_page query
172 // string. If post, add a trailing slash to the base URL if needed
173 if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
174 {
Derek Allardc6238e92008-05-21 05:04:26 +0000175 $this->base_url = rtrim($this->base_url).AMP.$this->query_string_segment.'=';
Derek Allard8ddc0db2008-05-21 04:54:39 +0000176 }
177 else
178 {
179 $this->base_url = rtrim($this->base_url, '/') .'/';
180 }
Derek Allardbbc5ec22007-05-02 11:34:08 +0000181
Derek Allardd2df9bc2007-04-15 17:41:17 +0000182 // And here we go...
183 $output = '';
184
185 // Render the "First" link
186 if ($this->cur_page > $this->num_links)
187 {
188 $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
189 }
190
191 // Render the "previous" link
Derek Allardf37fa6e2008-01-30 01:15:03 +0000192 if ($this->cur_page != 1)
Derek Allardd2df9bc2007-04-15 17:41:17 +0000193 {
194 $i = $uri_page_number - $this->per_page;
195 if ($i == 0) $i = '';
196 $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
197 }
198
199 // Write the digit links
200 for ($loop = $start -1; $loop <= $end; $loop++)
201 {
202 $i = ($loop * $this->per_page) - $this->per_page;
203
204 if ($i >= 0)
205 {
206 if ($this->cur_page == $loop)
207 {
208 $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
209 }
210 else
211 {
212 $n = ($i == 0) ? '' : $i;
213 $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
214 }
215 }
216 }
217
218 // Render the "next" link
219 if ($this->cur_page < $num_pages)
220 {
221 $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
222 }
223
224 // Render the "Last" link
225 if (($this->cur_page + $this->num_links) < $num_pages)
226 {
227 $i = (($num_pages * $this->per_page) - $this->per_page);
228 $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
229 }
230
231 // Kill double slashes. Note: Sometimes we can end up with a double slash
232 // in the penultimate link so we'll kill all double slashes.
233 $output = preg_replace("#([^:])//+#", "\\1/", $output);
234
235 // Add the wrapper HTML if exists
236 $output = $this->full_tag_open.$output.$this->full_tag_close;
237
238 return $output;
239 }
240}
241// END Pagination Class
Derek Jones0b59f272008-05-13 04:22:33 +0000242
243/* End of file Pagination.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000244/* Location: ./system/libraries/Pagination.php */