Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 1 | <?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 Jones | 7f3719f | 2010-01-05 13:35:37 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 10 | * @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 | */ |
| 27 | class CI_Pagination { |
| 28 | |
| 29 | var $base_url = ''; // The page we are linking to |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 30 | var $prefix = ''; // A custom prefix added to the path. |
| 31 | var $suffix = ''; // A custom suffix added to the path. |
| 32 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 33 | var $total_rows = ''; // Total number of items (database results) |
| 34 | var $per_page = 10; // Max number of items you want shown per page |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 36 | var $cur_page = 0; // The current page being viewed |
| 37 | var $first_link = '‹ First'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | var $next_link = '>'; |
| 39 | var $prev_link = '<'; |
| 40 | var $last_link = 'Last ›'; |
| 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 = ' '; |
| 46 | var $last_tag_open = ' '; |
| 47 | var $last_tag_close = ''; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 48 | var $first_url = ''; // Alternative URL for the First Page. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | var $cur_tag_open = ' <strong>'; |
| 50 | var $cur_tag_close = '</strong>'; |
| 51 | var $next_tag_open = ' '; |
| 52 | var $next_tag_close = ' '; |
| 53 | var $prev_tag_open = ' '; |
| 54 | var $prev_tag_close = ''; |
| 55 | var $num_tag_open = ' '; |
| 56 | var $num_tag_close = ''; |
| 57 | var $page_query_string = FALSE; |
| 58 | var $query_string_segment = 'per_page'; |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 59 | var $display_pages = TRUE; |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 60 | var $anchor_class = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Constructor |
| 64 | * |
| 65 | * @access public |
| 66 | * @param array initialization parameters |
| 67 | */ |
| 68 | function CI_Pagination($params = array()) |
| 69 | { |
| 70 | if (count($params) > 0) |
| 71 | { |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 72 | $this->initialize($params); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 74 | |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 75 | if ($this->anchor_class != '') |
| 76 | { |
| 77 | $this->anchor_class = 'class="'.$this->anchor_class.'" '; |
| 78 | } |
| 79 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | log_message('debug', "Pagination Class Initialized"); |
| 81 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 82 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | // -------------------------------------------------------------------- |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 84 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | /** |
| 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 105 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 106 | // -------------------------------------------------------------------- |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 107 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Generate the pagination links |
| 110 | * |
| 111 | * @access public |
| 112 | * @return string |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 113 | */ |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 114 | 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 119 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | } |
| 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 131 | // Determine the current page number. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | $CI =& get_instance(); |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 133 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 139 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 140 | // 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 149 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 150 | // 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 156 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 157 | if ($this->num_links < 1) |
| 158 | { |
| 159 | show_error('Your number of links must be a positive number.'); |
| 160 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | if ( ! is_numeric($this->cur_page)) |
| 163 | { |
| 164 | $this->cur_page = 0; |
| 165 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 166 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 167 | // 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 173 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 174 | $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).'&'.$this->query_string_segment.'='; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | $this->base_url = rtrim($this->base_url, '/') .'/'; |
| 191 | } |
| 192 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 193 | // And here we go... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | $output = ''; |
| 195 | |
| 196 | // Render the "First" link |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 197 | if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | { |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 199 | $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url; |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 200 | $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | // Render the "previous" link |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 204 | if ($this->prev_link !== FALSE AND $this->cur_page != 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | { |
| 206 | $i = $uri_page_number - $this->per_page; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 207 | |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 208 | if ($i == 0 && $this->first_url != '') |
| 209 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 210 | $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 211 | } |
| 212 | else |
| 213 | { |
| 214 | $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix; |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 215 | $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 216 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 217 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 218 | } |
| 219 | |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 220 | // Render the pages |
| 221 | if ($this->display_pages !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 223 | // Write the digit links |
| 224 | for ($loop = $start -1; $loop <= $end; $loop++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 226 | $i = ($loop * $this->per_page) - $this->per_page; |
| 227 | |
| 228 | if ($i >= 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 229 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 230 | if ($this->cur_page == $loop) |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 231 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 232 | $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 233 | } |
| 234 | else |
| 235 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 236 | $n = ($i == 0) ? '' : $i; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 237 | |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 238 | 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 Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame^] | 245 | |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 246 | $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close; |
| 247 | } |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 248 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // Render the "next" link |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 254 | if ($this->next_link !== FALSE AND $this->cur_page < $num_pages) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 256 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | // Render the "Last" link |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 260 | if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | { |
| 262 | $i = (($num_pages * $this->per_page) - $this->per_page); |
Derek Allard | 96bb75c | 2010-07-05 10:54:30 -0400 | [diff] [blame] | 263 | $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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | } |
| 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 Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 272 | |
| 273 | return $output; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | // END Pagination Class |
| 277 | |
| 278 | /* End of file Pagination.php */ |
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 279 | /* Location: ./system/libraries/Pagination.php */ |