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

/movabletype/praized-community/php/function.mtpraizedmerchantspagination.php

http://praized.googlecode.com/
PHP | 92 lines | 65 code | 20 blank | 7 comment | 17 complexity | f5f95152232902eab0eaae8b728c697c MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. function smarty_function_mtpraizedmerchantspagination($args, &$ctx) {
  3. $pagination = $ctx->stash("collections_praized_pagination");
  4. if( ! is_null($pagination) && intval($pagination->page_count) > 1) {
  5. // make the call to the api.
  6. // and use the values in the logic.
  7. $current_page = $pagination->current_page;
  8. $per_page = $pagination->per_page;
  9. $total_entries = $pagination->total_entries;
  10. $total_pages = ceil($total_entries / $per_page);
  11. $url = preg_replace("/\?.+$/", "", $_SERVER["REQUEST_URI"]);
  12. // implementing will_paginate in dirty php..
  13. // this is a straight port of it.
  14. $opt = array(
  15. "class" => "pagination",
  16. "prev_label" => '&laquo; Previous',
  17. "next_label" => 'Next &raquo;',
  18. "inner_window" => 4,
  19. "outer_window" => 1,
  20. "gap_marker" => "..."
  21. );
  22. $inner_window = $opt["inner_window"];
  23. $outer_window = $opt["outer_window"];
  24. $gap_marker = $opt["gap_marker"];
  25. $next_label = $opt["next_label"];
  26. $previous_label = $opt["prev_label"];
  27. $window_from = $current_page - $inner_window;
  28. $window_to = $current_page + $inner_window;
  29. if($window_to > $total_pages) {
  30. $window_from -= $window_to - $total_pages;
  31. $window_to = $total_pages;
  32. } else if($window_from < 1) {
  33. $window_to += 1 - $window_from;
  34. $window_from = 1;
  35. }
  36. $visible = range(1, $total_pages);
  37. $left_gap = range(2 + $outer_window, $window_from);
  38. $right_gap = range($window_to + 1, ($total_pages - $outer_window));
  39. if(($left_gap[sizeof($left_gap) - 1] - $last_gap[0]) > 1)
  40. $visible = array_diff($visible, $left_gap);
  41. if(($right_gap[sizeof($right_gap) - 1] - $right_gap[0]) > 1)
  42. $visible = array_diff($visible, $right_gap);
  43. // page to show
  44. $links = array();
  45. $prev = null;
  46. // building the html.
  47. // TODO.
  48. $str = "<ul class=\"praized-pagination\">";
  49. if($current_page == 1)
  50. $str .= "<li class=\"praized-pagination-disabled\">" . $previous_label . "</li>";
  51. else
  52. $str .= "<li><a href=\"" . $url . "?q=" . $_GET["q"] . "&l=" . $_GET["l"] . "&page=" . ($current_page - 1) . "\">" . $previous_label . "</a></li>";
  53. foreach($visible as $item) {
  54. if($prev != null && $item > $prev + 1)
  55. $str .= "<li>...</li>";
  56. if($current_page == $item)
  57. $str .= "<li class=\"praized-pagination-current\">" . $item . "</li>";
  58. else
  59. $str .= "<li><a href=\"" . $url . "?q=" . $_GET["q"] . "&l=" . $_GET["l"] . "&page=" . $item . "\">" . $item . "</a></li>";
  60. $prev = $item;
  61. }
  62. if($current_page == $total_pages)
  63. $str .= "<li class=\"praized-pagination-disabled\">" . $next_label . "</li>";
  64. else
  65. $str .= "<li><a href=\"" . $url . "?q=" . $_GET["q"] . "&l=" . $_GET["l"] . "&page=" . ($current_page + 1) . "\">" . $next_label . "</a></li>";
  66. $str .= "</ul>";
  67. return $str;
  68. } else {
  69. return "";
  70. }
  71. }
  72. ?>