custom/plugins/NetzpBlog6/src/Subscriber/FrontendSubscriber.php line 68

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NetzpBlog6\Subscriber;
  3. use NetzpBlog6\Core\SearchResult;
  4. use NetzpBlog6\Helper\BlogHelper;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\Struct\ArrayStruct;
  10. use Shopware\Core\Framework\Struct\StructCollection;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  14. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\DependencyInjection\ContainerInterface;
  17. use Symfony\Contracts\EventDispatcher\Event;
  18. class FrontendSubscriber implements EventSubscriberInterface
  19. {
  20.     const SEARCH_TYPE_BLOG 10;
  21.     private $container;
  22.     private $config;
  23.     private $helper;
  24.     public function __construct(ContainerInterface $container,
  25.                                 SystemConfigService $config,
  26.                                 BlogHelper $helper) {
  27.         $this->container $container;
  28.         $this->config $config;
  29.         $this->helper $helper;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductPageCriteriaEvent::class    => 'onProductCriteriaLoaded',
  35.             ProductPageLoadedEvent::class      => 'loadProductPage',
  36.             'netzp.search.register'            => 'registerSearchProvider'
  37.         ];
  38.     }
  39.     public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
  40.     {
  41.         $this->addBlogCriteria($event->getCriteria(), $event->getSalesChannelContext()->getSalesChannelId());
  42.     }
  43.     private function addBlogCriteria(Criteria $criteriastring $salesChannelId)
  44.     {
  45.         $criteria->addAssociation('blogs');
  46.         $blogAssociation $criteria->getAssociation('blogs');
  47.         $blogAssociation->addAssociation('tags');
  48.         $blogAssociation->addAssociation('items');
  49.         $blogAssociation->addAssociation('blogmedia');
  50.         $this->helper->addBlogDateFilterAndSorting($blogAssociationtrue);
  51.         $blogAssociation->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  52.             new EqualsFilter('category.saleschannelid'null),
  53.             new EqualsFilter('category.saleschannelid''00000000000000000000000000000000'),
  54.             new EqualsFilter('category.saleschannelid'$salesChannelId),
  55.         ]));
  56.     }
  57.     public function loadProductPage(ProductPageLoadedEvent $event): void
  58.     {
  59.         $product $event->getPage()->getProduct();
  60.         $productBlogs $product->getExtension('blogs');
  61.         $parentId $product->getParentId();
  62.         if($parentId) {
  63.             $repo $this->container->get('product.repository');
  64.             $criteria = new Criteria([$parentId]);
  65.             $this->addBlogCriteria($criteria$event->getSalesChannelContext()->getSalesChannelId());
  66.             $parentProduct $repo->search($criteria$event->getContext())->getEntities()->first();
  67.             $parentBlogs $parentProduct->getExtension('blogs');
  68.             if($parentBlogs->count() > 0) {
  69.                 $product->addExtension('blogs',
  70.                     new StructCollection(array_merge($parentBlogs->getElements(), $productBlogs->getElements()))
  71.                 );
  72.             }
  73.         }
  74.         $event->getPage()->assign([
  75.             'netzp_blogposts' => []
  76.         ]);
  77.     }
  78.     public function registerSearchProvider(Event $event)
  79.     {
  80.         $pluginConfig $this->config->get('NetzpBlog6.config');
  81.         if((bool)$pluginConfig['searchBlog'] === false) {
  82.             return;
  83.         }
  84.         $event->setData([
  85.             'key'      => 'blog',
  86.             'label'    => 'netzp.blog.searchLabel',
  87.             'function' => [$this'doSearch']
  88.         ]);
  89.     }
  90.     public function doSearch(string $querySalesChannelContext $salesChannelContextbool $isSuggest false): array
  91.     {
  92.         $results = [];
  93.         $blogEntries $this->getBlogEntries($query$salesChannelContext$isSuggest);
  94.         foreach ($blogEntries->getEntities() as $blogEntry) {
  95.             $tmpResult = new SearchResult();
  96.             $tmpResult->setType(static::SEARCH_TYPE_BLOG);
  97.             $tmpResult->setId($blogEntry->getId());
  98.             $tmpResult->setTitle($blogEntry->getTranslated()['title'] ?? '');
  99.             $tmpResult->setDescription($blogEntry->getTranslated()['teaser'] ?? '');
  100.             if($blogEntry->getImagepreview()) {
  101.                 $tmpResult->setMedia($blogEntry->getImagepreview());
  102.             }
  103.             else if($blogEntry->getImage()) {
  104.                 $tmpResult->setMedia($blogEntry->getImage());
  105.             }
  106.             $tmpResult->setTotal($blogEntries->getTotal());
  107.             $tmpResult->addExtension('slug', new ArrayStruct(['value' => $blogEntry->getTranslated()['slug'] ?? '']));
  108.             $results[] = $tmpResult;
  109.         }
  110.         return $results;
  111.     }
  112.     private function getBlogEntries($querySalesChannelContext $salesChannelContextbool $isSuggest false)
  113.     {
  114.         $query trim($query);
  115.         $words explode(' '$query);
  116.         if (count($words) > 0) {
  117.             $repo $this->container->get('s_plugin_netzp_blog.repository');
  118.             $criteria = new Criteria();
  119.             $criteria->addAssociation('category');
  120.             $criteria->addAssociation('image');
  121.             $criteria->addAssociation('imagepreview');
  122.             $this->helper->addBlogDateFilterAndSorting($criteriatrue);
  123.             $this->helper->addRestrictionsFilter($criteria$salesChannelContext);
  124.             if($isSuggest) {
  125.                 $criteria->setLimit(10);
  126.                 $criteria->setTotalCountMode(Criteria::TOTAL_COUNT_MODE_EXACT);
  127.             }
  128.             $filter = [];
  129.             foreach ($words as $word) {
  130.                 $filter[] = new ContainsFilter('title'$word);
  131.                 $filter[] = new ContainsFilter('teaser'$word);
  132.                 $filter[] = new ContainsFilter('contents'$word);
  133.             }
  134.             $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR$filter));
  135.             return $repo->search($criteria$salesChannelContext->getContext());
  136.         }
  137.         return null;
  138.     }
  139. }