PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/fuel/core/classes/pagination.php

https://github.com/tilur/taranoelle
PHP | 267 lines | 128 code | 45 blank | 94 comment | 20 complexity | 192794e3812987056fec147d3890570c MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * Fuel is a fast, lightweight, community driven PHP5 framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Dan Horrigan <http://dhorrigan.com>
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. */
  11. namespace Fuel\Core;
  12. class Pagination {
  13. /**
  14. * @var integer The current page
  15. */
  16. public static $current_page = null;
  17. /**
  18. * @var integer The offset that the current page starts at
  19. */
  20. public static $offset = 0;
  21. /**
  22. * @var integer The number of items per page
  23. */
  24. public static $per_page = 10;
  25. /**
  26. * @var integer The number of total pages
  27. */
  28. public static $total_pages = 0;
  29. /**
  30. * @var array The HTML for the display
  31. */
  32. public static $template = array(
  33. 'wrapper_start' => '<div class="pagination"> ',
  34. 'wrapper_end' => ' </div>',
  35. 'page_start' => '<span class="page-links"> ',
  36. 'page_end' => ' </span>',
  37. 'previous_start' => '<span class="previous"> ',
  38. 'previous_end' => ' </span>',
  39. 'previous_mark' => '&laquo; ',
  40. 'next_start' => '<span class="next"> ',
  41. 'next_end' => ' </span>',
  42. 'next_mark' => ' &raquo;',
  43. 'active_start' => '<span class="active"> ',
  44. 'active_end' => ' </span>',
  45. );
  46. /**
  47. * @var integer The total number of items
  48. */
  49. protected static $total_items = 0;
  50. /**
  51. * @var integer The total number of links to show
  52. */
  53. protected static $num_links = 5;
  54. /**
  55. * @var integer The URI segment containg page number
  56. */
  57. protected static $uri_segment = 3;
  58. /**
  59. * @var mixed The pagination URL
  60. */
  61. protected static $pagination_url;
  62. /**
  63. * Init
  64. *
  65. * Loads in the config and sets the variables
  66. *
  67. * @access public
  68. * @return void
  69. */
  70. public static function _init()
  71. {
  72. $config = \Config::get('pagination', array());
  73. static::set_config($config);
  74. }
  75. // --------------------------------------------------------------------
  76. /**
  77. * Set Config
  78. *
  79. * Sets the configuration for pagination
  80. *
  81. * @access public
  82. * @param array $config The configuration array
  83. * @return void
  84. */
  85. public static function set_config(array $config)
  86. {
  87. foreach ($config as $key => $value)
  88. {
  89. if ($key == 'template')
  90. {
  91. static::$template = array_merge(static::$template, $config['template']);
  92. continue;
  93. }
  94. static::${$key} = $value;
  95. }
  96. static::initialize();
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Prepares vars for creating links
  101. *
  102. * @access public
  103. * @return array The pagination variables
  104. */
  105. protected static function initialize()
  106. {
  107. static::$total_pages = ceil(static::$total_items / static::$per_page) ?: 1;
  108. static::$current_page = (int) \URI::segment(static::$uri_segment);
  109. if (static::$current_page > static::$total_pages)
  110. {
  111. static::$current_page = static::$total_pages;
  112. }
  113. elseif (static::$current_page < 1)
  114. {
  115. static::$current_page = 1;
  116. }
  117. // The current page must be zero based so that the offset for page 1 is 0.
  118. static::$offset = (static::$current_page - 1) * static::$per_page;
  119. }
  120. // --------------------------------------------------------------------
  121. /**
  122. * Creates the pagination links
  123. *
  124. * @access public
  125. * @return mixed The pagination links
  126. */
  127. public static function create_links()
  128. {
  129. if (static::$total_pages == 1)
  130. {
  131. return '';
  132. }
  133. \Lang::load('pagination', true);
  134. $pagination = static::$template['wrapper_start'];
  135. $pagination .= static::prev_link(\Lang::line('pagination.previous'));
  136. $pagination .= static::page_links();
  137. $pagination .= static::next_link(\Lang::line('pagination.next'));
  138. $pagination .= static::$template['wrapper_end'];
  139. return $pagination;
  140. }
  141. // --------------------------------------------------------------------
  142. /**
  143. * Pagination Page Number links
  144. *
  145. * @access public
  146. * @return mixed Markup for page number links
  147. */
  148. public static function page_links()
  149. {
  150. if (static::$total_pages == 1)
  151. {
  152. return '';
  153. }
  154. $pagination = '';
  155. // Let's get the starting page number, this is determined using num_links
  156. $start = ((static::$current_page - static::$num_links) > 0) ? static::$current_page - (static::$num_links - 1) : 1;
  157. // Let's get the ending page number
  158. $end = ((static::$current_page + static::$num_links) < static::$total_pages) ? static::$current_page + static::$num_links : static::$total_pages;
  159. for($i = $start; $i <= $end; $i++)
  160. {
  161. if (static::$current_page == $i)
  162. {
  163. $pagination .= static::$template['active_start'].$i.static::$template['active_end'];
  164. }
  165. else
  166. {
  167. $url = ($i == 1) ? '' : '/'.$i;
  168. $pagination .= \Html::anchor(rtrim(static::$pagination_url, '/').$url, $i);
  169. }
  170. }
  171. return static::$template['page_start'].$pagination.static::$template['page_end'];
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Pagination "Next" link
  176. *
  177. * @access public
  178. * @param string $value The text displayed in link
  179. * @return mixed The next link
  180. */
  181. public static function next_link($value)
  182. {
  183. if (static::$total_pages == 1)
  184. {
  185. return '';
  186. }
  187. if (static::$current_page == static::$total_pages)
  188. {
  189. return $value.static::$template['next_mark'];
  190. }
  191. else
  192. {
  193. $next_page = static::$current_page + 1;
  194. return \Html::anchor(rtrim(static::$pagination_url, '/').'/'.$next_page, $value.static::$template['next_mark']);
  195. }
  196. }
  197. // --------------------------------------------------------------------
  198. /**
  199. * Pagination "Previous" link
  200. *
  201. * @access public
  202. * @param string $value The text displayed in link
  203. * @return mixed The previous link
  204. */
  205. public static function prev_link($value)
  206. {
  207. if (static::$total_pages == 1)
  208. {
  209. return '';
  210. }
  211. if (static::$current_page == 1)
  212. {
  213. return static::$template['previous_mark'].$value;
  214. }
  215. else
  216. {
  217. $previous_page = static::$current_page - 1;
  218. $previous_page = ($previous_page == 1) ? '' : '/'.$previous_page;
  219. return \Html::anchor(rtrim(static::$pagination_url, '/').$previous_page, static::$template['previous_mark'].$value);
  220. }
  221. }
  222. }