1 <?php
defined('SYSPATH') or die('No direct script access.');
3 * Pagination links generator.
5 * @package Kohana/Pagination
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'),
17 'items_per_page' => 10,
18 'view' => 'pagination/basic',
20 'first_page_in_url' => FALSE,
23 // Current page number
24 protected $current_page;
27 protected $total_items;
29 // How many items to show per page
30 protected $items_per_page;
33 protected $total_pages;
35 // Item offset for the first item displayed on the current page
36 protected $current_first_item;
38 // Item offset for the last item displayed on the current page
39 protected $current_last_item;
41 // Previous page number; FALSE if the current page is the first one
42 protected $previous_page;
44 // Next page number; FALSE if the current page is the last one
47 // First page number; FALSE if the current page is the first one
48 protected $first_page;
50 // Last page number; FALSE if the current page is the last one
57 * Creates a new Pagination object.
59 * @param array configuration
62 public static function factory(array $config = array())
64 return new Pagination($config);
68 * Creates a new Pagination object.
70 * @param array configuration
73 public function __construct(array $config = array())
75 // Overwrite system defaults with application defaults
76 $this->config
= $this->config_group() +
$this->config
;
79 $this->setup($config);
83 * Retrieves a pagination config group from the config file. One config group can
84 * refer to another as its parent, which will be recursively loaded.
86 * @param string pagination config group; "default" if none given
87 * @return array config settings
89 public function config_group($group = 'default')
91 // Load the pagination config file
92 $config_file = Kohana
::config('pagination');
94 // Initialize the $config array
95 $config['group'] = (string) $group;
97 // Recursively load requested config groups
98 while (isset($config['group']) AND isset($config_file->$config['group']))
100 // Temporarily store config group name
101 $group = $config['group'];
102 unset($config['group']);
104 // Add config group values, not overwriting existing keys
105 $config +
= $config_file->$group;
108 // Get rid of possible stray config group names
109 unset($config['group']);
111 // Return the merged config group settings
116 * Loads configuration settings into the object and (re)calculates pagination if needed.
117 * Allows you to update config settings after a Pagination object has been constructed.
119 * @param array configuration
120 * @return object Pagination
122 public function setup(array $config = array())
124 if (isset($config['group']))
126 // Recursively load requested config groups
127 $config +
= $this->config_group($config['group']);
130 // Overwrite the current config settings
131 $this->config
= $config +
$this->config
;
133 // Only (re)calculate pagination when needed
134 if ($this->current_page
=== NULL
135 OR isset($config['current_page'])
136 OR isset($config['total_items'])
137 OR isset($config['items_per_page']))
139 // Retrieve the current page number
140 if ( ! empty($this->config
['current_page']['page']))
142 // The current page number has been set manually
143 $this->current_page
= (int) $this->config
['current_page']['page'];
147 switch ($this->config
['current_page']['source'])
150 $this->current_page
= isset($_GET[$this->config
['current_page']['key']])
151 ?
(int) $_GET[$this->config
['current_page']['key']]
156 $this->current_page
= (int) Request
::current()->param($this->config
['current_page']['key'], 1);
161 // Calculate and clean all pagination variables
162 $this->total_items
= (int) max(0, $this->config
['total_items']);
163 $this->items_per_page
= (int) max(1, $this->config
['items_per_page']);
164 $this->total_pages
= (int) ceil($this->total_items
/ $this->items_per_page
);
165 $this->current_page
= (int) min(max(1, $this->current_page
), max(1, $this->total_pages
));
166 $this->current_first_item
= (int) min((($this->current_page
- 1) * $this->items_per_page
) +
1, $this->total_items
);
167 $this->current_last_item
= (int) min($this->current_first_item +
$this->items_per_page
- 1, $this->total_items
);
168 $this->previous_page
= ($this->current_page
> 1) ?
$this->current_page
- 1 : FALSE;
169 $this->next_page
= ($this->current_page
< $this->total_pages
) ?
$this->current_page +
1 : FALSE;
170 $this->first_page
= ($this->current_page
=== 1) ?
FALSE : 1;
171 $this->last_page
= ($this->current_page
>= $this->total_pages
) ?
FALSE : $this->total_pages
;
172 $this->offset
= (int) (($this->current_page
- 1) * $this->items_per_page
);
180 * Generates the full URL for a certain page.
182 * @param integer page number
183 * @return string page URL
185 public function url($page = 1)
187 // Clean the page number
188 $page = max(1, (int) $page);
190 // No page number in URLs to first page
191 if ($page === 1 AND ! $this->config
['first_page_in_url'])
196 switch ($this->config
['current_page']['source'])
199 return URL
::site(Request
::current()->uri
).URL
::query(array($this->config
['current_page']['key'] => $page));
202 return URL
::site(Request
::current()->uri(array($this->config
['current_page']['key'] => $page))).URL
::query();
209 * Checks whether the given page number exists.
211 * @param integer page number
215 public function valid_page($page)
217 // Page number has to be a clean integer
218 if ( ! Validate
::digit($page))
221 return $page > 0 AND $page <= $this->total_pages
;
225 * Renders the pagination links.
227 * @param mixed string of the view to use, or a Kohana_View object
228 * @return string pagination output (HTML)
230 public function render($view = NULL)
232 // Automatically hide pagination whenever it is superfluous
233 if ($this->config
['auto_hide'] === TRUE AND $this->total_pages
<= 1)
238 // Use the view from config
239 $view = $this->config
['view'];
242 if ( ! $view instanceof Kohana_View
)
244 // Load the view file
245 $view = View
::factory($view);
248 // Pass on the whole Pagination object
249 return $view->set(get_object_vars($this))->set('page', $this)->render();
253 * Renders the pagination links.
255 * @return string pagination output (HTML)
257 public function __toString()
259 return $this->render();
263 * Returns a Pagination property.
265 * @param string URI of the request
266 * @return mixed Pagination property; NULL if not found
268 public function __get($key)
270 return isset($this->$key) ?
$this->$key : NULL;
274 * Updates a single config setting, and recalculates pagination if needed.
276 * @param string config key
277 * @param mixed config value
280 public function __set($key, $value)
282 $this->setup(array($key => $value));