custom/plugins/PixelLexikonSW6/src/Subscriber/Frontend.php line 100

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Pixel\LexikonSW6\Subscriber;
  3. use _HumbugBoxfac515c46e83\Nette\Iterators\Mapper;
  4. use Shopware\Core\Framework\Struct\ArrayEntity;
  5. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use Pixel\LexikonSW6\Component\CoreFunctions;
  9. use Shopware\Core\System\SystemConfig\SystemConfigService;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Shopware\Core\PlatformRequest;
  12. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  13. use Composer\IO\NullIO;
  14. use Doctrine\DBAL\Connection;
  15. use Doctrine\DBAL\FetchMode;
  16. use Doctrine\DBAL\ParameterType;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  21. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\Filter;
  24. use Shopware\Core\Content\Cms\CmsPageEvents;
  25. use Shopware\Core\Framework\Struct\Struct;
  26. use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
  27. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  28. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  29. use Shopware\Core\Framework\DataAbstractionLayer\Search\Grouping\FieldGrouping;
  30. use Psr\Log\LoggerInterface;
  31. use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
  32. use Shopware\Core\Content\Product\ProductEvents;
  33. use Shopware\Core\Content\Seo\SeoUrlUpdater;
  34. class Frontend implements EventSubscriberInterface
  35. {
  36.     private $systemConfigService;
  37.     private $config;
  38.     private $pluginConfig;
  39.     private $coreFunctions;
  40.     private $requestStack;
  41.     private $seoUrlRewriter;
  42.     /**
  43.      * @var Connection
  44.      */
  45.     private $connection;
  46.     /**
  47.      * @var LoggerInterface
  48.      */
  49.     private $logger;
  50.     public function __construct(
  51.         SeoUrlPlaceholderHandlerInterface $seoUrlRewriter,
  52.         SystemConfigService $systemConfigService,
  53.         CoreFunctions $coreFunctions,
  54.         RequestStack $requestStack
  55.     ) {
  56.         $this->systemConfigService $systemConfigService;
  57.         $this->coreFunctions $coreFunctions;
  58.         $this->requestStack $requestStack;
  59.         $this->seoUrlRewriter $seoUrlRewriter;
  60.     }
  61.     public static function getSubscribedEvents(): array
  62.     {
  63.         return [
  64.             //CmsPageEvents::SLOT_LOADED_EVENT => 'onCmsLoadedEvent',
  65.             ProductPageLoadedEvent::class => 'onArticleDetailLoaded',
  66.         ];
  67.     }
  68.     public function onCmsLoadedEvent(EntityLoadedEvent $event): void
  69.     {
  70.         $source $event->getContext()->getSource();
  71.         if (!$source instanceof SalesChannelApiSource) {
  72.             return;
  73.         }
  74.         $entities $event->getEntities();
  75.     }
  76.     public function onArticleDetailLoaded(ProductPageLoadedEvent $event)
  77.     {
  78.         $systemConfig $this->systemConfigService->get('PixelLexikonSW6.config',
  79.             $event->getSalesChannelContext()->getSalesChannel()->getId());
  80.         $activateOnArticle $systemConfig['LEXIKONARTICLE'] ? $systemConfig['LEXIKONARTICLE'] : false;
  81.         $activateOnVariants $systemConfig['LEXIKONARTICLEVARIANTS'] ? $systemConfig['LEXIKONARTICLEVARIANTS'] : 'nothing';
  82.         $activateOnAttribs $systemConfig['LEXIKONARTICLEATTRSPEZ'] ? $systemConfig['LEXIKONARTICLEATTRSPEZ'] : 'nothing';
  83.         $activateOnSpecField $systemConfig['LEXIKONARTICLECSTM'] ? $systemConfig['LEXIKONARTICLECSTM'] : false;
  84.         if ($systemConfig['active'] == true) {
  85.             if ($activateOnArticle == true) {
  86.                 $request $this->requestStack->getCurrentRequest();
  87.                 $currentShopUrl $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
  88.                 $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  89.                 $sales_channel_id $salesChannelContext->getSalesChannel()->getId();
  90.                 $page $event->getPage();
  91.                 $product $page->getProduct();
  92.                 $cms $page->getCmsPage();
  93.                 if ($activateOnVariants) {
  94.                     if ($activateOnVariants != 'nothing') {
  95.                         /*foreach ($product->getConfiguratorSettings()->getElements() as $option) {
  96.                             if ($systemConfig['LEXIKONARTICLEVARIANTS'] == 'groupandoptions' or $systemConfig['LEXIKONARTICLEVARIANTS'] == 'onlygroups') {
  97.                                 $option = $this->coreFunctions->lexikonConfiguratorElements($option, $currentShopUrl,
  98.                                     $sales_channel_id, $systemConfig);
  99.                             }
  100.                         }*/
  101.                     }
  102.                 }
  103.                 /*
  104.                  * Replace the article description
  105.                  *
  106.                  **/
  107.                 if ($activateOnArticle == true) {
  108.                     $getProductDataTranslated $product->getTranslated();
  109.                     $getProductDataTranslated['description'] = $this->coreFunctions->lexikonRewriter($getProductDataTranslated['description'],
  110.                         $currentShopUrl$sales_channel_id$systemConfig);
  111.                     $product->assign(['description' => $getProductDataTranslated['description']]);
  112.                     $product->assign(['translated' => $getProductDataTranslated]);
  113.                 }
  114.                 /*
  115.                  * Replace the properties
  116.                  *
  117.                  **/
  118.                 if ($activateOnAttribs) {
  119.                     if ($activateOnAttribs != 'nothing') {
  120.                         foreach ($product->get('sortedProperties')->getElements() as $sortedPropertyId => $sortedProperties) {
  121.                             if ($activateOnAttribs == 'nameandvalues' or $activateOnAttribs == 'onlyproperties') {
  122.                                 $sortedProperties $this->coreFunctions->lexikonProperties($sortedProperties,
  123.                                     $currentShopUrl$sales_channel_id$systemConfig);
  124.                             }
  125.                             if ($activateOnAttribs == 'nameandvalues' or $activateOnAttribs == 'onlyvalue') {
  126.                                 foreach ($sortedProperties->getOptions()->getElements() as $sortedPropertiesId => $sortedPropertiesItem) {
  127.                                     $sortedProperties->getOptions()->getElements()[$sortedPropertiesId] = $this->coreFunctions->lexikonPropertiesItems($sortedProperties,
  128.                                         $sortedPropertiesItem$sortedPropertiesId$currentShopUrl$sales_channel_id,
  129.                                         $systemConfig);
  130.                                 }
  131.                             }
  132.                         }
  133.                     }
  134.                 }
  135.                 /*
  136.                  * Replace the specified custom field (plugin configuration)
  137.                  * only customfields > text
  138.                  *
  139.                  **/
  140.                 if ($activateOnSpecField == true) {
  141.                     $customFields $product->getCustomFields();
  142.                     if ($systemConfig['CUSTOMARTFIELD']) {
  143.                         $specCustomField explode(','$systemConfig['CUSTOMARTFIELD']);
  144.                         if (is_array($specCustomField) and count($specCustomField) > 0) {
  145.                             foreach ($specCustomField as $cstmkey => $cstmvalues) {
  146.                                 if (array_key_exists($cstmvalues$customFields)) {
  147.                                     $getProductDataTranslated $product->getTranslated();
  148.                                     $getProductDataTranslated['customFields'][$cstmvalues] = $this->coreFunctions->lexikonCustomFields($customFields[$cstmvalues],
  149.                                         $currentShopUrl$sales_channel_id$systemConfig);
  150.                                     $product->assign(['translated' => $getProductDataTranslated]);
  151.                                 }
  152.                             }
  153.                         }
  154.                     }
  155.                 }
  156.             }
  157.         }
  158.     }
  159. }