From bc08e867f01237b151b212d3f260c4c2b580e852 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Thu, 11 Jan 2024 18:54:54 +0100 Subject: [PATCH] PhotoPage: make prev/next photo logic more direct --- controllers/ViewPhoto.php | 13 ++----------- models/Asset.php | 16 ++++++++-------- templates/PhotoPage.php | 26 ++++++++++---------------- 3 files changed, 20 insertions(+), 35 deletions(-) diff --git a/controllers/ViewPhoto.php b/controllers/ViewPhoto.php index e6a8c39..52cb2f8 100644 --- a/controllers/ViewPhoto.php +++ b/controllers/ViewPhoto.php @@ -40,17 +40,8 @@ class ViewPhoto extends HTMLController // What tag are we browsing? $tag = isset($_GET['in']) ? Tag::fromId($_GET['in']) : null; - $id_tag = isset($tag) ? $tag->id_tag : null; - - // Find previous photo in set. - $previous_url = $photo->getUrlForPreviousInSet($id_tag); - if ($previous_url) - $page->setPreviousPhotoUrl($previous_url); - - // ... and the next photo, too. - $next_url = $photo->getUrlForNextInSet($id_tag); - if ($next_url) - $page->setNextPhotoUrl($next_url); + if (isset($tag)) + $page->setTag($tag); $this->page->adopt($page); $this->page->setCanonicalUrl($photo->getPageUrl()); diff --git a/models/Asset.php b/models/Asset.php index 5779761..93e77bc 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -697,11 +697,11 @@ class Asset $params); } - public function getUrlForPreviousInSet($id_tag = null) + public function getUrlForPreviousInSet(?Tag $tag) { $row = Registry::get('db')->queryAssoc(' SELECT a.* - ' . (isset($id_tag) ? ' + ' . (isset($tag) ? ' FROM assets_tags AS t INNER JOIN assets AS a ON a.id_asset = t.id_asset WHERE t.id_tag = {int:id_tag} AND @@ -715,24 +715,24 @@ class Asset LIMIT 1', [ 'id_asset' => $this->id_asset, - 'id_tag' => $id_tag, + 'id_tag' => $tag->id_tag, 'date_captured' => $this->date_captured, ]); if ($row) { $obj = self::byRow($row, 'object'); - return $obj->getPageUrl() . ($id_tag ? '?in=' . $id_tag : ''); + return $obj->getPageUrl() . ($tag ? '?in=' . $tag->id_tag : ''); } else return false; } - public function getUrlForNextInSet($id_tag = null) + public function getUrlForNextInSet(?Tag $tag) { $row = Registry::get('db')->queryAssoc(' SELECT a.* - ' . (isset($id_tag) ? ' + ' . (isset($tag) ? ' FROM assets_tags AS t INNER JOIN assets AS a ON a.id_asset = t.id_asset WHERE t.id_tag = {int:id_tag} AND @@ -746,14 +746,14 @@ class Asset LIMIT 1', [ 'id_asset' => $this->id_asset, - 'id_tag' => $id_tag, + 'id_tag' => $tag->id_tag, 'date_captured' => $this->date_captured, ]); if ($row) { $obj = self::byRow($row, 'object'); - return $obj->getPageUrl() . ($id_tag ? '?in=' . $id_tag : ''); + return $obj->getPageUrl() . ($tag ? '?in=' . $tag->id_tag : ''); } else return false; diff --git a/templates/PhotoPage.php b/templates/PhotoPage.php index 9554106..1b61f2d 100644 --- a/templates/PhotoPage.php +++ b/templates/PhotoPage.php @@ -10,24 +10,13 @@ class PhotoPage extends Template { protected $photo; private $exif; - private $previous_photo_url = ''; - private $next_photo_url = ''; + private $tag; public function __construct(Image $photo) { $this->photo = $photo; } - public function setPreviousPhotoUrl($url) - { - $this->previous_photo_url = $url; - } - - public function setNextPhotoUrl($url) - { - $this->next_photo_url = $url; - } - public function html_main() { $this->photoNav(); @@ -86,18 +75,23 @@ class PhotoPage extends Template '; } + public function setTag(Tag $tag) + { + $this->tag = $tag; + } + private function photoNav() { - if ($this->previous_photo_url) + if ($previousUrl = $this->photo->getUrlForPreviousInSet($this->tag)) echo ' - '; + '; else echo ' '; - if ($this->next_photo_url) + if ($nextUrl = $this->photo->getUrlForNextInSet($this->tag)) echo ' - '; + '; else echo ' ';