From 571839ec4aa1719f98558335b24a694f42d0e2ee Mon Sep 17 00:00:00 2001 From: discomrade <83621080+discomrade@users.noreply.github.com> Date: Thu, 28 Mar 2024 01:36:34 +0000 Subject: [PATCH] [upload] build post fields using events --- ext/post_source/main.php | 15 ++++++ ext/post_source/theme.php | 19 +++++++ ext/post_tags/main.php | 16 ++++++ ext/post_tags/theme.php | 22 +++++++- .../events/upload_common_building_event.php | 14 +++++ .../events/upload_header_building_event.php | 12 +++++ .../events/upload_specific_building_event.php | 22 ++++++++ ext/upload/theme.php | 52 ++++++++++--------- 8 files changed, 146 insertions(+), 26 deletions(-) create mode 100644 ext/upload/events/upload_common_building_event.php create mode 100644 ext/upload/events/upload_header_building_event.php create mode 100644 ext/upload/events/upload_specific_building_event.php diff --git a/ext/post_source/main.php b/ext/post_source/main.php index a2fed4ef..f25fbcd4 100644 --- a/ext/post_source/main.php +++ b/ext/post_source/main.php @@ -98,6 +98,21 @@ class PostSource extends Extension } } + public function onUploadHeaderBuilding(UploadHeaderBuildingEvent $event): void + { + $event->add_part("Source", 11); + } + + public function onUploadCommonBuilding(UploadCommonBuildingEvent $event): void + { + $event->add_part($this->theme->get_upload_common_html(), 11); + } + + public function onUploadSpecificBuilding(UploadSpecificBuildingEvent $event): void + { + $event->add_part($this->theme->get_upload_specific_html($event->suffix), 11); + } + private function mass_source_edit(string $tags, string $source): void { $tags = Tag::explode($tags); diff --git a/ext/post_source/theme.php b/ext/post_source/theme.php index 40ef2ea5..a31994b6 100644 --- a/ext/post_source/theme.php +++ b/ext/post_source/theme.php @@ -51,4 +51,23 @@ class PostSourceTheme extends Themelet } return rawHTML("Unknown"); } + + public function get_upload_common_html(): HTMLElement + { + return TR( + TH(["width" => "20"], "Common Source"), + TD(["colspan" => "6"], INPUT(["name" => "source", "type" => "text", "placeholder" => "https://..."])) + ); + } + + public function get_upload_specific_html(string $suffix): HTMLElement + { + return TD( + INPUT([ + "type" => "text", + "name" => "source{$suffix}", + "value" => ($suffix == 0) ? @$_GET['source'] : null, + ]) + ); + } } diff --git a/ext/post_tags/main.php b/ext/post_tags/main.php index ff21433c..ed628294 100644 --- a/ext/post_tags/main.php +++ b/ext/post_tags/main.php @@ -218,6 +218,22 @@ class PostTags extends Extension } } + + public function onUploadHeaderBuilding(UploadHeaderBuildingEvent $event): void + { + $event->add_part("Tags", 10); + } + + public function onUploadCommonBuilding(UploadCommonBuildingEvent $event): void + { + $event->add_part($this->theme->get_upload_common_html(), 10); + } + + public function onUploadSpecificBuilding(UploadSpecificBuildingEvent $event): void + { + $event->add_part($this->theme->get_upload_specific_html($event->suffix), 10); + } + private function mass_tag_edit(string $search, string $replace, bool $commit): void { global $database, $tracer_enabled, $_tracer; diff --git a/ext/post_tags/theme.php b/ext/post_tags/theme.php index 4a3a932f..78665476 100644 --- a/ext/post_tags/theme.php +++ b/ext/post_tags/theme.php @@ -6,7 +6,7 @@ namespace Shimmie2; use MicroHTML\HTMLElement; -use function MicroHTML\{joinHTML, A, TEXTAREA}; +use function MicroHTML\{joinHTML, A, TEXTAREA, TR, TH, TD, INPUT}; class PostTagsTheme extends Themelet { @@ -53,4 +53,24 @@ class PostTagsTheme extends Themelet null, ); } + + public function get_upload_common_html(): HTMLElement + { + return TR( + TH(["width" => "20"], "Common Tags"), + TD(["colspan" => "6"], INPUT(["name" => "tags", "type" => "text", "placeholder" => "tagme", "class" => "autocomplete_tags"])) + ); + } + + public function get_upload_specific_html(string $suffix): HTMLElement + { + return TD( + INPUT([ + "type" => "text", + "name" => "tags{$suffix}", + "class" => "autocomplete_tags", + "value" => ($suffix == 0) ? @$_GET['tags'] : null, + ]) + ); + } } diff --git a/ext/upload/events/upload_common_building_event.php b/ext/upload/events/upload_common_building_event.php new file mode 100644 index 00000000..d719ae95 --- /dev/null +++ b/ext/upload/events/upload_common_building_event.php @@ -0,0 +1,14 @@ + + */ +class UploadCommonBuildingEvent extends PartListBuildingEvent +{ +} diff --git a/ext/upload/events/upload_header_building_event.php b/ext/upload/events/upload_header_building_event.php new file mode 100644 index 00000000..be8f5f50 --- /dev/null +++ b/ext/upload/events/upload_header_building_event.php @@ -0,0 +1,12 @@ + + */ +class UploadHeaderBuildingEvent extends PartListBuildingEvent +{ +} diff --git a/ext/upload/events/upload_specific_building_event.php b/ext/upload/events/upload_specific_building_event.php new file mode 100644 index 00000000..38f6285f --- /dev/null +++ b/ext/upload/events/upload_specific_building_event.php @@ -0,0 +1,22 @@ + + */ +class UploadSpecificBuildingEvent extends PartListBuildingEvent +{ + public string $suffix; + + public function __construct(string $suffix) + { + parent::__construct(); + + $this->suffix = $suffix; + } +} diff --git a/ext/upload/theme.php b/ext/upload/theme.php index aa2710e3..0773a098 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -4,6 +4,10 @@ declare(strict_types=1); namespace Shimmie2; +require_once "events/upload_common_building_event.php"; +require_once "events/upload_specific_building_event.php"; +require_once "events/upload_header_building_event.php"; + use MicroHTML\HTMLElement; use function MicroHTML\{TABLE,TR,TH,TD}; @@ -44,18 +48,17 @@ class UploadTheme extends Themelet $max_total_kb = to_shorthand_int($max_total_size); $upload_list = $this->build_upload_list(); + $common_fields = emptyHTML(); + $ucbe = send_event(new UploadCommonBuildingEvent()); + foreach ($ucbe->get_parts() as $part) { + $common_fields->appendChild($part); + } + $form = SHM_FORM("upload", multipart: true, form_id: "file_upload"); $form->appendChild( TABLE( ["id" => "large_upload_form", "class" => "form"], - TR( - TH(["width" => "20"], "Common Tags"), - TD(["colspan" => "6"], INPUT(["name" => "tags", "type" => "text", "placeholder" => "tagme", "class" => "autocomplete_tags"])) - ), - TR( - TH(["width" => "20"], "Common Source"), - TD(["colspan" => "6"], INPUT(["name" => "source", "type" => "text", "placeholder" => "https://..."])) - ), + $common_fields, $upload_list, TR( TD(["colspan" => "7"], INPUT(["id" => "uploadbutton", "type" => "submit", "value" => "Post"])) @@ -96,17 +99,30 @@ class UploadTheme extends Themelet $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); $accept = $this->get_accept(); + $headers = emptyHTML(); + $uhbe = send_event(new UploadHeaderBuildingEvent()); + foreach ($uhbe->get_parts() as $part) { + $headers->appendChild( + TH("Post-Specific $part") + ); + } + $upload_list->appendChild( TR( ["class" => "header"], TH(["colspan" => 2], "Select File"), TH($tl_enabled ? "or URL" : null), - TH("Post-Specific Tags"), - TH("Post-Specific Source"), + $headers, ) ); for ($i = 0; $i < $upload_count; $i++) { + $specific_fields = emptyHTML(); + $usfbe = send_event(new UploadSpecificBuildingEvent((string)$i)); + foreach ($usfbe->get_parts() as $part) { + $specific_fields->appendChild($part); + } + $upload_list->appendChild( TR( TD( @@ -131,21 +147,7 @@ class UploadTheme extends Themelet "value" => ($i == 0) ? @$_GET['url'] : null, ]) : null ), - TD( - INPUT([ - "type" => "text", - "name" => "tags{$i}", - "class" => "autocomplete_tags", - "value" => ($i == 0) ? @$_GET['tags'] : null, - ]) - ), - TD( - INPUT([ - "type" => "text", - "name" => "source{$i}", - "value" => ($i == 0) ? @$_GET['source'] : null, - ]) - ), + $specific_fields, ) ); }