/assets/snippets/ditto/snippet.splitpagination.php

https://github.com/good-web-master/modx.evo.custom · PHP · 168 lines · 82 code · 51 blank · 35 comment · 35 complexity · b25f96e0633f18748ba43afc6da0bf9b MD5 · raw file

  1. <?php
  2. if(!defined('MODX_BASE_PATH')){die('What are you doing? Get out of here!');}
  3. /*
  4. e.g. 1
  5. [[splitPagination]]
  6. will just create a placeholder named "splitPages" - to insert the pagination bar into the correct place of your document use the [+splitPages+] placeholder
  7. e.g.2
  8. [[splitPagination? &id=`myDitto` &pagesToShow=`10` &constantEndCount=`3` &ellipses=`myChunk` &return=`1`]]
  9. */
  10. if (!function_exists("generatePagination")) {
  11. function generatePagination($currentPage, $totalResults, $resultsPerPage, $pagesToShow, $constantEndCount) {
  12. // How many pages are there altogether?
  13. $totalPages = ceil($totalResults / $resultsPerPage);
  14. // How far through are we?
  15. $pagesBefore = $currentPage - 1;
  16. $pagesAfter = $totalPages - $currentPage;
  17. // An array of tabs to display (made up of page numbers)
  18. $tabArr = array();
  19. // If we need to split the pagination
  20. if($totalPages > $pagesToShow) {
  21. // If we're currently more than half way through
  22. if($pagesBefore > $pagesToShow/2) {
  23. // Always show pages 1 and 2 (or whatever the constant end count is), then a separator
  24. for($i=1; $i<=$constantEndCount; $i++) { $tabArr[] = $i; }
  25. $tabArr[] = 0;
  26. // If the number of pages after the current page is more than the number of available slots
  27. if($pagesAfter >= $pagesToShow/2) {
  28. $empty_slots_before = floor( ($pagesToShow - ($constantEndCount*2 /*there are two ends*/) - 1 /*the current page*/) / 2 /*only half*/) -1 /*the set of dots counts as one slot*/;
  29. } else {
  30. $empty_slots_before = $pagesToShow - count($tabArr) - 1 /*current page*/ - $pagesAfter /*How many after*/;
  31. }
  32. // Insert the tabs
  33. for($i=($currentPage-$empty_slots_before); $i<$currentPage; $i++) { $tabArr[] = $i; }
  34. } else {
  35. for($i=1; $i<$currentPage; $i++) { $tabArr[] = $i; }
  36. }
  37. // Insert current page
  38. $tabArr[] = $currentPage;
  39. // If we're less than half way through
  40. if($pagesAfter > $pagesToShow/2) {
  41. // How many more slots do we want to show after the current page?
  42. $empty_slots_after = $pagesToShow - count($tabArr) - $constantEndCount - 1/*the set of dots*/;
  43. // Insert the tabs
  44. for($i=($currentPage+1); $i<=$currentPage+$empty_slots_after; $i++) { $tabArr[] = $i; }
  45. // Add a separator
  46. $tabArr[] = 0;
  47. // Always show pages last 2 pages (or whatever the constant end count is)
  48. for($i=$totalPages-$constantEndCount+1; $i<=$totalPages; $i++) { $tabArr[] = $i; }
  49. } else {
  50. for($i=($currentPage+1); $i<=$totalPages; $i++) { $tabArr[] = $i; }
  51. }
  52. } else { // No need to split the pagination
  53. for($i=1;$i<=$totalPages;$i++) { $tabArr[] = $i; }
  54. }
  55. return $tabArr;
  56. } // end function
  57. } // end if
  58. // ----------------------------------------------------------------------------------------------------------------
  59. // Define some paramaters
  60. $id = isset($id) ? $id.'_' : '';
  61. $total = isset($total) ? $total : $modx->getPlaceholder($id."total");
  62. $start = isset($start) ? $start : $modx->getPlaceholder($id."start");
  63. $display = isset($display) ? $display : $modx->getPlaceholder($id."perPage");
  64. $currentPage = isset($currentPage) ? $currentPage : $modx->getPlaceholder($id."current");
  65. $landing = isset($tagDocumentID) ? $tagDocumentID : $modx->documentObject['id'];
  66. $pagesToShow = isset($pagesToShow) && is_numeric($pagesToShow) ? $pagesToShow : 15;
  67. $constantEndCount = isset($constantEndCount) && is_numeric($constantEndCount) ? $constantEndCount : 2;
  68. $tplEllipses = isset($tplEllipses) ? $modx->getChunk($tplEllipses) : '';
  69. $tplEllipses = !empty($tplEllipses) ? $tplEllipses : '<span class="splitPagination">...</span>';
  70. $tplCurrent = isset($tplCurrent) ? $modx->getChunk($tplCurrent) : '';
  71. $tplCurrent = !empty($tplCurrent) ? $tplCurrent : '<span class="ditto_currentpage ditto_page_[+page+][+class+]">[+page+]</span>';
  72. $tplPageLink = isset($tplPageLink) ? $modx->getChunk($tplPageLink) : '';
  73. $tplPageLink = !empty($tplPageLink) ? $tplPageLink : '<a class="ditto_page ditto_page_[+page+][+class+]" href="[+url+]">[+page+]</a>';
  74. $firstClass = isset($firstClass) ? $firstClass: 'first';
  75. $lastClass = isset($lastClass) ? $lastClass: 'last';
  76. $return = isset($return) ? $return : 0;
  77. // Do nothing if there's nothing to do
  78. if ($total == 0 || $display==0 || $total <= $display) {
  79. return false;
  80. }
  81. // What page are we on?
  82. $page = ceil($start/$display);
  83. // Generate the pagination
  84. $paginationArray = generatePagination($page, $total, $display, $pagesToShow, $constantEndCount);
  85. // Create empty placeholder for output
  86. $ph = "";
  87. // Build the output
  88. foreach($paginationArray as $page) {
  89. $inc = ($page-1)*$display;
  90. // First or last classes
  91. if ($page == 1) {
  92. $newClass = ' ' . $firstClass;
  93. } else if ($page == ceil($total/$display)) {
  94. $newClass = ' ' . $lastClass;
  95. } else {
  96. $newClass = '';
  97. }
  98. // Insert into output
  99. if($page == 0) {
  100. $ph .= $tplEllipses; // print an elipse, representing pages that aren't displayed
  101. } else if ($inc==$_GET[$id.'start']) {
  102. $newLink = str_replace('[+class+]', $newClass , $tplCurrent);
  103. $ph .= str_replace('[+page+]', $page, $newLink);
  104. } else {
  105. $newLink = str_replace('[+url+]', ditto::buildURL("start=".$inc,$landing,$id) , $tplPageLink);
  106. $newLink = str_replace('[+class+]', $newClass , $newLink);
  107. $ph .= str_replace('[+page+]', $page, $newLink);
  108. }
  109. }
  110. // Set the placeholder
  111. $modx->setPlaceholder($id."splitPages",$ph);
  112. // Or return the output
  113. if ($return) return $ph;
  114. ?>