Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 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
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 24 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 25 | * @link http://codeigniter.com/user_guide/libraries/pagination.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 26 | */
|
| 27 | class 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 = '‹ First';
|
| 35 | var $next_link = '>';
|
| 36 | var $prev_link = '<';
|
| 37 | var $last_link = 'Last ›';
|
| 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 = ' ';
|
| 43 | var $last_tag_open = ' ';
|
| 44 | var $last_tag_close = '';
|
| 45 | var $cur_tag_open = ' <b>';
|
| 46 | var $cur_tag_close = '</b>';
|
| 47 | var $next_tag_open = ' ';
|
| 48 | var $next_tag_close = ' ';
|
| 49 | var $prev_tag_open = ' ';
|
| 50 | var $prev_tag_close = '';
|
| 51 | var $num_tag_open = ' ';
|
| 52 | var $num_tag_close = '';
|
Derek Allard | 8ddc0db | 2008-05-21 04:54:39 +0000 | [diff] [blame] | 53 | var $page_query_string = FALSE;
|
Derek Allard | c6238e9 | 2008-05-21 05:04:26 +0000 | [diff] [blame] | 54 | var $query_string_segment = 'per_page';
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 55 |
|
| 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 Allard | c6238e9 | 2008-05-21 05:04:26 +0000 | [diff] [blame] | 122 |
|
| 123 | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 124 | {
|
Derek Allard | c6238e9 | 2008-05-21 05:04:26 +0000 | [diff] [blame] | 125 | 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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 142 | }
|
Derek Allard | 825bc9b | 2008-01-16 23:37:42 +0000 | [diff] [blame] | 143 |
|
| 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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 150 |
|
Derek Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 151 | if ( ! is_numeric($this->cur_page))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 152 | {
|
| 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 Allard | 8ddc0db | 2008-05-21 04:54:39 +0000 | [diff] [blame] | 171 | // 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 Allard | c6238e9 | 2008-05-21 05:04:26 +0000 | [diff] [blame] | 175 | $this->base_url = rtrim($this->base_url).AMP.$this->query_string_segment.'=';
|
Derek Allard | 8ddc0db | 2008-05-21 04:54:39 +0000 | [diff] [blame] | 176 | }
|
| 177 | else
|
| 178 | {
|
| 179 | $this->base_url = rtrim($this->base_url, '/') .'/';
|
| 180 | }
|
Derek Allard | bbc5ec2 | 2007-05-02 11:34:08 +0000 | [diff] [blame] | 181 |
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 182 | // 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 Allard | f37fa6e | 2008-01-30 01:15:03 +0000 | [diff] [blame] | 192 | if ($this->cur_page != 1)
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 193 | {
|
| 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 Jones | 0b59f27 | 2008-05-13 04:22:33 +0000 | [diff] [blame] | 242 |
|
| 243 | /* End of file Pagination.php */
|
Derek Jones | a3ffbbb | 2008-05-11 18:18:29 +0000 | [diff] [blame] | 244 | /* Location: ./system/libraries/Pagination.php */ |