2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-04-26 09:01:49 +00:00
|
|
|
|
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,BR,IMG,emptyHTML,joinHTML};
|
2023-06-27 13:10:53 +00:00
|
|
|
|
2012-03-12 05:04:29 +00:00
|
|
|
/**
|
2014-04-26 09:01:49 +00:00
|
|
|
* Class BaseThemelet
|
|
|
|
*
|
2012-03-12 05:04:29 +00:00
|
|
|
* A collection of common functions for theme parts
|
|
|
|
*/
|
2019-05-28 16:59:38 +00:00
|
|
|
class BaseThemelet
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Generic error message display
|
|
|
|
*/
|
|
|
|
public function display_error(int $code, string $title, string $message): void
|
|
|
|
{
|
|
|
|
global $page;
|
|
|
|
$page->set_code($code);
|
|
|
|
$page->set_title($title);
|
|
|
|
$page->set_heading($title);
|
|
|
|
$has_nav = false;
|
|
|
|
foreach ($page->blocks as $block) {
|
|
|
|
if ($block->header == "Navigation") {
|
|
|
|
$has_nav = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$has_nav) {
|
|
|
|
$page->add_block(new NavBlock());
|
|
|
|
}
|
|
|
|
$page->add_block(new Block("Error", $message));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A specific, common error message
|
|
|
|
*/
|
|
|
|
public function display_permission_denied(): void
|
|
|
|
{
|
|
|
|
$this->display_error(403, "Permission Denied", "You do not have permission to access this page");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generic thumbnail code; returns HTML rather than adding
|
|
|
|
* a block since thumbs tend to go inside blocks...
|
|
|
|
*/
|
2023-06-27 13:10:53 +00:00
|
|
|
public function build_thumb_html(Image $image): HTMLElement
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
|
2023-06-27 13:10:53 +00:00
|
|
|
$id = $image->id;
|
|
|
|
$view_link = make_link('post/view/'.$id);
|
|
|
|
$thumb_link = $image->get_thumb_link();
|
|
|
|
$tip = $image->get_tooltip();
|
|
|
|
$tags = strtolower($image->get_tag_list());
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2020-06-14 16:05:55 +00:00
|
|
|
// TODO: Set up a function for fetching what kind of files are currently thumbnailable
|
|
|
|
$mimeArr = array_flip([MimeType::MP3]); //List of thumbless filetypes
|
|
|
|
if (!isset($mimeArr[$image->get_mime()])) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$tsize = get_thumbnail_size($image->width, $image->height);
|
|
|
|
} else {
|
|
|
|
//Use max thumbnail size if using thumbless filetype
|
2019-06-18 18:45:59 +00:00
|
|
|
$tsize = get_thumbnail_size($config->get_int(ImageConfig::THUMB_WIDTH), $config->get_int(ImageConfig::THUMB_WIDTH));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$custom_classes = "";
|
2023-01-10 22:44:09 +00:00
|
|
|
if (class_exists("Shimmie2\Relationships")) {
|
2019-05-28 16:59:38 +00:00
|
|
|
if (property_exists($image, 'parent_id') && $image->parent_id !== null) {
|
|
|
|
$custom_classes .= "shm-thumb-has_parent ";
|
|
|
|
}
|
|
|
|
if (property_exists($image, 'has_children') && bool_escape($image->has_children)) {
|
|
|
|
$custom_classes .= "shm-thumb-has_child ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-04 14:07:32 +00:00
|
|
|
$attrs = [
|
|
|
|
"href" => $view_link,
|
|
|
|
"class" => "thumb shm-thumb shm-thumb-link $custom_classes",
|
|
|
|
"data-tags" => $tags,
|
|
|
|
"data-height" => $image->height,
|
|
|
|
"data-width" => $image->width,
|
|
|
|
"data-mime" => $image->get_mime(),
|
|
|
|
"data-post-id" => $id,
|
|
|
|
];
|
|
|
|
if(Extension::is_enabled(RatingsInfo::KEY)) {
|
2024-01-15 17:12:36 +00:00
|
|
|
$attrs["data-rating"] = $image['rating'];
|
2024-01-04 14:07:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-27 13:10:53 +00:00
|
|
|
return A(
|
2024-01-04 14:07:32 +00:00
|
|
|
$attrs,
|
2023-06-27 13:10:53 +00:00
|
|
|
IMG(
|
|
|
|
[
|
2023-11-11 21:49:12 +00:00
|
|
|
"id" => "thumb_$id",
|
|
|
|
"title" => $tip,
|
|
|
|
"alt" => $tip,
|
|
|
|
"height" => $tsize[1],
|
|
|
|
"width" => $tsize[0],
|
|
|
|
"src" => $thumb_link,
|
2023-06-27 13:10:53 +00:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
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, $show_random);
|
|
|
|
$page->add_block(new Block(null, $body, "main", 90, "paginator"));
|
2020-02-04 01:12:50 +00:00
|
|
|
|
2020-02-04 01:59:08 +00:00
|
|
|
$page->add_html_header("<link rel='first' href='".make_http(make_link($base.'/1', $query))."'>");
|
2020-02-04 01:15:25 +00:00
|
|
|
if ($page_number < $total_pages) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$page->add_html_header("<link rel='prefetch' href='".make_http(make_link($base.'/'.($page_number + 1), $query))."'>");
|
|
|
|
$page->add_html_header("<link rel='next' href='".make_http(make_link($base.'/'.($page_number + 1), $query))."'>");
|
2020-02-04 01:12:50 +00:00
|
|
|
}
|
2020-02-04 01:15:25 +00:00
|
|
|
if ($page_number > 1) {
|
2023-11-11 21:49:12 +00:00
|
|
|
$page->add_html_header("<link rel='previous' href='".make_http(make_link($base.'/'.($page_number - 1), $query))."'>");
|
2020-02-04 01:12:50 +00:00
|
|
|
}
|
2020-02-04 01:59:08 +00:00
|
|
|
$page->add_html_header("<link rel='last' href='".make_http(make_link($base.'/'.$total_pages, $query))."'>");
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
if ($page == $current_page) {
|
2023-06-27 13:10:53 +00:00
|
|
|
$paginator = B($paginator);
|
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, bool $show_random): HTMLElement
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
$next = $current_page + 1;
|
|
|
|
$prev = $current_page - 1;
|
|
|
|
|
|
|
|
$at_start = ($current_page <= 1 || $total_pages <= 1);
|
|
|
|
$at_end = ($current_page >= $total_pages);
|
|
|
|
|
|
|
|
$first_html = $at_start ? "First" : $this->gen_page_link($base_url, $query, 1, "First");
|
2021-12-14 18:32:47 +00:00
|
|
|
$prev_html = $at_start ? "Prev" : $this->gen_page_link($base_url, $query, $prev, "Prev");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$random_html = "-";
|
|
|
|
if ($show_random) {
|
|
|
|
$rand = mt_rand(1, $total_pages);
|
|
|
|
$random_html = $this->gen_page_link($base_url, $query, $rand, "Random");
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:32:47 +00:00
|
|
|
$next_html = $at_end ? "Next" : $this->gen_page_link($base_url, $query, $next, "Next");
|
|
|
|
$last_html = $at_end ? "Last" : $this->gen_page_link($base_url, $query, $total_pages, "Last");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2023-01-11 11:15:26 +00:00
|
|
|
$start = max($current_page - 5, 1);
|
|
|
|
$end = min($start + 10, $total_pages);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
$pages = [];
|
|
|
|
foreach (range($start, $end) as $i) {
|
2020-01-26 13:19:35 +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);
|
2023-06-27 13:10:53 +00:00
|
|
|
|
|
|
|
return emptyHTML(
|
2023-08-17 17:23:36 +00:00
|
|
|
joinHTML(" | ", [
|
2023-06-27 13:10:53 +00:00
|
|
|
$first_html,
|
|
|
|
$prev_html,
|
|
|
|
$random_html,
|
|
|
|
$next_html,
|
|
|
|
$last_html,
|
|
|
|
]),
|
|
|
|
BR(),
|
|
|
|
'<< ',
|
|
|
|
$pages_html,
|
|
|
|
' >>'
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2012-03-12 05:04:29 +00:00
|
|
|
}
|