Skip to content

Commit dcf9d85

Browse files
committed
collections/:key/tags endpoint includes tags of items' children
Fixes: #103
1 parent 2cf66c3 commit dcf9d85

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

model/Collection.inc.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,12 +614,24 @@ public function getTags($asIDs=false) {
614614

615615

616616
/*
617-
* Returns an array keyed by tagID with the number of linked items for each tag
617+
* Returns an array keyed by tagID with the number of linked items, including their children, for each tag
618618
* in this collection
619619
*/
620620
public function getTagItemCounts() {
621-
$sql = "SELECT tagID, COUNT(*) AS numItems FROM tags JOIN itemTags USING (tagID)
622-
JOIN collectionItems USING (itemID) WHERE collectionID=? GROUP BY tagID";
621+
$sql = 'SELECT tagID, COUNT(*) AS numItems
622+
FROM tags
623+
JOIN (
624+
SELECT itemID, tagID FROM itemTags
625+
UNION ALL
626+
SELECT sourceItemID AS itemID, tagID FROM itemTags
627+
JOIN itemAttachments ON itemTags.itemID = itemAttachments.itemID
628+
UNION ALL
629+
SELECT sourceItemID AS itemID, tagID FROM itemTags
630+
JOIN itemNotes ON itemTags.itemID = itemNotes.itemID
631+
) combinedTags USING (tagID)
632+
JOIN collectionItems USING (itemID)
633+
WHERE collectionID = ?
634+
GROUP BY tagID';
623635
$rows = Zotero_DB::query($sql, $this->id, Zotero_Shards::getByLibraryID($this->libraryID));
624636
if (!$rows) {
625637
return false;

0 commit comments

Comments
 (0)