2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2008-05-19 02:19:46 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
class Featured extends Extension
|
|
|
|
{
|
2020-02-04 00:46:36 +00:00
|
|
|
/** @var FeaturedTheme */
|
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 onInitExt(InitExtEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_int('featured_id', 0);
|
|
|
|
}
|
2008-05-19 02:19:46 +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, $user;
|
|
|
|
if ($event->page_matches("featured_image")) {
|
|
|
|
if ($event->get_arg(0) == "set" && $user->check_auth_token()) {
|
2019-07-09 14:10:21 +00:00
|
|
|
if ($user->can(Permissions::EDIT_FEATURE) && isset($_POST['image_id'])) {
|
2019-05-28 16:59:38 +00:00
|
|
|
$id = int_escape($_POST['image_id']);
|
|
|
|
if ($id > 0) {
|
|
|
|
$config->set_int("featured_id", $id);
|
2020-10-26 13:46:00 +00:00
|
|
|
log_info("featured", "Featured post set to >>$id", "Featured post set");
|
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("post/view/$id"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($event->get_arg(0) == "download") {
|
|
|
|
$image = Image::by_id($config->get_int("featured_id"));
|
|
|
|
if (!is_null($image)) {
|
2019-06-19 01:58:28 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
2020-06-14 16:05:55 +00:00
|
|
|
$page->set_mime($image->get_mime());
|
2019-05-28 16:59:38 +00:00
|
|
|
$page->set_data(file_get_contents($image->get_image_filename()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($event->get_arg(0) == "view") {
|
|
|
|
$image = Image::by_id($config->get_int("featured_id"));
|
|
|
|
if (!is_null($image)) {
|
|
|
|
send_event(new DisplayingImageEvent($image));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-19 02:19:46 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPostListBuilding(PostListBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2019-10-02 10:23:57 +00:00
|
|
|
global $cache, $config, $page, $user;
|
2019-05-28 16:59:38 +00:00
|
|
|
$fid = $config->get_int("featured_id");
|
|
|
|
if ($fid > 0) {
|
2023-12-14 17:06:54 +00:00
|
|
|
$image = cache_get_or_set(
|
|
|
|
"featured_image_object:$fid",
|
|
|
|
function () use ($fid) {
|
|
|
|
$image = Image::by_id($fid);
|
|
|
|
if ($image) { // make sure the object is fully populated before saving
|
|
|
|
$image->get_tag_array();
|
|
|
|
}
|
|
|
|
return $image;
|
|
|
|
},
|
|
|
|
600
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
if (!is_null($image)) {
|
2019-08-07 19:53:59 +00:00
|
|
|
if (Extension::is_enabled(RatingsInfo::KEY)) {
|
2019-06-28 00:49:58 +00:00
|
|
|
if (!in_array($image->rating, Ratings::get_user_class_privs($user))) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->theme->display_featured($page, $image);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-19 02:19:46 +00:00
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageDeletion(ImageDeletionEvent $event): void
|
2023-02-02 16:40:03 +00:00
|
|
|
{
|
|
|
|
global $config;
|
2023-02-02 16:50:09 +00:00
|
|
|
if ($event->image->id == $config->get_int("featured_id")) {
|
2023-02-02 16:40:03 +00:00
|
|
|
$config->set_int("featured_id", 0);
|
|
|
|
$config->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $user;
|
2021-04-25 10:20:55 +00:00
|
|
|
if ($user->can(Permissions::EDIT_FEATURE) && $event->context == "view") {
|
2019-05-28 16:59:38 +00:00
|
|
|
$event->add_part($this->theme->get_buttons_html($event->image->id));
|
|
|
|
}
|
|
|
|
}
|
2008-05-19 02:19:46 +00:00
|
|
|
}
|