1 <?php
defined('SYSPATH') or die('No direct script access.');
3 * Pagination links generator.
7 * @copyright (c) 2008-2009 Kohana Team
8 * @license http://kohanaphp.com/license.html
10 class Kohana_Pagination
{
13 protected $config = array(
14 'current_page' => array('source' => 'query', 'key' => 'page'),
16 'items_per_page' => 10,
17 'view' => 'pagination/basic',
21 protected $current_page;
22 protected $total_items;
23 protected $items_per_page;
24 protected $total_pages;
25 protected $current_first_item;
26 protected $current_last_item;
29 protected $first_page;
33 * Creates a new Pagination object.
35 * @param array configuration
38 public static function factory(array $config = array())
40 return new Pagination($config);
44 * Creates a new Pagination object.
46 * @param array configuration
49 public function __construct(array $config = array())
51 // Overwrite system defaults with application defaults
52 $this->config
= array_merge($this->config
, $this->config_group());
55 $this->setup($config);
59 * Retrieves a pagination config group from the config file. One config group can
60 * refer to another as its parent, which will be recursively loaded.
62 * @param string pagination config group; "default" if none given
63 * @return array config settings
65 public function config_group($group = 'default')
67 // Load the pagination config file
68 $config_file = Kohana
::config('pagination');
70 // Initialize the $config array
71 $config['group'] = (string) $group;
73 // Recursively load requested config groups
74 while (isset($config['group']) AND isset($config_file->$config['group']))
76 // Temporarily store config group name
77 $group = $config['group'];
78 unset($config['group']);
80 // Add config group values, not overwriting existing keys
81 $config +
= $config_file->$group;
84 // Get rid of possible stray config group names
85 unset($config['group']);
87 // Return the $config array
92 * Loads configuration settings into the object and (re)calculates all
93 * pagination variables if needed.
94 * You can call this method to update any config settings after the object has
95 * already been created.
97 * @param array configuration
98 * @return object Pagination
100 public function setup(array $config = array())
102 if (isset($config['group']))
104 // Recursively load requested config groups
105 $config +
= $this->config_group($config['group']);
108 // Overwrite the current config settings
109 $this->config
= array_merge($this->config
, $config);
111 // Retrieve the current page number
112 if (isset($config['current_page']))
114 switch ($this->config
['current_page']['source'])
117 $this->current_page
= isset($_GET[$this->config
['current_page']['key']])
118 ?
(int) $_GET[$this->config
['current_page']['key']]
123 $this->current_page
= (int) Request
::instance()->param($this->config
['current_page']['key'], 1);
127 $this->current_page
= 1;
131 // Only (re)calculate pagination when needed
132 if (isset($config['current_page']) OR isset($config['total_items']) OR isset($config['items_per_page']))
134 // Calculate and clean all pagination variables
135 $this->total_items
= (int) max(0, $this->config
['total_items']);
136 $this->items_per_page
= (int) max(1, $this->config
['items_per_page']);
137 $this->total_pages
= (int) ceil($this->total_items
/ $this->items_per_page
);
138 $this->current_page
= (int) min(max(1, $this->current_page
), max(1, $this->total_pages
));
139 $this->current_first_item
= (int) min((($this->current_page
- 1) * $this->items_per_page
) +
1, $this->total_items
);
140 $this->current_last_item
= (int) min($this->current_first_item +
$this->items_per_page
- 1, $this->total_items
);
141 $this->prev_page
= ($this->current_page
> 1) ?
$this->current_page
- 1 : FALSE;
142 $this->next_page
= ($this->current_page
< $this->total_pages
) ?
$this->current_page +
1 : FALSE;
143 $this->first_page
= ($this->current_page
=== 1) ?
FALSE : 1;
144 $this->last_page
= ($this->current_page
>= $this->total_pages
) ?
FALSE : $this->total_pages
;
152 * Generates the full URL for a certain page.
154 * @param integer page number
155 * @return string page URL
157 public function url($page = 1)
159 // Clean the page number
160 $page = max(1, (int) $page);
162 switch ($this->config
['current_page']['source'])
165 return URL
::site(Request
::instance()->uri
).URL
::query(array($this->config
['current_page']['key'] => $page));
168 return URL
::site(Request
::instance()->uri(array($this->config
['current_page']['key'] => $page))).URL
::query();
175 * Renders the pagination links.
177 * @param string view file to use; overrides config view setting
178 * @return string pagination output (HTML)
180 public function render($view = NULL)
182 // Automatically hide pagination whenever it is superfluous
183 if ($this->config
['auto_hide'] === TRUE AND $this->total_pages
<= 1)
188 // Use the view from config
189 $view = $this->config
['view'];
192 // Load the view file and pass on the whole Pagination object
193 return View
::factory($view, get_object_vars($this))->set('page', $this)->render();
197 * Renders the pagination links.
199 * @return string pagination output (HTML)
201 public function __toString()
203 return $this->render();
207 * Returns a Pagination property.
209 * @param string URI of the request
210 * @return mixed Pagination property; NULL if not found
212 public function __get($key)
214 return isset($this->$key) ?
$this->$key : NULL;
218 * Updates a single config setting, and recalculates pagination if needed.
220 * @param string config key
221 * @param mixed config value
224 public function __set($key, $value)
226 $this->setup(array($key => $value));