From f97539f3999a50015e1e5551f6a374b98755d553 Mon Sep 17 00:00:00 2001 From: Alex Cabal Date: Sun, 3 Nov 2024 15:15:07 -0600 Subject: [PATCH] Redirect an artwork if it's found under an artist's alternate name. --- lib/Artist.php | 21 ++++++++++++++++++++- www/artworks/get.php | 21 ++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/Artist.php b/lib/Artist.php index fd072c72..67b9467b 100644 --- a/lib/Artist.php +++ b/lib/Artist.php @@ -130,7 +130,26 @@ class Artist{ where ArtistId = ? ', [$artistId], Artist::class); - return $result[0] ?? throw new Exceptions\ArtistNotFoundException();; + return $result[0] ?? throw new Exceptions\ArtistNotFoundException(); + } + + /** + * @throws Exceptions\ArtistNotFoundException + */ + public static function GetByAlternateUrlName(?string $urlName): Artist{ + if($urlName === null){ + throw new Exceptions\ArtistNotFoundException(); + } + + $result = Db::Query(' + SELECT a.* + from Artists a + left outer join ArtistAlternateNames aan using (ArtistId) + where aan.UrlName = ? + limit 1 + ', [$urlName], Artist::class); + + return $result[0] ?? throw new Exceptions\ArtistNotFoundException(); } /** diff --git a/www/artworks/get.php b/www/artworks/get.php index 3c21095b..34e8b635 100644 --- a/www/artworks/get.php +++ b/www/artworks/get.php @@ -8,7 +8,26 @@ $isSaved = HttpInput::Bool(SESSION, 'is-saved') ?? false; $exception = $_SESSION['exception'] ?? null; try{ - $artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name')); + try{ + $artwork = Artwork::GetByUrl(HttpInput::Str(GET, 'artist-url-name'), HttpInput::Str(GET, 'artwork-url-name')); + } + catch(Exceptions\ArtworkNotFoundException $ex){ + // We didn't find the artwork under this artist, does the artist exist under an alternate name? + try{ + $artist = Artist::GetByAlternateUrlName(HttpInput::Str(GET, 'artist-url-name')); + $artwork = Artwork::GetByUrl($artist->UrlName, HttpInput::Str(GET, 'artwork-url-name')); + + // Artwork found under an artist alternate name, redirect there and exit. + http_response_code(Enums\HttpCode::MovedPermanently->value); + header('Location: ' . $artwork->Url); + exit(); + } + catch(Exceptions\ArtistNotFoundException){ + // The artwork is really not found, throw the original exception. + throw $ex; + } + } + $isReviewerView = $GLOBALS['User']->Benefits->CanReviewArtwork ?? false; $isAdminView = $GLOBALS['User']->Benefits->CanReviewOwnArtwork ?? false;