diff --git a/lib/Alchemy/Phrasea/Collection/Reference/ArrayCacheCollectionReferenceRepository.php b/lib/Alchemy/Phrasea/Collection/Reference/ArrayCacheCollectionReferenceRepository.php index 7351351e93..64c80ae955 100644 --- a/lib/Alchemy/Phrasea/Collection/Reference/ArrayCacheCollectionReferenceRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Reference/ArrayCacheCollectionReferenceRepository.php @@ -72,6 +72,25 @@ class ArrayCacheCollectionReferenceRepository implements CollectionReferenceRepo return null; } + /** + * @param array $baseIds + * @return CollectionReference[] + */ + public function findMany(array $baseIds) + { + $references = $this->findAll(); + + $requested = []; + + foreach ($baseIds as $baseId) { + if (isset($references[$baseId])) { + $requested[] = $references[$baseId]; + } + } + + return $requested; + } + /** * @param int $databoxId * @param int $collectionId @@ -90,6 +109,10 @@ class ArrayCacheCollectionReferenceRepository implements CollectionReferenceRepo return null; } + /** + * @param array|null $baseIdsSubset + * @return CollectionReference[] + */ public function findHavingOrderMaster(array $baseIdsSubset = null) { return $this->repository->findHavingOrderMaster($baseIdsSubset); diff --git a/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php b/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php index fdbf7289d1..7740f8ddc9 100644 --- a/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Reference/CollectionReferenceRepository.php @@ -29,6 +29,12 @@ interface CollectionReferenceRepository */ public function find($baseId); + /** + * @param int[] $baseIds + * @return CollectionReference[] + */ + public function findMany(array $baseIds); + /** * @param int $databoxId * @param int $collectionId diff --git a/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php b/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php index a67f8cad3d..00c8101259 100644 --- a/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php +++ b/lib/Alchemy/Phrasea/Collection/Reference/DbalCollectionReferenceRepository.php @@ -95,6 +95,25 @@ WHERE base_id = :baseId'; return null; } + /** + * @param array $basesId + * @return CollectionReference[] + */ + public function findMany(array $basesId) + { + if (empty($basesId)) { + return []; + } + + $rows = $this->connection->fetchAll( + self::$selectQuery . ' WHERE base_id IN (:baseIds)', + ['baseIds' => $basesId], + ['baseIds' => Connection::PARAM_INT_ARRAY] + ); + + return $this->createManyReferences($rows); + } + /** * @param int $databoxId * @param int $collectionId diff --git a/lib/classes/ACL.php b/lib/classes/ACL.php index b28c134af6..7bb0766e61 100644 --- a/lib/classes/ACL.php +++ b/lib/classes/ACL.php @@ -1763,20 +1763,39 @@ class ACL implements cache_cacheableInterface /** * Returns an array of collections on which the user is 'order master' * - * @return array + * @return collection[] */ public function get_order_master_collections() { $sql = 'SELECT base_id FROM basusr WHERE order_master="1" AND usr_id= :usr_id'; - $stmt = $this->app->getApplicationBox()->get_connection()->prepare($sql); - $stmt->execute([':usr_id' => $this->user->getId()]); - $rs = $stmt->fetchAll(\PDO::FETCH_ASSOC); - $stmt->closeCursor(); + $result = $this->app->getApplicationBox() + ->get_connection() + ->executeQuery($sql, [':usr_id' => $this->user->getId()]) + ->fetchAll(\PDO::FETCH_ASSOC); + + + $baseIds = []; + + foreach ($result as $item) { + $baseIds[] = $item['base_id']; + } + + $groups = []; + + foreach ($this->app['repo.collection-references']->findHavingOrderMaster($baseIds) as $index => $reference) { + $databoxId = $reference->getDataboxId(); + $group = isset($groups[$databoxId]) ? $groups[$databoxId] : []; + + $group[$reference->getCollectionId()] = $index; + $groups[$databoxId] = $group; + } $collections = []; - foreach ($rs as $row) { - $collections[] = \collection::getByBaseId($this->app, $row['base_id']); + foreach ($groups as $databoxId => $group) { + foreach ($group as $collectionId => $index) { + $collections[$index] = \collection::getByCollectionId($this->app, $databoxId, $collectionId); + } } return $collections; diff --git a/lib/classes/collection.php b/lib/classes/collection.php index 7ac76177bb..9c56620c05 100644 --- a/lib/classes/collection.php +++ b/lib/classes/collection.php @@ -192,16 +192,17 @@ class collection implements ThumbnailedElement, cache_cacheableInterface } /** - * @param Application $app - * @param databox $databox - * @param int $collectionId + * @param Application $app + * @param databox|int $databox + * @param int $collectionId * @return collection */ - public static function getByCollectionId(Application $app, databox $databox, $collectionId) + public static function getByCollectionId(Application $app, $databox, $collectionId) { assert(is_int($collectionId)); + $databoxId = $databox instanceof databox ? $databox->get_sbas_id() : (int)$databox; - return self::getAvailableCollection($app, $databox->get_sbas_id(), $collectionId); + return self::getAvailableCollection($app, $databoxId, $collectionId); } /**