/frameworks/kohana/3.0.9/modules/pagination/classes/kohana/pagination.php

https://github.com/ggunlugu/ornekler · PHP · 285 lines · 136 code · 42 blank · 107 comment · 11 complexity · 1ea3d309d3624313f94b5c325d56da0a 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. /**
  44. * Creates a new Pagination object.
  45. *
  46. * @param array configuration
  47. * @return Pagination
  48. */
  49. public static function factory(array $config = array())
  50. {
  51. return new Pagination($config);
  52. }
  53. /**
  54. * Creates a new Pagination object.
  55. *
  56. * @param array configuration
  57. * @return void
  58. */
  59. public function __construct(array $config = array())
  60. {
  61. // Overwrite system defaults with application defaults
  62. $this->config = $this->config_group() + $this->config;
  63. // Pagination setup
  64. $this->setup($config);
  65. }
  66. /**
  67. * Retrieves a pagination config group from the config file. One config group can
  68. * refer to another as its parent, which will be recursively loaded.
  69. *
  70. * @param string pagination config group; "default" if none given
  71. * @return array config settings
  72. */
  73. public function config_group($group = 'default')
  74. {
  75. // Load the pagination config file
  76. $config_file = Kohana::config('pagination');
  77. // Initialize the $config array
  78. $config['group'] = (string) $group;
  79. // Recursively load requested config groups
  80. while (isset($config['group']) AND isset($config_file->$config['group']))
  81. {
  82. // Temporarily store config group name
  83. $group = $config['group'];
  84. unset($config['group']);
  85. // Add config group values, not overwriting existing keys
  86. $config += $config_file->$group;
  87. }
  88. // Get rid of possible stray config group names
  89. unset($config['group']);
  90. // Return the merged config group settings
  91. return $config;
  92. }
  93. /**
  94. * Loads configuration settings into the object and (re)calculates pagination if needed.
  95. * Allows you to update config settings after a Pagination object has been constructed.
  96. *
  97. * @param array configuration
  98. * @return object Pagination
  99. */
  100. public function setup(array $config = array())
  101. {
  102. if (isset($config['group']))
  103. {
  104. // Recursively load requested config groups
  105. $config += $this->config_group($config['group']);
  106. }
  107. // Overwrite the current config settings
  108. $this->config = $config + $this->config;
  109. // Only (re)calculate pagination when needed
  110. if ($this->current_page === NULL
  111. OR isset($config['current_page'])
  112. OR isset($config['total_items'])
  113. OR isset($config['items_per_page']))
  114. {
  115. // Retrieve the current page number
  116. if ( ! empty($this->config['current_page']['page']))
  117. {
  118. // The current page number has been set manually
  119. $this->current_page = (int) $this->config['current_page']['page'];
  120. }
  121. else
  122. {
  123. switch ($this->config['current_page']['source'])
  124. {
  125. case 'query_string':
  126. $this->current_page = isset($_GET[$this->config['current_page']['key']])
  127. ? (int) $_GET[$this->config['current_page']['key']]
  128. : 1;
  129. break;
  130. case 'route':
  131. $this->current_page = (int) Request::current()->param($this->config['current_page']['key'], 1);
  132. break;
  133. }
  134. }
  135. // Calculate and clean all pagination variables
  136. $this->total_items = (int) max(0, $this->config['total_items']);
  137. $this->items_per_page = (int) max(1, $this->config['items_per_page']);
  138. $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
  139. $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
  140. $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
  141. $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
  142. $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
  143. $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
  144. $this->first_page = ($this->current_page === 1) ? FALSE : 1;
  145. $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
  146. $this->offset = (int) (($this->current_page - 1) * $this->items_per_page);
  147. }
  148. // Chainable method
  149. return $this;
  150. }
  151. /**
  152. * Generates the full URL for a certain page.
  153. *
  154. * @param integer page number
  155. * @return string page URL
  156. */
  157. public function url($page = 1)
  158. {
  159. // Clean the page number
  160. $page = max(1, (int) $page);
  161. // No page number in URLs to first page
  162. if ($page === 1 AND ! $this->config['first_page_in_url'])
  163. {
  164. $page = NULL;
  165. }
  166. switch ($this->config['current_page']['source'])
  167. {
  168. case 'query_string':
  169. return URL::site(Request::current()->uri).URL::query(array($this->config['current_page']['key'] => $page));
  170. case 'route':
  171. return URL::site(Request::current()->uri(array($this->config['current_page']['key'] => $page))).URL::query();
  172. }
  173. return '#';
  174. }
  175. /**
  176. * Checks whether the given page number exists.
  177. *
  178. * @param integer page number
  179. * @return boolean
  180. * @since 3.0.7
  181. */
  182. public function valid_page($page)
  183. {
  184. // Page number has to be a clean integer
  185. if ( ! Validate::digit($page))
  186. return FALSE;
  187. return $page > 0 AND $page <= $this->total_pages;
  188. }
  189. /**
  190. * Renders the pagination links.
  191. *
  192. * @param mixed string of the view to use, or a Kohana_View object
  193. * @return string pagination output (HTML)
  194. */
  195. public function render($view = NULL)
  196. {
  197. // Automatically hide pagination whenever it is superfluous
  198. if ($this->config['auto_hide'] === TRUE AND $this->total_pages <= 1)
  199. return '';
  200. if ($view === NULL)
  201. {
  202. // Use the view from config
  203. $view = $this->config['view'];
  204. }
  205. if ( ! $view instanceof Kohana_View)
  206. {
  207. // Load the view file
  208. $view = View::factory($view);
  209. }
  210. // Pass on the whole Pagination object
  211. return $view->set(get_object_vars($this))->set('page', $this)->render();
  212. }
  213. /**
  214. * Renders the pagination links.
  215. *
  216. * @return string pagination output (HTML)
  217. */
  218. public function __toString()
  219. {
  220. return $this->render();
  221. }
  222. /**
  223. * Returns a Pagination property.
  224. *
  225. * @param string URI of the request
  226. * @return mixed Pagination property; NULL if not found
  227. */
  228. public function __get($key)
  229. {
  230. return isset($this->$key) ? $this->$key : NULL;
  231. }
  232. /**
  233. * Updates a single config setting, and recalculates pagination if needed.
  234. *
  235. * @param string config key
  236. * @param mixed config value
  237. * @return void
  238. */
  239. public function __set($key, $value)
  240. {
  241. $this->setup(array($key => $value));
  242. }
  243. } // End Pagination