custom/plugins/NimbitsArticleQuestionsNext/src/Subscriber/Subscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nimbits\NimbitsArticleQuestionsNext\Subscriber;
  3. use Nimbits\NimbitsArticleQuestionsNext\Setting\Service\SettingService;
  4. use Shopware\Core\Content\Mail\Service\AbstractMailService;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Struct\StructCollection;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. class Subscriber implements EventSubscriberInterface
  13. {
  14.     /** @var EntityRepositoryInterface $articleQuestionsRepository */
  15.     protected $articleQuestionsRepository;
  16.     private $settingsService;
  17.     private $mailService;
  18.     private $salesChannelRepository;
  19.     private $twig;
  20.     public function __construct(
  21.         SettingService            $settingsService,
  22.         EntityRepositoryInterface $articleQuestionsRepository,
  23.         AbstractMailService       $mailService,
  24.         EntityRepositoryInterface $salesChannelRepository,
  25.         \Twig\Environment         $twig,
  26.         EntityRepositoryInterface $productRepository
  27.     )
  28.     {
  29.         $this->settingsService $settingsService;
  30.         $this->articleQuestionsRepository $articleQuestionsRepository;
  31.         $this->mailService $mailService;
  32.         $this->salesChannelRepository $salesChannelRepository;
  33.         $this->twig $twig;
  34.         $this->productRepository $productRepository;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
  39.         return [
  40.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  41.             FooterPageletLoadedEvent::class => 'onFooterLoaded'
  42.         ];
  43.     }
  44.     public function onFooterLoaded(FooterPageletLoadedEvent $event)
  45.     {
  46.         $event->getPagelet()->addExtension('nimbitsArticleQuestionsSettings',
  47.             $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId())
  48.         );
  49.     }
  50.     public function onProductPageLoaded(ProductPageLoadedEvent $event)
  51.     {
  52.         $this->checkSalesChannel($event);
  53.         $productId $event->getPage()->getProduct()->getId();
  54.         $event->getPage()->getProduct()->addExtension('nimbitsArticleQuestionsSettings'$this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId()));
  55.         $criteria = (new Criteria)
  56.             ->addFilter(new EqualsFilter('article_id'$productId))
  57.             ->addFilter(new EqualsFilter('active'1));
  58.         if ($this->settingsService->getSettingsAsArray()['onlyShowCurrentLanguage']) {
  59.             $criteria->addFilter(new EqualsFilter('language_id'$event->getContext()->getLanguageId()));
  60.         }
  61.         $questions $this->articleQuestionsRepository->search(
  62.             $criteria,
  63.             \Shopware\Core\Framework\Context::createDefaultContext()
  64.         );
  65.         $questions $questions->getElements();
  66.         $event->getPage()->getProduct()->addExtension('nimbitsArticleQuestions', (new StructCollection())->assign(['questions' => $questions]));
  67.     }
  68.     public function checkSalesChannel(ProductPageLoadedEvent $event)
  69.     {
  70.         $productId $event->getPage()->getProduct()->getId();
  71.         $criteria = (new Criteria)
  72.             ->addFilter(new EqualsFilter('article_id'$productId))
  73.             ->addFilter(new EqualsFilter('active'1));
  74.         $allowedStorefronts = [];
  75.         $allowedStorefronts $this->settingsService->getSettingsAsArray()['defaultQuestionVisibilities'];
  76.         $currentStorefront $event->getContext()->getSource()->getSalesChannelId();
  77.         $showInSalesChannel false;
  78.         foreach ($allowedStorefronts as $allowedStorefront) {
  79.             if ($currentStorefront == $allowedStorefront) {
  80.                 $showInSalesChannel true;
  81.             }
  82.             $event->getPage()->addExtension('nimbitsArticleQuestions', (new StructCollection())->assign(['showInSalesChannel' => $showInSalesChannel]));
  83.         }
  84.     }
  85. }