diff --git a/model/Collection.inc.php b/model/Collection.inc.php index e51af75f..703d75c8 100644 --- a/model/Collection.inc.php +++ b/model/Collection.inc.php @@ -629,6 +629,24 @@ public function getTagItemCounts() { foreach ($rows as $row) { $counts[$row['tagID']] = $row['numItems']; } + // Fetch the tags of annotations as well + $annotationsSql = "SELECT tagID, COUNT(*) AS numItems FROM itemTags + JOIN itemAnnotations USING (itemID) + JOIN itemAttachments ON (itemAttachments.itemID = itemAnnotations.parentItemID) + JOIN collectionItems ON (collectionItems.itemID = itemAttachments.sourceItemID) + WHERE collectionID=? GROUP BY tagID"; + + $rows = Zotero_DB::query($annotationsSql, $this->id, Zotero_Shards::getByLibraryID($this->libraryID)); + if (!$rows) { + return $counts; + } + // Add numItems into the same array. + foreach ($rows as $row) { + if (!array_key_exists($row['tagID'], $counts)) { + $counts[$row['tagID']] = 0; + } + $counts[$row['tagID']] += $row['numItems']; + } return $counts; } diff --git a/model/Items.inc.php b/model/Items.inc.php index 76c99634..f079e08d 100644 --- a/model/Items.inc.php +++ b/model/Items.inc.php @@ -431,7 +431,12 @@ public static function search($libraryID, $onlyTopLevel = false, array $params = $tagIDs = array_unique($tagIDs); - $tmpSQL = "SELECT itemID FROM items JOIN itemTags USING (itemID) " + // If /top items are requested, fetch itemIDs of top level items whose + // children match the tag query + $tmpSelect = $onlyTopLevel ? "COALESCE(topLevelItemID, itemID)" : "itemID"; + $tmpJoin = $onlyTopLevel ? "LEFT JOIN itemTopLevel USING (itemID) " : " "; + $tmpSQL = "SELECT $tmpSelect FROM items JOIN itemTags USING (itemID) " + . $tmpJoin . "WHERE tagID IN (" . implode(',', array_fill(0, sizeOf($tagIDs), '?')) . ")"; $ids = Zotero_DB::columnQuery($tmpSQL, $tagIDs, $shardID);