PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/packages/hybrid/classes/pagination.php

https://github.com/mentariworks/feedmalaya-draft
PHP | 304 lines | 137 code | 41 blank | 126 comment | 22 complexity | 5d6270e60a8cb7907962f3ec51068bfd 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 Hybrid;
  12. class Pagination {
  13. /**
  14. * The current page
  15. *
  16. * @access public
  17. * @staticvar int
  18. */
  19. public static $current_page = null;
  20. /**
  21. * The offset that the current page starts at
  22. *
  23. * @access public
  24. * @staticvar int
  25. */
  26. public static $offset = 0;
  27. /**
  28. * The number of items per page
  29. *
  30. * @access public
  31. * @staticvar int
  32. */
  33. public static $per_page = 10;
  34. /**
  35. * The number of total pages
  36. *
  37. * @access public
  38. * @staticvar int
  39. */
  40. public static $total_pages = 0;
  41. /**
  42. * The total number of items
  43. *
  44. * @access protected
  45. * @staticvar int
  46. */
  47. protected static $total_items = 0;
  48. /**
  49. * The total number of links to show
  50. *
  51. * @access protected
  52. * @staticvar int
  53. */
  54. protected static $num_links = 5;
  55. /**
  56. * The URI segment containg page number
  57. *
  58. * @access protected
  59. * @staticvar int
  60. */
  61. protected static $uri_segment = 3;
  62. /**
  63. * The pagination URL
  64. *
  65. * @access protected
  66. * @staticvar mixed
  67. */
  68. protected static $pagination_url;
  69. /**
  70. * The pagination URL (after the page number URI segment)
  71. *
  72. * @access protected
  73. * @staticvar mixed
  74. */
  75. protected static $suffix_url;
  76. /**
  77. * Init
  78. *
  79. * Loads in the config and sets the variables
  80. *
  81. * @access public
  82. * @return void
  83. */
  84. public static function _init()
  85. {
  86. $config = \Config::get('pagination', array());
  87. static::set_config($config);
  88. }
  89. /**
  90. * Set Config
  91. *
  92. * Sets the configuration for pagination
  93. *
  94. * @access public
  95. * @param array $config The configuration array
  96. * @return void
  97. */
  98. public static function set_config(array $config)
  99. {
  100. foreach ($config as $key => $value)
  101. {
  102. static::${$key} = $value;
  103. }
  104. static::initialize();
  105. }
  106. /**
  107. * Prepares vars for creating links
  108. *
  109. * @access public
  110. * @return array The pagination variables
  111. */
  112. protected static function initialize()
  113. {
  114. static::$total_pages = ceil(static::$total_items / static::$per_page) ?: 1;
  115. is_null(static::$current_page) and static::$current_page = (int) \URI::segment(static::$uri_segment);
  116. if (static::$current_page > static::$total_pages)
  117. {
  118. static::$current_page = static::$total_pages;
  119. }
  120. elseif (static::$current_page < 1)
  121. {
  122. static::$current_page = 1;
  123. }
  124. // The current page must be zero based so that the offset for page 1 is 0.
  125. static::$offset = (static::$current_page - 1) * static::$per_page;
  126. }
  127. /**
  128. * Creates the pagination links
  129. *
  130. * @access public
  131. * @return mixed The pagination links
  132. */
  133. public static function create_links()
  134. {
  135. if (static::$total_pages == 1)
  136. {
  137. return '';
  138. }
  139. $pagination = '';
  140. $pagination .= '&nbsp;'.static::prev_link('&laquo; Previous').'&nbsp;&nbsp;';
  141. $pagination .= static::page_links();
  142. $pagination .= '&nbsp;'.static::next_link('Next &raquo;');
  143. return $pagination;
  144. }
  145. /**
  146. * Pagination Page Number links
  147. *
  148. * @access public
  149. * @return mixed Markup for page number links
  150. */
  151. public static function page_links()
  152. {
  153. if (static::$total_pages == 1)
  154. {
  155. return '';
  156. }
  157. $pagination = '';
  158. // Let's get the starting page number, this is determined using num_links
  159. $start = ((static::$current_page - static::$num_links) > 0) ? static::$current_page - (static::$num_links - 1) : 1;
  160. // Let's get the ending page number
  161. $end = ((static::$current_page + static::$num_links) < static::$total_pages) ? static::$current_page + static::$num_links : static::$total_pages;
  162. for($i = $start; $i <= $end; $i++)
  163. {
  164. if (static::$current_page == $i)
  165. {
  166. $pagination .= '<b>'.$i.'</b>';
  167. }
  168. else
  169. {
  170. $url = ($i == 1) ? '' : '/'.$i;
  171. $pagination .= \Html::anchor(rtrim(static::$pagination_url, '/') . $url . '/' . static::$suffix_url, $i);
  172. }
  173. }
  174. return $pagination;
  175. }
  176. /**
  177. * Pagination "Next" link
  178. *
  179. * @static
  180. * @access public
  181. * @param string $value The text displayed in link
  182. * @return mixed The next link
  183. */
  184. public static function next_link($value)
  185. {
  186. if (static::$total_pages == 1)
  187. {
  188. return '';
  189. }
  190. if (static::$current_page == static::$total_pages)
  191. {
  192. return $value;
  193. }
  194. else
  195. {
  196. $next_page = static::$current_page + 1;
  197. return \Html::anchor(rtrim(static::$pagination_url, '/').'/'.$next_page . '/'. static::$suffix_url, $value);
  198. }
  199. }
  200. /**
  201. * Pagination "Previous" link
  202. *
  203. * @static
  204. * @access public
  205. * @param string $value The text displayed in link
  206. * @return mixed The previous link
  207. */
  208. public static function prev_link($value)
  209. {
  210. if (static::$total_pages == 1)
  211. {
  212. return '';
  213. }
  214. if (static::$current_page == 1)
  215. {
  216. return $value;
  217. }
  218. else
  219. {
  220. $previous_page = static::$current_page - 1;
  221. $previous_page = ($previous_page == 1) ? '' : '/' . $previous_page;
  222. return \Html::anchor(rtrim(static::$pagination_url, '/') . $previous_page . '/' . static::$suffix_url, $value);
  223. }
  224. }
  225. /**
  226. * Build query string
  227. *
  228. * @static
  229. * @access public
  230. * @param mixed $values
  231. * @return string
  232. */
  233. public static function build_get_query($values)
  234. {
  235. $dataset = array ();
  236. $check_get_input = function($value, & $dataset)
  237. {
  238. $data = \Hybrid\Input::get($value);
  239. if (empty($data))
  240. {
  241. return false;
  242. }
  243. else
  244. {
  245. array_push($dataset, sprintf('%s=%s', $value, $data));
  246. return;
  247. }
  248. };
  249. if (is_array($values))
  250. {
  251. foreach ($values as $value)
  252. {
  253. $check_get_input($value, $dataset);
  254. }
  255. }
  256. else
  257. {
  258. $check_get_input($values, $dataset);
  259. }
  260. return '?' . implode('&', $dataset);
  261. }
  262. }
  263. /* End of file pagination.php */