This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/random_image/main.php

70 lines
2.2 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class RandomImage extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var RandomImageTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onPageRequest(PageRequestEvent $event)
{
global $page;
2014-04-28 07:26:35 +00:00
if ($event->page_matches("random_image")) {
if ($event->count_args() == 1) {
$action = $event->get_arg(0);
$search_terms = [];
} elseif ($event->count_args() == 2) {
$action = $event->get_arg(0);
$search_terms = Tag::explode($event->get_arg(1));
} else {
throw new SCoreException("Error: too many arguments.");
}
$image = Image::by_random($search_terms);
2020-01-29 00:49:21 +00:00
if (!$image) {
throw new SCoreException(
2020-10-26 15:17:15 +00:00
"Couldn't find any posts randomly",
Tag::implode($search_terms)
);
}
if ($action === "download") {
send_event(new ImageDownloadingEvent($image, $image->get_image_filename(), $image->get_mime()));
} elseif ($action === "view") {
send_event(new DisplayingImageEvent($image));
} elseif ($action === "widget") {
$page->set_mode(PageMode::DATA);
2020-06-14 16:05:55 +00:00
$page->set_mime(MimeType::HTML);
$page->set_data($this->theme->build_thumb_html($image));
}
}
}
2009-01-16 08:18:15 +00:00
public function onSetupBuilding(SetupBuildingEvent $event)
{
$sb = $event->panel->create_new_block("Random Post");
$sb->add_bool_option("show_random_block", "Show Random Block: ");
}
2009-01-16 08:18:15 +00:00
public function onPostListBuilding(PostListBuildingEvent $event)
{
global $config, $page;
if ($config->get_bool("show_random_block")) {
$image = Image::by_random($event->search_terms);
if (!is_null($image)) {
$this->theme->display_random($page, $image);
}
}
}
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event)
{
2019-09-29 13:30:55 +00:00
if ($event->parent=="posts") {
2020-10-26 15:17:15 +00:00
$event->add_nav_link("posts_random", new Link('random_image/view'), "Random Post");
}
}
}