PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/pagination.php

https://github.com/mentariworks/feedmalaya-draft
PHP | 240 lines | 107 code | 41 blank | 92 comment | 18 complexity | 422d45a21884efbf8ff915d2e8b0e0f3 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 integer The total number of items
  31. */
  32. protected static $total_items = 0;
  33. /**
  34. * @var integer The total number of links to show
  35. */
  36. protected static $num_links = 5;
  37. /**
  38. * @var integer The URI segment containg page number
  39. */
  40. protected static $uri_segment = 3;
  41. /**
  42. * @var mixed The pagination URL
  43. */
  44. protected static $pagination_url;
  45. /**
  46. * Init
  47. *
  48. * Loads in the config and sets the variables
  49. *
  50. * @access public
  51. * @return void
  52. */
  53. public static function _init()
  54. {
  55. $config = \Config::get('pagination', array());
  56. static::set_config($config);
  57. }
  58. // --------------------------------------------------------------------
  59. /**
  60. * Set Config
  61. *
  62. * Sets the configuration for pagination
  63. *
  64. * @access public
  65. * @param array $config The configuration array
  66. * @return void
  67. */
  68. public static function set_config(array $config)
  69. {
  70. foreach ($config as $key => $value)
  71. {
  72. static::${$key} = $value;
  73. }
  74. static::initialize();
  75. }
  76. // --------------------------------------------------------------------
  77. /**
  78. * Prepares vars for creating links
  79. *
  80. * @access public
  81. * @return array The pagination variables
  82. */
  83. protected static function initialize()
  84. {
  85. static::$total_pages = ceil(static::$total_items / static::$per_page) ?: 1;
  86. static::$current_page = (int) \URI::segment(static::$uri_segment);
  87. if (static::$current_page > static::$total_pages)
  88. {
  89. static::$current_page = static::$total_pages;
  90. }
  91. elseif (static::$current_page < 1)
  92. {
  93. static::$current_page = 1;
  94. }
  95. // The current page must be zero based so that the offset for page 1 is 0.
  96. static::$offset = (static::$current_page - 1) * static::$per_page;
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Creates the pagination links
  101. *
  102. * @access public
  103. * @return mixed The pagination links
  104. */
  105. public static function create_links()
  106. {
  107. if (static::$total_pages == 1)
  108. {
  109. return '';
  110. }
  111. $pagination = '';
  112. $pagination .= '&nbsp;'.static::prev_link('&laquo; Previous').'&nbsp;&nbsp;';
  113. $pagination .= static::page_links();
  114. $pagination .= '&nbsp;'.static::next_link('Next &raquo;');
  115. return $pagination;
  116. }
  117. // --------------------------------------------------------------------
  118. /**
  119. * Pagination Page Number links
  120. *
  121. * @access public
  122. * @return mixed Markup for page number links
  123. */
  124. public static function page_links()
  125. {
  126. if (static::$total_pages == 1)
  127. {
  128. return '';
  129. }
  130. $pagination = '';
  131. // Let's get the starting page number, this is determined using num_links
  132. $start = ((static::$current_page - static::$num_links) > 0) ? static::$current_page - (static::$num_links - 1) : 1;
  133. // Let's get the ending page number
  134. $end = ((static::$current_page + static::$num_links) < static::$total_pages) ? static::$current_page + static::$num_links : static::$total_pages;
  135. for($i = $start; $i <= $end; $i++)
  136. {
  137. if (static::$current_page == $i)
  138. {
  139. $pagination .= '<b>'.$i.'</b>';
  140. }
  141. else
  142. {
  143. $url = ($i == 1) ? '' : '/'.$i;
  144. $pagination .= \Html::anchor(rtrim(static::$pagination_url, '/') . $url, $i);
  145. }
  146. }
  147. return $pagination;
  148. }
  149. // --------------------------------------------------------------------
  150. /**
  151. * Pagination "Next" link
  152. *
  153. * @access public
  154. * @param string $value The text displayed in link
  155. * @return mixed The next link
  156. */
  157. public static function next_link($value)
  158. {
  159. if (static::$total_pages == 1)
  160. {
  161. return '';
  162. }
  163. if (static::$current_page == static::$total_pages)
  164. {
  165. return $value;
  166. }
  167. else
  168. {
  169. $next_page = static::$current_page + 1;
  170. return \Html::anchor(rtrim(static::$pagination_url, '/').'/'.$next_page, $value);
  171. }
  172. }
  173. // --------------------------------------------------------------------
  174. /**
  175. * Pagination "Previous" link
  176. *
  177. * @access public
  178. * @param string $value The text displayed in link
  179. * @return mixed The previous link
  180. */
  181. public static function prev_link($value)
  182. {
  183. if (static::$total_pages == 1)
  184. {
  185. return '';
  186. }
  187. if (static::$current_page == 1)
  188. {
  189. return $value;
  190. }
  191. else
  192. {
  193. $previous_page = static::$current_page - 1;
  194. $previous_page = ($previous_page == 1) ? '' : '/' . $previous_page;
  195. return \Html::anchor(rtrim(static::$pagination_url, '/') . $previous_page, $value);
  196. }
  197. }
  198. }
  199. /* End of file pagination.php */