PageRenderTime 81ms CodeModel.GetById 38ms app.highlight 9ms RepoModel.GetById 31ms app.codeStats 0ms

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