Andrey Andreev | 33987e6 | 2011-12-24 19:48:45 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * This source file is subject to the Open Software License (OSL 3.0) that is |
| 12 | * bundled with this package in the files license.txt / license.rst. It is |
| 13 | * also available through the world wide web at this URL: |
| 14 | * http://opensource.org/licenses/OSL-3.0 |
| 15 | * If you did not receive a copy of the license and are unable to obtain it |
| 16 | * through the world wide web, please send an email to |
| 17 | * licensing@ellislab.com so we can send you a copy immediately. |
| 18 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @link http://codeigniter.com |
| 24 | * @since Version 1.0 |
| 25 | * @filesource |
| 26 | */ |
| 27 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | /** |
| 29 | * Pagination Class |
| 30 | * |
| 31 | * @package CodeIgniter |
| 32 | * @subpackage Libraries |
| 33 | * @category Pagination |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 34 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 35 | * @link http://codeigniter.com/user_guide/libraries/pagination.html |
| 36 | */ |
| 37 | class CI_Pagination { |
| 38 | |
Dumk0 | 9e5a9b5 | 2012-06-19 12:02:27 +0300 | [diff] [blame] | 39 | public $base_url = ''; // The page we are linking to |
| 40 | public $prefix = ''; // A custom prefix added to the path. |
| 41 | public $suffix = ''; // A custom suffix added to the path. |
| 42 | public $total_rows = 0; // Total number of items (database results) |
| 43 | public $per_page = 10; // Max number of items you want shown per page |
| 44 | public $num_links = 2; // Number of "digit" links to show before/after the currently viewed page |
| 45 | public $cur_page = 0; // The current page being viewed |
| 46 | public $use_page_numbers = FALSE; // Use page number for segment instead of offset |
| 47 | public $first_link = '‹ First'; |
| 48 | public $next_link = '>'; |
| 49 | public $prev_link = '<'; |
| 50 | public $last_link = 'Last ›'; |
| 51 | public $uri_segment = 3; |
Andrey Andreev | 7eb7ebf | 2012-06-12 10:26:27 +0300 | [diff] [blame] | 52 | public $full_tag_open = ''; |
| 53 | public $full_tag_close = ''; |
| 54 | public $first_tag_open = ''; |
| 55 | public $first_tag_close = ' '; |
| 56 | public $last_tag_open = ' '; |
| 57 | public $last_tag_close = ''; |
Dumk0 | 9e5a9b5 | 2012-06-19 12:02:27 +0300 | [diff] [blame] | 58 | public $first_url = ''; // Alternative URL for the First Page. |
| 59 | public $cur_tag_open = ' <strong>'; |
Andrey Andreev | 7eb7ebf | 2012-06-12 10:26:27 +0300 | [diff] [blame] | 60 | public $cur_tag_close = '</strong>'; |
| 61 | public $next_tag_open = ' '; |
| 62 | public $next_tag_close = ' '; |
| 63 | public $prev_tag_open = ' '; |
| 64 | public $prev_tag_close = ''; |
Dumk0 | 9e5a9b5 | 2012-06-19 12:02:27 +0300 | [diff] [blame] | 65 | public $num_tag_open = ' '; |
Andrey Andreev | 7eb7ebf | 2012-06-12 10:26:27 +0300 | [diff] [blame] | 66 | public $num_tag_close = ''; |
| 67 | public $page_query_string = FALSE; |
Dumk0 | 9e5a9b5 | 2012-06-19 12:02:27 +0300 | [diff] [blame] | 68 | public $query_string_segment = 'per_page'; |
Andrey Andreev | 7eb7ebf | 2012-06-12 10:26:27 +0300 | [diff] [blame] | 69 | public $display_pages = TRUE; |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 70 | protected $_attributes = ''; |
| 71 | protected $_link_types = array(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Constructor |
| 75 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | * @param array initialization parameters |
Andrey Andreev | 5645479 | 2012-05-17 14:32:19 +0300 | [diff] [blame] | 77 | * @return void |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 78 | */ |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 79 | public function __construct($params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | { |
Eric Barnes | bce4140 | 2011-12-03 23:47:41 -0500 | [diff] [blame] | 81 | $this->initialize($params); |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 82 | log_message('debug', 'Pagination Class Initialized'); |
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 | // -------------------------------------------------------------------- |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 86 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | /** |
| 88 | * Initialize Preferences |
| 89 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 90 | * @param array initialization parameters |
| 91 | * @return void |
| 92 | */ |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 93 | public function initialize($params = array()) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 95 | $attributes = array(); |
| 96 | |
| 97 | if (isset($params['attributes']) && is_array($params['attributes'])) |
| 98 | { |
| 99 | $attributes = $params['attributes']; |
| 100 | unset($params['attributes']); |
| 101 | } |
| 102 | |
| 103 | // Deprecated legacy support for the anchor_class option |
| 104 | // Should be removed in CI 3.1+ |
| 105 | if (isset($params['anchor_class'])) |
| 106 | { |
| 107 | empty($params['anchor_class']) OR $attributes['class'] = $params['anchor_class']; |
| 108 | unset($params['anchor_class']); |
| 109 | } |
| 110 | |
| 111 | $this->_parse_attributes($attributes); |
| 112 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 113 | if (count($params) > 0) |
| 114 | { |
| 115 | foreach ($params as $key => $val) |
| 116 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 117 | if (isset($this->$key)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 118 | { |
| 119 | $this->$key = $val; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 124 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 125 | // -------------------------------------------------------------------- |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 126 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | /** |
| 128 | * Generate the pagination links |
| 129 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | * @return string |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 131 | */ |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 132 | public function create_links() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 133 | { |
| 134 | // If our item count or per-page total is zero there is no need to continue. |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 135 | if ($this->total_rows === 0 OR $this->per_page === 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 136 | { |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 137 | return ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | // Calculate the total number of pages |
Ronald Beilsma | cfb7021 | 2011-12-29 09:57:49 +0100 | [diff] [blame] | 141 | $num_pages = (int) ceil($this->total_rows / $this->per_page); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | |
| 143 | // Is there only one page? Hm... nothing more to do here then. |
Ronald Beilsma | cfb7021 | 2011-12-29 09:57:49 +0100 | [diff] [blame] | 144 | if ($num_pages === 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 145 | { |
| 146 | return ''; |
| 147 | } |
| 148 | |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 149 | // Set the base page index for starting page number |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 150 | $base_page = ($this->use_page_numbers) ? 1 : 0; |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 151 | |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 152 | // Determine the current page number. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 153 | $CI =& get_instance(); |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 154 | |
Eric Barnes | 3b37659 | 2012-01-04 00:28:27 -0500 | [diff] [blame] | 155 | // See if we are using a prefix or suffix on links |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 156 | if ($this->prefix !== '' OR $this->suffix !== '') |
Eric Barnes | 8d727f1 | 2012-01-04 00:06:36 -0500 | [diff] [blame] | 157 | { |
Eric Barnes | 3b37659 | 2012-01-04 00:28:27 -0500 | [diff] [blame] | 158 | $this->cur_page = (int) str_replace(array($this->prefix, $this->suffix), '', $CI->uri->segment($this->uri_segment)); |
Eric Barnes | 8d727f1 | 2012-01-04 00:06:36 -0500 | [diff] [blame] | 159 | } |
| 160 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 161 | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) |
| 162 | { |
Andrey Andreev | 7eb7ebf | 2012-06-12 10:26:27 +0300 | [diff] [blame] | 163 | if ($CI->input->get($this->query_string_segment) != $base_page) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | { |
Andrey Andreev | 33987e6 | 2011-12-24 19:48:45 +0200 | [diff] [blame] | 165 | $this->cur_page = (int) $CI->input->get($this->query_string_segment); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 168 | elseif ( ! $this->cur_page && $CI->uri->segment($this->uri_segment) !== $base_page) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 169 | { |
Andrey Andreev | 33987e6 | 2011-12-24 19:48:45 +0200 | [diff] [blame] | 170 | $this->cur_page = (int) $CI->uri->segment($this->uri_segment); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | } |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 172 | |
Andrey Andreev | 33987e6 | 2011-12-24 19:48:45 +0200 | [diff] [blame] | 173 | // Set current page to 1 if it's not valid or if using page numbers instead of offset |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 174 | if ( ! is_numeric($this->cur_page) OR ($this->use_page_numbers && $this->cur_page === 0)) |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 175 | { |
| 176 | $this->cur_page = $base_page; |
| 177 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 178 | |
Eric Barnes | 3b37659 | 2012-01-04 00:28:27 -0500 | [diff] [blame] | 179 | $this->num_links = (int) $this->num_links; |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 180 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 181 | if ($this->num_links < 1) |
| 182 | { |
| 183 | show_error('Your number of links must be a positive number.'); |
| 184 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 185 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | // Is the page number beyond the result range? |
| 187 | // If so we show the last page |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 188 | if ($this->use_page_numbers) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 189 | { |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 190 | if ($this->cur_page > $num_pages) |
| 191 | { |
| 192 | $this->cur_page = $num_pages; |
| 193 | } |
| 194 | } |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 195 | elseif ($this->cur_page > $this->total_rows) |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 196 | { |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 197 | $this->cur_page = ($num_pages - 1) * $this->per_page; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | } |
Derek Allard | faac15e | 2009-03-07 17:17:58 +0000 | [diff] [blame] | 199 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 200 | $uri_page_number = $this->cur_page; |
Eric Barnes | 9a4902a | 2011-12-03 23:46:06 -0500 | [diff] [blame] | 201 | |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 202 | if ( ! $this->use_page_numbers) |
| 203 | { |
| 204 | $this->cur_page = floor(($this->cur_page/$this->per_page) + 1); |
| 205 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | |
| 207 | // Calculate the start and end numbers. These determine |
| 208 | // which number to start and end the digit links with |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 209 | $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1; |
| 210 | $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 212 | // Is pagination being used over GET or POST? If get, add a per_page query |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 213 | // string. If post, add a trailing slash to the base URL if needed |
| 214 | if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) |
| 215 | { |
| 216 | $this->base_url = rtrim($this->base_url).'&'.$this->query_string_segment.'='; |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | $this->base_url = rtrim($this->base_url, '/') .'/'; |
| 221 | } |
| 222 | |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 223 | // And here we go... |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 224 | $output = ''; |
| 225 | |
| 226 | // Render the "First" link |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 227 | if ($this->first_link !== FALSE && $this->cur_page > ($this->num_links + 1)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 228 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 229 | $first_url = ($this->first_url === '') ? $this->base_url : $this->first_url; |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 230 | $output .= $this->first_tag_open.'<a href="'.$first_url.'"'.$this->_attributes.$this->_attr_rel('start').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 231 | .$this->first_link.'</a>'.$this->first_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | // Render the "previous" link |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 235 | if ($this->prev_link !== FALSE && $this->cur_page !== 1) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 236 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 237 | $i = ($this->use_page_numbers) ? $uri_page_number - 1 : $uri_page_number - $this->per_page; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 238 | |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 239 | if ($i === $base_page && $this->first_url !== '') |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 240 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 241 | $output .= $this->prev_tag_open.'<a href="'.$this->first_url.'"'.$this->_attributes.$this->_attr_rel('prev').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 242 | .$this->prev_link.'</a>'.$this->prev_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 243 | } |
| 244 | else |
| 245 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 246 | $i = ($i === $base_page) ? '' : $this->prefix.$i.$this->suffix; |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 247 | $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'"'.$this->_attributes.$this->_attr_rel('prev').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 248 | .$this->prev_link.'</a>'.$this->prev_tag_close; |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 249 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 250 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 253 | // Render the pages |
| 254 | if ($this->display_pages !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 256 | // Write the digit links |
| 257 | for ($loop = $start -1; $loop <= $end; $loop++) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 258 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 259 | $i = ($this->use_page_numbers) ? $loop : ($loop * $this->per_page) - $this->per_page; |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 260 | if ($i >= $base_page) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 261 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 262 | if ($this->cur_page === $loop) |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 263 | { |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 264 | $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 265 | } |
| 266 | else |
| 267 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 268 | $n = ($i === $base_page) ? '' : $i; |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 269 | if ($n === '' && ! empty($this->first_url)) |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 270 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 271 | $output .= $this->num_tag_open.'<a href="'.$this->first_url.'"'.$this->_attributes.$this->_attr_rel('start').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 272 | .$loop.'</a>'.$this->num_tag_close; |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 273 | } |
| 274 | else |
| 275 | { |
Alex Bilbie | d261b1e | 2012-06-02 11:12:16 +0100 | [diff] [blame] | 276 | $n = ($n === '') ? '' : $this->prefix.$n.$this->suffix; |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 277 | $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'"'.$this->_attributes.$this->_attr_rel('start').'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 278 | .$loop.'</a>'.$this->num_tag_close; |
Derek Allard | e01fd0f | 2010-07-05 11:06:07 -0400 | [diff] [blame] | 279 | } |
Robin Sowell | 2a6c1da | 2010-05-24 12:20:03 -0400 | [diff] [blame] | 280 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Render the "next" link |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 286 | if ($this->next_link !== FALSE && $this->cur_page < $num_pages) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 287 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 288 | $i = ($this->use_page_numbers) ? $this->cur_page + 1 : $this->cur_page * $this->per_page; |
Aaron Kuzemchak | 11c5f16 | 2011-09-03 20:59:07 -0400 | [diff] [blame] | 289 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 290 | $output .= $this->next_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$this->_attributes |
| 291 | .$this->_attr_rel('next').'>'.$this->next_link.'</a>'.$this->next_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Render the "Last" link |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 295 | if ($this->last_link !== FALSE && ($this->cur_page + $this->num_links) < $num_pages) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 296 | { |
Aaron Kuzemchak | a5e13f9 | 2011-09-04 16:39:47 -0400 | [diff] [blame] | 297 | $i = ($this->use_page_numbers) ? $num_pages : ($num_pages * $this->per_page) - $this->per_page; |
Phil Sturgeon | c00a5a0 | 2011-11-22 15:25:32 +0000 | [diff] [blame] | 298 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 299 | $output .= $this->last_tag_open.'<a href="'.$this->base_url.$this->prefix.$i.$this->suffix.'"'.$this->_attributes.'>' |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 300 | .$this->last_link.'</a>'.$this->last_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Phil Sturgeon | c00a5a0 | 2011-11-22 15:25:32 +0000 | [diff] [blame] | 303 | // Kill double slashes. Note: Sometimes we can end up with a double slash |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 304 | // in the penultimate link so we'll kill all double slashes. |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 305 | $output = preg_replace('#([^:])//+#', '\\1/', $output); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 306 | |
| 307 | // Add the wrapper HTML if exists |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 308 | return $this->full_tag_open.$output.$this->full_tag_close; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 309 | } |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 310 | |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 311 | // -------------------------------------------------------------------- |
| 312 | |
| 313 | /** |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 314 | * Parse attributes |
| 315 | * |
| 316 | * @param array |
| 317 | * @return void |
| 318 | */ |
| 319 | protected function _parse_attributes($attributes) |
| 320 | { |
| 321 | isset($attributes['rel']) OR $attributes['rel'] = TRUE; |
| 322 | $this->_link_types = ($attributes['rel']) |
| 323 | ? array('start' => 'start', 'prev' => 'prev', 'next' => 'next') |
| 324 | : array(); |
| 325 | unset($attributes['rel']); |
| 326 | |
| 327 | $this->_attributes = ''; |
| 328 | foreach ($attributes as $key => $value) |
| 329 | { |
| 330 | $this->_attributes .= ' '.$key.'="'.$value.'"'; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // -------------------------------------------------------------------- |
| 335 | |
| 336 | /** |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 337 | * Add "rel" attribute |
| 338 | * |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 339 | * @link http://www.w3.org/TR/html5/links.html#linkTypes |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 340 | * @param string |
| 341 | * @return string |
| 342 | */ |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 343 | protected function _attr_rel($type) |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 344 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 345 | if (isset($this->_link_types[$type])) |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 346 | { |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 347 | unset($this->_link_types[$type]); |
| 348 | return ' rel="'.$type.'"'; |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 349 | } |
| 350 | |
Andrey Andreev | 88c4727 | 2012-06-17 02:32:31 +0300 | [diff] [blame] | 351 | return ''; |
Andrey Andreev | 5a1e5e3 | 2012-06-12 11:28:26 +0300 | [diff] [blame] | 352 | } |
| 353 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 354 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 355 | |
| 356 | /* End of file Pagination.php */ |
Andrey Andreev | 796bc95 | 2012-03-26 19:40:40 +0300 | [diff] [blame] | 357 | /* Location: ./system/libraries/Pagination.php */ |