Skip to content
This repository has been archived by the owner on May 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #130 from atrocore/i-asset
Browse files Browse the repository at this point in the history
Fixed Bool filters on asset
  • Loading branch information
rratsun authored Jan 11, 2024
2 parents 4341a8f + cc7c208 commit 363d12c
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions app/SelectManagers/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,23 @@

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;
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;
Expand All @@ -31,18 +41,34 @@ 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=: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
{
$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=: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=:true AND deleted=:false)");
$qb->setParameter('true', true, ParameterType::BOOLEAN);
$qb->setParameter('false', false, ParameterType::BOOLEAN);
}
}

0 comments on commit 363d12c

Please sign in to comment.