pics/templates/PageIndexWidget.php

83 lines
2.3 KiB
PHP
Raw Permalink Normal View History

<?php
/*****************************************************************************
* PageIndexWidget.php
* Contains the template that displays a page index.
*
* Kabuki CMS (C) 2013-2023, Aaron van Geffen
*****************************************************************************/
class PageIndexWidget extends Template
{
private $index;
private string $class;
private static $unique_index_count = 0;
public function __construct(PageIndex $index)
{
$this->index = $index;
$this->class = $index->getPageIndexClass();
}
public function html_main()
{
self::paginate($this->index, $this->class);
}
public static function paginate(PageIndex $index, $class = null)
{
$page_index = $index->getPageIndex();
if (empty($page_index) || count($page_index) == 1)
return;
if (!isset($class))
$class = $index->getPageIndexClass();
echo '
<ul class="pagination', $class ? ' ' . $class : '', '">
<li class="page-item', empty($page_index['previous']) ? ' disabled' : '', '">',
'<a class="page-link"', !empty($page_index['previous']) ? ' href="' . $page_index['previous']['href'] . '"' : '', '>',
'&laquo; previous</a></li>';
$num_wildcards = 0;
foreach ($page_index as $key => $page)
{
if (!is_numeric($key))
continue;
if (!is_array($page))
{
$first_wildcard = $num_wildcards === 0;
$num_wildcards++;
echo '
<li class="page-item page-padding wildcard',
$first_wildcard ? ' first-wildcard' : '',
'" onclick="javascript:promptGoToPage(',
self::$unique_index_count, ')"><a class="page-link">...</a></li>';
}
else
echo '
<li class="page-item page-number', $page['is_selected'] ? ' active" aria-current="page' : '', '">',
'<a class="page-link" href="', $page['href'], '">', $page['index'], '</a></li>';
}
echo '
<li class="page-item', empty($page_index['next']) ? ' disabled' : '', '">',
'<a class="page-link"', !empty($page_index['next']) ? ' href="' . $page_index['next']['href'] . '"' : '', '>',
'next &raquo;</a></li>
</ul>';
if ($num_wildcards)
{
echo '
<script type="text/javascript">
var page_index_', self::$unique_index_count++, ' = {
wildcard_url: "', $index->getLink("%d"), '",
num_pages: ', $index->getNumberOfPages(), ',
per_page: ', $index->getItemsPerPage(), '
};
</script>';
}
}
}