diff --git a/ext/rating/main.php b/ext/rating/main.php index 04b04f7a..81ca9ca3 100644 --- a/ext/rating/main.php +++ b/ext/rating/main.php @@ -42,6 +42,17 @@ add_rating(new ImageRating("e", "Explicit", "explicit", 1000)); /** @noinspection PhpIncludeInspection */ @include_once "data/config/ratings.conf.php"; +class RatingSetException extends UserError +{ + public ?string $redirect; + + public function __construct(string $msg, ?string $redirect = null) + { + parent::__construct($msg); + $this->redirect = $redirect; + } +} + class RatingSetEvent extends Event { public Image $image; @@ -197,7 +208,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) @@ -208,11 +219,27 @@ class Ratings extends Extension public function onImageInfoSet(ImageInfoSetEvent $event): void { - global $user; - $rating = $event->get_param('rating'); - if ($user->can(Permissions::EDIT_IMAGE_RATING) && !is_null($rating)) { + global $page, $user; + if ( + $user->can(Permissions::EDIT_IMAGE_RATING) && ( + isset($event->params['rating']) + || isset($event->params["rating{$event->slot}"]) + ) + ) { + $common_rating = $event->params['rating'] ?? ""; + $my_rating = $event->params["rating{$event->slot}"] ?? ""; + $rating = Ratings::rating_is_valid($my_rating) ? $my_rating : $common_rating; if (Ratings::rating_is_valid($rating)) { - send_event(new RatingSetEvent($event->image, $rating)); + try { + send_event(new RatingSetEvent($event->image, $rating)); + } catch (RatingSetException $e) { + if ($e->redirect) { + $page->flash("{$e->getMessage()}, please see {$e->redirect}"); + } else { + $page->flash($e->getMessage()); + } + throw $e; + } } } } @@ -387,6 +414,16 @@ class Ratings extends Extension } } + public function onUploadHeaderBuilding(UploadHeaderBuildingEvent $event): void + { + $event->add_part("Rating"); + } + + public function onUploadSpecificBuilding(UploadSpecificBuildingEvent $event): void + { + $event->add_part($this->theme->get_upload_specific_rater_html($event->suffix)); + } + /** * @return ImageRating[] */ diff --git a/ext/rating/theme.php b/ext/rating/theme.php index 72f6efb1..0c3f1f57 100644 --- a/ext/rating/theme.php +++ b/ext/rating/theme.php @@ -20,7 +20,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 +29,11 @@ class RatingsTheme extends Themelet ); } + public function get_upload_specific_rater_html(string $suffix): HTMLElement + { + return TD($this->get_selection_rater_html(name:"rating${suffix}", selected_options: ["?"])); + } + /** * @param array $current_ratings */