<?php declare(strict_types=1);
namespace Pixel\LexikonSW6\Twig;
use Doctrine\DBAL\Connection;
use Pixel\LexikonSW6\Component\CoreFunctions;
use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
use Shopware\Core\Content\Product\SalesChannel\Price\AbstractProductPriceCalculator;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Controller\CartLineItemController as ShopwareCartLineItemController;
use Shopware\Storefront\Page\GenericPageLoader;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\PlatformRequest;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
class twigLexicon extends AbstractExtension
{
/**
* @var SystemConfigService
*/
private $systemConfigService;
private $config;
/**
* @var RouterInterface
*/
private $router;
/**
* @var RequestStack
*/
private $stack;
/**
* @var ContainerInterface
*/
protected $container;
private $helper;
/**
* @var CoreFunctions
*/
private $coreFunctions;
private $connection;
public function __construct(
SystemConfigService $systemConfigService,
CoreFunctions $coreFunctions,
RouterInterface $router,
RequestStack $stack,
Connection $connection
) {
$this->systemConfigService = $systemConfigService;
$this->coreFunctions = $coreFunctions;
$this->router = $router;
$this->stack = $stack;
$this->connection = $connection;
}
public function getFunctions(): array
{
return [
new TwigFunction('get_lexicon_data', [$this, 'getLexiconData'], [ 'needs_context' => true ]),
new TwigFunction('get_lexicon_description', [$this, 'getLexiconDesc'], [ 'needs_context' => true ]),
new TwigFunction('get_lexicon_properties', [$this, 'getLexiconProp'], [ 'needs_context' => true ]),
new TwigFunction('get_lexicon_variants', [$this, 'getLexiconVario'], [ 'needs_context' => true ]),
new TwigFunction('get_lexicon_count', [$this, 'getLexiconCount'], [ 'needs_context' => true ])
];
}
/**
* {@inheritdoc}
*/
public function getFilters(): array
{
return [
new TwigFilter('lexiconfilter', [$this, 'lexiconFilter'], ['needs_context' => true]),
];
}
public function getLexiconDesc(array $twigContext, ?string $data ): ?string
{
if($data != null) {
$request = $this->stack->getMasterRequest();
$currentShopUrl = $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $sales_channel_id);
if ($systemConfig['LEXIKONARTICLE'] == true) {
$generatedLexiconData = $this->coreFunctions->lexikonData($data, $currentShopUrl, $sales_channel_id, $systemConfig);
return $generatedLexiconData;
}else{
return $data;
}
}else{
return $data;
}
}
public function getLexiconProp(array $twigContext, ?object $data ): ?object
{
if($data != null) {
$request = $this->stack->getMasterRequest();
$currentShopUrl = $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $sales_channel_id);
if ($systemConfig['LEXIKONARTICLEATTRSPEZ']) {
if ($systemConfig['LEXIKONARTICLEATTRSPEZ'] != 'nothing') {
foreach ($data->getElements() as $sortedPropertyId => $sortedProperties) {
if ($systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'nameandvalues' or $systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'onlyproperties') {
$sortedProperties = $this->coreFunctions->lexikonProperties($sortedProperties,
$currentShopUrl, $sales_channel_id, $systemConfig);
}
if ($systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'nameandvalues' or $systemConfig['LEXIKONARTICLEATTRSPEZ'] == 'onlyvalue') {
foreach ($sortedProperties->getOptions()->getElements() as $sortedPropertiesId => $sortedPropertiesItem) {
$sortedProperties->getOptions()->getElements()[$sortedPropertiesId] = $this->coreFunctions->lexikonPropertiesItems($sortedProperties,
$sortedPropertiesItem, $sortedPropertiesId, $currentShopUrl, $sales_channel_id,
$systemConfig);
}
}
}
}
}
return $data;
}else{
return $data;
}
}
public function getLexiconVario(array $twigContext, ?object $data ): ?object
{
if($data != null) {
$request = $this->stack->getMasterRequest();
$currentShopUrl = $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $sales_channel_id);
if ($systemConfig['LEXIKONARTICLEVARIANTS']) {
if ($systemConfig['LEXIKONARTICLEVARIANTS'] != 'nothing') {
}
}
return $data;
}else{
return $data;
}
}
public function getLexiconData(array $twigContext, ?string $data ): ?string
{
if($data != null) {
$request = $this->stack->getMasterRequest();
$currentShopUrl = $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $sales_channel_id);
$generatedLexiconData = $this->coreFunctions->lexikonData($data, $currentShopUrl, $sales_channel_id, $systemConfig);
return $generatedLexiconData;
}else{
return $data;
}
}
/*
* can be used by {{ get_lexicon_count(entry.id) }} to get count of lexikon entries
*
* */
public function getLexiconCount(array $twigContext, ?string $categoryId): ?int
{
$count = 0;
if($categoryId != null) {
$count = $this->connection->fetchColumn('SELECT count(id) FROM pixel_lexikon WHERE HEX(categoryid) = ?', [
$categoryId
]);
return (int)$count;
}else{
return $count;
}
}
public function lexiconFilter(array $twigContext, ?string $data ): ?string
{
if($data != null) {
$request = $this->stack->getMasterRequest();
$currentShopUrl = $request->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL);
$salesChannelContext = $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
$sales_channel_id = $salesChannelContext->getSalesChannel()->getId();
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config', $sales_channel_id);
$generatedLexiconData = $this->coreFunctions->lexikonData($data, $currentShopUrl, $sales_channel_id, $systemConfig);
return $generatedLexiconData;
}else{
return $data;
}
}
}