2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2023-01-10 22:44:09 +00:00
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
2023-06-27 13:10:53 +00:00
|
|
|
use MicroHTML\HTMLElement;
|
|
|
|
|
2023-08-17 17:23:36 +00:00
|
|
|
use function MicroHTML\{A,B,DIV,joinHTML};
|
2023-06-27 13:10:53 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Themelet extends BaseThemelet
|
|
|
|
{
|
2024-01-15 14:23:00 +00:00
|
|
|
public function display_paginator(Page $page, string $base, ?string $query, int $page_number, int $total_pages, bool $show_random = false): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if ($total_pages == 0) {
|
|
|
|
$total_pages = 1;
|
|
|
|
}
|
|
|
|
$body = $this->build_paginator($page_number, $total_pages, $base, $query);
|
|
|
|
$page->add_block(new Block(null, $body, "main", 90));
|
|
|
|
}
|
|
|
|
|
2023-06-27 13:10:53 +00:00
|
|
|
private function gen_page_link(string $base_url, ?string $query, int $page, string $name): HTMLElement
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +00:00
|
|
|
return A(["href" => make_link("$base_url/$page", $query)], $name);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 13:10:53 +00:00
|
|
|
private function gen_page_link_block(string $base_url, ?string $query, int $page, int $current_page, string $name): HTMLElement
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if ($page == $current_page) {
|
2023-06-27 13:10:53 +00:00
|
|
|
$paginator = B($page);
|
2019-05-28 16:59:38 +00:00
|
|
|
} else {
|
2023-06-27 13:10:53 +00:00
|
|
|
$paginator = $this->gen_page_link($base_url, $query, $page, $name);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
return $paginator;
|
|
|
|
}
|
|
|
|
|
2023-06-27 13:10:53 +00:00
|
|
|
private function build_paginator(int $current_page, int $total_pages, string $base_url, ?string $query): HTMLElement
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$next = $current_page + 1;
|
|
|
|
$prev = $current_page - 1;
|
|
|
|
|
|
|
|
$at_start = ($current_page <= 3 || $total_pages <= 3);
|
2023-11-11 21:49:12 +00:00
|
|
|
$at_end = ($current_page >= $total_pages - 2);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$first_html = $at_start ? "" : $this->gen_page_link($base_url, $query, 1, "1");
|
2023-06-27 13:10:53 +00:00
|
|
|
$prev_html = $at_start ? "" : $this->gen_page_link($base_url, $query, $prev, "<<");
|
|
|
|
$next_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $next, ">>");
|
2021-12-14 18:32:47 +00:00
|
|
|
$last_html = $at_end ? "" : $this->gen_page_link($base_url, $query, $total_pages, "$total_pages");
|
2014-04-24 23:13:20 +00:00
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
$start = $current_page - 2 > 1 ? $current_page - 2 : 1;
|
|
|
|
$end = $current_page + 2 <= $total_pages ? $current_page + 2 : $total_pages;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$pages = [];
|
|
|
|
foreach (range($start, $end) as $i) {
|
2020-03-08 15:49:36 +00:00
|
|
|
$pages[] = $this->gen_page_link_block($base_url, $query, $i, $current_page, (string)$i);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2023-08-17 17:23:36 +00:00
|
|
|
$pages_html = joinHTML(" ", $pages);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-09-06 11:01:08 +00:00
|
|
|
if ($start > 2) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$pdots = "...";
|
|
|
|
} else {
|
|
|
|
$pdots = "";
|
|
|
|
}
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
if ($total_pages > $end + 1) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$ndots = "...";
|
|
|
|
} else {
|
|
|
|
$ndots = "";
|
|
|
|
}
|
|
|
|
|
2023-11-11 21:49:12 +00:00
|
|
|
return DIV(["id" => "paginator"], joinHTML(" ", [$prev_html, $first_html, $pdots, $pages_html, $ndots, $last_html, $next_html]));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|