custom/plugins/AcrisCategoryCustomerGroupCS/src/Storefront/Subscriber/CategorySubscriber.php line 79

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CategoryCustomerGroup\Storefront\Subscriber;
  3. use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
  4. use Shopware\Core\Content\Category\Event\NavigationLoadedEvent;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  13. class CategorySubscriber implements EventSubscriberInterface
  14. {
  15.     public const ACRIS_SEARCH_CATEGORY_SEARCH_CRITERIA_EVENT 'Acris\Search\Components\ContentSearch\Events\CategorySearchCriteriaEvent';
  16.     /**
  17.      * @var BlockCategoryService
  18.      */
  19.     private $blockCategoryService;
  20.     /**
  21.      * @var SystemConfigService
  22.      */
  23.     private $configService;
  24.     public function __construct(BlockCategoryService $blockCategoryServiceSystemConfigService $configService)
  25.     {
  26.         $this->blockCategoryService $blockCategoryService;
  27.         $this->configService $configService;
  28.     }
  29.     public static function getSubscribedEvents(): array
  30.     {
  31.         return[
  32.             NavigationPageLoadedEvent::class => 'onNavigationPageLoaded',
  33.             NavigationLoadedEvent::class => 'onNavigationLoaded',
  34.             self::ACRIS_SEARCH_CATEGORY_SEARCH_CRITERIA_EVENT => 'onCategorySearchCriteria'
  35.         ];
  36.     }
  37.     public function onNavigationPageLoaded(NavigationPageLoadedEvent $event): void
  38.     {
  39.         $navigationId $event->getRequest()->get('navigationId'$event->getSalesChannelContext()->getSalesChannel()->getNavigationCategoryId());
  40.         if(empty($navigationId)) {
  41.             return;
  42.         }
  43.         $blockedCategoryIds $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($event->getSalesChannelContext()->getCurrentCustomerGroup()->getId(), $event->getContext());
  44.         if($this->configService->get('AcrisCategoryCustomerGroupCS.config.blockCategoriesIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === BlockCategoryService::DEFAULT_PLUGIN_CONFIG_BLOCK_CATEGORY_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->blockCategoryService->checkIfNoCustomerGroupsAssigned($navigationId$event->getSalesChannelContext()->getContext())) throw new NotFoundHttpException();
  45.         if(in_array($navigationId$blockedCategoryIds)) {
  46.             throw new NotFoundHttpException();
  47.         }
  48.     }
  49.     public function onNavigationLoaded(NavigationLoadedEvent $event): void
  50.     {
  51.         $blockedCategoryIds $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($event->getSalesChannelContext()->getCurrentCustomerGroup()->getId(), $event->getContext());
  52.         $navigation $event->getNavigation();
  53.         $convertedTreeItems = [];
  54.         foreach ($navigation->getTree() as $key => $treeItem) {
  55.             if($this->blockCategoryService->blockCategory($treeItem$blockedCategoryIds) === false) {
  56.                 continue;
  57.             }
  58.             if($this->configService->get('AcrisCategoryCustomerGroupCS.config.blockCategoriesIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === BlockCategoryService::DEFAULT_PLUGIN_CONFIG_BLOCK_CATEGORY_IF_NO_CUSTOMER_GROUPS_ASSIGNED && $this->blockCategoryService->checkIfNoCustomerGroupsAssigned($treeItem->getCategory()->getId(), $event->getSalesChannelContext()->getContext())) continue;
  59.             $convertedTreeItems[$key] = $treeItem;
  60.         }
  61.         $navigation->setTree($convertedTreeItems);
  62.     }
  63.     public function onCategorySearchCriteria($event): void
  64.     {
  65.         $blockedCategoryIds $this->blockCategoryService->getBlockedCategoryIdsForCustomerGroupId($event->getSalesChannelContext()->getCurrentCustomerGroup()->getId(), $event->getContext());
  66.         $filters = [];
  67.         if($this->configService->get('AcrisCategoryCustomerGroupCS.config.blockCategoriesIfNoCustomerGroupAssigned'$event->getSalesChannelContext()->getSalesChannel()->getId()) === BlockCategoryService::DEFAULT_PLUGIN_CONFIG_BLOCK_CATEGORY_IF_NO_CUSTOMER_GROUPS_ASSIGNED) {
  68.             $event->getCriteria()->addAssociation('customerGroup');
  69.             $filters[] = new NotFilter(NotFilter::CONNECTION_AND, [
  70.                 new EqualsFilter('customerGroup.id'null)
  71.             ]);
  72.         }
  73.         if (!empty($blockedCategoryIds)) {
  74.             $filters[] = new NotFilter(NotFilter::CONNECTION_AND, [new EqualsAnyFilter('id'$blockedCategoryIds)]);
  75.         }
  76.         if (!empty($filters)) {
  77.             $event->getCriteria()->addFilter(new MultiFilter(MultiFilter::CONNECTION_AND$filters));
  78.         }
  79.     }
  80. }