From ae58d49716bc544878b2e3ad031849870b00ec6f Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Fri, 5 May 2023 18:49:04 +0200 Subject: [PATCH] Improve usability of the `TaggedPage` If the page was called without filtering by `objectType`, the first available `objectType` was taken, even if this did not provide any results at all for the respective tag. --- com.woltlab.wcf/templates/combinedTagged.tpl | 7 ++++- com.woltlab.wcf/templates/tagged.tpl | 7 ++++- .../lib/page/CombinedTaggedPage.class.php | 31 +++++++++++++++++-- .../files/lib/page/TaggedPage.class.php | 31 +++++++++++++++++-- 4 files changed, 70 insertions(+), 6 deletions(-) diff --git a/com.woltlab.wcf/templates/combinedTagged.tpl b/com.woltlab.wcf/templates/combinedTagged.tpl index 5ed73950bc..887e9670b3 100644 --- a/com.woltlab.wcf/templates/combinedTagged.tpl +++ b/com.woltlab.wcf/templates/combinedTagged.tpl @@ -27,7 +27,12 @@ diff --git a/com.woltlab.wcf/templates/tagged.tpl b/com.woltlab.wcf/templates/tagged.tpl index 496200c76d..0d0627af12 100644 --- a/com.woltlab.wcf/templates/tagged.tpl +++ b/com.woltlab.wcf/templates/tagged.tpl @@ -19,7 +19,12 @@ diff --git a/wcfsetup/install/files/lib/page/CombinedTaggedPage.class.php b/wcfsetup/install/files/lib/page/CombinedTaggedPage.class.php index 78fc0b5d6f..078b354dac 100644 --- a/wcfsetup/install/files/lib/page/CombinedTaggedPage.class.php +++ b/wcfsetup/install/files/lib/page/CombinedTaggedPage.class.php @@ -2,6 +2,7 @@ namespace wcf\page; +use wcf\data\DatabaseObjectList; use wcf\data\object\type\ObjectType; use wcf\data\object\type\ObjectTypeCache; use wcf\data\tag\Tag; @@ -64,6 +65,12 @@ class CombinedTaggedPage extends MultipleLinkPage */ public $tagCloud; + /** + * @var int[] + * @since 6.0 + */ + public $itemsPerType = []; + /** * @inheritDoc */ @@ -104,6 +111,8 @@ class CombinedTaggedPage extends MultipleLinkPage throw new IllegalLinkException(); } + $this->readItemsPerType(); + if (isset($_REQUEST['objectType'])) { $objectType = StringUtil::trim($_REQUEST['objectType']); if (!isset($this->availableObjectTypes[$objectType])) { @@ -111,8 +120,16 @@ class CombinedTaggedPage extends MultipleLinkPage } $this->objectType = $this->availableObjectTypes[$objectType]; } else { - // No object type provided, use the first object type. - $this->objectType = \reset($this->availableObjectTypes); + foreach ($this->availableObjectTypes as $key => $objectType) { + if ($this->itemsPerType[$key]) { + $this->objectType = $objectType; + break; + } + } + + if (!$this->objectType) { + $this->objectType = \reset($this->availableObjectTypes); + } } $this->processor = $this->objectType->getProcessor(); @@ -150,10 +167,20 @@ class CombinedTaggedPage extends MultipleLinkPage 'objectType' => $this->objectType->objectType, 'resultListTemplateName' => $this->processor->getTemplateName(), 'resultListApplication' => $this->processor->getApplication(), + 'itemsPerType' => $this->itemsPerType, ]); if (\count($this->objectList) === 0) { @\header('HTTP/1.1 404 Not Found'); } } + + private function readItemsPerType(): void + { + foreach ($this->availableObjectTypes as $key => $objectType) { + $objectList = $objectType->getProcessor()->getObjectListFor($this->tags); + \assert($objectList instanceof DatabaseObjectList); + $this->itemsPerType[$key] = $objectList->countObjects(); + } + } } diff --git a/wcfsetup/install/files/lib/page/TaggedPage.class.php b/wcfsetup/install/files/lib/page/TaggedPage.class.php index afc64a7f67..1ded7bdb95 100644 --- a/wcfsetup/install/files/lib/page/TaggedPage.class.php +++ b/wcfsetup/install/files/lib/page/TaggedPage.class.php @@ -2,6 +2,7 @@ namespace wcf\page; +use wcf\data\DatabaseObjectList; use wcf\data\object\type\ObjectType; use wcf\data\object\type\ObjectTypeCache; use wcf\data\tag\Tag; @@ -59,6 +60,12 @@ class TaggedPage extends MultipleLinkPage */ public $tagCloud; + /** + * @var int[] + * @since 6.0 + */ + public $itemsPerType = []; + /** * @inheritDoc */ @@ -87,6 +94,8 @@ class TaggedPage extends MultipleLinkPage throw new IllegalLinkException(); } + $this->readItemsPerType(); + // get object type if (isset($_REQUEST['objectType'])) { $objectType = StringUtil::trim($_REQUEST['objectType']); @@ -95,8 +104,16 @@ class TaggedPage extends MultipleLinkPage } $this->objectType = $this->availableObjectTypes[$objectType]; } else { - // use first object type - $this->objectType = \reset($this->availableObjectTypes); + foreach ($this->availableObjectTypes as $key => $objectType) { + if ($this->itemsPerType[$key]) { + $this->objectType = $objectType; + break; + } + } + + if (!$this->objectType) { + $this->objectType = \reset($this->availableObjectTypes); + } } } @@ -132,10 +149,20 @@ class TaggedPage extends MultipleLinkPage 'objectType' => $this->objectType->objectType, 'resultListTemplateName' => $this->objectType->getProcessor()->getTemplateName(), 'resultListApplication' => $this->objectType->getProcessor()->getApplication(), + 'itemsPerType' => $this->itemsPerType, ]); if (\count($this->objectList) === 0) { @\header('HTTP/1.1 404 Not Found'); } } + + private function readItemsPerType(): void + { + foreach ($this->availableObjectTypes as $key => $objectType) { + $objectList = $objectType->getProcessor()->getObjectList($this->tag); + \assert($objectList instanceof DatabaseObjectList); + $this->itemsPerType[$key] = $objectList->countObjects(); + } + } } -- 2.20.1