PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Pagination.php

https://bitbucket.org/zhemel/cloudengine
PHP | 244 lines | 143 code | 33 blank | 68 comment | 30 complexity | 8a691beb77b10117546ac274c63fb702 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 4.3.2 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2009, 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 $total_rows = ''; // Total number of items (database results)
  28. var $per_page = 10; // Max number of items you want shown per page
  29. var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
  30. var $cur_page = 0; // The current page being viewed
  31. var $first_link = '&lsaquo; First';
  32. var $next_link = '&gt;';
  33. var $prev_link = '&lt;';
  34. var $last_link = 'Last &rsaquo;';
  35. var $uri_segment = 3;
  36. var $full_tag_open = '';
  37. var $full_tag_close = '';
  38. var $first_tag_open = '';
  39. var $first_tag_close = '&nbsp;';
  40. var $last_tag_open = '&nbsp;';
  41. var $last_tag_close = '';
  42. var $cur_tag_open = '&nbsp;<strong>';
  43. var $cur_tag_close = '</strong>';
  44. var $next_tag_open = '&nbsp;';
  45. var $next_tag_close = '&nbsp;';
  46. var $prev_tag_open = '&nbsp;';
  47. var $prev_tag_close = '';
  48. var $num_tag_open = '&nbsp;';
  49. var $num_tag_close = '';
  50. var $page_query_string = FALSE;
  51. var $query_string_segment = 'per_page';
  52. /**
  53. * Constructor
  54. *
  55. * @access public
  56. * @param array initialization parameters
  57. */
  58. function CI_Pagination($params = array())
  59. {
  60. if (count($params) > 0)
  61. {
  62. $this->initialize($params);
  63. }
  64. log_message('debug', "Pagination Class Initialized");
  65. }
  66. // --------------------------------------------------------------------
  67. /**
  68. * Initialize Preferences
  69. *
  70. * @access public
  71. * @param array initialization parameters
  72. * @return void
  73. */
  74. function initialize($params = array())
  75. {
  76. if (count($params) > 0)
  77. {
  78. foreach ($params as $key => $val)
  79. {
  80. if (isset($this->$key))
  81. {
  82. $this->$key = $val;
  83. }
  84. }
  85. }
  86. }
  87. // --------------------------------------------------------------------
  88. /**
  89. * Generate the pagination links
  90. *
  91. * @access public
  92. * @return string
  93. */
  94. function create_links()
  95. {
  96. // If our item count or per-page total is zero there is no need to continue.
  97. if ($this->total_rows == 0 OR $this->per_page == 0)
  98. {
  99. return '';
  100. }
  101. // Calculate the total number of pages
  102. $num_pages = ceil($this->total_rows / $this->per_page);
  103. // Is there only one page? Hm... nothing more to do here then.
  104. if ($num_pages == 1)
  105. {
  106. return '';
  107. }
  108. // Determine the current page number.
  109. $CI =& get_instance();
  110. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  111. {
  112. if ($CI->input->get($this->query_string_segment) != 0)
  113. {
  114. $this->cur_page = $CI->input->get($this->query_string_segment);
  115. // Prep the current page - no funny business!
  116. $this->cur_page = (int) $this->cur_page;
  117. }
  118. }
  119. else
  120. {
  121. if ($CI->uri->segment($this->uri_segment) != 0)
  122. {
  123. $this->cur_page = $CI->uri->segment($this->uri_segment);
  124. // Prep the current page - no funny business!
  125. $this->cur_page = (int) $this->cur_page;
  126. }
  127. }
  128. $this->num_links = (int)$this->num_links;
  129. if ($this->num_links < 1)
  130. {
  131. show_error('Your number of links must be a positive number.');
  132. }
  133. if ( ! is_numeric($this->cur_page))
  134. {
  135. $this->cur_page = 0;
  136. }
  137. // Is the page number beyond the result range?
  138. // If so we show the last page
  139. if ($this->cur_page > $this->total_rows)
  140. {
  141. $this->cur_page = ($num_pages - 1) * $this->per_page;
  142. }
  143. $uri_page_number = $this->cur_page;
  144. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  145. // Calculate the start and end numbers. These determine
  146. // which number to start and end the digit links with
  147. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  148. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  149. // Is pagination being used over GET or POST? If get, add a per_page query
  150. // string. If post, add a trailing slash to the base URL if needed
  151. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE)
  152. {
  153. $this->base_url = rtrim($this->base_url).'&amp;'.$this->query_string_segment.'=';
  154. }
  155. else
  156. {
  157. $this->base_url = rtrim($this->base_url, '/') .'/';
  158. }
  159. // And here we go...
  160. $output = '';
  161. // Render the "First" link
  162. if ($this->cur_page > ($this->num_links + 1))
  163. {
  164. $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  165. }
  166. // Render the "previous" link
  167. if ($this->cur_page != 1)
  168. {
  169. $i = $uri_page_number - $this->per_page;
  170. if ($i == 0) $i = '';
  171. $output .= $this->prev_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  172. }
  173. // Write the digit links
  174. for ($loop = $start -1; $loop <= $end; $loop++)
  175. {
  176. $i = ($loop * $this->per_page) - $this->per_page;
  177. if ($i >= 0)
  178. {
  179. if ($this->cur_page == $loop)
  180. {
  181. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  182. }
  183. else
  184. {
  185. $n = ($i == 0) ? '' : $i;
  186. $output .= $this->num_tag_open.'<a href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
  187. }
  188. }
  189. }
  190. // Render the "next" link
  191. if ($this->cur_page < $num_pages)
  192. {
  193. $output .= $this->next_tag_open.'<a href="'.$this->base_url.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
  194. }
  195. // Render the "Last" link
  196. if (($this->cur_page + $this->num_links) < $num_pages)
  197. {
  198. $i = (($num_pages * $this->per_page) - $this->per_page);
  199. $output .= $this->last_tag_open.'<a href="'.$this->base_url.$i.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  200. }
  201. // Kill double slashes. Note: Sometimes we can end up with a double slash
  202. // in the penultimate link so we'll kill all double slashes.
  203. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  204. // Add the wrapper HTML if exists
  205. $output = $this->full_tag_open.$output.$this->full_tag_close;
  206. return $output;
  207. }
  208. }
  209. // END Pagination Class
  210. /* End of file Pagination.php */
  211. /* Location: ./system/libraries/Pagination.php */