PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Kohana/Pagination.php

https://github.com/sortex/kohana-pagination
PHP | 413 lines | 201 code | 68 blank | 144 comment | 17 complexity | cefcd0d59a5f4bde7f95d4f47af53499 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * Pagination links generator.
  4. *
  5. * @package Kohana/Pagination
  6. * @category Base
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license.html
  10. */
  11. class Kohana_Pagination {
  12. // Merged configuration settings
  13. protected $config = array(
  14. 'current_page' => array('source' => 'query_string', 'key' => 'page'),
  15. 'total_items' => 0,
  16. 'items_per_page' => 10,
  17. 'view' => 'pagination/basic',
  18. 'auto_hide' => TRUE,
  19. 'first_page_in_url' => FALSE,
  20. );
  21. // Current page number
  22. protected $current_page;
  23. // Total item count
  24. protected $total_items;
  25. // How many items to show per page
  26. protected $items_per_page;
  27. // Total page count
  28. protected $total_pages;
  29. // Item offset for the first item displayed on the current page
  30. protected $current_first_item;
  31. // Item offset for the last item displayed on the current page
  32. protected $current_last_item;
  33. // Previous page number; FALSE if the current page is the first one
  34. protected $previous_page;
  35. // Next page number; FALSE if the current page is the last one
  36. protected $next_page;
  37. // First page number; FALSE if the current page is the first one
  38. protected $first_page;
  39. // Last page number; FALSE if the current page is the last one
  40. protected $last_page;
  41. // Query offset
  42. protected $offset;
  43. // Route to use for URIs
  44. protected $route;
  45. // Parameters to use with Route to create URIs
  46. protected $route_params = array();
  47. // Request object
  48. protected $request;
  49. /**
  50. * Creates a new Pagination object.
  51. *
  52. * @param array configuration
  53. * @return Pagination
  54. */
  55. public static function factory(array $config = array(), Request $request = NULL)
  56. {
  57. return new Pagination($config, $request);
  58. }
  59. /**
  60. * Creates a new Pagination object.
  61. *
  62. * @param array configuration
  63. * @return void
  64. * @todo Assign Request::current() instead in 3.2
  65. */
  66. public function __construct(array $config = array(), Request $request = NULL)
  67. {
  68. // Overwrite system defaults with application defaults
  69. $this->config = $this->config_group() + $this->config;
  70. if ($request === NULL)
  71. {
  72. $request = Request::initial();
  73. }
  74. $this->request = $request;
  75. $this->route = $request->route();
  76. // Assign default route params
  77. $this->route_params = array(
  78. 'directory' => $request->directory(),
  79. 'controller' => $request->controller(),
  80. 'action' => $request->action(),
  81. ) + $request->param();
  82. // Pagination setup
  83. $this->setup($config);
  84. }
  85. /**
  86. * Retrieves a pagination config group from the config file. One config group can
  87. * refer to another as its parent, which will be recursively loaded.
  88. *
  89. * @param string pagination config group; "default" if none given
  90. * @return array config settings
  91. */
  92. public function config_group($group = 'default')
  93. {
  94. // Load the pagination config file
  95. $config_file = Kohana::$config->load('pagination');
  96. // Initialize the $config array
  97. $config['group'] = (string) $group;
  98. // Recursively load requested config groups
  99. while (isset($config['group']) AND isset($config_file->$config['group']))
  100. {
  101. // Temporarily store config group name
  102. $group = $config['group'];
  103. unset($config['group']);
  104. // Add config group values, not overwriting existing keys
  105. $config += $config_file->$group;
  106. }
  107. // Get rid of possible stray config group names
  108. unset($config['group']);
  109. // Return the merged config group settings
  110. return $config;
  111. }
  112. /**
  113. * Loads configuration settings into the object and (re)calculates pagination if needed.
  114. * Allows you to update config settings after a Pagination object has been constructed.
  115. *
  116. * @param array configuration
  117. * @return object Pagination
  118. */
  119. public function setup(array $config = array())
  120. {
  121. if (isset($config['group']))
  122. {
  123. // Recursively load requested config groups
  124. $config += $this->config_group($config['group']);
  125. }
  126. // Overwrite the current config settings
  127. $this->config = $config + $this->config;
  128. // Only (re)calculate pagination when needed
  129. if ($this->current_page === NULL
  130. OR isset($config['current_page'])
  131. OR isset($config['total_items'])
  132. OR isset($config['items_per_page']))
  133. {
  134. // Retrieve the current page number
  135. if ( ! empty($this->config['current_page']['page']))
  136. {
  137. // The current page number has been set manually
  138. $this->current_page = (int) $this->config['current_page']['page'];
  139. }
  140. else
  141. {
  142. $query_key = $this->config['current_page']['key'];
  143. switch ($this->config['current_page']['source'])
  144. {
  145. case 'query_string':
  146. case 'mixed':
  147. $this->current_page = ($this->request->query($query_key) !== NULL)
  148. ? (int) $this->request->query($query_key)
  149. : 1;
  150. break;
  151. case 'route':
  152. $this->current_page = (int) $this->request->param($query_key, 1);
  153. break;
  154. }
  155. }
  156. // Calculate and clean all pagination variables
  157. $this->total_items = (int) max(0, $this->config['total_items']);
  158. $this->items_per_page = (int) max(1, $this->config['items_per_page']);
  159. $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
  160. $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
  161. $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
  162. $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
  163. $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
  164. $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
  165. $this->first_page = ($this->current_page === 1) ? FALSE : 1;
  166. $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
  167. $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
  168. }
  169. // Chainable method
  170. return $this;
  171. }
  172. /**
  173. * Generates the full URL for a certain page.
  174. *
  175. * @param integer page number
  176. * @return string page URL
  177. */
  178. public function url($page = 1)
  179. {
  180. // Clean the page number
  181. $page = max(1, (int) $page);
  182. // No page number in URLs to first page
  183. if ($page === 1 AND ! $this->config['first_page_in_url'])
  184. {
  185. $page = NULL;
  186. }
  187. switch ($this->config['current_page']['source'])
  188. {
  189. case 'query_string':
  190. case 'mixed':
  191. return URL::site($this->route->uri($this->route_params).
  192. $this->query(array($this->config['current_page']['key'] => $page)));
  193. case 'route':
  194. return URL::site($this->route->uri(array_merge($this->route_params,
  195. array($this->config['current_page']['key'] => $page))).$this->query());
  196. }
  197. return '#';
  198. }
  199. /**
  200. * Checks whether the given page number exists.
  201. *
  202. * @param integer page number
  203. * @return boolean
  204. * @since 3.0.7
  205. */
  206. public function valid_page($page)
  207. {
  208. // Page number has to be a clean integer
  209. if ( ! Valid::digit($page))
  210. return FALSE;
  211. return $page > 0 AND $page <= $this->total_pages;
  212. }
  213. /**
  214. * Renders the pagination links.
  215. *
  216. * @param mixed string of the view to use, or a Kohana_View object
  217. * @return string pagination output (HTML)
  218. */
  219. public function render($view = NULL)
  220. {
  221. // Automatically hide pagination whenever it is superfluous
  222. if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
  223. return '';
  224. if ($view === NULL)
  225. {
  226. // Use the view from config
  227. $view = $this->config['view'];
  228. }
  229. if ( ! $view instanceof View)
  230. {
  231. // Load the view file
  232. $view = View::factory($view);
  233. }
  234. // Pass on the whole Pagination object
  235. return $view->set(get_object_vars($this))->set('page', $this)->render();
  236. }
  237. /**
  238. * Request setter / getter
  239. *
  240. * @param Request
  241. * @return Request If used as getter
  242. * @return $this Chainable as setter
  243. */
  244. public function request(Request $request = NULL)
  245. {
  246. if ($request === NULL)
  247. return $this->request;
  248. $this->request = $request;
  249. return $this;
  250. }
  251. /**
  252. * Route setter / getter
  253. *
  254. * @param Route
  255. * @return Route Route if used as getter
  256. * @return $this Chainable as setter
  257. */
  258. public function route(Route $route = NULL)
  259. {
  260. if ($route === NULL)
  261. return $this->route;
  262. $this->route = $route;
  263. return $this;
  264. }
  265. /**
  266. * Route parameters setter / getter
  267. *
  268. * @param array Route parameters to set
  269. * @return array Route parameters if used as getter
  270. * @return $this Chainable as setter
  271. */
  272. public function route_params(array $route_params = NULL)
  273. {
  274. if ($route_params === NULL)
  275. return $this->route_params;
  276. $this->route_params = $route_params;
  277. return $this;
  278. }
  279. /**
  280. * URL::query() replacement for Pagination use only
  281. *
  282. * @param array Parameters to override
  283. * @return string
  284. */
  285. public function query(array $params = NULL)
  286. {
  287. if ($params === NULL)
  288. {
  289. // Use only the current parameters
  290. $params = $this->request->query();
  291. }
  292. else
  293. {
  294. // Merge the current and new parameters
  295. $params = array_merge($this->request->query(), $params);
  296. }
  297. if (empty($params))
  298. {
  299. // No query parameters
  300. return '';
  301. }
  302. // Note: http_build_query returns an empty string for a params array with only NULL values
  303. $query = http_build_query($params, '', '&');
  304. // Don't prepend '?' to an empty string
  305. return ($query === '') ? '' : ('?'.$query);
  306. }
  307. /**
  308. * Renders the pagination links.
  309. *
  310. * @return string pagination output (HTML)
  311. */
  312. public function __toString()
  313. {
  314. try
  315. {
  316. return $this->render();
  317. }
  318. catch(Exception $e)
  319. {
  320. Kohana_Exception::handler($e);
  321. return '';
  322. }
  323. }
  324. /**
  325. * Returns a Pagination property.
  326. *
  327. * @param string property name
  328. * @return mixed Pagination property; NULL if not found
  329. */
  330. public function __get($key)
  331. {
  332. return isset($this->$key) ? $this->$key : NULL;
  333. }
  334. /**
  335. * Updates a single config setting, and recalculates pagination if needed.
  336. *
  337. * @param string config key
  338. * @param mixed config value
  339. * @return void
  340. */
  341. public function __set($key, $value)
  342. {
  343. $this->setup(array($key => $value));
  344. }
  345. } // End Pagination