Improve usability of the `TaggedPage`
authorMarcel Werk <burntime@woltlab.com>
Fri, 5 May 2023 16:49:04 +0000 (18:49 +0200)
committerMarcel Werk <burntime@woltlab.com>
Fri, 5 May 2023 16:49:04 +0000 (18:49 +0200)
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
com.woltlab.wcf/templates/tagged.tpl
wcfsetup/install/files/lib/page/CombinedTaggedPage.class.php
wcfsetup/install/files/lib/page/TaggedPage.class.php

index 5ed73950bcacb5ba73bfb3ed980d70cd5438f4cd..887e9670b37256fcc53be35c8deac8fcdabc0469 100644 (file)
                <nav class="boxContent">
                        <ul class="boxMenu">
                                {foreach from=$availableObjectTypes item=availableObjectType}
-                                       <li{if $objectType == $availableObjectType->objectType} class="active"{/if}><a class="boxMenuLink" href="{link controller='CombinedTagged'}{@$linkParameters}&objectType={@$availableObjectType->objectType}{/link}">{lang}wcf.tagging.objectType.{@$availableObjectType->objectType}{/lang}</a></li>
+                                       <li{if $objectType == $availableObjectType->objectType} class="active"{/if}>
+                                               <a class="boxMenuLink" href="{link controller='CombinedTagged'}{@$linkParameters}&objectType={@$availableObjectType->objectType}{/link}">
+                                                       <span class="boxMenuLinkTitle">{lang}wcf.tagging.objectType.{@$availableObjectType->objectType}{/lang}</span>
+                                                       <span class="badge">{#$itemsPerType[$availableObjectType->objectType]}</span>
+                                               </a>
+                                       </li>
                                {/foreach}
                        </ul>
                </nav>
index 496200c76dad03a2ac51cb7bc29740b9ca14b07b..0d0627af12e62f1395d623e78908886465dc65f7 100644 (file)
                <nav class="boxContent">
                        <ul class="boxMenu">
                                {foreach from=$availableObjectTypes item=availableObjectType}
-                                       <li{if $objectType == $availableObjectType->objectType} class="active"{/if}><a class="boxMenuLink" href="{link controller='Tagged' object=$tag}objectType={@$availableObjectType->objectType}{/link}">{lang}wcf.tagging.objectType.{@$availableObjectType->objectType}{/lang}</a></li>
+                                       <li{if $objectType == $availableObjectType->objectType} class="active"{/if}>
+                                               <a class="boxMenuLink" href="{link controller='Tagged' object=$tag}objectType={@$availableObjectType->objectType}{/link}">
+                                                       <span class="boxMenuLinkTitle">{lang}wcf.tagging.objectType.{@$availableObjectType->objectType}{/lang}</span>
+                                                       <span class="badge">{#$itemsPerType[$availableObjectType->objectType]}</span>
+                                               </a>
+                                       </li>
                                {/foreach}
                        </ul>
                </nav>
index 78fc0b5d6f5fb14aec6ea9508fe17b273b2671cc..078b354daccfbadb63efeca4ec9918302af152b0 100644 (file)
@@ -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();
+        }
+    }
 }
index afc64a7f67c3a402fc44ad8566aecccff93aa543..1ded7bdb95a21fc97004bb10dcd4b0d919cba357 100644 (file)
@@ -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();
+        }
+    }
 }