Better recalculation check in setup()
[kohana-pagination/radio.git] / classes / kohana / pagination.php
blobc6b85b62c895a5ae64a66a0a4732b96d138a2402
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 // 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'])
120 case 'query':
121 $this->current_page = isset($_GET[$this->config['current_page']['key']])
122 ? (int) $_GET[$this->config['current_page']['key']]
123 : 1;
124 break;
126 case 'route':
127 $this->current_page = (int) Request::instance()->param($this->config['current_page']['key'], 1);
128 break;
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;
144 // Chainable method
145 return $this;
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'])
161 case 'query':
162 return URL::site(Request::instance()->uri).URL::query(array($this->config['current_page']['key'] => $page));
164 case 'route':
165 return URL::site(Request::instance()->uri(array($this->config['current_page']['key'] => $page))).URL::query();
168 return '#';
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)
181 return '';
183 if ($view === NULL)
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
219 * @return void
221 public function __set($key, $value)
223 $this->setup(array($key => $value));
226 } // End Pagination