<?php declare(strict_types=1);
namespace Cbax\ModulManufacturers\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionCollection;
use Cbax\ModulManufacturers\Components\ConfigReaderHelper;
use Cbax\ModulManufacturers\Components\ManufacturersHelper;
class FrontendSubscriber implements EventSubscriberInterface
{
private $config = null;
/**
* @var ConfigReaderHelper
*/
private $configReader;
/**
* @var ManufacturersHelper
*/
private $helperComponent;
public function __construct(
ConfigReaderHelper $configReader,
ManufacturersHelper $helperComponent)
{
$this->configReader = $configReader;
$this->helperComponent = $helperComponent;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
$this->config = $this->config ?? $this->configReader->getConfig();
if ($this->config['active'] != true)
{
return;
}
$page = $event->getPage();
$cmsPage = $page->getCmsPage();
$cbaxManufacturersProductSlider = null;
if (!empty($cmsPage))
{
$cbaxManufacturersProductSlider = $cmsPage->getFirstElementOfType('cbax-manufacturers-product-slider');
}
$hasCmsProductSlider = !empty($cbaxManufacturersProductSlider);
$sliderConfig = $hasCmsProductSlider ? $cbaxManufacturersProductSlider->getFieldConfig()->getElements() : [];
$cbaxProductSliderBlockId = $hasCmsProductSlider ? $cbaxManufacturersProductSlider->getBlockId() : null;
if (($this->config['showProductSlider'] == true && empty($cmsPage)) || ($hasCmsProductSlider && !empty($cmsPage)))
{
$salesChannelContext = $event->getSalesChannelContext();
$product = $page->getProduct();
$currentProductId = $product->getParentId() ?? $product->getId();
// Shopware übergibt hier nur einen Teil der nötigen Daten, daher später kompletten Hersteller holen
$thinManufacurer = $product->getManufacturer();
// prüfen, ob ein Hersteller hinterlegt ist
if ($thinManufacurer !== null) {
$manufaturerId = $thinManufacurer->getId();
$categoryId = null;
if (($this->config['sliderByCategory'] && !$hasCmsProductSlider) || ($hasCmsProductSlider && $sliderConfig['sliderByCategory']->getValue()))
{
$categoryTree = $product->getCategoryTree();
$categoryId = end($categoryTree);
}
$manufacturerProducts = $this->helperComponent->getProductsByManufacturer(
$manufaturerId,
$categoryId,
$salesChannelContext,
$hasCmsProductSlider ? $sliderConfig['productSliderSorting']->getValue() : $this->config['productSliderSorting'],
$hasCmsProductSlider ? $sliderConfig['maxSliderProducts']->getValue() : $this->config['maxSliderProducts'],
$currentProductId
);
if (($this->config['sliderWithManufacturer'] && !$hasCmsProductSlider) || ($hasCmsProductSlider && $sliderConfig['sliderWithManufacturer']->getValue()))
{
if (empty($thinManufacurer->getMedia()) && !empty($thinManufacurer->getMediaId())) {
$manufacturer = $this->helperComponent->getManufacturerById($manufaturerId, $salesChannelContext);
} else {
$manufacturer = [$thinManufacurer];
}
$manufacturerProducts = array_merge($manufacturer, $manufacturerProducts);
}
} else {
$manufacturerProducts = [];
}
// leeren Slider entfernen
if (empty($manufacturerProducts) && !empty($cbaxProductSliderBlockId))
{
$cmsSectionCollection = new CmsSectionCollection();
foreach ($cmsPage->getSections() as $section) {
$newBlocksCollection = $section->getBlocks();
$newBlocksCollection->filterAndReduceByProperty('id', $cbaxProductSliderBlockId);
$section->setBlocks($newBlocksCollection);
$cmsSectionCollection->add($section);
}
$cmsPage->setSections($cmsSectionCollection);
$page->setCmsPage($cmsPage);
}
$page->assign( ['cbaxModulManufacturers' => $manufacturerProducts]);
}
}
}