Big Pagination update.
[kohana-pagination/radio.git] / classes / kohana / pagination.php
blob3cb4a0da5adb418d1d50b56fd4be5f93e7f6b51c
1 <?php defined('SYSPATH') or die('No direct script access.');
2 /**
3 * Pagination links generator.
5 * @package Kohana
6 * @author Kohana Team
7 * @copyright (c) 2008-2009 Kohana Team
8 * @license http://kohanaphp.com/license.html
9 */
10 class Kohana_Pagination {
12 // System defaults
13 protected $config = array(
14 'current_page' => array('source' => 'query', 'key' => 'page'),
15 'total_items' => 0,
16 'items_per_page' => 10,
17 'view' => 'pagination/basic',
18 'auto_hide' => TRUE,
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;
27 protected $prev_page;
28 protected $next_page;
29 protected $first_page;
30 protected $last_page;
32 /**
33 * Creates a new Pagination object.
35 * @param array configuration
36 * @return Pagination
38 public static function factory(array $config = array())
40 return new Pagination($config);
43 /**
44 * Creates a new Pagination object.
46 * @param array configuration
47 * @return void
49 public function __construct(array $config = array())
51 // Overwrite system defaults with application defaults
52 $this->config = array_merge($this->config, $this->config_group());
54 // Pagination setup
55 $this->setup($config);
58 /**
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
88 return $config;
91 /**
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'])
116 case 'query':
117 $this->current_page = isset($_GET[$this->config['current_page']['key']])
118 ? (int) $_GET[$this->config['current_page']['key']]
119 : 1;
120 break;
122 case 'route':
123 $this->current_page = (int) Request::instance()->param($this->config['current_page']['key'], 1);
124 break;
126 default:
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;
147 // Chainable method
148 return $this;
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'])
164 case 'query':
165 return URL::site(Request::instance()->uri).URL::query(array($this->config['current_page']['key'] => $page));
167 case 'route':
168 return URL::site(Request::instance()->uri(array($this->config['current_page']['key'] => $page))).URL::query();
171 return '#';
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)
184 return '';
186 if ($view === NULL)
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
222 * @return void
224 public function __set($key, $value)
226 $this->setup(array($key => $value));
229 } // End Pagination