PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Pagination.php

http://github.com/ushahidi/Ushahidi_Web
PHP | 236 lines | 120 code | 31 blank | 85 comment | 12 complexity | cdb5c079d715684b5ab2e9ed6c85fedc MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3. * Pagination library.
  4. *
  5. * $Id: Pagination.php 3917 2009-01-21 03:06:22Z zombor $
  6. *
  7. * @package Core
  8. * @author Kohana Team
  9. * @copyright (c) 2007-2008 Kohana Team
  10. * @license http://kohanaphp.com/license.html
  11. */
  12. class Pagination_Core {
  13. // Config values
  14. protected $base_url = '';
  15. protected $directory = 'pagination';
  16. protected $style = 'classic';
  17. protected $uri_segment = 3;
  18. protected $query_string = '';
  19. protected $items_per_page = 20;
  20. protected $total_items = 0;
  21. protected $auto_hide = FALSE;
  22. // Autogenerated values
  23. protected $url;
  24. protected $current_page;
  25. protected $total_pages;
  26. protected $current_first_item;
  27. protected $current_last_item;
  28. protected $first_page;
  29. protected $last_page;
  30. protected $previous_page;
  31. protected $next_page;
  32. protected $sql_offset;
  33. protected $sql_limit;
  34. /**
  35. * Constructs and returns a new Pagination object.
  36. *
  37. * @param array configuration settings
  38. * @return object
  39. */
  40. public function factory($config = array())
  41. {
  42. return new Pagination($config);
  43. }
  44. /**
  45. * Constructs a new Pagination object.
  46. *
  47. * @param array configuration settings
  48. * @return void
  49. */
  50. public function __construct($config = array())
  51. {
  52. // No custom group name given
  53. if ( ! isset($config['group']))
  54. {
  55. $config['group'] = 'default';
  56. }
  57. // Pagination setup
  58. $this->initialize($config);
  59. Kohana::log('debug', 'Pagination Library initialized');
  60. }
  61. /**
  62. * Sets config values.
  63. *
  64. * @throws Kohana_Exception
  65. * @param array configuration settings
  66. * @return void
  67. */
  68. public function initialize($config = array())
  69. {
  70. // Load config group
  71. if (isset($config['group']))
  72. {
  73. // Load and validate config group
  74. if ( ! is_array($group_config = Kohana::config('pagination.'.$config['group'])))
  75. throw new Kohana_Exception('pagination.undefined_group', $config['group']);
  76. // All pagination config groups inherit default config group
  77. if ($config['group'] !== 'default')
  78. {
  79. // Load and validate default config group
  80. if ( ! is_array($default_config = Kohana::config('pagination.default')))
  81. throw new Kohana_Exception('pagination.undefined_group', 'default');
  82. // Merge config group with default config group
  83. $group_config += $default_config;
  84. }
  85. // Merge custom config items with config group
  86. $config += $group_config;
  87. }
  88. // Assign config values to the object
  89. foreach ($config as $key => $value)
  90. {
  91. if (property_exists($this, $key))
  92. {
  93. $this->$key = $value;
  94. }
  95. }
  96. // Clean view directory
  97. $this->directory = trim($this->directory, '/').'/';
  98. // Build generic URL with page in query string
  99. if ($this->query_string !== '')
  100. {
  101. // Extract current page
  102. $this->current_page = isset($_GET[$this->query_string]) ? (int) $_GET[$this->query_string] : 1;
  103. // Insert {page} placeholder
  104. $_GET[$this->query_string] = '{page}';
  105. // Create full URL
  106. $base_url = ($this->base_url === '') ? Router::$current_uri : $this->base_url;
  107. $this->url = url::site($base_url).'?'.str_replace('%7Bpage%7D', '{page}', http_build_query($_GET));
  108. // Reset page number
  109. $_GET[$this->query_string] = $this->current_page;
  110. }
  111. // Build generic URL with page as URI segment
  112. else
  113. {
  114. // Use current URI if no base_url set
  115. $this->url = ($this->base_url === '') ? Router::$segments : explode('/', trim($this->base_url, '/'));
  116. // Convert uri 'label' to corresponding integer if needed
  117. if (is_string($this->uri_segment))
  118. {
  119. if (($key = array_search($this->uri_segment, $this->url)) === FALSE)
  120. {
  121. // If uri 'label' is not found, auto add it to base_url
  122. $this->url[] = $this->uri_segment;
  123. $this->uri_segment = count($this->url) + 1;
  124. }
  125. else
  126. {
  127. $this->uri_segment = $key + 2;
  128. }
  129. }
  130. // Insert {page} placeholder
  131. $this->url[$this->uri_segment - 1] = '{page}';
  132. // Create full URL
  133. $this->url = url::site(implode('/', $this->url)).Router::$query_string;
  134. // Extract current page
  135. $this->current_page = URI::instance()->segment($this->uri_segment);
  136. }
  137. // Core pagination values
  138. $this->total_items = (int) max(0, $this->total_items);
  139. $this->items_per_page = (int) max(1, $this->items_per_page);
  140. $this->total_pages = (int) ceil($this->total_items / $this->items_per_page);
  141. $this->current_page = (int) min(max(1, $this->current_page), max(1, $this->total_pages));
  142. $this->current_first_item = (int) min((($this->current_page - 1) * $this->items_per_page) + 1, $this->total_items);
  143. $this->current_last_item = (int) min($this->current_first_item + $this->items_per_page - 1, $this->total_items);
  144. // If there is no first/last/previous/next page, relative to the
  145. // current page, value is set to FALSE. Valid page number otherwise.
  146. $this->first_page = ($this->current_page === 1) ? FALSE : 1;
  147. $this->last_page = ($this->current_page >= $this->total_pages) ? FALSE : $this->total_pages;
  148. $this->previous_page = ($this->current_page > 1) ? $this->current_page - 1 : FALSE;
  149. $this->next_page = ($this->current_page < $this->total_pages) ? $this->current_page + 1 : FALSE;
  150. // SQL values
  151. $this->sql_offset = (int) ($this->current_page - 1) * $this->items_per_page;
  152. $this->sql_limit = sprintf(' LIMIT %d OFFSET %d ', $this->items_per_page, $this->sql_offset);
  153. }
  154. /**
  155. * Generates the HTML for the chosen pagination style.
  156. *
  157. * @param string pagination style
  158. * @return string pagination html
  159. */
  160. public function render($style = NULL)
  161. {
  162. // Hide single page pagination
  163. if ($this->auto_hide === TRUE AND $this->total_pages <= 1)
  164. return '';
  165. if ($style === NULL)
  166. {
  167. // Use default style
  168. $style = $this->style;
  169. }
  170. // Return rendered pagination view
  171. return View::factory($this->directory.$style, get_object_vars($this))->render();
  172. }
  173. /**
  174. * Magically converts Pagination object to string.
  175. *
  176. * @return string pagination html
  177. */
  178. public function __toString()
  179. {
  180. return $this->render();
  181. }
  182. /**
  183. * Magically gets a pagination variable.
  184. *
  185. * @param string variable key
  186. * @return mixed variable value if the key is found
  187. * @return void if the key is not found
  188. */
  189. public function __get($key)
  190. {
  191. if (isset($this->$key))
  192. return $this->$key;
  193. }
  194. /**
  195. * Adds a secondary interface for accessing properties, e.g. $pagination->total_pages().
  196. * Note that $pagination->total_pages is the recommended way to access properties.
  197. *
  198. * @param string function name
  199. * @return string
  200. */
  201. public function __call($func, $args = NULL)
  202. {
  203. return $this->__get($func);
  204. }
  205. } // End Pagination Class