<?php
namespace GlobusSW6\Subscriber;
use GlobusSW6\Service\IaneoBusinessExtension;
use GlobusSW6\Service\KlimaSplit\KlimaSplitService;
use Shopware\Core\Framework\Api\Context\SystemSource;
use Psr\Log\LoggerInterface;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\Adapter\Translation\Translator;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
use Shopware\Core\Framework\Uuid\Uuid;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
class KlimaSplitSubscriber implements EventSubscriberInterface
{
/** @var KlimaSplitService */
private $klimaSplitService;
/** @var IaneoBusinessExtension */
private $ianeoBusinessExtensionService;
/** @var Translator */
private $translator;
/** @var LoggerInterface */
private $logger;
/**
* KlimaSplitSubscriber constructor.
* @param KlimaSplitService $klimaSplitService
* @param IaneoBusinessExtension $ianeoBusinessExtensionService
* @param Translator $translator
* @param LoggerInterface $logger
*/
public function __construct(KlimaSplitService $klimaSplitService, IaneoBusinessExtension $ianeoBusinessExtensionService, Translator $translator, LoggerInterface $logger)
{
$this->klimaSplitService = $klimaSplitService;
$this->ianeoBusinessExtensionService = $ianeoBusinessExtensionService;
$this->translator = $translator;
$this->logger = $logger;
}
public static function getSubscribedEvents()
{
return [
'product.search.result.loaded' => 'onProduct'
];
}
// TODO: currently missing: controller for handling ajax 'if pop-up was read' missing, as we first need the JS in the storefront
public function onProduct(EntitySearchResultLoadedEvent $event){
// do not fire event in backend context
if ($event->getContext()->getSource() instanceof AdminApiSource) {
if ($event->getContext()->getSource()->isAdmin()) {
return;
}
}
if ($event->getContext()->getSource() instanceof SystemSource) {
return;
}
$products = $event->getResult()->getElements();
if(empty($products)){
return;
}
try {
$salesChannelId = $event->getContext()->getSource()->getSalesChannelId();
$languageId = '2fbb5fe2e29a4d70aa5854ce7ce3e20b';
$this->translator->injectSettings(
$salesChannelId,
$languageId,
'de-DE',// TODO-NGS: currently hardcoded
$event->getContext()
);
} catch (\Throwable $t) {
$this->logger->info('Unable to translate snippets for api calls using the following params: saleschannelId: ' . $salesChannelId . ' ; languageId: ' . $languageId . ' ; context: ' . json_encode($event->getContext())
);
}
$klimaSplitProducts = $this->klimaSplitService->checkIsKlimaSplit($event->getResult()->getEntities());
// add klima flags to product extension
$this->ianeoBusinessExtensionService->addAttributes($event->getResult()->getEntities(), $klimaSplitProducts, 'isKlimaSplit');
$this->ianeoBusinessExtensionService->addAttributes($event->getResult()->getEntities(), $klimaSplitProducts, 'showKlimaSplitModal');
$klimaSplitModalContent = [
'p1' => $this->translator->trans("klimasplit.Modal.p1"),
'p2' => $this->translator->trans("klimasplit.Modal.p2"),
'p3' => $this->translator->trans("klimasplit.Modal.p3"),
'p4' => $this->translator->trans("klimasplit.Modal.p4"),
'p5' => $this->translator->trans("klimasplit.Modal.p5")
];
/** @var ProductEntity $product */
foreach ($products as $product) {
$this->ianeoBusinessExtensionService->addSingleAttributeToProduct($product, null, 'klimaSplitModalContent');
if($product->getExtensions()['ianeoExtensions']['isKlimaSplit']){
$this->ianeoBusinessExtensionService->addSingleAttributeToProduct($product, $klimaSplitModalContent, 'klimaSplitModalContent');
}
}
}
}