2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2008-05-19 03:30:56 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
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
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $page;
|
2014-04-28 07:26:35 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
if (
|
|
|
|
$event->page_matches("random_image/{action}")
|
|
|
|
|| $event->page_matches("random_image/{action}/{search}")
|
|
|
|
) {
|
|
|
|
$action = $event->get_arg('action');
|
|
|
|
$search_terms = Tag::explode($event->get_arg('search', ""), false);
|
2019-05-28 16:59:38 +00:00
|
|
|
$image = Image::by_random($search_terms);
|
2020-01-29 00:49:21 +00:00
|
|
|
if (!$image) {
|
2024-02-11 15:47:40 +00:00
|
|
|
throw new ImageNotFound("Couldn't find any posts randomly");
|
2020-01-28 21:19:59 +00:00
|
|
|
}
|
2008-05-19 03:30:56 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($action === "download") {
|
2024-02-09 16:36:57 +00:00
|
|
|
send_event(new ImageDownloadingEvent($image, $image->get_image_filename(), $image->get_mime(), $event->GET));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif ($action === "view") {
|
2020-01-28 21:19:59 +00:00
|
|
|
send_event(new DisplayingImageEvent($image));
|
2019-05-28 16:59:38 +00:00
|
|
|
} elseif ($action === "widget") {
|
2020-01-28 21:19:59 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2020-06-14 16:05:55 +00:00
|
|
|
$page->set_mime(MimeType::HTML);
|
2020-01-28 21:19:59 +00:00
|
|
|
$page->set_data($this->theme->build_thumb_html($image));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-16 08:18:15 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onSetupBuilding(SetupBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2020-10-26 15:13:28 +00:00
|
|
|
$sb = $event->panel->create_new_block("Random Post");
|
2019-05-28 16:59:38 +00:00
|
|
|
$sb->add_bool_option("show_random_block", "Show Random Block: ");
|
|
|
|
}
|
2009-01-16 08:18:15 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPostListBuilding(PostListBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 19:54:48 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageSubNavBuilding(PageSubNavBuildingEvent $event): void
|
2019-08-02 19:54:48 +00:00
|
|
|
{
|
2023-11-11 21:49:12 +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");
|
2019-08-02 19:54:48 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-19 03:30:56 +00:00
|
|
|
}
|