custom/plugins/SasBlogModule/src/Content/Blog/Subscriber/BlogSubscriber.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Sas\BlogModule\Content\Blog\Subscriber;
  3. use Sas\BlogModule\Content\Blog\BlogEntriesDefinition;
  4. use Sas\BlogModule\Content\Blog\BlogListingFilterBuildEvent;
  5. use Sas\BlogModule\Content\Blog\Events\BlogMainFilterEvent;
  6. use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
  7. use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
  8. use Shopware\Core\Content\Product\SalesChannel\Listing\FilterCollection;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\EntityAggregation;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. class BlogSubscriber implements EventSubscriberInterface
  17. {
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             BlogListingFilterBuildEvent::BLOG_MAIIN_FILTER_EVENT => 'loaded',
  22.         ];
  23.     }
  24.     public function loaded(BlogMainFilterEvent $event): void
  25.     {
  26.         $dateTime = new \DateTime();
  27.         $criteria $event->getCriteria();
  28.         $criteria->addFilter(
  29.             new EqualsFilter('active'true),
  30.             new RangeFilter('publishedAt', [RangeFilter::LTE => $dateTime->format(\DATE_ATOM)])
  31.         );
  32.         $criteriaCollection = new CriteriaCollection();
  33.         $criteriaCollection->add(
  34.             'sas_blog',
  35.             BlogEntriesDefinition::class,
  36.             $criteria
  37.         );
  38.         $request $event->getRequest();
  39.         $filters $this->getFilters($request);
  40.         $aggregations $this->getAggregations($request$filters);
  41.         foreach ($aggregations as $aggregation) {
  42.             $criteria->addAggregation($aggregation);
  43.         }
  44.         foreach ($filters as $filter) {
  45.             if ($filter->isFiltered()) {
  46.                 $criteria->addPostFilter($filter->getFilter());
  47.             }
  48.         }
  49.         $criteria->addExtension('filters'$filters);
  50.     }
  51.     private function getAggregations(Request $requestFilterCollection $filters): array
  52.     {
  53.         $aggregations = [];
  54.         if ($request->get('reduce-aggregations'null) === null) {
  55.             foreach ($filters as $filter) {
  56.                 $aggregations array_merge($aggregations$filter->getAggregations());
  57.             }
  58.             return $aggregations;
  59.         }
  60.         foreach ($filters as $filter) {
  61.             $excluded $filters->filtered();
  62.             if ($filter->exclude()) {
  63.                 $excluded $excluded->blacklist($filter->getName());
  64.             }
  65.             foreach ($filter->getAggregations() as $aggregation) {
  66.                 if ($aggregation instanceof FilterAggregation) {
  67.                     $aggregation->addFilters($excluded->getFilters());
  68.                     $aggregations[] = $aggregation;
  69.                     continue;
  70.                 }
  71.                 $aggregation = new FilterAggregation(
  72.                     $aggregation->getName() . '-filtered',
  73.                     $aggregation,
  74.                     $excluded->getFilters()
  75.                 );
  76.                 $aggregations[] = $aggregation;
  77.             }
  78.         }
  79.         return $aggregations;
  80.     }
  81.     private function getFilters(Request $request): FilterCollection
  82.     {
  83.         $filters = new FilterCollection();
  84.         $filters->add($this->getCategoriesFilter($request));
  85.         $filters->add($this->getAuthorsFilter($request));
  86.         return $filters;
  87.     }
  88.     private function getCategoriesFilter(Request $request): Filter
  89.     {
  90.         $ids $this->getFilterByCustomIds('categories'$request);
  91.         return new Filter(
  92.             'categories',
  93.             !empty($ids),
  94.             [
  95.                 new EntityAggregation('blogCategories''blogCategories.id''sas_blog_category'),
  96.             ],
  97.             new EqualsAnyFilter('blogCategories.id'$ids),
  98.             $ids
  99.         );
  100.     }
  101.     private function getAuthorsFilter(Request $request): Filter
  102.     {
  103.         $ids $this->getFilterByCustomIds('authors'$request);
  104.         return new Filter(
  105.             'authors',
  106.             !empty($ids),
  107.             [
  108.                 new EntityAggregation('authors''authorId''sas_blog_author'),
  109.             ],
  110.             new EqualsAnyFilter('authorId'$ids),
  111.             $ids
  112.         );
  113.     }
  114.     private function getFilterByCustomIds(string $inputRequest $request): array
  115.     {
  116.         $ids $request->query->get($input'');
  117.         if ($request->isMethod(Request::METHOD_POST)) {
  118.             $ids $request->request->get($input'');
  119.         }
  120.         $ids explode('|'$ids);
  121.         return array_filter($ids);
  122.     }
  123. }