PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/releases/application/libraries/MY_Pagination.php

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