This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Views/paginator.phtml

90 lines
2.3 KiB
PHTML
Raw Normal View History

2013-10-16 13:07:01 +02:00
<?php
if (!isset($this->context->transport->paginator))
return;
$page = $this->context->transport->paginator->page;
$pageCount = $this->context->transport->paginator->pageCount;
2013-10-16 13:07:01 +02:00
$delta = 3;
$pagesVisible = [1, $pageCount];
$pagesVisible = array_merge($pagesVisible, range($page - $delta, $page + $delta));
$pagesVisible = array_filter($pagesVisible, function($x) use ($pageCount) { return $x >= 1 and $x <= $pageCount; });
$pagesVisible = array_unique($pagesVisible);
sort($pagesVisible, SORT_NUMERIC);
$finalPages = [$pagesVisible[0]];
for ($i = 1; $i < count($pagesVisible); $i ++)
{
$prevPage = $pagesVisible[$i - 1];
$subPage = $pagesVisible[$i];
if ($subPage - $prevPage == 2)
$finalPages []= $subPage - 1;
elseif ($subPage - $prevPage > 2)
$finalPages []= null;
$finalPages []= $subPage;
}
$pagesVisible = $finalPages;
if (!function_exists('pageUrl'))
{
function pageUrl($page)
2013-10-16 13:07:01 +02:00
{
$context = \Chibi\Registry::getContext();
$controller = $context->route->simpleControllerName;
$action = $context->route->simpleActionName;
$page = max(1, min($context->transport->paginator->pageCount, $page));
$params = $context->route->arguments;
$params['page'] = $page;
return \Chibi\UrlHelper::route($controller, $action, $params);
2013-10-16 13:07:01 +02:00
}
}
2013-10-16 13:07:01 +02:00
?>
<?php if (!empty($pagesVisible)): ?>
<?php
LayoutHelper::addStylesheet('paginator.css');
if ($this->context->user->hasEnabledEndlessScrolling())
LayoutHelper::addScript('paginator-endless.js');
?>
2013-10-16 13:07:01 +02:00
<nav class="paginator-wrapper">
<ul class="paginator">
<?php if ($page > 1): ?>
2013-10-16 13:07:01 +02:00
<li class="prev">
<?php else: ?>
<li class="prev disabled">
<?php endif ?>
<a href="<?php echo pageUrl($page - 1) ?>">
2013-10-16 13:07:01 +02:00
&laquo;
</a>
</li>
<?php foreach ($pagesVisible as $subPage): ?>
<?php if ($subPage === null): ?>
<li>&hellip;</li>
2013-10-16 13:07:01 +02:00
<?php else: ?>
<?php if ($subPage == $page): ?>
<li class="active">
<?php else: ?>
<li>
<?php endif ?>
<a href="<?php echo pageUrl($subPage) ?>">
<?php echo $subPage ?>
</a>
</li>
2013-10-16 13:07:01 +02:00
<?php endif ?>
<?php endforeach ?>
<?php if ($page < $pageCount): ?>
2013-10-16 13:07:01 +02:00
<li class="next">
<?php else: ?>
<li class="next disabled">
<?php endif ?>
<a href="<?php echo pageUrl($page + 1) ?>">
2013-10-16 13:07:01 +02:00
&raquo;
</a>
</li>
</ul>
</nav>
<?php endif ?>