custom/plugins/PixelLexikonSW6/src/Twig/twigLexicon.php line 110

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Pixel\LexikonSW6\Twig;
  3. use Doctrine\DBAL\Connection;
  4. use Pixel\LexikonSW6\Component\CoreFunctions;
  5. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  6. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  7. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  8. use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
  9. use Shopware\Core\Framework\Context;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Shopware\Storefront\Controller\CartLineItemController as ShopwareCartLineItemController;
  16. use Shopware\Storefront\Page\GenericPageLoader;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\Routing\RouterInterface;
  19. use Twig\Environment;
  20. use Twig\Extension\AbstractExtension;
  21. use Twig\TwigFilter;
  22. use Twig\TwigFunction;
  23. use Symfony\Component\HttpFoundation\RequestStack;
  24. use Shopware\Core\PlatformRequest;
  25. use Shopware\Storefront\Framework\Routing\RequestTransformer;
  26. class twigLexicon extends AbstractExtension
  27. {
  28.     /**
  29.      * @var SystemConfigService
  30.      */
  31.     private $systemConfigService;
  32.     private $config;
  33.     /**
  34.      * @var RouterInterface
  35.      */
  36.     private $router;
  37.     /**
  38.      * @var RequestStack
  39.      */
  40.     private $stack;
  41.     /**
  42.      * @var ContainerInterface
  43.      */
  44.     protected $container;
  45.     private $helper;
  46.     /**
  47.      * @var CoreFunctions
  48.      */
  49.     private $coreFunctions;
  50.     private $connection;
  51.     public function __construct(
  52.         SystemConfigService $systemConfigService,
  53.         CoreFunctions $coreFunctions,
  54.         RouterInterface $router,
  55.         RequestStack $stack,
  56.         Connection $connection
  57.     ) {
  58.         $this->systemConfigService $systemConfigService;
  59.         $this->coreFunctions $coreFunctions;
  60.         $this->router $router;
  61.         $this->stack $stack;
  62.         $this->connection $connection;
  63.     }
  64.     public function getFunctions(): array
  65.     {
  66.         return [
  67.             new TwigFunction('get_lexicon_data', [$this'getLexiconData'], [ 'needs_context' => true ]),
  68.             new TwigFunction('get_lexicon_description', [$this'getLexiconDesc'], [ 'needs_context' => true ]),
  69.             new TwigFunction('get_lexicon_properties', [$this'getLexiconProp'], [ 'needs_context' => true ]),
  70.             new TwigFunction('get_lexicon_variants', [$this'getLexiconVario'], [ 'needs_context' => true ]),
  71.             new TwigFunction('get_lexicon_count', [$this'getLexiconCount'], [ 'needs_context' => true ])
  72.         ];
  73.     }
  74.     /**
  75.      * {@inheritdoc}
  76.      */
  77.     public function getFilters(): array
  78.     {
  79.         return [
  80.             new TwigFilter('lexiconfilter', [$this'lexiconFilter'], ['needs_context' => true]),
  81.         ];
  82.     }
  83.     public function getLexiconDesc(array $twigContext, ?string $data ): ?string
  84.     {
  85.         if($data != null) {
  86.             $request $this->stack->getMasterRequest();
  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.             $systemConfig $this->systemConfigService->get('PixelLexikonSW6.config'$sales_channel_id);
  91.             if ($systemConfig['LEXIKONARTICLE'] == true) {
  92.             $generatedLexiconData $this->coreFunctions->lexikonData($data$currentShopUrl$sales_channel_id$systemConfig);
  93.             return  $generatedLexiconData;
  94.             }else{
  95.                 return $data;
  96.             }
  97.         }else{
  98.             return $data;
  99.         }
  100.     }
  101.     public function getLexiconProp(array $twigContext, ?object $data ): ?object
  102.     {
  103.         if($data != null) {
  104.             $request $this->stack->getMasterRequest();
  105.             $currentShopUrl $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
  106.             $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  107.             $sales_channel_id $salesChannelContext->getSalesChannel()->getId();
  108.             $systemConfig $this->systemConfigService->get('PixelLexikonSW6.config'$sales_channel_id);
  109.             if ($systemConfig['LEXIKONARTICLEATTRSPEZ']) {
  110.                 if ($systemConfig['LEXIKONARTICLEATTRSPEZ'] != 'nothing') {
  111.                     foreach ($data->getElements() as $sortedPropertyId => $sortedProperties) {
  112.                         if ($systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'nameandvalues' or $systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'onlyproperties') {
  113.                             $sortedProperties $this->coreFunctions->lexikonProperties($sortedProperties,
  114.                                 $currentShopUrl$sales_channel_id$systemConfig);
  115.                         }
  116.                         if ($systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'nameandvalues' or $systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'onlyvalue') {
  117.                             foreach ($sortedProperties->getOptions()->getElements() as $sortedPropertiesId => $sortedPropertiesItem) {
  118.                                 $sortedProperties->getOptions()->getElements()[$sortedPropertiesId] = $this->coreFunctions->lexikonPropertiesItems($sortedProperties,
  119.                                     $sortedPropertiesItem$sortedPropertiesId$currentShopUrl$sales_channel_id,
  120.                                     $systemConfig);
  121.                             }
  122.                         }
  123.                     }
  124.                 }
  125.             }
  126.             return $data;
  127.         }else{
  128.             return $data;
  129.         }
  130.     }
  131.     public function getLexiconVario(array $twigContext, ?object $data ): ?object
  132.     {
  133.         if($data != null) {
  134.             $request $this->stack->getMasterRequest();
  135.             $currentShopUrl $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
  136.             $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  137.             $sales_channel_id $salesChannelContext->getSalesChannel()->getId();
  138.             $systemConfig $this->systemConfigService->get('PixelLexikonSW6.config'$sales_channel_id);
  139.             if ($systemConfig['LEXIKONARTICLEVARIANTS']) {
  140.                 if ($systemConfig['LEXIKONARTICLEVARIANTS'] != 'nothing') {
  141.                 }
  142.             }
  143.             return $data;
  144.         }else{
  145.             return $data;
  146.         }
  147.     }
  148.     public function getLexiconData(array $twigContext, ?string $data ): ?string
  149.     {
  150.         if($data != null) {
  151.             $request $this->stack->getMasterRequest();
  152.             $currentShopUrl $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
  153.             $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  154.             $sales_channel_id $salesChannelContext->getSalesChannel()->getId();
  155.             $systemConfig $this->systemConfigService->get('PixelLexikonSW6.config'$sales_channel_id);
  156.             $generatedLexiconData $this->coreFunctions->lexikonData($data$currentShopUrl$sales_channel_id$systemConfig);
  157.             return $generatedLexiconData;
  158.         }else{
  159.             return $data;
  160.         }
  161.     }
  162.     /*
  163.      * can be used by {{ get_lexicon_count(entry.id) }} to get count of lexikon entries
  164.      *
  165.      * */
  166.     public function getLexiconCount(array $twigContext, ?string $categoryId): ?int
  167.     {
  168.         $count 0;
  169.         if($categoryId != null) {
  170.             $count $this->connection->fetchColumn('SELECT count(id) FROM pixel_lexikon WHERE HEX(categoryid) = ?', [
  171.                 $categoryId
  172.             ]);
  173.             return (int)$count;
  174.         }else{
  175.             return $count;
  176.         }
  177.     }
  178.     public function lexiconFilter(array $twigContext, ?string $data  ): ?string
  179.     {
  180.         if($data != null) {
  181.             $request $this->stack->getMasterRequest();
  182.             $currentShopUrl $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
  183.             $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  184.             $sales_channel_id $salesChannelContext->getSalesChannel()->getId();
  185.             $systemConfig $this->systemConfigService->get('PixelLexikonSW6.config'$sales_channel_id);
  186.             $generatedLexiconData $this->coreFunctions->lexikonData($data$currentShopUrl$sales_channel_id$systemConfig);
  187.             return $generatedLexiconData;
  188.         }else{
  189.             return $data;
  190.         }
  191.     }
  192. }