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 arraySearch 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 + +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