PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/application/libraries/Pagination.php

https://bitbucket.org/earnest-tekkies/sample-php-ci
PHP | 357 lines | 231 code | 51 blank | 75 comment | 52 complexity | 28ce50521caf33ca82705a6bf0929c52 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  10. * @license http://codeigniter.com/user_guide/license.html
  11. * @link http://codeigniter.com
  12. * @since Version 1.0
  13. * @filesource
  14. */
  15. // ------------------------------------------------------------------------
  16. /**
  17. * Pagination Class
  18. *
  19. * @package CodeIgniter
  20. * @subpackage Libraries
  21. * @category Pagination
  22. * @author ExpressionEngine Dev Team
  23. * @link http://codeigniter.com/user_guide/libraries/pagination.html
  24. */
  25. class CI_Pagination {
  26. var $base_url = ''; // The page we are linking to
  27. var $prefix = ''; // A custom prefix added to the path.
  28. var $suffix = ''; // A custom suffix added to the path.
  29. var $total_rows = 0; // Total number of items (database results)
  30. var $per_page = 10; // Max number of items you want shown per page
  31. var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
  32. var $cur_page = 0; // The current page being viewed
  33. var $use_page_numbers = FALSE; // Use page number for segment instead of offset
  34. var $first_link = '&lsaquo; First';
  35. //var $next_link = '&gt;';
  36. var $next_link = 'Next &raquo;';
  37. //var $prev_link = '&lt;';
  38. var $prev_link = 'Prev';
  39. var $last_link = 'Last &rsaquo;';
  40. var $uri_segment = 3;
  41. var $full_tag_open = '';
  42. var $full_tag_close = '';
  43. var $first_tag_open = '';
  44. var $first_tag_close = '&nbsp;';
  45. var $last_tag_open = '&nbsp;';
  46. var $last_tag_close = '';
  47. var $first_url = ''; // Alternative URL for the First Page.
  48. var $cur_tag_open = '&nbsp;<strong>';
  49. var $cur_tag_close = '</strong>';
  50. var $next_tag_open = '&nbsp;';
  51. var $next_tag_close = '&nbsp;';
  52. var $prev_tag_open = '&nbsp;';
  53. var $prev_tag_close = '';
  54. var $num_tag_open = '&nbsp;';
  55. var $num_tag_close = '';
  56. var $page_query_string = FALSE;
  57. var $query_string_segment = 'per_page';
  58. var $display_pages = TRUE;
  59. var $anchor_class = '';
  60. /**
  61. * Constructor
  62. *
  63. * @access public
  64. * @param array initialization parameters
  65. */
  66. public function __construct($params = array())
  67. {
  68. if (count($params) > 0)
  69. {
  70. $this->initialize($params);
  71. }
  72. if ($this->anchor_class != '')
  73. {
  74. $this->anchor_class = 'class="'.$this->anchor_class.'" ';
  75. }
  76. log_message('debug', "Pagination Class Initialized");
  77. }
  78. // --------------------------------------------------------------------
  79. /**
  80. * Initialize Preferences
  81. *
  82. * @access public
  83. * @param array initialization parameters
  84. * @return void
  85. */
  86. function initialize($params = array())
  87. {
  88. if (count($params) > 0)
  89. {
  90. foreach ($params as $key => $val)
  91. {
  92. if (isset($this->$key))
  93. {
  94. $this->$key = $val;
  95. }
  96. }
  97. }
  98. }
  99. // --------------------------------------------------------------------
  100. /**
  101. * Generate the pagination links
  102. *
  103. * @access public
  104. * @return string
  105. */
  106. function create_links()
  107. {
  108. // If our item count or per-page total is zero there is no need to continue.
  109. if ($this->total_rows == 0 OR $this->per_page == 0)
  110. {
  111. return '';
  112. }
  113. // Calculate the total number of pages
  114. $num_pages = ceil($this->total_rows / $this->per_page);
  115. // Is there only one page? Hm... nothing more to do here then.
  116. if ($num_pages == 1)
  117. {
  118. return '';
  119. }
  120. // Set the base page index for starting page number
  121. if ($this->use_page_numbers)
  122. {
  123. $base_page = 1;
  124. }
  125. else
  126. {
  127. $base_page = 0;
  128. }
  129. // Determine the current page number.
  130. $CI =& get_instance();
  131. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  132. {
  133. if ($CI->input->get($this->query_string_segment) != $base_page)
  134. {
  135. $this->cur_page = $CI->input->get($this->query_string_segment);
  136. // Prep the current page - no funny business!
  137. $this->cur_page = (int) $this->cur_page;
  138. }
  139. }
  140. else
  141. {
  142. if ($CI->uri->segment($this->uri_segment) != $base_page)
  143. {
  144. $this->cur_page = $CI->uri->segment($this->uri_segment);
  145. // Prep the current page - no funny business!
  146. $this->cur_page = (int) $this->cur_page;
  147. }
  148. }
  149. // Set current page to 1 if using page numbers instead of offset
  150. if ($this->use_page_numbers AND $this->cur_page == 0)
  151. {
  152. $this->cur_page = $base_page;
  153. }
  154. $this->num_links = (int)$this->num_links;
  155. if ($this->num_links < 1)
  156. {
  157. show_error('Your number of links must be a positive number.');
  158. }
  159. if ( ! is_numeric($this->cur_page))
  160. {
  161. $this->cur_page = $base_page;
  162. }
  163. // Is the page number beyond the result range?
  164. // If so we show the last page
  165. if ($this->use_page_numbers)
  166. {
  167. if ($this->cur_page > $num_pages)
  168. {
  169. $this->cur_page = $num_pages;
  170. }
  171. }
  172. else
  173. {
  174. if ($this->cur_page > $this->total_rows)
  175. {
  176. $this->cur_page = ($num_pages - 1) * $this->per_page;
  177. }
  178. }
  179. $uri_page_number = $this->cur_page;
  180. if ( ! $this->use_page_numbers)
  181. {
  182. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  183. }
  184. // Calculate the start and end numbers. These determine
  185. // which number to start and end the digit links with
  186. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  187. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  188. // Is pagination being used over GET or POST? If get, add a per_page query
  189. // string. If post, add a trailing slash to the base URL if needed
  190. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  191. {
  192. $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
  193. }
  194. else
  195. {
  196. $this->base_url = rtrim($this->base_url, '/') .'/';
  197. }
  198. // And here we go...
  199. $output = '<ul>';
  200. // Render the "First" link
  201. if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
  202. {
  203. $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
  204. $output .= $this->first_tag_open.'<li><a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a></li>'.$this->first_tag_close;
  205. }
  206. // Render the "previous" link
  207. if ($this->prev_link !== FALSE AND $this->cur_page != 1)
  208. {
  209. if ($this->use_page_numbers)
  210. {
  211. $i = $uri_page_number - 1;
  212. }
  213. else
  214. {
  215. $i = $uri_page_number - $this->per_page;
  216. }
  217. if ($i == 0 && $this->first_url != '')
  218. {
  219. $output .= $this->prev_tag_open.'<li><a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a></li>'.$this->prev_tag_close;
  220. }
  221. else
  222. {
  223. $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
  224. $output .= $this->prev_tag_open.'<li><a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a></li>'.$this->prev_tag_close;
  225. }
  226. }else{
  227. $output .= '<li class="disabled"><a href="">'.$this->prev_link.'</a></li>';
  228. }
  229. // Render the pages
  230. if ($this->display_pages !== FALSE)
  231. {
  232. // Write the digit links
  233. for ($loop = $start -1; $loop <= $end; $loop++)
  234. {
  235. if ($this->use_page_numbers)
  236. {
  237. $i = $loop;
  238. }
  239. else
  240. {
  241. $i = ($loop * $this->per_page) - $this->per_page;
  242. }
  243. if ($i >= $base_page)
  244. {
  245. if ($this->cur_page == $loop)
  246. {
  247. //$output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  248. $output .= '<li class="active"><a href="">'.$loop.'</a></li>';
  249. }
  250. else
  251. {
  252. $n = ($i == $base_page) ? '' : $i;
  253. if ($n == '' && $this->first_url != '')
  254. {
  255. $output .= $this->num_tag_open.'<li><a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a></li>'.$this->num_tag_close;
  256. }
  257. else
  258. {
  259. $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
  260. $output .= $this->num_tag_open.'<li><a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a></li>'.$this->num_tag_close;
  261. }
  262. }
  263. }
  264. }
  265. }
  266. // Render the "next" link
  267. if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
  268. {
  269. if ($this->use_page_numbers)
  270. {
  271. $i = $this->cur_page + 1;
  272. }
  273. else
  274. {
  275. $i = ($this->cur_page * $this->per_page);
  276. }
  277. $output .= $this->next_tag_open.'<li><a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a></li>'.$this->next_tag_close;
  278. }else{
  279. //shows 'next' link default as disabled
  280. $output .= $this->next_tag_open.'<li class="disabled"><a '.$this->anchor_class.'href="">'.$this->next_link.'</a></li>';
  281. }
  282. // Render the "Last" link
  283. if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
  284. {
  285. if ($this->use_page_numbers)
  286. {
  287. $i = $num_pages;
  288. }
  289. else
  290. {
  291. $i = (($num_pages * $this->per_page) - $this->per_page);
  292. }
  293. $output .= $this->last_tag_open.'<li><a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a></li>'.$this->last_tag_close;
  294. }
  295. // Kill double slashes. Note: Sometimes we can end up with a double slash
  296. // in the penultimate link so we'll kill all double slashes.
  297. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  298. // Add the wrapper HTML if exists
  299. $output = $this->full_tag_open.$output.$this->full_tag_close;
  300. $output.'</ul>';
  301. return $output;
  302. }
  303. }
  304. // END Pagination Class
  305. /* End of file Pagination.php */
  306. /* Location: ./system/libraries/Pagination.php */