Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
namespace Synolia\Bundle\FavoriteBundle\Entity\Repository;

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\OrganizationBundle\Entity\Organization;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Synolia\Bundle\FavoriteBundle\Entity\Favorite;

class FavoriteRepository extends EntityRepository
{
public function findAllFilteredByAcl(AclHelper $aclHelper): array
public function findAllFilteredByAcl(AclHelper $aclHelper, CustomerUser $user, Organization $organization): array
{
return $aclHelper->apply($this->createQueryBuilder('f'))->getResult();
return $aclHelper->apply($this->getFavoritesProductsCollection($user, $organization))->getResult();
}

public function getFavoritesProductsCollection(CustomerUser $user, Organization $organization): array
public function getFavoritesProductsCollection(CustomerUser $user, Organization $organization): Query
{
return $this->createQueryBuilder('f')
->resetDQLPart('select')
Expand All @@ -28,29 +29,17 @@ public function getFavoritesProductsCollection(CustomerUser $user, Organization
'user' => $user,
'organization' => $organization,
])
->getQuery()->getArrayResult();
->getQuery();
}

public function getFavoritesProductsInSingleArray(CustomerUser $user, Organization $organization): array
{
$newArray = [];
$favorites = $this->getFavoritesProductsCollection($user, $organization);

foreach ($favorites as $favorite) {
$newArray[] = $favorite['product_id'];
}

return $newArray;
}

public function findAllProductIdsFilteredByAcl(AclHelper $aclHelper): array
public function findAllProductIdsFilteredByAcl(AclHelper $aclHelper, CustomerUser $user, Organization $organization): array
{
$ids = [];
$favorites = $this->findAllFilteredByAcl($aclHelper);
$favorites = $this->findAllFilteredByAcl($aclHelper, $user, $organization);

/** @var Favorite $favorite */
foreach ($favorites as $favorite) {
$ids[] = $favorite->getProduct()?->getId();
$ids[] = $favorite['product_id'];
}

return $ids;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\NotSupported;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\DataGridBundle\Event\BuildAfter;
use Oro\Bundle\OrganizationBundle\Entity\Organization;
use Oro\Bundle\SearchBundle\Datagrid\Datasource\SearchDatasource;
use Oro\Bundle\SecurityBundle\Authentication\TokenAccessorInterface;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Synolia\Bundle\FavoriteBundle\Entity\Favorite;
use Synolia\Bundle\FavoriteBundle\Entity\Repository\FavoriteRepository;
Expand All @@ -17,7 +20,8 @@ class FavoriteDatagridListener
{
public function __construct(
protected EntityManager $entityManager,
protected AclHelper $aclHelper
protected AclHelper $aclHelper,
protected TokenAccessorInterface $tokenAccessor,
) {
}

Expand All @@ -33,8 +37,14 @@ public function onBuildAfter(BuildAfter $event): void

/** @var FavoriteRepository $repo */
$repo = $this->entityManager->getRepository(Favorite::class);

$favProductIds = $repo->findAllProductIdsFilteredByAcl($this->aclHelper);
$user = $this->tokenAccessor->getUser();
$organization = $this->tokenAccessor->getOrganization();
$favProductIds = [];
if ($user instanceof CustomerUser and $organization instanceof Organization) {
/** @var FavoriteRepository $favoriteRepo */
$favoriteRepo = $this->entityManager->getRepository(Favorite::class);
$favProductIds = $favoriteRepo->findAllProductIdsFilteredByAcl($this->aclHelper, $user, $organization);
}

if (empty($favProductIds)) {
$favProductIds = [0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
namespace Synolia\Bundle\FavoriteBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\DataGridBundle\Datasource\ResultRecord;
use Oro\Bundle\DataGridBundle\Event\BuildBefore;
use Oro\Bundle\DataGridBundle\Extension\Formatter\Property\PropertyInterface;
use Oro\Bundle\OrganizationBundle\Entity\Organization;
use Oro\Bundle\SecurityBundle\Authentication\TokenAccessorInterface;
use Oro\Bundle\SearchBundle\Datagrid\Event\SearchResultAfter;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Synolia\Bundle\FavoriteBundle\Entity\Favorite;
Expand All @@ -17,7 +20,8 @@ class FrontendProductFavoriteDatagridListener
{
public function __construct(
private readonly EntityManager $entityManager,
private readonly AclHelper $aclHelper
private readonly AclHelper $aclHelper,
private readonly TokenAccessorInterface $tokenAccessor,
) {
}

Expand All @@ -43,9 +47,14 @@ public function onResultAfter(SearchResultAfter $event): void
return;
}

/** @var FavoriteRepository $favoriteRepo */
$favoriteRepo = $this->entityManager->getRepository(Favorite::class);
$favProductIds = $favoriteRepo->findAllProductIdsFilteredByAcl($this->aclHelper);
$user = $this->tokenAccessor->getUser();
$organization = $this->tokenAccessor->getOrganization();
$favProductIds = [];
if ($user instanceof CustomerUser and $organization instanceof Organization) {
/** @var FavoriteRepository $favoriteRepo */
$favoriteRepo = $this->entityManager->getRepository(Favorite::class);
$favProductIds = $favoriteRepo->findAllProductIdsFilteredByAcl($this->aclHelper, $user, $organization);
}
/** @var ResultRecord $record */
foreach ($records as $record) {
$productId = $record->getValue('id');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Synolia\Bundle\FavoriteBundle\EventListener;

use Doctrine\ORM\EntityManager;
use Oro\Bundle\CustomerBundle\Entity\CustomerUser;
use Oro\Bundle\OrganizationBundle\Entity\Organization;
use Oro\Bundle\ProductBundle\Event\BuildResultProductListEvent;
use Oro\Bundle\SecurityBundle\Authentication\TokenAccessorInterface;
use Oro\Bundle\SecurityBundle\ORM\Walker\AclHelper;
use Synolia\Bundle\FavoriteBundle\Entity\Favorite;
use Synolia\Bundle\FavoriteBundle\Entity\Repository\FavoriteRepository;
Expand All @@ -14,7 +17,8 @@ class ProductListAddFavoriteEventListener
{
public function __construct(
private readonly EntityManager $entityManager,
private readonly AclHelper $aclHelper
private readonly AclHelper $aclHelper,
private readonly TokenAccessorInterface $tokenAccessor,
) {
}

Expand All @@ -26,9 +30,14 @@ public function onBuildResult(BuildResultProductListEvent $event): void
return;
}

/** @var FavoriteRepository $favoriteRepo */
$favoriteRepo = $this->entityManager->getRepository(Favorite::class);
$favProductIds = $favoriteRepo->findAllProductIdsFilteredByAcl($this->aclHelper);
$user = $this->tokenAccessor->getUser();
$organization = $this->tokenAccessor->getOrganization();
$favProductIds = [];
if ($user instanceof CustomerUser and $organization instanceof Organization) {
/** @var FavoriteRepository $favoriteRepo */
$favoriteRepo = $this->entityManager->getRepository(Favorite::class);
$favProductIds = $favoriteRepo->findAllProductIdsFilteredByAcl($this->aclHelper, $user, $organization);
}

foreach (array_keys($event->getProductData()) as $productId) {
$productView = $event->getProductView($productId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ services:
arguments:
- '@doctrine.orm.entity_manager'
- '@oro_security.acl_helper'
- '@oro_security.token_accessor'
tags:
- { name: kernel.event_listener, event: oro_datagrid.datagrid.build.before.frontend-product-search-grid, method: onBuildBefore }
- { name: kernel.event_listener, event: oro_datagrid.search_datasource.result.after.frontend-product-search-grid, method: onResultAfter }
Expand All @@ -11,12 +12,14 @@ services:
arguments:
- '@doctrine.orm.entity_manager'
- '@oro_security.acl_helper'
- '@oro_security.token_accessor'
tags:
- { name: kernel.event_listener, event: oro_datagrid.datagrid.build.after.synolia-favorite-grid, method: onBuildAfter }

Synolia\Bundle\FavoriteBundle\EventListener\ProductListAddFavoriteEventListener:
arguments:
- '@doctrine.orm.entity_manager'
- '@oro_security.acl_helper'
- '@oro_security.token_accessor'
tags:
- { name: kernel.event_listener, event: oro_product.product_list.build_result, method: onBuildResult, priority: 10 }
Loading