Pagination.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 $prev_link = '&lt;';
  37. var $last_link = 'Last &rsaquo;';
  38. var $uri_segment = 3;
  39. var $full_tag_open = '';
  40. var $full_tag_close = '';
  41. var $first_tag_open = '';
  42. var $first_tag_close = '&nbsp;';
  43. var $last_tag_open = '&nbsp;';
  44. var $last_tag_close = '';
  45. var $first_url = ''; // Alternative URL for the First Page.
  46. var $cur_tag_open = '&nbsp;<strong>';
  47. var $cur_tag_close = '</strong>';
  48. var $next_tag_open = '&nbsp;';
  49. var $next_tag_close = '&nbsp;';
  50. var $prev_tag_open = '&nbsp;';
  51. var $prev_tag_close = '';
  52. var $num_tag_open = '&nbsp;';
  53. var $num_tag_close = '';
  54. var $page_query_string = FALSE;
  55. var $query_string_segment = 'per_page';
  56. var $display_pages = TRUE;
  57. var $anchor_class = '';
  58. /**
  59. * Constructor
  60. *
  61. * @access public
  62. * @param array initialization parameters
  63. */
  64. public function __construct($params = array())
  65. {
  66. if (count($params) > 0)
  67. {
  68. $this->initialize($params);
  69. }
  70. if ($this->anchor_class != '')
  71. {
  72. $this->anchor_class = 'class="'.$this->anchor_class.'" ';
  73. }
  74. log_message('debug', "Pagination Class Initialized");
  75. }
  76. // --------------------------------------------------------------------
  77. /**
  78. * Initialize Preferences
  79. *
  80. * @access public
  81. * @param array initialization parameters
  82. * @return void
  83. */
  84. function initialize($params = array())
  85. {
  86. if (count($params) > 0)
  87. {
  88. foreach ($params as $key => $val)
  89. {
  90. if (isset($this->$key))
  91. {
  92. $this->$key = $val;
  93. }
  94. }
  95. }
  96. }
  97. // --------------------------------------------------------------------
  98. /**
  99. * Generate the pagination links
  100. *
  101. * @access public
  102. * @return string
  103. */
  104. function create_links()
  105. {
  106. // If our item count or per-page total is zero there is no need to continue.
  107. if ($this->total_rows == 0 OR $this->per_page == 0)
  108. {
  109. return '';
  110. }
  111. // Calculate the total number of pages
  112. $num_pages = ceil($this->total_rows / $this->per_page);
  113. // Is there only one page? Hm... nothing more to do here then.
  114. if ($num_pages == 1)
  115. {
  116. return '';
  117. }
  118. // Set the base page index for starting page number
  119. if ($this->use_page_numbers)
  120. {
  121. $base_page = 1;
  122. }
  123. else
  124. {
  125. $base_page = 0;
  126. }
  127. // Determine the current page number.
  128. $CI =& get_instance();
  129. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  130. {
  131. if ($CI->input->get($this->query_string_segment) != $base_page)
  132. {
  133. $this->cur_page = $CI->input->get($this->query_string_segment);
  134. // Prep the current page - no funny business!
  135. $this->cur_page = (int) $this->cur_page;
  136. }
  137. }
  138. else
  139. {
  140. if ($CI->uri->segment($this->uri_segment) != $base_page)
  141. {
  142. $this->cur_page = $CI->uri->segment($this->uri_segment);
  143. // Prep the current page - no funny business!
  144. $this->cur_page = (int) $this->cur_page;
  145. }
  146. }
  147. // Set current page to 1 if using page numbers instead of offset
  148. if ($this->use_page_numbers AND $this->cur_page == 0)
  149. {
  150. $this->cur_page = $base_page;
  151. }
  152. $this->num_links = (int)$this->num_links;
  153. if ($this->num_links < 1)
  154. {
  155. show_error('Your number of links must be a positive number.');
  156. }
  157. if ( ! is_numeric($this->cur_page))
  158. {
  159. $this->cur_page = $base_page;
  160. }
  161. // Is the page number beyond the result range?
  162. // If so we show the last page
  163. if ($this->use_page_numbers)
  164. {
  165. if ($this->cur_page > $num_pages)
  166. {
  167. $this->cur_page = $num_pages;
  168. }
  169. }
  170. else
  171. {
  172. if ($this->cur_page > $this->total_rows)
  173. {
  174. $this->cur_page = ($num_pages - 1) * $this->per_page;
  175. }
  176. }
  177. $uri_page_number = $this->cur_page;
  178. if ( ! $this->use_page_numbers)
  179. {
  180. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  181. }
  182. // Calculate the start and end numbers. These determine
  183. // which number to start and end the digit links with
  184. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  185. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  186. // Is pagination being used over GET or POST? If get, add a per_page query
  187. // string. If post, add a trailing slash to the base URL if needed
  188. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  189. {
  190. $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
  191. }
  192. else
  193. {
  194. $this->base_url = rtrim($this->base_url, '/') .'/';
  195. }
  196. // And here we go...
  197. $output = '';
  198. // Render the "First" link
  199. if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
  200. {
  201. $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
  202. $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  203. }
  204. // Render the "previous" link
  205. if ($this->prev_link !== FALSE AND $this->cur_page != 1)
  206. {
  207. if ($this->use_page_numbers)
  208. {
  209. $i = $uri_page_number - 1;
  210. }
  211. else
  212. {
  213. $i = $uri_page_number - $this->per_page;
  214. }
  215. if ($i == 0 && $this->first_url != '')
  216. {
  217. $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  218. }
  219. else
  220. {
  221. $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
  222. $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  223. }
  224. }
  225. // Render the pages
  226. if ($this->display_pages !== FALSE)
  227. {
  228. // Write the digit links
  229. for ($loop = $start -1; $loop <= $end; $loop++)
  230. {
  231. if ($this->use_page_numbers)
  232. {
  233. $i = $loop;
  234. }
  235. else
  236. {
  237. $i = ($loop * $this->per_page) - $this->per_page;
  238. }
  239. if ($i >= $base_page)
  240. {
  241. if ($this->cur_page == $loop)
  242. {
  243. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  244. }
  245. else
  246. {
  247. $n = ($i == $base_page) ? '' : $i;
  248. if ($n == '' && $this->first_url != '')
  249. {
  250. $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
  251. }
  252. else
  253. {
  254. $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
  255. $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
  256. }
  257. }
  258. }
  259. }
  260. }
  261. // Render the "next" link
  262. if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
  263. {
  264. if ($this->use_page_numbers)
  265. {
  266. $i = $this->cur_page + 1;
  267. }
  268. else
  269. {
  270. $i = ($this->cur_page * $this->per_page);
  271. }
  272. $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  273. }
  274. // Render the "Last" link
  275. if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
  276. {
  277. if ($this->use_page_numbers)
  278. {
  279. $i = $num_pages;
  280. }
  281. else
  282. {
  283. $i = (($num_pages * $this->per_page) - $this->per_page);
  284. }
  285. $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  286. }
  287. // Kill double slashes. Note: Sometimes we can end up with a double slash
  288. // in the penultimate link so we'll kill all double slashes.
  289. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  290. // Add the wrapper HTML if exists
  291. $output = $this->full_tag_open.$output.$this->full_tag_close;
  292. return $output;
  293. }
  294. }
  295. // END Pagination Class
  296. /* End of file Pagination.php */
  297. /* Location: ./system/libraries/Pagination.php */