PageRenderTime 27ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/Pagination.php

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