aboutsummaryrefslogtreecommitdiff
path: root/views/default/navigation/pagination.php
blob: 5d4954144a7ac5aa3f42c8bacc1a40ba4ba8fad1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php

	/**
	 * Elgg pagination
	 * 
	 * @package Elgg
	 * @subpackage Core
	 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
	 * @author Curverider Ltd
	 * @copyright Curverider Ltd 2008
	 * @link http://elgg.org/
	 * 
	 */

	if (!isset($vars['offset'])) {
		$offset = 0;
	} else {
		$offset = $vars['offset'];
	}
	if (!isset($vars['limit'])) {
		$limit = 10;
	} else {
		$limit = $vars['limit'];
	}
	if (!isset($vars['count'])) {
		$count = 0;
	} else {
		$count = $vars['count'];
	}
	if (!isset($vars['word'])) {
		$word = "offset";
	} else {
		$word = $vars['word'];
	}
	if (isset($vars['nonefound'])) {
		$nonefound = $vars['nonefound'];
	} else {
		$nonefound = true;
	}
	
	$totalpages = ceil($count / $limit);
	$currentpage = ceil($offset / $limit) + 1;

	$baseurl = preg_replace('/[\&\?]'.$word.'\=[0-9]*/',"",$vars['baseurl']);
	
	//only display if there is content to paginate through or if we already have an offset
	if ($count >= $limit || $offset > 0){

?>

<div class="pagination">
	<p>
<?php

	//if ($count == 0) {
		
	//	static $notfounddisplayed;
	//	if (!isset($notfounddisplayed) && $nonefound) {
	//		echo elgg_echo("notfound");
	//		$notfounddisplayed = true;
	//	}
		
	//}

	if ($offset > 0) {
		
		$prevoffset = $offset - $limit;
		if ($prevoffset < 0) $prevoffset = 0;
		
		$prevurl = $baseurl;
		if (substr_count($baseurl,'?')) {
			$prevurl .= "&{$word}=" . $prevoffset;
		} else {
			$prevurl .= "?{$word}=" . $prevoffset;
		}
		
		echo "<a href=\"{$prevurl}\" class=\"pagination_previous\">&laquo; ". elgg_echo("previous") ."</a> ";
		
	}

	if ($offset > 0 || $offset < ($count - $limit)) {
		
		$currentpage = round($offset / $limit) + 1;
		$allpages = ceil($count / $limit);
		
		$i = 1;
		$pagesarray = array();
		while ($i <= $allpages && $i <= 4) {
			$pagesarray[] = $i;
			$i++;
		}
		$i = $currentpage - 2;
		while ($i <= $allpages && $i <= ($currentpage + 2)) {
			if ($i > 0 && !in_array($i,$pagesarray))
				$pagesarray[] = $i;
			$i++;
		}
		$i = $allpages - 3;
		while ($i <= $allpages) {
			if ($i > 0 && !in_array($i,$pagesarray))
				$pagesarray[] = $i;
			$i++;
		}
		
		sort($pagesarray);
		
		$prev = 0;
		foreach($pagesarray as $i) {
		
			if (($i - $prev) > 1) {
				
				echo "<span class=\"pagination_more\">...</span>";
				
			}
			
			$counturl = $baseurl;
			$curoffset = (($i - 1) * $limit);
			if (substr_count($baseurl,'?')) {
				$counturl .= "&{$word}=" . $curoffset;
			} else {
				$counturl .= "?{$word}=" . $curoffset;
			}
			if ($curoffset != $offset) {
				echo " <a href=\"{$counturl}\" class=\"pagination_number\">{$i}</a> ";
			} else {
				echo "<span class=\"pagination_currentpage\"> {$i} </span>";
			}
			$prev = $i;
		
		}

	}
	
	if ($offset < ($count - $limit)) {
		
		$nextoffset = $offset + $limit;
		if ($nextoffset >= $count) $nextoffset--;
		
		$nexturl = $baseurl;
		if (substr_count($baseurl,'?')) {
			$nexturl .= "&{$word}=" . $nextoffset;
		} else {
			$nexturl .= "?{$word}=" . $nextoffset;
		}
		
		echo " <a href=\"{$nexturl}\" class=\"pagination_next\">" . elgg_echo("next") . " &raquo;</a>";
		
	}

?>
	</p>
</div>

<?php
    } // end of pagination check if statement
?>