diff --git a/ext/rating/main.php b/ext/rating/main.php index b9f45049..df82323a 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -165,6 +165,11 @@ class Ratings extends Extension } } + public function onUploadRatingBuilding(UploadRatingBuildingEvent $event): void + { + $event->part = (string)$this->theme->get_upload_rater_html($event->suffix); + } + public function onDisplayingImage(DisplayingImageEvent $event): void { global $page; @@ -204,7 +209,7 @@ class Ratings extends Extension { global $user; $event->add_part( - $this->theme->get_rater_html( + $this->theme->get_image_rater_html( $event->image->id, $event->image['rating'], $user->can(Permissions::EDIT_IMAGE_RATING) diff --git a/ext/rating/theme.php b/ext/rating/theme.php index 72f6efb1..b58f6551 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -7,6 +7,7 @@ namespace Shimmie2; use MicroHTML\HTMLElement; use function MicroHTML\emptyHTML; +use function MicroHTML\rawHTML; use function MicroHTML\{A,P,TABLE,TD,TH,TR}; class RatingsTheme extends Themelet @@ -20,7 +21,7 @@ class RatingsTheme extends Themelet return SHM_SELECT($name, !empty($ratings) ? $ratings : Ratings::get_ratings_dict(), required: true, selected_options: $selected_options); } - public function get_rater_html(int $image_id, string $rating, bool $can_rate): HTMLElement + public function get_image_rater_html(int $image_id, string $rating, bool $can_rate): HTMLElement { return SHM_POST_INFO( "Rating", @@ -29,6 +30,11 @@ class RatingsTheme extends Themelet ); } + public function get_upload_rater_html(string $suffix): HTMLElement + { + return $this->get_selection_rater_html(name:"rating${suffix}", selected_options: ["?"]); + } + /** * @param array $current_ratings */ diff --git a/ext/upload/events/upload_rating_building_event.php b/ext/upload/events/upload_rating_building_event.php new file mode 100644 index 00000000..9d3ae698 --- /dev/null +++ b/ext/upload/events/upload_rating_building_event.php @@ -0,0 +1,18 @@ +suffix = $suffix; + } +} diff --git a/ext/upload/main.php b/ext/upload/main.php index b2d620d4..ff00744c 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Shimmie2; require_once "config.php"; +require_once "events/upload_rating_building_event.php"; /** * Occurs when some data is being uploaded. @@ -36,6 +37,9 @@ class DataUploadEvent extends Event assert(is_string($metadata["filename"])); assert(is_array($metadata["tags"])); assert(is_string($metadata["source"]) || is_null($metadata["source"])); + if (Extension::is_enabled(RatingsInfo::KEY)) { + assert(is_string($metadata['rating'])); + } // DB limits to 255 char filenames $metadata['filename'] = substr($metadata['filename'], 0, 255); @@ -237,7 +241,8 @@ class Upload extends Extension $slot = int_escape(substr($name, 4)); $tags = $this->tags_for_upload_slot($event->POST, $slot); $source = $this->source_for_upload_slot($event->POST, $slot); - $results = array_merge($results, $this->try_upload($file, $tags, $source)); + $rating = $this->rating_for_upload_slot($event->POST, $slot); + $results = array_merge($results, $this->try_upload($file, $tags, $source, $rating)); } $urls = array_filter($event->POST, function ($value, $key) { @@ -247,7 +252,8 @@ class Upload extends Extension $slot = int_escape(substr($name, 3)); $tags = $this->tags_for_upload_slot($event->POST, $slot); $source = $this->source_for_upload_slot($event->POST, $slot); - $results = array_merge($results, $this->try_transload($value, $tags, $source)); + $rating = $this->rating_for_upload_slot($event->POST, $slot); + $results = array_merge($results, $this->try_transload($value, $tags, $source, $rating)); } $this->theme->display_upload_status($page, $results); @@ -287,6 +293,22 @@ class Upload extends Extension return null; } + /** + * @param array $params + */ + private function rating_for_upload_slot(array $params, int $id): ?string + { + if (Extension::is_enabled(RatingsInfo::KEY) && (!empty($params["rating"]) || !empty($params["rating$id"]))) { + if($params["rating$id"] != "?") { + return $params["rating$id"]; + } + if($params["rating"] != "?") { + return $params["rating"]; + } + } + return null; + } + /** * Returns a descriptive error message for the specified PHP error code. * @@ -323,7 +345,7 @@ class Upload extends Extension * @param string[] $tags * @return UploadResult[] */ - private function try_upload(array $file, array $tags, ?string $source = null): array + private function try_upload(array $file, array $tags, ?string $source = null, ?string $rating = null): array { global $page, $config, $database; @@ -352,11 +374,12 @@ class Upload extends Extension throw new UploadException($this->upload_error_message($error)); } - $new_images = $database->with_savepoint(function () use ($tmp_name, $name, $tags, $source) { + $new_images = $database->with_savepoint(function () use ($tmp_name, $name, $tags, $source, $rating) { $event = send_event(new DataUploadEvent($tmp_name, [ 'filename' => pathinfo($name, PATHINFO_BASENAME), 'tags' => $tags, 'source' => $source, + 'rating' => $rating, ])); if (count($event->images) == 0) { throw new UploadException("MIME type not supported: " . $event->mime); @@ -378,7 +401,7 @@ class Upload extends Extension * @param string[] $tags * @return UploadResult[] */ - private function try_transload(string $url, array $tags, string $source = null): array + private function try_transload(string $url, array $tags, string $source = null, ?string $rating = null): array { global $page, $config, $user, $database; @@ -402,6 +425,9 @@ class Upload extends Extension $metadata['filename'] = $filename; $metadata['tags'] = $tags; $metadata['source'] = $source; + if (Extension::is_enabled(RatingsInfo::KEY)) { + $metadata['rating'] = $rating; + } $new_images = $database->with_savepoint(function () use ($tmp_filename, $metadata) { $event = send_event(new DataUploadEvent($tmp_filename, $metadata)); diff --git a/ext/upload/theme.php b/ext/upload/theme.php index aa2710e3..11da7595 100644 --- a/ext/upload/theme.php +++ b/ext/upload/theme.php @@ -94,6 +94,7 @@ class UploadTheme extends Themelet $upload_list = emptyHTML(); $upload_count = $config->get_int(UploadConfig::COUNT); $tl_enabled = ($config->get_string(UploadConfig::TRANSLOAD_ENGINE, "none") != "none"); + $ratings_enabled = Extension::is_enabled(RatingsInfo::KEY); $accept = $this->get_accept(); $upload_list->appendChild( @@ -103,9 +104,12 @@ class UploadTheme extends Themelet TH($tl_enabled ? "or URL" : null), TH("Post-Specific Tags"), TH("Post-Specific Source"), + TH($ratings_enabled ? "Rating" : null), ) ); + + for ($i = 0; $i < $upload_count; $i++) { $upload_list->appendChild( TR( @@ -146,6 +150,9 @@ class UploadTheme extends Themelet "value" => ($i == 0) ? @$_GET['source'] : null, ]) ), + TD( + $ratings_enabled ? rawHTML(send_event(new UploadRatingBuildingEvent((string)$i))->part) : null + ), ) ); }