Added option to go to next image when an image is deleted

This commit is contained in:
Matthew Barbour 2020-10-08 17:07:33 -05:00
parent 79a2fe23b3
commit 6bc8b791d2
3 changed files with 42 additions and 2 deletions

View file

@ -187,3 +187,27 @@ function create_scaled_image(string $inname, string $outname, array $tsize, stri
true
));
}
function redirect_to_next_image(Image $image): void
{
global $page;
if (isset($_GET['search'])) {
$search_terms = Tag::explode(Tag::decaret($_GET['search']));
$query = "search=" . url_escape($_GET['search']);
} else {
$search_terms = [];
$query = null;
}
$target_image = $image->get_next($search_terms);
if ($target_image == null) {
$redirect_target = referer_or(make_link("post/list"), ['post/view']);
} else {
$redirect_target = make_link("post/view/{$target_image->id}", null, $query);
}
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect($redirect_target);
}

View file

@ -23,4 +23,8 @@ abstract class ImageConfig
const COLLISION_MERGE = 'merge';
const COLLISION_ERROR = 'error';
const ON_DELETE = 'image_on_delete';
const ON_DELETE_NEXT = 'next';
const ON_DELETE_LIST = 'list';
}

View file

@ -15,6 +15,11 @@ class ImageIO extends Extension
'Merge'=>ImageConfig::COLLISION_MERGE
];
const ON_DELETE_OPTIONS = [
'Return to post list'=>ImageConfig::ON_DELETE_LIST,
'Go to next image'=>ImageConfig::ON_DELETE_NEXT
];
const EXIF_READ_FUNCTION = "exif_read_data";
const THUMBNAIL_ENGINES = [
@ -70,14 +75,20 @@ class ImageIO extends Extension
public function onPageRequest(PageRequestEvent $event)
{
global $config;
if ($event->page_matches("image/delete")) {
global $page, $user;
if ($user->can(Permissions::DELETE_IMAGE) && isset($_POST['image_id']) && $user->check_auth_token()) {
$image = Image::by_id(int_escape($_POST['image_id']));
if ($image) {
send_event(new ImageDeletionEvent($image));
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link("post/list"), ['post/view']));
if ($config->get_string(ImageConfig::ON_DELETE)===ImageConfig::ON_DELETE_NEXT) {
redirect_to_next_image($image);
} else {
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link("post/list"), ['post/view']));
}
}
}
} elseif ($event->page_matches("image/replace")) {
@ -254,6 +265,7 @@ class ImageIO extends Extension
$sb->add_text_option(ImageConfig::TIP, "Image tooltip", true);
$sb->add_text_option(ImageConfig::INFO, "Image info", true);
$sb->add_choice_option(ImageConfig::UPLOAD_COLLISION_HANDLER, self::COLLISION_OPTIONS, "Upload collision handler", true);
$sb->add_choice_option(ImageConfig::ON_DELETE, self::ON_DELETE_OPTIONS, "On Delete", true);
if (function_exists(self::EXIF_READ_FUNCTION)) {
$sb->add_bool_option(ImageConfig::SHOW_META, "Show metadata", true);
}