Pagination.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * Pagination links generator.
  4. *
  5. * @package Koseven/Pagination
  6. * @category Base
  7. *
  8. * @copyright (c) 2007-2016 Kohana Team
  9. * @copyright (c) since 2016 Koseven Team
  10. * @license https://koseven.dev/LICENSE
  11. */
  12. class KO7_Pagination {
  13. // Merged configuration settings
  14. protected $config = [
  15. 'current_page' => ['source' => 'query_string', 'key' => 'page'],
  16. 'total_items' => 0,
  17. 'items_per_page' => 10,
  18. 'view' => 'pagination/basic',
  19. 'auto_hide' => TRUE,
  20. 'first_page_in_url' => FALSE,
  21. ];
  22. // Current page number
  23. protected $current_page;
  24. // Total item count
  25. protected $total_items;
  26. // How many items to show per page
  27. protected $items_per_page;
  28. // Total page count
  29. protected $total_pages;
  30. // Item offset for the first item displayed on the current page
  31. protected $current_first_item;
  32. // Item offset for the last item displayed on the current page
  33. protected $current_last_item;
  34. // Previous page number; FALSE if the current page is the first one
  35. protected $previous_page;
  36. // Next page number; FALSE if the current page is the last one
  37. protected $next_page;
  38. // First page number; FALSE if the current page is the first one
  39. protected $first_page;
  40. // Last page number; FALSE if the current page is the last one
  41. protected $last_page;
  42. // Query offset
  43. protected $offset;
  44. // Request object
  45. protected $_request;
  46. // Route to use for URIs
  47. protected $_route;
  48. // Parameters to use with Route to create URIs
  49. protected $_route_params = [];
  50. /**
  51. * Creates a new Pagination object.
  52. *
  53. * @param array configuration
  54. * @return Pagination
  55. */
  56. public static function factory(array $config = [], Request $request = NULL)
  57. {
  58. return new Pagination($config, $request);
  59. }
  60. /**
  61. * Creates a new Pagination object.
  62. *
  63. * @param array configuration
  64. * @return void
  65. */
  66. public function __construct(array $config = [], Request $request = NULL)
  67. {
  68. // Overwrite system defaults with application defaults
  69. $this->config = $this->config_group() + $this->config;
  70. // Assing Request
  71. if ($request === NULL)
  72. {
  73. $request = Request::current();
  74. }
  75. $this->_request = $request;
  76. // Assign default Route
  77. $this->_route = $request->route();
  78. // Assign default route params
  79. $this->_route_params = $request->param();
  80. // Add controller and action to route params for routes with variable controllers and actions
  81. $this->_route_params['controller'] = $request->controller();
  82. $this->_route_params['action'] = $request->action();
  83. $this->_route_params['directory'] = $request->directory();
  84. // Pagination setup
  85. $this->setup($config);
  86. }
  87. /**
  88. * Retrieves a pagination config group from the config file. One config group can
  89. * refer to another as its parent, which will be recursively loaded.
  90. *
  91. * @param string pagination config group; "default" if none given
  92. * @return array config settings
  93. */
  94. public function config_group($group = 'default')
  95. {
  96. // Load the pagination config file
  97. $config_file = KO7::$config->load('pagination');
  98. // Initialize the $config array
  99. $config['group'] = (string) $group;
  100. // Recursively load requested config groups
  101. while (isset($config['group']) AND isset($config_file->{$config['group']}))
  102. {
  103. // Temporarily store config group name
  104. $group = $config['group'];
  105. unset($config['group']);
  106. // Add config group values, not overwriting existing keys
  107. $config += $config_file->$group;
  108. }
  109. // Get rid of possible stray config group names
  110. unset($config['group']);
  111. // Return the merged config group settings
  112. return $config;
  113. }
  114. /**
  115. * Loads configuration settings into the object and (re)calculates pagination if needed.
  116. * Allows you to update config settings after a Pagination object has been constructed.
  117. *
  118. * @param array configuration
  119. * @return object Pagination
  120. */
  121. public function setup(array $config = [])
  122. {
  123. if (isset($config['group']))
  124. {
  125. // Recursively load requested config groups
  126. $config += $this->config_group($config['group']);
  127. }
  128. // Overwrite the current config settings
  129. $this->config = $config + $this->config;
  130. // Only (re)calculate pagination when needed
  131. if ($this->current_page === NULL
  132. OR isset($config['current_page'])
  133. OR isset($config['total_items'])
  134. OR isset($config['items_per_page']))
  135. {
  136. // Retrieve the current page number
  137. if ( ! empty($this->config['current_page']['page']))
  138. {
  139. // The current page number has been set manually
  140. $this->current_page = (int) $this->config['current_page']['page'];
  141. }
  142. else
  143. {
  144. $query_key = $this->config['current_page']['key'];
  145. switch ($this->config['current_page']['source'])
  146. {
  147. case 'query_string':
  148. $this->current_page = ($this->_request->query($query_key) !== NULL)
  149. ? (int) $this->_request->query($query_key)
  150. : 1;
  151. break;
  152. case 'route':
  153. $this->current_page = (int) $this->_request->param($query_key, 1);
  154. break;
  155. }
  156. }
  157. // Calculate and clean all pagination variables
  158. $this->total_items = (int) max(0, $this->config['total_items']);
  159. $this->items_per_page = (int) max(1, $this->config['items_per_page']);
  160. $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
  161. $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
  162. $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
  163. $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
  164. $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
  165. $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
  166. $this->first_page = ($this->current_page === 1) ? FALSE : 1;
  167. $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
  168. $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
  169. }
  170. // Chainable method
  171. return $this;
  172. }
  173. /**
  174. * Generates the full URL for a certain page.
  175. *
  176. * @param integer page number
  177. * @return string page URL
  178. */
  179. public function url($page = 1)
  180. {
  181. // Clean the page number
  182. $page = max(1, (int) $page);
  183. // No page number in URLs to first page
  184. if ($page === 1 AND ! $this->config['first_page_in_url'])
  185. {
  186. $page = NULL;
  187. }
  188. switch ($this->config['current_page']['source'])
  189. {
  190. case 'query_string':
  191. return URL::site($this->_route->uri($this->_route_params).
  192. $this->query([$this->config['current_page']['key'] => $page]));
  193. case 'route':
  194. return URL::site($this->_route->uri(array_merge($this->_route_params,
  195. [$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 KO7_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. KO7_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([$key => $value]);
  344. }
  345. }