2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2013-03-10 06:00:24 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class RandomList extends Extension
|
|
|
|
{
|
2020-01-26 13:19:35 +00:00
|
|
|
/** @var RandomListTheme */
|
2023-06-27 14:56:49 +00:00
|
|
|
protected Themelet $theme;
|
2020-01-26 13:19:35 +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 $config, $page;
|
2013-03-10 06:00:24 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
if ($event->page_matches("random")) {
|
2024-02-09 16:36:57 +00:00
|
|
|
if ($event->get_GET('search')) {
|
2019-05-28 16:59:38 +00:00
|
|
|
// implode(explode()) to resolve aliases and sanitise
|
2024-02-09 16:36:57 +00:00
|
|
|
$search = url_escape(Tag::implode(Tag::explode($event->get_GET('search'), false)));
|
2019-05-28 16:59:38 +00:00
|
|
|
if (empty($search)) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_redirect(make_link("random"));
|
|
|
|
} else {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::REDIRECT);
|
2024-02-09 11:00:18 +00:00
|
|
|
$page->set_redirect(make_link('random/'.url_escape($search)));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2016-08-14 22:56:24 +00:00
|
|
|
|
2024-02-11 11:34:09 +00:00
|
|
|
$search_terms = [];
|
|
|
|
if ($event->page_matches("random/{search}")) {
|
|
|
|
$search_terms = explode(' ', $event->get_arg('search'));
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2016-08-14 22:56:24 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// set vars
|
|
|
|
$images_per_page = $config->get_int("random_images_list_count", 12);
|
|
|
|
$random_images = [];
|
2013-03-10 06:00:24 +00:00
|
|
|
|
2020-10-26 15:18:13 +00:00
|
|
|
// generate random posts
|
2019-05-28 16:59:38 +00:00
|
|
|
for ($i = 0; $i < $images_per_page; $i++) {
|
|
|
|
$random_image = Image::by_random($search_terms);
|
|
|
|
if (!$random_image) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-11 11:15:26 +00:00
|
|
|
$random_images[] = $random_image;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2014-02-16 23:24:05 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
$this->theme->set_page($search_terms);
|
|
|
|
$this->theme->display_page($page, $random_images);
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 23:24:05 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_int("random_images_list_count", 12);
|
|
|
|
}
|
2014-02-16 23:24:05 +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 Posts List");
|
2014-02-16 23:24:05 +00:00
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// custom headers
|
|
|
|
$sb->add_int_option(
|
|
|
|
"random_images_list_count",
|
2020-10-26 15:18:13 +00:00
|
|
|
"Amount of Random posts to display "
|
2019-05-28 16:59:38 +00:00
|
|
|
);
|
|
|
|
}
|
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") {
|
2019-08-02 19:54:48 +00:00
|
|
|
$event->add_nav_link("posts_random", new Link('random'), "Shuffle");
|
|
|
|
}
|
|
|
|
}
|
2013-03-10 06:00:24 +00:00
|
|
|
}
|