PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/MY_Pagination.php

http://github.com/imagecms/ImageCMS
PHP | 348 lines | 229 code | 69 blank | 50 comment | 99 complexity | 36b5cd9ebbcaa46f8f18a0ae0f60aea9 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. if (!defined('BASEPATH')) {
  3. exit('No direct script access allowed');
  4. }
  5. class MY_Pagination extends CI_Pagination
  6. {
  7. public $container = 'page';
  8. public function __construct() {
  9. parent::__construct();
  10. $this->first_link = '&lsaquo; ' . lang('First');
  11. $this->last_link = lang('Last') . ' &rsaquo;';
  12. }
  13. /**
  14. * Fix & -> ? first param
  15. * @return string
  16. */
  17. public function create_links() {
  18. // If our item count or per-page total is zero there is no need to continue.
  19. if ($this->total_rows == 0 OR $this->per_page == 0) {
  20. return '';
  21. }
  22. // Calculate the total number of pages
  23. $num_pages = ceil($this->total_rows / $this->per_page);
  24. // Is there only one page? Hm... nothing more to do here then.
  25. if ($num_pages == 1) {
  26. return '';
  27. }
  28. // Set the base page index for starting page number
  29. if ($this->use_page_numbers) {
  30. $base_page = 1;
  31. } else {
  32. $base_page = 0;
  33. }
  34. // Determine the current page number.
  35. $CI =& get_instance();
  36. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) {
  37. if ($CI->input->get($this->query_string_segment) != $base_page) {
  38. $this->cur_page = $CI->input->get($this->query_string_segment);
  39. // Prep the current page - no funny business!
  40. $this->cur_page = (int) $this->cur_page;
  41. }
  42. } else {
  43. if ($CI->uri->segment($this->uri_segment) != $base_page) {
  44. $this->cur_page = $CI->uri->segment($this->uri_segment);
  45. // Prep the current page - no funny business!
  46. $this->cur_page = (int) $this->cur_page;
  47. }
  48. }
  49. // Set current page to 1 if using page numbers instead of offset
  50. if ($this->use_page_numbers AND $this->cur_page == 0) {
  51. $this->cur_page = $base_page;
  52. }
  53. $this->num_links = (int) $this->num_links;
  54. if ($this->num_links < 1) {
  55. show_error('Your number of links must be a positive number.');
  56. }
  57. if (!is_numeric($this->cur_page)) {
  58. $this->cur_page = $base_page;
  59. }
  60. // Is the page number beyond the result range?
  61. // If so we show the last page
  62. if ($this->use_page_numbers) {
  63. if ($this->cur_page > $num_pages) {
  64. $this->cur_page = $num_pages;
  65. }
  66. } else {
  67. if ($this->cur_page > $this->total_rows) {
  68. $this->cur_page = ($num_pages - 1) * $this->per_page;
  69. }
  70. }
  71. $uri_page_number = $this->cur_page;
  72. if (!$this->use_page_numbers) {
  73. $this->cur_page = floor(($this->cur_page / $this->per_page) + 1);
  74. }
  75. // Calculate the start and end numbers. These determine
  76. // which number to start and end the digit links with
  77. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  78. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  79. // Is pagination being used over GET or POST? If get, add a per_page query
  80. // string. If post, add a trailing slash to the base URL if needed
  81. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) {
  82. if (strpos($this->base_url, '?') === false) {
  83. $this->base_url = rtrim($this->base_url) . '?' . $this->query_string_segment . '=';
  84. } else {
  85. $this->base_url = rtrim($this->base_url) . '&amp;' . $this->query_string_segment . '=';
  86. }
  87. } else {
  88. $this->base_url = rtrim($this->base_url, '/') . '/';
  89. }
  90. // And here we go...
  91. $output = '';
  92. // Render the "First" link
  93. if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1)) {
  94. $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
  95. $output .= $this->first_tag_open . '<a ' . $this->anchor_class . 'href="' . $first_url . '">' . $this->first_link . '</a>' . $this->first_tag_close;
  96. }
  97. // Render the "previous" link
  98. if ($this->prev_link !== FALSE AND $this->cur_page != 1) {
  99. if ($this->use_page_numbers) {
  100. $i = $uri_page_number - 1;
  101. } else {
  102. $i = $uri_page_number - $this->per_page;
  103. }
  104. if ($i == 0 && $this->first_url != '') {
  105. $output .= $this->prev_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->first_url . '">' . $this->prev_link . '</a>' . $this->prev_tag_close;
  106. } else {
  107. $i = ($i == 0) ? '' : $this->prefix . $i . $this->suffix;
  108. $output .= $this->prev_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->base_url . $i . '">' . $this->prev_link . '</a>' . $this->prev_tag_close;
  109. }
  110. }
  111. // Render the pages
  112. if ($this->display_pages !== FALSE) {
  113. // Write the digit links
  114. for ($loop = $start - 1; $loop <= $end; $loop++) {
  115. if ($this->use_page_numbers) {
  116. $i = $loop;
  117. } else {
  118. $i = ($loop * $this->per_page) - $this->per_page;
  119. }
  120. if ($i >= $base_page) {
  121. if ($this->cur_page == $loop) {
  122. $output .= $this->cur_tag_open . $loop . $this->cur_tag_close; // Current page
  123. } else {
  124. $n = ($i == $base_page) ? '' : $i;
  125. if ($n == '' && $this->first_url != '') {
  126. $output .= $this->num_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->first_url . '">' . $loop . '</a>' . $this->num_tag_close;
  127. } else {
  128. $n = ($n == '') ? '' : $this->prefix . $n . $this->suffix;
  129. $output .= $this->num_tag_open . '<a ' . $this->anchor_class . 'href="' . $this->base_url . $n . '">' . $loop . '</a>' . $this->num_tag_close;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. // Render the "next" link
  136. if ($this->next_link !== FALSE AND $this->cur_page < $num_pages) {
  137. if ($this->use_page_numbers) {
  138. $i = $this->cur_page + 1;
  139. } else {
  140. $i = ($this->cur_page * $this->per_page);
  141. }
  142. $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;
  143. }
  144. // Render the "Last" link
  145. if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages) {
  146. if ($this->use_page_numbers) {
  147. $i = $num_pages;
  148. } else {
  149. $i = (($num_pages * $this->per_page) - $this->per_page);
  150. }
  151. $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;
  152. }
  153. // Kill double slashes. Note: Sometimes we can end up with a double slash
  154. // in the penultimate link so we'll kill all double slashes.
  155. $output = preg_replace('#([^:])//+#', "\\1/", $output);
  156. // Add the wrapper HTML if exists
  157. $output = $this->full_tag_open . $output . $this->full_tag_close;
  158. return $output;
  159. }
  160. public function create_links_ajax() {
  161. $CI = &get_instance();
  162. // If our item count or per-page total is zero there is no need to continue.
  163. if ($this->total_rows == 0 OR $this->per_page == 0) {
  164. return '';
  165. }
  166. // Calculate the total number of pages
  167. $num_pages = ceil($this->total_rows / $this->per_page);
  168. // Is there only one page? Hm... nothing more to do here then.
  169. if ($num_pages == 1) {
  170. return '';
  171. }
  172. // Determine the current page number.
  173. $CI = &get_instance();
  174. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) {
  175. if ($CI->input->get($this->query_string_segment) != 0) {
  176. $this->cur_page = $CI->input->get($this->query_string_segment);
  177. // Prep the current page - no funny business!
  178. $this->cur_page = (int) $this->cur_page;
  179. }
  180. } else {
  181. if ($CI->uri->segment($this->uri_segment) != 0) {
  182. $this->cur_page = $CI->uri->segment($this->uri_segment);
  183. // Prep the current page - no funny business!
  184. $this->cur_page = (int) $this->cur_page;
  185. }
  186. }
  187. $this->num_links = (int) $this->num_links;
  188. if ($this->num_links < 1) {
  189. show_error('Your number of links must be a positive number.');
  190. }
  191. if (!is_numeric($this->cur_page)) {
  192. $this->cur_page = 0;
  193. }
  194. // Is the page number beyond the result range?
  195. // If so we show the last page
  196. if ($this->cur_page > $this->total_rows) {
  197. $this->cur_page = ($num_pages - 1) * $this->per_page;
  198. }
  199. $uri_page_number = $this->cur_page;
  200. $this->cur_page = floor(($this->cur_page / $this->per_page) + 1);
  201. // Calculate the start and end numbers. These determine
  202. // which number to start and end the digit links with
  203. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  204. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  205. // Is pagination being used over GET or POST? If get, add a per_page query
  206. // string. If post, add a trailing slash to the base URL if needed
  207. if ($CI->config->item('enable_query_strings') === TRUE OR $this->page_query_string === TRUE) {
  208. $this->base_url = rtrim($this->base_url) . '&' . $this->query_string_segment . '=';
  209. } else {
  210. $this->base_url = rtrim($this->base_url, '/') . '/';
  211. }
  212. // And here we go...
  213. $output = '';
  214. // Check for separated controls
  215. if ($this->separate_controls) {
  216. $controls_output = '';
  217. }
  218. // Render the "First" link
  219. if ($this->cur_page > $this->num_links) {
  220. $output .= $this->first_tag_open . '<a href="' . $this->base_url . 'offset' . '/' . $this->suffix . '">' . $this->first_link . '</a>' . $this->first_tag_close;
  221. }
  222. // Render the "previous" link
  223. if ($this->cur_page != 1) {
  224. $i = $uri_page_number - $this->per_page;
  225. if ($i == 0) {
  226. $i = 'offset';
  227. }
  228. if (!$this->separate_controls) {
  229. $output .= $this->prev_tag_open . '<a href="' . $this->base_url . $i . '/' . $this->suffix . '">' . $this->prev_link . '</a>' . $this->prev_tag_close;
  230. } else {
  231. $controls_output .= $this->prev_tag_open . '<a href="' . $this->base_url . $i . '/' . $this->suffix . '">' . $this->prev_link . '</a>' . $this->prev_tag_close;
  232. }
  233. } else {
  234. if ($this->separate_controls) {
  235. $controls_output .= str_replace('>', ' class="disabled">', $this->prev_tag_open) . '<span>' . $this->prev_link . '</span>' . $this->prev_tag_close;
  236. }
  237. }
  238. // Write the digit links
  239. for ($loop = $start - 1; $loop <= $end; $loop++) {
  240. $i = ($loop * $this->per_page) - $this->per_page;
  241. if ($i >= 0) {
  242. if ($this->cur_page == $loop) {
  243. $output .= $this->cur_tag_open . $loop . $this->cur_tag_close; // Current page
  244. } else {
  245. $n = ($i == 0) ? 'offset' : $i;
  246. $output .= $this->num_tag_open . '<a href="' . $this->base_url . $n . '/' . $this->suffix . '">' . $loop . '</a>' . $this->num_tag_close;
  247. }
  248. }
  249. }
  250. // Render the "next" link
  251. if ($this->cur_page < $num_pages) {
  252. if (!$this->separate_controls) {
  253. $output .= $this->next_tag_open . '<a href="' . $this->base_url . ($this->cur_page * $this->per_page) . '/' . $this->suffix . '">' . $this->next_link . '</a>' . $this->next_tag_close;
  254. } else {
  255. $controls_output .= $this->next_tag_open . '<a href="' . $this->base_url . ($this->cur_page * $this->per_page) . '/' . $this->suffix . '">' . $this->next_link . '</a>' . $this->next_tag_close;
  256. }
  257. } else {
  258. if ($this->separate_controls) {
  259. $controls_output .= str_replace('>', ' class="disabled">', $this->next_tag_open) . '<span>' . $this->next_link . '</span>' . $this->next_tag_close;
  260. }
  261. }
  262. // Render the "Last" link
  263. if (($this->cur_page + $this->num_links) < $num_pages) {
  264. $i = (($num_pages * $this->per_page) - $this->per_page);
  265. $output .= $this->last_tag_open . '<a href="' . $this->base_url . $i . '/' . $this->suffix . '">' . $this->last_link . '</a>' . $this->last_tag_close;
  266. }
  267. // Kill double slashes. Note: Sometimes we can end up with a double slash
  268. // in the penultimate link so we'll kill all double slashes.
  269. $output = preg_replace('#([^:])//+#', "\\1/", $output);
  270. // Add the wrapper HTML if exists
  271. $output = $this->full_tag_open . $output . $this->full_tag_close;
  272. if ($this->separate_controls) {
  273. $controls_output = preg_replace('#([^:])//+#', "\\1/", $controls_output);
  274. $controls_output = $this->controls_tag_open . $controls_output . $this->controls_tag_close;
  275. $output = $output . $controls_output;
  276. }
  277. return $output;
  278. }
  279. }