<?php declare(strict_types=1);
namespace NetzpGallery6\Subscriber;
use NetzpGallery6\Components\GalleryHelper;
use NetzpGallery6\Core\Content\Gallery\GalleryDefinition;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class FrontendSubscriber implements EventSubscriberInterface
{
private $container;
private $config;
private $requestStack;
private $helper;
public function __construct(
ContainerInterface $container,
SystemConfigService $config,
RequestStack $requestStack,
GalleryHelper $helper
) {
$this->container = $container;
$this->config = $config;
$this->requestStack = $requestStack;
$this->helper = $helper;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageCriteriaEvent::class => 'onProductCriteriaLoaded',
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductCriteriaLoaded(ProductPageCriteriaEvent $event): void
{
$event->getCriteria()->addAssociation('galleries');
$event->getCriteria()->getAssociation('galleries')->addAssociation('gallerymedia');
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
$config = $this->config->get('NetzpGallery6.config', $event->getSalesChannelContext()->getSalesChannel()->getId());
$galleries = $event->getPage()->getProduct()->getExtension('galleries');
if(count($galleries) > 0) {
foreach($galleries as $gallery) {
if($gallery->getType() == GalleryDefinition::TYPE_FOLDER) {
$mediaFolderItems = $this->helper->getMediaFromFolderId(
$gallery->getMediaFolderId(),
$event->getContext(),
$config && array_key_exists('sortmode', $config) ? $config['sortmode'] : 0
);
$gallery->addExtension('mediafolder', $mediaFolderItems);
}
else if($gallery->getType() == GalleryDefinition::TYPE_TAGS) {
$mediaTagsItems = $this->helper->getMediaFromTags(
array_filter(explode(',', $gallery->getTags())),
$event->getContext(),
$config && array_key_exists('sortmode', $config) ? $config['sortmode'] : 0
);
$gallery->addExtension('mediafolder', $mediaTagsItems);
}
}
}
}
}