aboutsummaryrefslogtreecommitdiff
path: root/views/default/navigation
diff options
context:
space:
mode:
Diffstat (limited to 'views/default/navigation')
-rw-r--r--views/default/navigation/pagination.php151
1 files changed, 79 insertions, 72 deletions
diff --git a/views/default/navigation/pagination.php b/views/default/navigation/pagination.php
index fc15470bc..9fcd8153e 100644
--- a/views/default/navigation/pagination.php
+++ b/views/default/navigation/pagination.php
@@ -12,99 +12,106 @@
* @uses string $vars['baseurl'] Base URL to use in links
*/
-$offset = (int) elgg_get_array_value('offset', $vars, 0);
+if (elgg_in_context('widget')) {
+ // widgets do not show pagination
+ return true;
+}
+
+$offset = abs((int) elgg_get_array_value('offset', $vars, 0));
// because you can say $vars['limit'] = 0
if (!$limit = (int) elgg_get_array_value('limit', $vars, 10)) {
$limit = 10;
}
+
$count = (int) elgg_get_array_value('count', $vars, 0);
$word = elgg_get_array_value('word', $vars, 'offset');
-$baseurl = elgg_get_array_value('baseurl', $vars, current_page_url());
+$base_url = elgg_get_array_value('baseurl', $vars, current_page_url());
+
+$num_pages = elgg_get_array_value('num_pages', $vars, 10);
+$delta = ceil($num_pages / 2);
-$totalpages = ceil($count / $limit);
-$currentpage = ceil($offset / $limit) + 1;
+if ($count <= $limit && $offset == 0) {
+ // no need for pagination
+ return true;
+}
-//only display if there is content to paginate through or if we already have an offset
-if (($count > $limit || $offset > 0) && elgg_get_context() != 'widget') {
+$total_pages = ceil($count / $limit);
+$current_page = ceil($offset / $limit) + 1;
+
+$pages = new stdClass();
+$pages->prev = array(
+ 'text' => '&laquo; ' . elgg_echo('previous'),
+ 'href' => '',
+);
+$pages->next = array(
+ 'text' => elgg_echo('next') . ' &raquo;',
+ 'href' => '',
+);
+$pages->items = array();
+
+// Add pages before the current page
+if ($current_page > 1) {
+ $prev_offset = $offset - $limit;
+ if ($prev_offset < 0) {
+ $prev_offset = 0;
+ }
- ?>
+ $pages->prev['href'] = elgg_http_add_url_query_elements($base_url, array($word => $prev_offset));
- <div class="pagination clearfix">
- <?php
+ $first_page = $current_page - $delta;
+ if ($first_page < 1) {
+ $first_page = 1;
+ }
- if ($offset > 0) {
+ $pages->items = range($first_page, $current_page - 1);
+}
- $prevoffset = $offset - $limit;
- if ($prevoffset < 0) {
- $prevoffset = 0;
- }
- $prevurl = elgg_http_add_url_query_elements($baseurl, array($word => $prevoffset));
+$pages->items[] = $current_page;
- echo "<a href=\"{$prevurl}\" class='pagination-previous'>&laquo; ". elgg_echo("previous") ."</a> ";
+
+// add pages after the current one
+if ($current_page < $total_pages) {
+ $next_offset = $offset + $limit;
+ if ($next_offset >= $count) {
+ $next_offset--;
}
- 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>";
- }
-
- $curoffset = (($i - 1) * $limit);
- $counturl = elgg_http_add_url_query_elements($baseurl, array($word => $curoffset));
-
- if ($curoffset != $offset) {
- echo " <a href=\"{$counturl}\" class='pagination-number'>{$i}</a> ";
- } else {
- echo "<span class='pagination-currentpage'>{$i}</span>";
- }
- $prev = $i;
-
- }
+ $pages->next['href'] = elgg_http_add_url_query_elements($base_url, array($word => $next_offset));
+
+ $last_page = $current_page + $delta;
+ if ($last_page > $total_pages) {
+ $last_page = $total_pages;
}
- if ($offset < ($count - $limit)) {
+ $pages->items = array_merge($pages->items, range($current_page + 1, $last_page));
+}
- $nextoffset = $offset + $limit;
- if ($nextoffset >= $count) {
- $nextoffset--;
- }
- $nexturl = elgg_http_add_url_query_elements($baseurl, array($word => $nextoffset));
+echo '<ul class="elgg-pagination">';
- echo " <a href=\"{$nexturl}\" class='pagination-next'>" . elgg_echo("next") . " &raquo;</a>";
+if ($pages->prev['href']) {
+ $link = elgg_view('output/url', $pages->prev);
+ echo "<li>$link</li>";
+} else {
+ echo "<li><span class=\"inactive\">{$pages->prev['text']}</span></li>";
+}
+foreach ($pages->items as $page) {
+ if ($page == $current_page) {
+ echo "<li><span class=\"active\">$page</span></li>";
+ } else {
+ $page_offset = (($page - 1) * $limit);
+ $url = elgg_http_add_url_query_elements($base_url, array($word => $page_offset));
+ echo "<li><a href=\"$url\">$page</a></li>";
}
+}
+
+if ($pages->next['href']) {
+ $link = elgg_view('output/url', $pages->next);
+ echo "<li>$link</li>";
+} else {
+ echo "<li><span class=\"inactive\">{$pages->next['text']}</span></li>";
+}
- ?>
- </div>
- <?php
-} // end of pagination check if statement
+echo '</ul>';