From 81a8587543f685c0276f89a0456dedd5edf38ffd Mon Sep 17 00:00:00 2001 From: Shish Date: Sat, 10 Feb 2024 18:35:55 +0000 Subject: [PATCH] ImageAction->add_button + display_crud --- core/basethemelet.php | 9 +++++ core/event.php | 6 +++ ext/approval/main.php | 7 +++- ext/approval/theme.php | 17 --------- ext/favorites/main.php | 31 +++++++++------- ext/favorites/theme.php | 12 ------ ext/featured/main.php | 2 +- ext/featured/theme.php | 9 ----- ext/image/main.php | 12 ++++-- ext/image/theme.php | 24 ------------ ext/image_hash_ban/main.php | 15 +++++--- ext/image_hash_ban/theme.php | 37 ------------------- ext/log_db/main.php | 5 +-- ext/log_db/theme.php | 21 ----------- ext/media/main.php | 9 ++--- ext/media/theme.php | 11 ------ ext/not_a_tag/main.php | 5 +-- ext/not_a_tag/theme.php | 20 ---------- ext/pools/main.php | 10 ++++- ext/pools/theme.php | 13 ------- ext/private_image/main.php | 6 ++- ext/private_image/theme.php | 17 --------- ext/regen_thumb/main.php | 6 +-- ext/regen_thumb/test.php | 2 +- ext/regen_thumb/theme.php | 12 ------ ext/replace_file/main.php | 2 +- ext/replace_file/theme.php | 10 ----- ext/resize/main.php | 28 ++++++++++++-- ext/resize/theme.php | 31 ---------------- ext/rotate/main.php | 10 ++++- ext/rotate/theme.php | 14 ------- ext/s3/main.php | 13 ++----- ext/source_history/main.php | 6 +-- ext/tag_history/main.php | 6 +-- ext/trash/main.php | 2 +- ext/trash/theme.php | 9 ----- ext/user/main.php | 2 +- ext/user/theme.php | 8 ---- .../image_admin_block_building_event.php | 16 ++++++++ 39 files changed, 139 insertions(+), 336 deletions(-) delete mode 100644 ext/image/theme.php delete mode 100644 ext/image_hash_ban/theme.php delete mode 100644 ext/log_db/theme.php delete mode 100644 ext/not_a_tag/theme.php diff --git a/core/basethemelet.php b/core/basethemelet.php index b3cc0545..ecfdd77b 100644 --- a/core/basethemelet.php +++ b/core/basethemelet.php @@ -183,4 +183,13 @@ class BaseThemelet ' >>' ); } + + public function display_crud(string $title, HTMLElement $table, HTMLElement $paginator): void + { + global $page; + $page->set_title($title); + $page->set_heading($title); + $page->add_block(new NavBlock()); + $page->add_block(new Block("$title Table", emptyHTML($table, $paginator))); + } } diff --git a/core/event.php b/core/event.php index b139d92b..1fa9b27d 100644 --- a/core/event.php +++ b/core/event.php @@ -181,6 +181,12 @@ class PageRequestEvent extends Event return true; } + public function authed_page_matches(string $name): bool + { + global $user; + return $this->page_matches($name) && $user->check_auth_token(); + } + /** * Get the n th argument of the page request (if it exists.) */ diff --git a/ext/approval/main.php b/ext/approval/main.php index 5223e4d7..2e129425 100644 --- a/ext/approval/main.php +++ b/ext/approval/main.php @@ -217,7 +217,12 @@ class Approval extends Extension { global $user, $config; if ($user->can(Permissions::APPROVE_IMAGE) && $config->get_bool(ApprovalConfig::IMAGES)) { - $event->add_part($this->theme->get_image_admin_html($event->image)); + if ($event->image['approved'] === true) { + $event->add_button("Disapprove", "disapprove_image/".$event->image->id); + } else { + $event->add_button("Approve", "approve_image/".$event->image->id); + } + } } diff --git a/ext/approval/theme.php b/ext/approval/theme.php index e1783625..b4cb2dc0 100644 --- a/ext/approval/theme.php +++ b/ext/approval/theme.php @@ -12,23 +12,6 @@ use function MicroHTML\{BUTTON,INPUT,P}; class ApprovalTheme extends Themelet { - public function get_image_admin_html(Image $image): HTMLElement - { - if ($image['approved'] === true) { - $form = SHM_SIMPLE_FORM( - 'disapprove_image/'.$image->id, - SHM_SUBMIT("Disapprove") - ); - } else { - $form = SHM_SIMPLE_FORM( - 'approve_image/'.$image->id, - SHM_SUBMIT("Approve") - ); - } - - return $form; - } - public function get_help_html(): HTMLElement { return emptyHTML( diff --git a/ext/favorites/main.php b/ext/favorites/main.php index 4fc1dab2..4cc0a4af 100644 --- a/ext/favorites/main.php +++ b/ext/favorites/main.php @@ -44,7 +44,11 @@ class Favorites extends Extension ["user_id" => $user_id, "image_id" => $image_id] ) > 0; - $event->add_part($this->theme->get_voter_html($event->image, $is_favorited)); + if($is_favorited) { + $event->add_button("Un-Favorite", "favourite/remove/{$event->image->id}"); + } else { + $event->add_button("Favorite", "favourite/add/{$event->image->id}"); + } } } @@ -59,18 +63,19 @@ class Favorites extends Extension public function onPageRequest(PageRequestEvent $event): void { global $page, $user; - if ($event->page_matches("change_favorite") && !$user->is_anonymous() && $user->check_auth_token()) { - $image_id = int_escape($event->req_POST('image_id')); - $action = $event->req_POST('favorite_action'); - if ((($action == "set") || ($action == "unset")) && ($image_id > 0)) { - if ($action == "set") { - send_event(new FavoriteSetEvent($image_id, $user, true)); - log_debug("favourite", "Favourite set for $image_id", "Favourite added"); - } else { - send_event(new FavoriteSetEvent($image_id, $user, false)); - log_debug("favourite", "Favourite removed for $image_id", "Favourite removed"); - } - } + if ($user->is_anonymous()) { + return; + } // FIXME: proper permissions + + if ($event->authed_page_matches("favourite/add")) { + $image_id = int_escape($event->get_arg(0)); + send_event(new FavoriteSetEvent($image_id, $user, true)); + $page->set_mode(PageMode::REDIRECT); + $page->set_redirect(make_link("post/view/$image_id")); + } + if ($event->authed_page_matches("favourite/remove")) { + $image_id = int_escape($event->get_arg(0)); + send_event(new FavoriteSetEvent($image_id, $user, false)); $page->set_mode(PageMode::REDIRECT); $page->set_redirect(make_link("post/view/$image_id")); } diff --git a/ext/favorites/theme.php b/ext/favorites/theme.php index e9fff045..95a1bb9e 100644 --- a/ext/favorites/theme.php +++ b/ext/favorites/theme.php @@ -10,18 +10,6 @@ use function MicroHTML\INPUT; class FavoritesTheme extends Themelet { - public function get_voter_html(Image $image, bool $is_favorited): HTMLElement - { - $name = $is_favorited ? "unset" : "set"; - $label = $is_favorited ? "Un-Favorite" : "Favorite"; - return SHM_SIMPLE_FORM( - "change_favorite", - INPUT(["type" => "hidden", "name" => "image_id", "value" => $image->id]), - INPUT(["type" => "hidden", "name" => "favorite_action", "value" => $name]), - INPUT(["type" => "submit", "value" => $label]), - ); - } - /** * @param string[] $username_array */ diff --git a/ext/featured/main.php b/ext/featured/main.php index 803f5cb1..013064ff 100644 --- a/ext/featured/main.php +++ b/ext/featured/main.php @@ -85,7 +85,7 @@ class Featured extends Extension { global $user; if ($user->can(Permissions::EDIT_FEATURE) && $event->context == "view") { - $event->add_part($this->theme->get_buttons_html($event->image->id)); + $event->add_button("Feature This", "featured_image/set/{$event->image->id}"); } } } diff --git a/ext/featured/theme.php b/ext/featured/theme.php index d3c5f58d..4ffe71d0 100644 --- a/ext/featured/theme.php +++ b/ext/featured/theme.php @@ -16,15 +16,6 @@ class FeaturedTheme extends Themelet $page->add_block(new Block("Featured Post", $this->build_featured_html($image), "left", 3)); } - public function get_buttons_html(int $image_id): \MicroHTML\HTMLElement - { - return SHM_SIMPLE_FORM( - "featured_image/set", - INPUT(["type" => 'hidden', "name" => 'image_id', "value" => $image_id]), - INPUT(["type" => 'submit', "value" => 'Feature This']), - ); - } - public function build_featured_html(Image $image, ?string $query = null): \MicroHTML\HTMLElement { $tsize = get_thumbnail_size($image->width, $image->height); diff --git a/ext/image/main.php b/ext/image/main.php index ef39b191..ca2e31c4 100644 --- a/ext/image/main.php +++ b/ext/image/main.php @@ -8,6 +8,8 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\{InputInterface,InputArgument}; use Symfony\Component\Console\Output\OutputInterface; +use function MicroHTML\{INPUT, emptyHTML}; + require_once "config.php"; /** @@ -15,9 +17,6 @@ require_once "config.php"; */ class ImageIO extends Extension { - /** @var ImageIOTheme */ - protected Themelet $theme; - public const COLLISION_OPTIONS = [ 'Error' => ImageConfig::COLLISION_ERROR, 'Merge' => ImageConfig::COLLISION_MERGE @@ -118,7 +117,12 @@ class ImageIO extends Extension global $user; if ($user->can(Permissions::DELETE_IMAGE)) { - $event->add_part($this->theme->get_deleter_html($event->image->id)); + $form = SHM_FORM("image/delete", form_id: "image_delete_form"); + $form->appendChild(emptyHTML( + INPUT(["type" => 'hidden', "name" => 'image_id', "value" => $event->image->id]), + INPUT(["type" => 'submit', "value" => 'Delete', "onclick" => 'return confirm("Delete the image?");', "id" => "image_delete_button"]), + )); + $event->add_part($form); } } diff --git a/ext/image/theme.php b/ext/image/theme.php deleted file mode 100644 index 8d5200fb..00000000 --- a/ext/image/theme.php +++ /dev/null @@ -1,24 +0,0 @@ -appendChild(emptyHTML( - INPUT(["type" => 'hidden', "name" => 'image_id', "value" => $image_id]), - INPUT(["type" => 'submit', "value" => 'Delete', "onclick" => 'return confirm("Delete the image?");', "id" => "image_delete_button"]), - )); - return $form; - } -} diff --git a/ext/image_hash_ban/main.php b/ext/image_hash_ban/main.php index 65f9bd21..34cd8639 100644 --- a/ext/image_hash_ban/main.php +++ b/ext/image_hash_ban/main.php @@ -10,6 +10,8 @@ use MicroCRUD\DateColumn; use MicroCRUD\TextColumn; use MicroCRUD\Table; +use function MicroHTML\{INPUT,emptyHTML}; + class HashBanTable extends Table { public function __construct(\FFSPHP\PDO $db) @@ -59,9 +61,6 @@ class AddImageHashBanEvent extends Event class ImageBan extends Extension { - /** @var ImageBanTheme */ - protected Themelet $theme; - public function onDatabaseUpgrade(DatabaseUpgradeEvent $event): void { global $database; @@ -122,7 +121,7 @@ class ImageBan extends Extension $t = new HashBanTable($database->raw_db()); $t->token = $user->get_auth_token(); $t->inputs = $event->GET; - $this->theme->display_bans($page, $t->table($t->query()), $t->paginator()); + $this->theme->display_crud("Post Bans", $t->table($t->query()), $t->paginator()); } } } @@ -166,7 +165,13 @@ class ImageBan extends Extension { global $user; if ($user->can(Permissions::BAN_IMAGE)) { - $event->add_part($this->theme->get_buttons_html($event->image)); + $event->add_part(SHM_SIMPLE_FORM( + "image_hash_ban/add", + INPUT(["type" => 'hidden', "name" => 'c_hash', "value" => $event->image->hash]), + INPUT(["type" => 'hidden', "name" => 'c_image_id', "value" => $event->image->id]), + INPUT(["type" => 'text', "name" => 'c_reason']), + INPUT(["type" => 'submit', "value" => 'Ban Hash and Delete Post']), + )); } } diff --git a/ext/image_hash_ban/theme.php b/ext/image_hash_ban/theme.php deleted file mode 100644 index e0d5d2c1..00000000 --- a/ext/image_hash_ban/theme.php +++ /dev/null @@ -1,37 +0,0 @@ -set_title("Post Bans"); - $page->set_heading("Post Bans"); - $page->add_block(new NavBlock()); - $page->add_block(new Block("Edit Post Bans", emptyHTML($table, $paginator))); - } - - /* - * Display a link to delete an image - */ - public function get_buttons_html(Image $image): HTMLElement - { - return SHM_SIMPLE_FORM( - "image_hash_ban/add", - INPUT(["type" => 'hidden', "name" => 'c_hash', "value" => $image->hash]), - INPUT(["type" => 'hidden', "name" => 'c_image_id', "value" => $image->id]), - INPUT(["type" => 'text', "name" => 'c_reason']), - INPUT(["type" => 'submit', "value" => 'Ban Hash and Delete Post']), - ); - } -} diff --git a/ext/log_db/main.php b/ext/log_db/main.php index 6de2329e..2da2d34f 100644 --- a/ext/log_db/main.php +++ b/ext/log_db/main.php @@ -238,9 +238,6 @@ class LogTable extends Table class LogDatabase extends Extension { - /** @var LogDatabaseTheme */ - protected Themelet $theme; - public function onInitExt(InitExtEvent $event): void { global $config; @@ -285,7 +282,7 @@ class LogDatabase extends Extension if ($user->can(Permissions::VIEW_EVENTLOG)) { $t = new LogTable($database->raw_db()); $t->inputs = $event->GET; - $this->theme->display_events($t->table($t->query()), $t->paginator()); + $this->theme->display_crud("Event Log", $t->table($t->query()), $t->paginator()); } } } diff --git a/ext/log_db/theme.php b/ext/log_db/theme.php deleted file mode 100644 index f57183cf..00000000 --- a/ext/log_db/theme.php +++ /dev/null @@ -1,21 +0,0 @@ -set_title("Event Log"); - $page->set_heading("Event Log"); - $page->add_block(new NavBlock()); - $page->add_block(new Block("Events", emptyHTML($table, $paginator))); - } -} diff --git a/ext/media/main.php b/ext/media/main.php index 29c4312b..8a085fae 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -75,11 +75,10 @@ class Media extends Extension global $page, $user; if ( - $event->page_matches("media_rescan/") && - $user->can(Permissions::RESCAN_MEDIA) && - $event->get_POST('image_id') + $event->authed_page_matches("media_rescan") && + $user->can(Permissions::RESCAN_MEDIA) ) { - $image = Image::by_id(int_escape($event->get_POST('image_id'))); + $image = Image::by_id(int_escape($event->get_arg(0))); send_event(new MediaCheckPropertiesEvent($image)); $image->save_to_db(); @@ -118,7 +117,7 @@ class Media extends Extension { global $user; if ($user->can(Permissions::DELETE_IMAGE)) { - $event->add_part($this->theme->get_buttons_html($event->image->id)); + $event->add_button("Scan Media Properties", "media_rescan/{$event->image->id}"); } } diff --git a/ext/media/theme.php b/ext/media/theme.php index 3438b866..79320650 100644 --- a/ext/media/theme.php +++ b/ext/media/theme.php @@ -4,19 +4,8 @@ declare(strict_types=1); namespace Shimmie2; -use function MicroHTML\INPUT; - class MediaTheme extends Themelet { - public function get_buttons_html(int $image_id): \MicroHTML\HTMLElement - { - return SHM_SIMPLE_FORM( - "media_rescan/", - INPUT(["type" => 'hidden', "name" => 'image_id', "value" => $image_id]), - SHM_SUBMIT('Scan Media Properties'), - ); - } - public function get_help_html(): string { return '

Search for posts based on the type of media.

diff --git a/ext/not_a_tag/main.php b/ext/not_a_tag/main.php index b9dfc73c..294a9c19 100644 --- a/ext/not_a_tag/main.php +++ b/ext/not_a_tag/main.php @@ -32,9 +32,6 @@ class NotATagTable extends Table class NotATag extends Extension { - /** @var NotATagTheme */ - protected Themelet $theme; - public function get_priority(): int { return 30; @@ -154,7 +151,7 @@ class NotATag extends Extension $t = new NotATagTable($database->raw_db()); $t->token = $user->get_auth_token(); $t->inputs = $event->GET; - $this->theme->display_untags($page, $t->table($t->query()), $t->paginator()); + $this->theme->display_crud("UnTags", $t->table($t->query()), $t->paginator()); } } } diff --git a/ext/not_a_tag/theme.php b/ext/not_a_tag/theme.php deleted file mode 100644 index 04acdfe4..00000000 --- a/ext/not_a_tag/theme.php +++ /dev/null @@ -1,20 +0,0 @@ -set_title("UnTags"); - $page->set_heading("UnTags"); - $page->add_block(new NavBlock()); - $page->add_block(new Block("Edit UnTags", emptyHTML($table, $paginator))); - } -} diff --git a/ext/pools/main.php b/ext/pools/main.php index a5285fce..3a6d1b99 100644 --- a/ext/pools/main.php +++ b/ext/pools/main.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shimmie2; +use function MicroHTML\{INPUT}; + abstract class PoolsConfig { public const MAX_IMPORT_RESULTS = "poolsMaxImportResults"; @@ -523,7 +525,13 @@ class Pools extends Extension $pools = $database->get_pairs("SELECT id,title FROM pools WHERE user_id=:id ORDER BY title", ["id" => $user->id]); } if (count($pools) > 0) { - $event->add_part($this->theme->get_adder_html($event->image, $pools)); + $html = SHM_SIMPLE_FORM( + "pool/add_post", + SHM_SELECT("pool_id", $pools), + INPUT(["type" => "hidden", "name" => "image_id", "value" => $event->image->id]), + SHM_SUBMIT("Add Post to Pool") + ); + $event->add_part($html); } } } diff --git a/ext/pools/theme.php b/ext/pools/theme.php index df4eda66..df3b4466 100644 --- a/ext/pools/theme.php +++ b/ext/pools/theme.php @@ -47,19 +47,6 @@ class PoolsTheme extends Themelet } } - /** - * @param array $pools - */ - public function get_adder_html(Image $image, array $pools): HTMLElement - { - return SHM_SIMPLE_FORM( - "pool/add_post", - SHM_SELECT("pool_id", $pools), - INPUT(["type" => "hidden", "name" => "image_id", "value" => $image->id]), - SHM_SUBMIT("Add Post to Pool") - ); - } - /** * HERE WE SHOWS THE LIST OF POOLS. * diff --git a/ext/private_image/main.php b/ext/private_image/main.php index c87be1ea..a8502162 100644 --- a/ext/private_image/main.php +++ b/ext/private_image/main.php @@ -212,7 +212,11 @@ class PrivateImage extends Extension { global $user; if (($user->can(Permissions::SET_PRIVATE_IMAGE) && $user->id == $event->image->owner_id) || $user->can(Permissions::SET_OTHERS_PRIVATE_IMAGES)) { - $event->add_part($this->theme->get_image_admin_html($event->image)); + if ($event->image['private'] === false) { + $event->add_button("Make Private", "privatize_image/".$event->image->id); + } else { + $event->add_button("Make Public", "publicize_image/".$event->image->id); + } } } diff --git a/ext/private_image/theme.php b/ext/private_image/theme.php index dc5d2fb8..a2aa45a7 100644 --- a/ext/private_image/theme.php +++ b/ext/private_image/theme.php @@ -8,23 +8,6 @@ use function MicroHTML\INPUT; class PrivateImageTheme extends Themelet { - public function get_image_admin_html(Image $image): \MicroHTML\HTMLElement - { - if ($image['private'] === false) { - $html = SHM_SIMPLE_FORM( - 'privatize_image/'.$image->id, - SHM_SUBMIT("Make Private") - ); - } else { - $html = SHM_SIMPLE_FORM( - 'publicize_image/'.$image->id, - SHM_SUBMIT("Make Public") - ); - } - - return $html; - } - public function get_help_html(): string { return '

Search for posts that are private/public.

diff --git a/ext/regen_thumb/main.php b/ext/regen_thumb/main.php index 789481eb..bd05b165 100644 --- a/ext/regen_thumb/main.php +++ b/ext/regen_thumb/main.php @@ -22,8 +22,8 @@ class RegenThumb extends Extension global $page, $user; if ($event->page_matches("regen_thumb") && $user->can(Permissions::DELETE_IMAGE)) { - if ($event->page_matches("regen_thumb/one")) { - $image = Image::by_id(int_escape($event->req_POST('image_id'))); + if ($event->authed_page_matches("regen_thumb/one")) { + $image = Image::by_id(int_escape($event->get_arg(0))); $this->regenerate_thumbnail($image); @@ -47,7 +47,7 @@ class RegenThumb extends Extension { global $user; if ($user->can(Permissions::DELETE_IMAGE)) { - $event->add_part($this->theme->get_buttons_html($event->image->id)); + $event->add_button("Regenerate Thumbnail", "regen_thumb/one/{$event->image->id}"); } } diff --git a/ext/regen_thumb/test.php b/ext/regen_thumb/test.php index 2eef060c..2583268a 100644 --- a/ext/regen_thumb/test.php +++ b/ext/regen_thumb/test.php @@ -12,7 +12,7 @@ class RegenThumbTest extends ShimmiePHPUnitTestCase $image_id = $this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot"); $this->get_page("post/view/$image_id"); - $this->post_page("regen_thumb/one", ['image_id' => $image_id]); + $this->post_page("regen_thumb/one/$image_id"); $this->assert_title("Thumbnail Regenerated"); # FIXME: test that the thumb's modified time has been updated diff --git a/ext/regen_thumb/theme.php b/ext/regen_thumb/theme.php index e6f4a16f..82f68905 100644 --- a/ext/regen_thumb/theme.php +++ b/ext/regen_thumb/theme.php @@ -8,18 +8,6 @@ use function MicroHTML\INPUT; class RegenThumbTheme extends Themelet { - /** - * Show a form which offers to regenerate the thumb of an image with ID #$image_id - */ - public function get_buttons_html(int $image_id): \MicroHTML\HTMLElement - { - return SHM_SIMPLE_FORM( - "regen_thumb/one", - INPUT(["type" => 'hidden', "name" => 'image_id', "value" => $image_id]), - SHM_SUBMIT('Regenerate Thumbnail') - ); - } - /** * Show a link to the new thumbnail. */ diff --git a/ext/replace_file/main.php b/ext/replace_file/main.php index 2351ee2a..da5aab70 100644 --- a/ext/replace_file/main.php +++ b/ext/replace_file/main.php @@ -51,7 +51,7 @@ class ReplaceFile extends Extension /* In the future, could perhaps allow users to replace images that they own as well... */ if ($user->can(Permissions::REPLACE_IMAGE)) { - $event->add_part($this->theme->get_replace_html($event->image->id)); + $event->add_button("Replace", "replace/{$event->image->id}"); } } diff --git a/ext/replace_file/theme.php b/ext/replace_file/theme.php index 8fbbb4ca..5df4a0b3 100644 --- a/ext/replace_file/theme.php +++ b/ext/replace_file/theme.php @@ -63,16 +63,6 @@ class ReplaceFileTheme extends Themelet $page->add_block(new Block("Upload Replacement File", $html, "main", 20)); } - /** - * Display link to replace the image - */ - public function get_replace_html(int $image_id): \MicroHTML\HTMLElement - { - $form = SHM_FORM("replace/$image_id", "GET"); - $form->appendChild(INPUT(["type" => 'submit', "value" => 'Replace'])); - return $form; - } - protected function get_accept(): string { return ".".join(",.", DataHandlerExtension::get_all_supported_exts()); diff --git a/ext/resize/main.php b/ext/resize/main.php index a4fc2ee3..9c4a234e 100644 --- a/ext/resize/main.php +++ b/ext/resize/main.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shimmie2; +use function MicroHTML\{rawHTML}; + abstract class ResizeConfig { public const ENABLED = 'resize_enabled'; @@ -45,10 +47,28 @@ class ResizeImage extends Extension public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void { global $user, $config; - if ($user->can(Permissions::EDIT_FILES) && $config->get_bool(ResizeConfig::ENABLED) - && $this->can_resize_mime($event->image->get_mime())) { + if ( + $user->can(Permissions::EDIT_FILES) && + $config->get_bool(ResizeConfig::ENABLED) && + $this->can_resize_mime($event->image->get_mime()) + ) { /* Add a link to resize the image */ - $event->add_part($this->theme->get_resize_html($event->image)); + global $config; + + $default_width = $config->get_int(ResizeConfig::DEFAULT_WIDTH, $event->image->width); + $default_height = $config->get_int(ResizeConfig::DEFAULT_HEIGHT, $event->image->height); + + $event->add_part(SHM_SIMPLE_FORM( + "resize/{$event->image->id}", + rawHTML(" + + + x + +
+
+ ") + )); } } @@ -121,7 +141,7 @@ class ResizeImage extends Extension { global $page, $user; - if ($event->page_matches("resize") && $user->can(Permissions::EDIT_FILES)) { + if ($event->authed_page_matches("resize") && $user->can(Permissions::EDIT_FILES)) { // Try to get the image ID $image_id = int_escape(null_throws($event->get_arg(0))); $image = Image::by_id($image_id); diff --git a/ext/resize/theme.php b/ext/resize/theme.php index 74dfb705..f1d4504e 100644 --- a/ext/resize/theme.php +++ b/ext/resize/theme.php @@ -8,37 +8,6 @@ use function MicroHTML\{rawHTML}; class ResizeImageTheme extends Themelet { - /* - * Display a link to resize an image - */ - public function get_resize_html(Image $image): \MicroHTML\HTMLElement - { - global $config; - - $default_width = $config->get_int(ResizeConfig::DEFAULT_WIDTH); - $default_height = $config->get_int(ResizeConfig::DEFAULT_HEIGHT); - - if (!$default_width) { - $default_width = $image->width; - } - if (!$default_height) { - $default_height = $image->height; - } - - $html = rawHTML(" - ".make_form(make_link("resize/{$image->id}"))." - - - x - -
-
- - "); - - return $html; - } - public function display_resize_error(Page $page, string $title, string $message): void { $page->set_title("Resize Image"); diff --git a/ext/rotate/main.php b/ext/rotate/main.php index 813dce34..119e03ac 100644 --- a/ext/rotate/main.php +++ b/ext/rotate/main.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Shimmie2; +use function MicroHTML\{INPUT}; + // TODO Add warning that rotate doesn't support lossless webp output /** @@ -36,7 +38,11 @@ class RotateImage extends Extension if ($user->can(Permissions::EDIT_FILES) && $config->get_bool("rotate_enabled") && MimeType::matches_array($event->image->get_mime(), self::SUPPORTED_MIME)) { /* Add a link to rotate the image */ - $event->add_part($this->theme->get_rotate_html($event->image->id)); + $event->add_part(SHM_SIMPLE_FORM( + 'rotate/'.$event->image->id, + INPUT(["type" => 'number', "name" => 'rotate_deg', "id" => "rotate_deg", "placeholder" => "Rotation degrees"]), + INPUT(["type" => 'submit', "value" => 'Rotate', "id" => "rotatebutton"]), + )); } } @@ -53,7 +59,7 @@ class RotateImage extends Extension { global $page, $user; - if ($event->page_matches("rotate") && $user->can(Permissions::EDIT_FILES)) { + if ($event->authed_page_matches("rotate") && $user->can(Permissions::EDIT_FILES)) { // Try to get the image ID $image_id = int_escape(null_throws($event->get_arg(0))); $image = Image::by_id($image_id); diff --git a/ext/rotate/theme.php b/ext/rotate/theme.php index cafcb06e..cc8164b7 100644 --- a/ext/rotate/theme.php +++ b/ext/rotate/theme.php @@ -4,22 +4,8 @@ declare(strict_types=1); namespace Shimmie2; -use function MicroHTML\INPUT; - class RotateImageTheme extends Themelet { - /** - * Display a link to rotate an image. - */ - public function get_rotate_html(int $image_id): \MicroHTML\HTMLElement - { - return SHM_SIMPLE_FORM( - 'rotate/'.$image_id, - INPUT(["type" => 'number', "name" => 'rotate_deg', "id" => "rotate_deg", "placeholder" => "Rotation degrees"]), - INPUT(["type" => 'submit', "value" => 'Rotate', "id" => "rotatebutton"]), - ); - } - /** * Display the error. */ diff --git a/ext/s3/main.php b/ext/s3/main.php index 3ad92fbc..e14fb247 100644 --- a/ext/s3/main.php +++ b/ext/s3/main.php @@ -130,11 +130,10 @@ class S3 extends Extension { global $config, $page, $user; if ( - $event->page_matches("s3/sync") && - $user->can(Permissions::DELETE_IMAGE) && - $user->check_auth_token() + $event->authed_page_matches("s3/sync") && + $user->can(Permissions::DELETE_IMAGE) ) { - $id = int_escape($event->req_POST('image_id')); + $id = int_escape($event->get_arg(0)); $this->sync_post(Image::by_id($id)); log_info("s3", "Manual resync for >>$id", "File re-sync'ed"); $page->set_mode(PageMode::REDIRECT); @@ -146,11 +145,7 @@ class S3 extends Extension { global $user; if ($user->can(Permissions::DELETE_IMAGE)) { - $event->add_part(SHM_SIMPLE_FORM( - "s3/sync", - INPUT(["type" => 'hidden', "name" => 'image_id', "value" => $event->image->id]), - INPUT(["type" => 'submit', "value" => 'CDN Re-Sync']), - )); + $event->add_button("CDN Re-Sync", "s3/sync/{$event->image->id}"); } } diff --git a/ext/source_history/main.php b/ext/source_history/main.php index 530039a6..c05d498a 100644 --- a/ext/source_history/main.php +++ b/ext/source_history/main.php @@ -58,11 +58,7 @@ class SourceHistory extends Extension public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void { - $event->add_part(rawHTML(" -
- -
- "), 20); + $event->add_button("View Source History", "source_history/{$event->image->id}", 20); } /* diff --git a/ext/tag_history/main.php b/ext/tag_history/main.php index c109ce28..601ed7d3 100644 --- a/ext/tag_history/main.php +++ b/ext/tag_history/main.php @@ -52,11 +52,7 @@ class TagHistory extends Extension public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void { - $event->add_part(rawHTML(" -
- -
- "), 20); + $event->add_button("View Tag History", "tag_history/{$event->image->id}", 20); } /* diff --git a/ext/trash/main.php b/ext/trash/main.php index 53830ec2..a945612e 100644 --- a/ext/trash/main.php +++ b/ext/trash/main.php @@ -153,7 +153,7 @@ class Trash extends Extension { global $user; if ($event->image['trash'] === true && $user->can(Permissions::VIEW_TRASH)) { - $event->add_part($this->theme->get_image_admin_html($event->image->id)); + $event->add_button("Restore From Trash", "trash_restore/".$event->image->id); } } diff --git a/ext/trash/theme.php b/ext/trash/theme.php index 1e8feb2f..00e1d39f 100644 --- a/ext/trash/theme.php +++ b/ext/trash/theme.php @@ -8,15 +8,6 @@ use function MicroHTML\INPUT; class TrashTheme extends Themelet { - public function get_image_admin_html(int $image_id): \MicroHTML\HTMLElement - { - return SHM_SIMPLE_FORM( - 'trash_restore/'.$image_id, - INPUT(["type" => 'submit', "value" => 'Restore From Trash']), - ); - } - - public function get_help_html(): string { return '

Search for posts in the trash.

diff --git a/ext/user/main.php b/ext/user/main.php index a22a6691..f7df6d89 100644 --- a/ext/user/main.php +++ b/ext/user/main.php @@ -200,7 +200,7 @@ class UserPage extends Extension // $t->columns[] = $col; array_splice($t->columns, 2, 0, [$col]); } - $this->theme->display_user_list($page, $t->table($t->query()), $t->paginator()); + $this->theme->display_crud("Users", $t->table($t->query()), $t->paginator()); } elseif ($event->get_arg(0) == "classes") { $this->theme->display_user_classes( $page, diff --git a/ext/user/theme.php b/ext/user/theme.php index a3fc9fae..7759327c 100644 --- a/ext/user/theme.php +++ b/ext/user/theme.php @@ -36,14 +36,6 @@ class UserPageTheme extends Themelet )); } - public function display_user_list(Page $page, HTMLElement $table, HTMLElement $paginator): void - { - $page->set_title("User List"); - $page->set_heading("User List"); - $page->add_block(new NavBlock()); - $page->add_block(new Block("Users", emptyHTML($table, $paginator))); - } - /** * @param array $parts */ diff --git a/ext/view/events/image_admin_block_building_event.php b/ext/view/events/image_admin_block_building_event.php index 6a79c8d2..a3acf614 100644 --- a/ext/view/events/image_admin_block_building_event.php +++ b/ext/view/events/image_admin_block_building_event.php @@ -6,6 +6,8 @@ namespace Shimmie2; use MicroHTML\HTMLElement; +use function MicroHTML\{FORM,INPUT}; + class ImageAdminBlockBuildingEvent extends Event { /** @var HTMLElement[] */ @@ -29,4 +31,18 @@ class ImageAdminBlockBuildingEvent extends Event } $this->parts[$position] = $html; } + + public function add_button(string $name, string $path, int $position = 50): void + { + $this->add_part( + SHM_SIMPLE_FORM( + make_link($path), + INPUT([ + "type" => "submit", + "value" => $name, + ]) + ), + $position + ); + } }