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 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'];
146 switch ($this->config
['current_page']['source'])
149 $this->current_page
= isset($_GET[$this->config
['current_page']['key']])
150 ?
(int) $_GET[$this->config
['current_page']['key']]
155 $this->current_page
= (int) Request
::current()->param($this->config
['current_page']['key'], 1);
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
);
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
195 switch ($this->config
['current_page']['source'])
198 return URL
::site(Request
::current()->uri
).URL
::query(array($this->config
['current_page']['key'] => $page));
201 return URL
::site(Request
::current()->uri(array($this->config
['current_page']['key'] => $page))).URL
::query();
208 * Checks whether the given page number exists.
210 * @param integer page number
214 public function valid_page($page)
216 // Page number has to be a clean integer
217 if ( ! Validate
::digit($page))
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)
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
279 public function __set($key, $value)
281 $this->setup(array($key => $value));