<?php
namespace EnetSubshop\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Shopware\Storefront\Pagelet\PageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HeaderPageletSubscriber implements EventSubscriberInterface
{
/** @var SalesChannelRepository $salesChannelRepository */
private $salesChannelRepository;
/** @var SystemConfigService $systemConfigService */
private $systemConfigService;
public function __construct(EntityRepository $salesChannelRepository, SystemConfigService $systemConfigService)
{
$this->salesChannelRepository = $salesChannelRepository;
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents()
{
return [
HeaderPageletLoadedEvent::class => 'onSalesChannelLoaded',
CheckoutConfirmPageLoadedEvent::class => 'onPageLoaded',
];
}
public function onPageLoaded(PageLoadedEvent $event)
{
$isMasterStore = $this->systemConfigService->get('EnetSubshop.config.isMaster');
if(!$isMasterStore) {
$event->getPage()->addExtension(
'enetSubshop',
$this->fetchSubshops($event->getContext())
);
}
}
public function onSalesChannelLoaded(PageletLoadedEvent $event)
{
$isMasterStore = $this->systemConfigService->get('EnetSubshop.config.isMaster');
if(!$isMasterStore) {
$event->getPagelet()->addExtension(
'enetSubshop',
$this->fetchSubshops($event->getContext())
);
}
}
/**
* @param Context $context
* @return EntitySearchResult
*/
public function fetchSubshops(Context $context)
{
$criteria = new Criteria();
// $criteria->addFilter(new EqualsFilter('sales_channel.active', true));
$criteria->addFilter(new EqualsFilter('sales_channel.maintenance', false));
// only entities which have "showasselection: 1"
$criteria->addFilter(new ContainsFilter('sales_channel.translations.customFields', '"enet_adress_showasselection": "1"'));
$criteria->addAssociation('domains');
$criteria->addAssociation('type');
$criteria->addAssociation('type_translation');
$criteria->addAssociation('translation');
return $this->salesChannelRepository->search($criteria, $context);
}
}