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