1 <?php
defined('SYSPATH') or die('No direct script access.');
3 * This Pagination class will create pagination links for you;
4 * however, it won't touch the data you are paginating.
8 * @copyright (c) 2008-2009 Kohana Team
9 * @license http://kohanaphp.com/license.html
11 class Kohana_Pagination
{
16 protected $query_string;
17 protected $items_per_page;
18 protected $total_items;
19 protected $total_pages;
20 protected $current_page;
21 protected $current_first_item;
22 protected $current_last_item;
27 * Creates a new Pagination object.
29 * @param array configuration
32 public static function factory(array $config = array())
34 return new Pagination($config);
38 * Creates a new Pagination object.
40 * @param array configuration
43 public function __construct(array $config = array())
45 if (isset($config['group']))
47 // Recursively load requested config groups
48 $config +
= $this->load_config($config['group']);
51 // Add default config values, not overwriting existing keys
52 $config +
= $this->load_config();
54 // Load config into object and calculate pagination variables
55 $this->config($config);
59 * Loads 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 name of the pagination config group
63 * @param boolean enable caching
64 * @return array configuration
66 public function load_config($group = 'default', $cache = TRUE)
68 // Load the pagination config file (object)
69 $config_file = Kohana
::config('pagination', $cache);
71 // Initialize the $config array
72 $config['group'] = $group;
74 // Recursively load requested config groups
75 while (isset($config['group']) AND isset($config_file->$config['group']))
77 // Temporarily store config group name
78 $group = $config['group'];
79 unset($config['group']);
81 // Add config group values, not overwriting existing keys
82 $config +
= $config_file->$group;
85 // Get rid of possible stray config group names
86 unset($config['group']);
88 // Return the $config array
93 * Loads configuration settings into the object and (re)calculates all
94 * pagination variables.
96 * @param array configuration
99 public function config(array $config = array())
101 if (isset($config['group']))
103 // Recursively load requested config groups
104 $config +
= $this->load_config($config['group']);
107 // Convert config array to object properties
108 foreach ($config as $key => $value)
110 $this->$key = $value;
113 if ($this->uri
=== NULL)
115 // Use the current URI by default
116 $this->uri
= Request
::instance()->uri
;
119 // Grab the current page number from the URL
120 $this->current_page
= isset($_GET[$this->query_string
]) ?
(int) $_GET[$this->query_string
] : 1;
122 // Clean up and calculate pagination variables
123 $this->total_items
= (int) max(0, $this->total_items
);
124 $this->items_per_page
= (int) max(1, $this->items_per_page
);
125 $this->total_pages
= (int) ceil($this->total_items
/ $this->items_per_page
);
126 $this->current_page
= (int) min(max(1, $this->current_page
), max(1, $this->total_pages
));
127 $this->current_first_item
= (int) min((($this->current_page
- 1) * $this->items_per_page
) +
1, $this->total_items
);
128 $this->current_last_item
= (int) min($this->current_first_item +
$this->items_per_page
- 1, $this->total_items
);
129 $this->prev_page
= ($this->current_page
> 1) ?
$this->current_page
- 1 : FALSE;
130 $this->next_page
= ($this->current_page
< $this->total_pages
) ?
$this->current_page +
1 : FALSE;
137 * Generates the full URL for a certain page.
139 * @param integer page number
140 * @return string page URL
142 public function url($page = 1)
144 // Clean the page number
145 $page = max(1, (int) $page);
148 return URL
::site($this->uri
).URL
::query(array($this->query_string
=> $page));
152 * Renders the pagination links.
154 * @param string view file to use; style
155 * @param boolean hide pagination for single pages
156 * @return string pagination output (HTML)
158 public function render($view = NULL, $auto_hide = NULL)
160 // Possibly overload config settings
161 $view = ($view === NULL) ?
$this->view
: $view;
162 $auto_hide = ($auto_hide === NULL) ?
$this->auto_hide
: $auto_hide;
164 // Automatically hide pagination whenever it is superfluous
165 if ($auto_hide === TRUE AND $this->total_pages
< 2)
168 // Load the view file and pass on the whole Pagination object
169 return View
::factory($view, get_object_vars($this))->set('page', $this)->render();
173 * Renders the pagination links.
175 * @return string pagination output (HTML)
177 public function __toString()
179 return $this->render();
183 * Returns a Pagination property.
185 * @param string URI of the request
186 * @return mixed Pagination property; NULL if not found
188 public function __get($key)
190 return isset($this->$key) ?
$this->$key : NULL;
194 * Updates a single config setting, and recalculates all pagination variables.
195 * Setting multiple config items should be done via the config() method.
197 * @param string config key
198 * @param mixed config value
201 public function __set($key, $value)
203 $this->config(array($key => $value));