Fixed #2541. You can now manually set the current page number.
[kohana-pagination.git] / classes / kohana / pagination.php
blob86a72d8469c391f545c46f021eb12bfab0c401d4
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * Pagination links generator.
5 * @package Kohana/Pagination
6 * @category Base
7 * @author Kohana Team
8 * @copyright (c) 2008-2009 Kohana Team
9 * @license http://kohanaphp.com/license.html
11 class Kohana_Pagination {
13 // Merged configuration settings
14 protected $config = array(
15 'current_page' => array('source' => 'query_string', 'key' => 'page'),
16 'total_items' => 0,
17 'items_per_page' => 10,
18 'view' => 'pagination/basic',
19 'auto_hide' => TRUE,
22 // Current page number
23 protected $current_page;
25 // Total item count
26 protected $total_items;
28 // How many items to show per page
29 protected $items_per_page;
31 // Total page count
32 protected $total_pages;
34 // Item offset for the first item displayed on the current page
35 protected $current_first_item;
37 // Item offset for the last item displayed on the current page
38 protected $current_last_item;
40 // Previous page number; FALSE if the current page is the first one
41 protected $previous_page;
43 // Next page number; FALSE if the current page is the last one
44 protected $next_page;
46 // First page number; FALSE if the current page is the first one
47 protected $first_page;
49 // Last page number; FALSE if the current page is the last one
50 protected $last_page;
52 // Query offset
53 protected $offset;
55 /**
56 * Creates a new Pagination object.
58 * @param array configuration
59 * @return Pagination
61 public static function factory(array $config = array())
63 return new Pagination($config);
66 /**
67 * Creates a new Pagination object.
69 * @param array configuration
70 * @return void
72 public function __construct(array $config = array())
74 // Overwrite system defaults with application defaults
75 $this->config = $this->config_group() + $this->config;
77 // Pagination setup
78 $this->setup($config);
81 /**
82 * Retrieves a pagination config group from the config file. One config group can
83 * refer to another as its parent, which will be recursively loaded.
85 * @param string pagination config group; "default" if none given
86 * @return array config settings
88 public function config_group($group = 'default')
90 // Load the pagination config file
91 $config_file = Kohana::config('pagination');
93 // Initialize the $config array
94 $config['group'] = (string) $group;
96 // Recursively load requested config groups
97 while (isset($config['group']) AND isset($config_file->$config['group']))
99 // Temporarily store config group name
100 $group = $config['group'];
101 unset($config['group']);
103 // Add config group values, not overwriting existing keys
104 $config += $config_file->$group;
107 // Get rid of possible stray config group names
108 unset($config['group']);
110 // Return the merged config group settings
111 return $config;
115 * Loads configuration settings into the object and (re)calculates pagination if needed.
116 * Allows you to update config settings after a Pagination object has been constructed.
118 * @param array configuration
119 * @return object Pagination
121 public function setup(array $config = array())
123 if (isset($config['group']))
125 // Recursively load requested config groups
126 $config += $this->config_group($config['group']);
129 // Overwrite the current config settings
130 $this->config = $config + $this->config;
132 // Only (re)calculate pagination when needed
133 if ($this->current_page === NULL
134 OR isset($config['current_page'])
135 OR isset($config['total_items'])
136 OR isset($config['items_per_page']))
138 // Retrieve the current page number
139 if ( ! empty($this->config['current_page']['page']))
141 // The current page number has been set manually
142 $this->current_page = (int) $this->config['current_page']['page'];
144 else
146 switch ($this->config['current_page']['source'])
148 case 'query_string':
149 $this->current_page = isset($_GET[$this->config['current_page']['key']])
150 ? (int) $_GET[$this->config['current_page']['key']]
151 : 1;
152 break;
154 case 'route':
155 $this->current_page = (int) Request::current()->param($this->config['current_page']['key'], 1);
156 break;
160 // Calculate and clean all pagination variables
161 $this->total_items = (int) max(0, $this->config['total_items']);
162 $this->items_per_page = (int) max(1, $this->config['items_per_page']);
163 $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
164 $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
165 $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
166 $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
167 $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
168 $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
169 $this->first_page = ($this->current_page === 1) ? FALSE : 1;
170 $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
171 $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
174 // Chainable method
175 return $this;
179 * Generates the full URL for a certain page.
181 * @param integer page number
182 * @return string page URL
184 public function url($page = 1)
186 // Clean the page number
187 $page = max(1, (int) $page);
189 // No page number in URLs to first page
190 if ($page === 1)
192 $page = NULL;
195 switch ($this->config['current_page']['source'])
197 case 'query_string':
198 return URL::site(Request::current()->uri).URL::query(array($this->config['current_page']['key'] => $page));
200 case 'route':
201 return URL::site(Request::current()->uri(array($this->config['current_page']['key'] => $page))).URL::query();
204 return '#';
208 * Checks whether the given page number exists.
210 * @param integer page number
211 * @return boolean
212 * @since 3.0.7
214 public function valid_page($page)
216 // Page number has to be a clean integer
217 if ( ! Validate::digit($page))
218 return FALSE;
220 return $page > 0 AND $page <= $this->total_pages;
224 * Renders the pagination links.
226 * @param mixed string of the view to use, or a Kohana_View object
227 * @return string pagination output (HTML)
229 public function render($view = NULL)
231 // Automatically hide pagination whenever it is superfluous
232 if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
233 return '';
235 if ($view === NULL)
237 // Use the view from config
238 $view = $this->config['view'];
241 if ( ! $view instanceof Kohana_View)
243 // Load the view file
244 $view = View::factory($view);
247 // Pass on the whole Pagination object
248 return $view->set(get_object_vars($this))->set('page', $this)->render();
252 * Renders the pagination links.
254 * @return string pagination output (HTML)
256 public function __toString()
258 return $this->render();
262 * Returns a Pagination property.
264 * @param string URI of the request
265 * @return mixed Pagination property; NULL if not found
267 public function __get($key)
269 return isset($this->$key) ? $this->$key : NULL;
273 * Updates a single config setting, and recalculates pagination if needed.
275 * @param string config key
276 * @param mixed config value
277 * @return void
279 public function __set($key, $value)
281 $this->setup(array($key => $value));
284 } // End Pagination