<?php declare(strict_types=1);
namespace Pixel\LexikonSW6\Subscriber;
use _HumbugBoxfac515c46e83\Nette\Iterators\Mapper;
use Shopware\Core\Framework\Struct\ArrayEntity;
use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Pixel\LexikonSW6\Component\CoreFunctions;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Symfony\Component\HttpFoundation\RequestStack;
use Shopware\Core\PlatformRequest;
use Shopware\Storefront\Framework\Routing\RequestTransformer;
use Composer\IO\NullIO;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Doctrine\DBAL\ParameterType;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\Filter;
use Shopware\Core\Content\Cms\CmsPageEvents;
use Shopware\Core\Framework\Struct\Struct;
use Shopware\Core\Content\Seo\SeoUrlPlaceholderHandlerInterface;
use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Grouping\FieldGrouping;
use Psr\Log\LoggerInterface;
use Shopware\Core\Content\Product\Events\ProductIndexerEvent;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Content\Seo\SeoUrlUpdater;
class Frontend implements EventSubscriberInterface
{
private $systemConfigService;
private $config;
private $pluginConfig;
private $coreFunctions;
private $requestStack;
private $seoUrlRewriter;
/**
* @var Connection
*/
private $connection;
/**
* @var LoggerInterface
*/
private $logger;
public function __construct(
SeoUrlPlaceholderHandlerInterface $seoUrlRewriter,
SystemConfigService $systemConfigService,
CoreFunctions $coreFunctions,
RequestStack $requestStack
) {
$this->systemConfigService = $systemConfigService;
$this->coreFunctions = $coreFunctions;
$this->requestStack = $requestStack;
$this->seoUrlRewriter = $seoUrlRewriter;
}
public static function getSubscribedEvents(): array
{
return [
//CmsPageEvents::SLOT_LOADED_EVENT => 'onCmsLoadedEvent',
ProductPageLoadedEvent::class => 'onArticleDetailLoaded',
];
}
public function onCmsLoadedEvent(EntityLoadedEvent $event): void
{
$source = $event->getContext()->getSource();
if (!$source instanceof SalesChannelApiSource) {
return;
}
$entities = $event->getEntities();
}
public function onArticleDetailLoaded(ProductPageLoadedEvent $event)
{
$systemConfig = $this->systemConfigService->get('PixelLexikonSW6.config',
$event->getSalesChannelContext()->getSalesChannel()->getId());
$activateOnArticle = $systemConfig['LEXIKONARTICLE'] ? $systemConfig['LEXIKONARTICLE'] : false;
$activateOnVariants = $systemConfig['LEXIKONARTICLEVARIANTS'] ? $systemConfig['LEXIKONARTICLEVARIANTS'] : 'nothing';
$activateOnAttribs = $systemConfig['LEXIKONARTICLEATTRSPEZ'] ? $systemConfig['LEXIKONARTICLEATTRSPEZ'] : 'nothing';
$activateOnSpecField = $systemConfig['LEXIKONARTICLECSTM'] ? $systemConfig['LEXIKONARTICLECSTM'] : false;
if ($systemConfig['active'] == true) {
if ($activateOnArticle == true) {
$request = $this->requestStack->getCurrentRequest();
$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();
$page = $event->getPage();
$product = $page->getProduct();
$cms = $page->getCmsPage();
if ($activateOnVariants) {
if ($activateOnVariants != 'nothing') {
/*foreach ($product->getConfiguratorSettings()->getElements() as $option) {
if ($systemConfig['LEXIKONARTICLEVARIANTS'] == 'groupandoptions' or $systemConfig['LEXIKONARTICLEVARIANTS'] == 'onlygroups') {
$option = $this->coreFunctions->lexikonConfiguratorElements($option, $currentShopUrl,
$sales_channel_id, $systemConfig);
}
}*/
}
}
/*
* Replace the article description
*
**/
if ($activateOnArticle == true) {
$getProductDataTranslated = $product->getTranslated();
$getProductDataTranslated['description'] = $this->coreFunctions->lexikonRewriter($getProductDataTranslated['description'],
$currentShopUrl, $sales_channel_id, $systemConfig);
$product->assign(['description' => $getProductDataTranslated['description']]);
$product->assign(['translated' => $getProductDataTranslated]);
}
/*
* Replace the properties
*
**/
if ($activateOnAttribs) {
if ($activateOnAttribs != 'nothing') {
foreach ($product->get('sortedProperties')->getElements() as $sortedPropertyId => $sortedProperties) {
if ($activateOnAttribs == 'nameandvalues' or $activateOnAttribs == 'onlyproperties') {
$sortedProperties = $this->coreFunctions->lexikonProperties($sortedProperties,
$currentShopUrl, $sales_channel_id, $systemConfig);
}
if ($activateOnAttribs == 'nameandvalues' or $activateOnAttribs == 'onlyvalue') {
foreach ($sortedProperties->getOptions()->getElements() as $sortedPropertiesId => $sortedPropertiesItem) {
$sortedProperties->getOptions()->getElements()[$sortedPropertiesId] = $this->coreFunctions->lexikonPropertiesItems($sortedProperties,
$sortedPropertiesItem, $sortedPropertiesId, $currentShopUrl, $sales_channel_id,
$systemConfig);
}
}
}
}
}
/*
* Replace the specified custom field (plugin configuration)
* only customfields > text
*
**/
if ($activateOnSpecField == true) {
$customFields = $product->getCustomFields();
if ($systemConfig['CUSTOMARTFIELD']) {
$specCustomField = explode(',', $systemConfig['CUSTOMARTFIELD']);
if (is_array($specCustomField) and count($specCustomField) > 0) {
foreach ($specCustomField as $cstmkey => $cstmvalues) {
if (array_key_exists($cstmvalues, $customFields)) {
$getProductDataTranslated = $product->getTranslated();
$getProductDataTranslated['customFields'][$cstmvalues] = $this->coreFunctions->lexikonCustomFields($customFields[$cstmvalues],
$currentShopUrl, $sales_channel_id, $systemConfig);
$product->assign(['translated' => $getProductDataTranslated]);
}
}
}
}
}
}
}
}
}