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',
22 // Current page number
23 protected $current_page;
26 protected $total_items;
28 // How many items to show per page
29 protected $items_per_page;
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
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
56 * Creates a new Pagination object.
58 * @param array configuration
61 public static function factory(array $config = array())
63 return new Pagination($config);
67 * Creates a new Pagination object.
69 * @param array configuration
72 public function __construct(array $config = array())
74 // Overwrite system defaults with application defaults
75 $this->config
= $this->config_group() +
$this->config
;
78 $this->setup($config);
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
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 switch ($this->config
['current_page']['source'])
142 $this->current_page
= isset($_GET[$this->config
['current_page']['key']])
143 ?
(int) $_GET[$this->config
['current_page']['key']]
148 $this->current_page
= (int) Request
::current()->param($this->config
['current_page']['key'], 1);
152 // Calculate and clean all pagination variables
153 $this->total_items
= (int) max(0, $this->config
['total_items']);
154 $this->items_per_page
= (int) max(1, $this->config
['items_per_page']);
155 $this->total_pages
= (int) ceil($this->total_items
/ $this->items_per_page
);
156 $this->current_page
= (int) min(max(1, $this->current_page
), max(1, $this->total_pages
));
157 $this->current_first_item
= (int) min((($this->current_page
- 1) * $this->items_per_page
) +
1, $this->total_items
);
158 $this->current_last_item
= (int) min($this->current_first_item +
$this->items_per_page
- 1, $this->total_items
);
159 $this->previous_page
= ($this->current_page
> 1) ?
$this->current_page
- 1 : FALSE;
160 $this->next_page
= ($this->current_page
< $this->total_pages
) ?
$this->current_page +
1 : FALSE;
161 $this->first_page
= ($this->current_page
=== 1) ?
FALSE : 1;
162 $this->last_page
= ($this->current_page
>= $this->total_pages
) ?
FALSE : $this->total_pages
;
163 $this->offset
= (int) (($this->current_page
- 1) * $this->items_per_page
);
171 * Generates the full URL for a certain page.
173 * @param integer page number
174 * @return string page URL
176 public function url($page = 1)
178 // Clean the page number
179 $page = max(1, (int) $page);
181 // No page number in URLs to first page
187 switch ($this->config
['current_page']['source'])
190 return URL
::site(Request
::current()->uri
).URL
::query(array($this->config
['current_page']['key'] => $page));
193 return URL
::site(Request
::current()->uri(array($this->config
['current_page']['key'] => $page))).URL
::query();
200 * Checks whether the given page number exists.
202 * @param integer page number
206 public function valid_page($page)
208 // Page number has to be a clean integer
209 if ( ! Validate
::digit($page))
212 return $page > 0 AND $page <= $this->total_pages
;
216 * Renders the pagination links.
218 * @param mixed string of the view to use, or a Kohana_View object
219 * @return string pagination output (HTML)
221 public function render($view = NULL)
223 // Automatically hide pagination whenever it is superfluous
224 if ($this->config
['auto_hide'] === TRUE AND $this->total_pages
<= 1)
229 // Use the view from config
230 $view = $this->config
['view'];
233 if ( ! $view instanceof Kohana_View
)
235 // Load the view file
236 $view = View
::factory($view);
239 // Pass on the whole Pagination object
240 return $view->set(get_object_vars($this))->set('page', $this)->render();
244 * Renders the pagination links.
246 * @return string pagination output (HTML)
248 public function __toString()
250 return $this->render();
254 * Returns a Pagination property.
256 * @param string URI of the request
257 * @return mixed Pagination property; NULL if not found
259 public function __get($key)
261 return isset($this->$key) ?
$this->$key : NULL;
265 * Updates a single config setting, and recalculates pagination if needed.
267 * @param string config key
268 * @param mixed config value
271 public function __set($key, $value)
273 $this->setup(array($key => $value));