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 // Only (re)calculate pagination when needed
112 if ($this->current_page
=== NULL
113 OR isset($config['current_page'])
114 OR isset($config['total_items'])
115 OR isset($config['items_per_page']))
117 // Retrieve the current page number
118 switch ($this->config
['current_page']['source'])
121 $this->current_page
= isset($_GET[$this->config
['current_page']['key']])
122 ?
(int) $_GET[$this->config
['current_page']['key']]
127 $this->current_page
= (int) Request
::instance()->param($this->config
['current_page']['key'], 1);
131 // Calculate and clean all pagination variables
132 $this->total_items
= (int) max(0, $this->config
['total_items']);
133 $this->items_per_page
= (int) max(1, $this->config
['items_per_page']);
134 $this->total_pages
= (int) ceil($this->total_items
/ $this->items_per_page
);
135 $this->current_page
= (int) min(max(1, $this->current_page
), max(1, $this->total_pages
));
136 $this->current_first_item
= (int) min((($this->current_page
- 1) * $this->items_per_page
) +
1, $this->total_items
);
137 $this->current_last_item
= (int) min($this->current_first_item +
$this->items_per_page
- 1, $this->total_items
);
138 $this->prev_page
= ($this->current_page
> 1) ?
$this->current_page
- 1 : FALSE;
139 $this->next_page
= ($this->current_page
< $this->total_pages
) ?
$this->current_page +
1 : FALSE;
140 $this->first_page
= ($this->current_page
=== 1) ?
FALSE : 1;
141 $this->last_page
= ($this->current_page
>= $this->total_pages
) ?
FALSE : $this->total_pages
;
149 * Generates the full URL for a certain page.
151 * @param integer page number
152 * @return string page URL
154 public function url($page = 1)
156 // Clean the page number
157 $page = max(1, (int) $page);
159 switch ($this->config
['current_page']['source'])
162 return URL
::site(Request
::instance()->uri
).URL
::query(array($this->config
['current_page']['key'] => $page));
165 return URL
::site(Request
::instance()->uri(array($this->config
['current_page']['key'] => $page))).URL
::query();
172 * Renders the pagination links.
174 * @param string view file to use; overrides config view setting
175 * @return string pagination output (HTML)
177 public function render($view = NULL)
179 // Automatically hide pagination whenever it is superfluous
180 if ($this->config
['auto_hide'] === TRUE AND $this->total_pages
<= 1)
185 // Use the view from config
186 $view = $this->config
['view'];
189 // Load the view file and pass on the whole Pagination object
190 return View
::factory($view, get_object_vars($this))->set('page', $this)->render();
194 * Renders the pagination links.
196 * @return string pagination output (HTML)
198 public function __toString()
200 return $this->render();
204 * Returns a Pagination property.
206 * @param string URI of the request
207 * @return mixed Pagination property; NULL if not found
209 public function __get($key)
211 return isset($this->$key) ?
$this->$key : NULL;
215 * Updates a single config setting, and recalculates pagination if needed.
217 * @param string config key
218 * @param mixed config value
221 public function __set($key, $value)
223 $this->setup(array($key => $value));