From 32673f53ce2b363abd16ad35347939d0f70f6764 Mon Sep 17 00:00:00 2001 From: Raoul Dzoukou Date: Wed, 10 Jan 2024 14:09:48 +0100 Subject: [PATCH 1/2] . --- app/SelectManagers/Asset.php | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/app/SelectManagers/Asset.php b/app/SelectManagers/Asset.php index 9da25f8..2832258 100644 --- a/app/SelectManagers/Asset.php +++ b/app/SelectManagers/Asset.php @@ -13,13 +13,22 @@ namespace Dam\SelectManagers; +use Atro\ORM\DB\RDB\Mapper; +use Doctrine\DBAL\Query\QueryBuilder; use Espo\Core\Exceptions\BadRequest; use Espo\Core\SelectManagers\Base; +use Espo\ORM\IEntity; class Asset extends Base { protected function boolFilterLinkedWithAssetCategory(array &$result): void { + $result['callbacks'][] = [$this, 'filterLinkedWithAssetCategory']; + } + + public function filterLinkedWithAssetCategory(QueryBuilder $qb, IEntity $relEntity, array $params, Mapper $mapper): void + { + $tableAlias = $mapper->getQueryConverter()->getMainTableAlias(); $assetCategoryId = (string)$this->getBoolFilterParameter('linkedWithAssetCategory'); if (empty($assetCategoryId)) { return; @@ -31,18 +40,30 @@ protected function boolFilterLinkedWithAssetCategory(array &$result): void } $ids = $this->getEntityManager()->getRepository('AssetCategory')->getChildrenRecursivelyArray($assetCategoryId); - $ids = implode("','", array_merge($ids, [$assetCategoryId])); - - $result['customWhere'] .= " AND asset.id IN (SELECT asset_id FROM `asset_category_asset` WHERE asset_id IS NOT NULL AND deleted=0 AND asset_category_id IN ('$ids'))"; + $ids = array_merge($ids, [$assetCategoryId]); + $qb->andWhere("EXISTS (SELECT asset_id FROM `asset_category_asset` WHERE asset_id=$tableAlias.id AND deleted=0 AND asset_category_id IN (:categoryIds))"); + $qb->setParameter('categoryIds', $ids, Mapper::getParameterType($ids)); } protected function boolFilterOnlyPrivate(array &$result): void { - $result['customWhere'] .= " AND EXISTS (SELECT e_attachment.id FROM `attachment` e_attachment WHERE e_attachment.id=asset.file_id AND e_attachment.private=1 AND deleted=0)"; + $result['callbacks'][] = [$this, 'filterOnlyPrivate']; } protected function boolFilterOnlyPublic(array &$result): void { - $result['customWhere'] .= " AND EXISTS (SELECT a2.id FROM `attachment` a2 WHERE a2.id=asset.file_id AND a2.private=0 AND deleted=0)"; + $result['callbacks'][] = [$this, 'filterOnlyPublic']; + } + + public function filterOnlyPublic(QueryBuilder $qb, IEntity $relEntity, array $params, Mapper $mapper): void + { + $tableAlias = $mapper->getQueryConverter()->getMainTableAlias(); + $qb->andWhere("EXISTS (SELECT e_attachment.id FROM `attachment` e_attachment WHERE e_attachment.id=$tableAlias.file_id AND e_attachment.private=0 AND deleted=0)"); + } + + public function filterOnlyPrivate(QueryBuilder $qb, IEntity $relEntity, array $params, Mapper $mapper): void + { + $tableAlias = $mapper->getQueryConverter()->getMainTableAlias(); + $qb->andWhere("EXISTS (SELECT e_attachment.id FROM `attachment` e_attachment WHERE e_attachment.id=$tableAlias.file_id AND e_attachment.private=1 AND deleted=0)"); } } From cc7c208942d0186920a8895d181e4468d6ca1aa9 Mon Sep 17 00:00:00 2001 From: Raoul Dzoukou Date: Wed, 10 Jan 2024 17:41:32 +0100 Subject: [PATCH 2/2] . --- app/SelectManagers/Asset.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/SelectManagers/Asset.php b/app/SelectManagers/Asset.php index 2832258..118c595 100644 --- a/app/SelectManagers/Asset.php +++ b/app/SelectManagers/Asset.php @@ -14,6 +14,7 @@ namespace Dam\SelectManagers; use Atro\ORM\DB\RDB\Mapper; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Query\QueryBuilder; use Espo\Core\Exceptions\BadRequest; use Espo\Core\SelectManagers\Base; @@ -41,8 +42,9 @@ public function filterLinkedWithAssetCategory(QueryBuilder $qb, IEntity $relEnti $ids = $this->getEntityManager()->getRepository('AssetCategory')->getChildrenRecursivelyArray($assetCategoryId); $ids = array_merge($ids, [$assetCategoryId]); - $qb->andWhere("EXISTS (SELECT asset_id FROM `asset_category_asset` WHERE asset_id=$tableAlias.id AND deleted=0 AND asset_category_id IN (:categoryIds))"); + $qb->andWhere("EXISTS (SELECT asset_id FROM asset_category_asset WHERE asset_id=$tableAlias.id AND deleted=:false AND asset_category_id IN (:categoryIds))"); $qb->setParameter('categoryIds', $ids, Mapper::getParameterType($ids)); + $qb->setParameter('false', false, ParameterType::BOOLEAN); } protected function boolFilterOnlyPrivate(array &$result): void @@ -58,12 +60,15 @@ protected function boolFilterOnlyPublic(array &$result): void public function filterOnlyPublic(QueryBuilder $qb, IEntity $relEntity, array $params, Mapper $mapper): void { $tableAlias = $mapper->getQueryConverter()->getMainTableAlias(); - $qb->andWhere("EXISTS (SELECT e_attachment.id FROM `attachment` e_attachment WHERE e_attachment.id=$tableAlias.file_id AND e_attachment.private=0 AND deleted=0)"); + $qb->andWhere("EXISTS (SELECT e_attachment.id FROM attachment e_attachment WHERE e_attachment.id=$tableAlias.file_id AND e_attachment.private=:false AND deleted=:false)"); + $qb->setParameter('false', false, ParameterType::BOOLEAN); } public function filterOnlyPrivate(QueryBuilder $qb, IEntity $relEntity, array $params, Mapper $mapper): void { $tableAlias = $mapper->getQueryConverter()->getMainTableAlias(); - $qb->andWhere("EXISTS (SELECT e_attachment.id FROM `attachment` e_attachment WHERE e_attachment.id=$tableAlias.file_id AND e_attachment.private=1 AND deleted=0)"); + $qb->andWhere("EXISTS (SELECT e_attachment.id FROM attachment e_attachment WHERE e_attachment.id=$tableAlias.file_id AND e_attachment.private=:true AND deleted=:false)"); + $qb->setParameter('true', true, ParameterType::BOOLEAN); + $qb->setParameter('false', false, ParameterType::BOOLEAN); } }