Pagination.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. /**
  3. * Pagination links generator.
  4. *
  5. * @package Koseven/Pagination
  6. * @category Base
  7. * @author Koseven Team
  8. * @copyright (c) Kohana Team
  9. * @license https://koseven.ga/LICENSE.md
  10. */
  11. class Kohana_Pagination {
  12. // Merged configuration settings
  13. protected $config = [
  14. 'current_page' => ['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. // Request object
  44. protected $_request;
  45. // Route to use for URIs
  46. protected $_route;
  47. // Parameters to use with Route to create URIs
  48. protected $_route_params = [];
  49. /**
  50. * Creates a new Pagination object.
  51. *
  52. * @param array configuration
  53. * @return Pagination
  54. */
  55. public static function factory(array $config = [], 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. */
  65. public function __construct(array $config = [], Request $request = NULL)
  66. {
  67. // Overwrite system defaults with application defaults
  68. $this->config = $this->config_group() + $this->config;
  69. // Assing Request
  70. if ($request === NULL)
  71. {
  72. $request = Request::current();
  73. }
  74. $this->_request = $request;
  75. // Assign default Route
  76. $this->_route = $request->route();
  77. // Assign default route params
  78. $this->_route_params = $request->param();
  79. // Add controller and action to route params for routes with variable controllers and actions
  80. $this->_route_params['controller'] = $request->controller();
  81. $this->_route_params['action'] = $request->action();
  82. $this->_route_params['directory'] = $request->directory();
  83. // Pagination setup
  84. $this->setup($config);
  85. }
  86. /**
  87. * Retrieves a pagination config group from the config file. One config group can
  88. * refer to another as its parent, which will be recursively loaded.
  89. *
  90. * @param string pagination config group; "default" if none given
  91. * @return array config settings
  92. */
  93. public function config_group($group = 'default')
  94. {
  95. // Load the pagination config file
  96. $config_file = Kohana::$config->load('pagination');
  97. // Initialize the $config array
  98. $config['group'] = (string) $group;
  99. // Recursively load requested config groups
  100. while (isset($config['group']) AND isset($config_file->{$config['group']}))
  101. {
  102. // Temporarily store config group name
  103. $group = $config['group'];
  104. unset($config['group']);
  105. // Add config group values, not overwriting existing keys
  106. $config += $config_file->$group;
  107. }
  108. // Get rid of possible stray config group names
  109. unset($config['group']);
  110. // Return the merged config group settings
  111. return $config;
  112. }
  113. /**
  114. * Loads configuration settings into the object and (re)calculates pagination if needed.
  115. * Allows you to update config settings after a Pagination object has been constructed.
  116. *
  117. * @param array configuration
  118. * @return object Pagination
  119. */
  120. public function setup(array $config = [])
  121. {
  122. if (isset($config['group']))
  123. {
  124. // Recursively load requested config groups
  125. $config += $this->config_group($config['group']);
  126. }
  127. // Overwrite the current config settings
  128. $this->config = $config + $this->config;
  129. // Only (re)calculate pagination when needed
  130. if ($this->current_page === NULL
  131. OR isset($config['current_page'])
  132. OR isset($config['total_items'])
  133. OR isset($config['items_per_page']))
  134. {
  135. // Retrieve the current page number
  136. if ( ! empty($this->config['current_page']['page']))
  137. {
  138. // The current page number has been set manually
  139. $this->current_page = (int) $this->config['current_page']['page'];
  140. }
  141. else
  142. {
  143. $query_key = $this->config['current_page']['key'];
  144. switch ($this->config['current_page']['source'])
  145. {
  146. case 'query_string':
  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. return URL::site($this->_route->uri($this->_route_params).
  191. $this->query([$this->config['current_page']['key'] => $page]));
  192. case 'route':
  193. return URL::site($this->_route->uri(array_merge($this->_route_params,
  194. [$this->config['current_page']['key'] => $page])).$this->query());
  195. }
  196. return '#';
  197. }
  198. /**
  199. * Checks whether the given page number exists.
  200. *
  201. * @param integer page number
  202. * @return boolean
  203. * @since 3.0.7
  204. */
  205. public function valid_page($page)
  206. {
  207. // Page number has to be a clean integer
  208. if ( ! Valid::digit($page))
  209. return FALSE;
  210. return $page > 0 AND $page <= $this->total_pages;
  211. }
  212. /**
  213. * Renders the pagination links.
  214. *
  215. * @param mixed string of the view to use, or a Kohana_View object
  216. * @return string pagination output (HTML)
  217. */
  218. public function render($view = NULL)
  219. {
  220. // Automatically hide pagination whenever it is superfluous
  221. if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
  222. return '';
  223. if ($view === NULL)
  224. {
  225. // Use the view from config
  226. $view = $this->config['view'];
  227. }
  228. if ( ! $view instanceof View)
  229. {
  230. // Load the view file
  231. $view = View::factory($view);
  232. }
  233. // Pass on the whole Pagination object
  234. return $view->set(get_object_vars($this))->set('page', $this)->render();
  235. }
  236. /**
  237. * Request setter / getter
  238. *
  239. * @param Request
  240. * @return Request If used as getter
  241. * @return $this Chainable as setter
  242. */
  243. public function request(Request $request = NULL)
  244. {
  245. if ($request === NULL)
  246. return $this->_request;
  247. $this->_request = $request;
  248. return $this;
  249. }
  250. /**
  251. * Route setter / getter
  252. *
  253. * @param Route
  254. * @return Route Route if used as getter
  255. * @return $this Chainable as setter
  256. */
  257. public function route(Route $route = NULL)
  258. {
  259. if ($route === NULL)
  260. return $this->_route;
  261. $this->_route = $route;
  262. return $this;
  263. }
  264. /**
  265. * Route parameters setter / getter
  266. *
  267. * @param array Route parameters to set
  268. * @return array Route parameters if used as getter
  269. * @return $this Chainable as setter
  270. */
  271. public function route_params(array $route_params = NULL)
  272. {
  273. if ($route_params === NULL)
  274. return $this->_route_params;
  275. $this->_route_params = $route_params;
  276. return $this;
  277. }
  278. /**
  279. * URL::query() replacement for Pagination use only
  280. *
  281. * @param array Parameters to override
  282. * @return string
  283. */
  284. public function query(array $params = NULL)
  285. {
  286. if ($params === NULL)
  287. {
  288. // Use only the current parameters
  289. $params = $this->_request->query();
  290. }
  291. else
  292. {
  293. // Merge the current and new parameters
  294. $params = array_merge($this->_request->query(), $params);
  295. }
  296. if (empty($params))
  297. {
  298. // No query parameters
  299. return '';
  300. }
  301. // Note: http_build_query returns an empty string for a params array with only NULL values
  302. $query = http_build_query($params, '', '&');
  303. // Don't prepend '?' to an empty string
  304. return ($query === '') ? '' : ('?'.$query);
  305. }
  306. /**
  307. * Renders the pagination links.
  308. *
  309. * @return string pagination output (HTML)
  310. */
  311. public function __toString()
  312. {
  313. try
  314. {
  315. return $this->render();
  316. }
  317. catch(Exception $e)
  318. {
  319. Kohana_Exception::handler($e);
  320. return '';
  321. }
  322. }
  323. /**
  324. * Returns a Pagination property.
  325. *
  326. * @param string property name
  327. * @return mixed Pagination property; NULL if not found
  328. */
  329. public function __get($key)
  330. {
  331. return isset($this->$key) ? $this->$key : NULL;
  332. }
  333. /**
  334. * Updates a single config setting, and recalculates pagination if needed.
  335. *
  336. * @param string config key
  337. * @param mixed config value
  338. * @return void
  339. */
  340. public function __set($key, $value)
  341. {
  342. $this->setup([$key => $value]);
  343. }
  344. }