<?php declare(strict_types=1);
/**
* gb media
* All Rights Reserved.
*
* Unauthorized copying of this file, via any medium is strictly prohibited.
* The content of this file is proprietary and confidential.
*
* @category Shopware
* @package Shopware_Plugins
* @subpackage GbmedCustomFieldsFilter
* @copyright Copyright (c) 2023, gb media
* @license proprietary
* @author Giuseppe Bottino
* @link http://www.gb-media.biz
*/
namespace Gbmed\CustomFieldsFilter\Subscriber;
use Gbmed\CustomFieldsFilter\Components\Filter\FilterAbstract;
use Gbmed\CustomFieldsFilter\Core\Content\CustomFieldsFilter\CustomFieldsFilterEntity;
use Gbmed\CustomFieldsFilter\Service\Filter as ServiceFilter;
use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\AggregationResult\Bucket\BucketResult;
use Shopware\Core\Framework\Struct\ArrayStruct;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class Filter implements EventSubscriberInterface
{
private ServiceFilter $serviceFilter;
/** @var CustomFieldsFilterEntity[] */
private array $customFieldsFilters = [];
public function __construct(ServiceFilter $serviceFilter)
{
$this->serviceFilter = $serviceFilter;
}
public static function getSubscribedEvents()
{
return [
ProductListingCriteriaEvent::class => 'onProductListingCriteriaEvent',
ProductListingCollectFilterEvent::class => 'addFilter',
ProductListingResultEvent::class => 'onProductListingResultEvent',
];
}
public function onProductListingCriteriaEvent(ProductListingCriteriaEvent $event)
{
$event->getCriteria()->addAssociation('translations');
}
public function addFilter(ProductListingCollectFilterEvent $event): void
{
$customFieldsFilters = $this->serviceFilter->getActiveFilters($event->getContext());
/**
* @var CustomFieldsFilterEntity $customFieldsFilter
*/
foreach ($customFieldsFilters as $customFieldsFilter) {
$filterType = $this->serviceFilter->getCollection()->get($customFieldsFilter->getType());
if (!$filterType) {
continue;
}
$this->customFieldsFilters[FilterAbstract::NAME_PREFIX . $customFieldsFilter->getId()] = $customFieldsFilter;
$filterType->setProductListingEvent($event)
->setCustomFieldsFilter($customFieldsFilter)
->addProductListing($event);
}
}
public function onProductListingResultEvent(ProductListingResultEvent $event)
{
$aggregations = $event->getResult()->getAggregations();
$list = [];
/*** @var BucketResult $aggregation */
foreach ($aggregations as $aggregationName => $aggregation) {
if (!strstr($aggregationName, FilterAbstract::NAME_PREFIX)
|| !isset($this->customFieldsFilters[$aggregation->getName()])
) {
continue;
}
$list[] = [
'aggregation' => $aggregation,
'customFieldsFilters' => $this->customFieldsFilters[$aggregation->getName()]
];
}
usort($list, fn($a, $b) => $a['customFieldsFilters']->getPosition() <=> $b['customFieldsFilters']->getPosition());
$event->getResult()->addExtension('gbmedCustomFieldsFilters', new ArrayStruct($list));
}
}