diff options
Diffstat (limited to 'views/default/navigation/pagination.php')
| -rw-r--r-- | views/default/navigation/pagination.php | 248 |
1 files changed, 118 insertions, 130 deletions
diff --git a/views/default/navigation/pagination.php b/views/default/navigation/pagination.php index 11e3b215f..04044c51c 100644 --- a/views/default/navigation/pagination.php +++ b/views/default/navigation/pagination.php @@ -1,144 +1,132 @@ <?php +/** + * Elgg pagination + * + * @package Elgg + * @subpackage Core + * + * @uses int $vars['offset'] The offset in the list + * @uses int $vars['limit'] Number of items per page + * @uses int $vars['count'] Number of items in list + * @uses string $vars['base_url'] Base URL to use in links + * @uses string $vars['offset_key'] The string to use for offet in the URL + */ - /** - * 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-2009 - * @link http://elgg.org/ - * - */ - - if (!isset($vars['offset'])) { - $offset = 0; - } else { - $offset = $vars['offset']; - } - if ((!isset($vars['limit'])) || (!$vars['limit'])) { - $limit = 10; - } else { - $limit = (int)$vars['limit']; - } - if (!isset($vars['count'])) { - $count = 0; - } else { - $count = $vars['count']; - } - if (!isset($vars['word'])) { - $word = "offset"; - } else { - $word = $vars['word']; +if (elgg_in_context('widget')) { + // widgets do not show pagination + return true; +} + +$offset = abs((int) elgg_extract('offset', $vars, 0)); +// because you can say $vars['limit'] = 0 +if (!$limit = (int) elgg_extract('limit', $vars, 10)) { + $limit = 10; +} + +$count = (int) elgg_extract('count', $vars, 0); +$offset_key = elgg_extract('offset_key', $vars, 'offset'); +// some views pass an empty string for base_url +if (isset($vars['base_url']) && $vars['base_url']) { + $base_url = $vars['base_url']; +} else if (isset($vars['baseurl']) && $vars['baseurl']) { + elgg_deprecated_notice("Use 'base_url' instead of 'baseurl' for the navigation/pagination view", 1.8); + $base_url = $vars['baseurl']; +} else { + $base_url = current_page_url(); +} + +$num_pages = elgg_extract('num_pages', $vars, 10); +$delta = ceil($num_pages / 2); + +if ($count <= $limit && $offset == 0) { + // no need for pagination + return true; +} + +$total_pages = ceil($count / $limit); +$current_page = ceil($offset / $limit) + 1; + +$pages = new stdClass(); +$pages->prev = array( + 'text' => '« ' . elgg_echo('previous'), + 'href' => '', + 'is_trusted' => true, +); +$pages->next = array( + 'text' => elgg_echo('next') . ' »', + 'href' => '', + 'is_trusted' => true, +); +$pages->items = array(); + +// Add pages before the current page +if ($current_page > 1) { + $prev_offset = $offset - $limit; + if ($prev_offset < 0) { + $prev_offset = 0; } - if (isset($vars['nonefound'])) { - $nonefound = $vars['nonefound']; - } else { - $nonefound = true; + + $pages->prev['href'] = elgg_http_add_url_query_elements($base_url, array($offset_key => $prev_offset)); + + $first_page = $current_page - $delta; + if ($first_page < 1) { + $first_page = 1; } - - $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) && get_context() != 'widget') { + $pages->items = range($first_page, $current_page - 1); +} -?> -<div class="pagination"> -<?php +$pages->items[] = $current_page; - 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\">« ". 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>"; - - } - - $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; - - } + $pages->next['href'] = elgg_http_add_url_query_elements($base_url, array($offset_key => $next_offset)); + $last_page = $current_page + $delta; + if ($last_page > $total_pages) { + $last_page = $total_pages; } - - 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") . " »</a>"; - + + $pages->items = array_merge($pages->items, range($current_page + 1, $last_page)); +} + + +echo '<ul class="elgg-pagination">'; + +if ($pages->prev['href']) { + $link = elgg_view('output/url', $pages->prev); + echo "<li>$link</li>"; +} else { + echo "<li class=\"elgg-state-disabled\"><span>{$pages->prev['text']}</span></li>"; +} + +foreach ($pages->items as $page) { + if ($page == $current_page) { + echo "<li class=\"elgg-state-selected\"><span>$page</span></li>"; + } else { + $page_offset = (($page - 1) * $limit); + $url = elgg_http_add_url_query_elements($base_url, array($offset_key => $page_offset)); + $link = elgg_view('output/url', array( + 'href' => $url, + 'text' => $page, + 'is_trusted' => true, + )); + echo "<li>$link</li>"; } +} -?> -<br class="clearfloat" /> -</div> -<?php - } // end of pagination check if statement -?>
\ No newline at end of file +if ($pages->next['href']) { + $link = elgg_view('output/url', $pages->next); + echo "<li>$link</li>"; +} else { + echo "<li class=\"elgg-state-disabled\"><span>{$pages->next['text']}</span></li>"; +} + +echo '</ul>'; |
