From 15a4a9215ae16a91d2a7b783d03d5d4c59c16d69 Mon Sep 17 00:00:00 2001 From: Bogdan Abaev Date: Thu, 22 Jun 2023 16:00:04 -0400 Subject: [PATCH] editedBy item sort option for group libraries /groups/X/items?sort=editedBy Fixes: #153 --- controllers/ApiController.php | 2 +- model/API.inc.php | 1 + model/Items.inc.php | 11 +++++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/controllers/ApiController.php b/controllers/ApiController.php index b02bb4e0..4e343bf0 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -482,7 +482,7 @@ public function init($extra) { // Sorting by Item Type or Added By currently require writing to shard tables, so don't // send those to the read replicas - if ($this->queryParams['sort'] == 'itemType' || $this->queryParams['sort'] == 'addedBy') { + if ($this->queryParams['sort'] == 'itemType' || $this->queryParams['sort'] == 'addedBy' || $this->queryParams['sort'] == 'editedBy') { Zotero_DB::readOnly(false); } diff --git a/model/API.inc.php b/model/API.inc.php index 863104a3..0a015394 100644 --- a/model/API.inc.php +++ b/model/API.inc.php @@ -397,6 +397,7 @@ public static function parseQueryParams($queryString, $action, $singleObject, $a //case 'numChildren': case 'addedBy': + case 'editedBy': case 'numItems': case 'serverDateModified': diff --git a/model/Items.inc.php b/model/Items.inc.php index aa1e5bce..9ab05e95 100644 --- a/model/Items.inc.php +++ b/model/Items.inc.php @@ -274,13 +274,15 @@ public static function search($libraryID, $onlyTopLevel = false, array $params = // Join temp table to query $sql .= "JOIN tmpItemTypeNames TITN ON (TITN.itemTypeID=$itemTypeIDSelector) "; break; - + + case 'editedBy': case 'addedBy': $isGroup = Zotero_Libraries::getType($libraryID) == 'group'; + $userParameter = $params['sort'] == "editedBy" ? 'lastModifiedByUserID' : 'createdByUserID'; if ($isGroup) { - $sql2 = "SELECT DISTINCT createdByUserID FROM items + $sql2 = "SELECT DISTINCT $userParameter FROM items JOIN groupItems USING (itemID) WHERE - createdByUserID IS NOT NULL AND "; + $userParameter IS NOT NULL AND "; if ($itemIDs) { $sql2 .= "itemID IN (" . implode(', ', array_fill(0, sizeOf($itemIDs), '?')) @@ -307,7 +309,7 @@ public static function search($libraryID, $onlyTopLevel = false, array $params = // Join temp table to query $sql .= "LEFT JOIN groupItems GI ON (GI.itemID=I.itemID) - LEFT JOIN tmpCreatedByUsers TCBU ON (TCBU.userID=GI.createdByUserID) "; + LEFT JOIN tmpCreatedByUsers TCBU ON (TCBU.userID=GI.$userParameter) "; } } break; @@ -550,6 +552,7 @@ public static function search($libraryID, $onlyTopLevel = false, array $params = $orderSQL = "$sortTable.value"; break; + case 'editedBy': case 'addedBy': if ($isGroup && $createdByUserIDs) { $orderSQL = "TCBU.username";