<?php
namespace Twigel\SportPlugin\Subscriber;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
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\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PageSubscriber implements EventSubscriberInterface
{
/**
* @var SystemConfigService
*/
private SystemConfigService $config;
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $twigelCareerRepository;
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $countryRepository;
/**
* @var SalesChannelRepository
*/
private SalesChannelRepository $categoryRepository;
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $promotionRepository;
public function __construct(
SystemConfigService $config,
EntityRepositoryInterface $twigelCareerRepository,
EntityRepositoryInterface $countryRepository,
SalesChannelRepository $categoryRepository,
EntityRepositoryInterface $promotionRepository
)
{
$this->config = $config;
$this->twigelCareerRepository = $twigelCareerRepository;
$this->countryRepository = $countryRepository;
$this->categoryRepository = $categoryRepository;
$this->promotionRepository = $promotionRepository;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'onLoad'
];
}
/**
* @param GenericPageLoadedEvent $event
* @return void
*/
public function onLoad(GenericPageLoadedEvent $event)
{
$config = $this->config->get('TwigelSportPlugin')['config'];
if (array_key_exists('careerCategory', $config)) {
$careerCategoryId = $config['careerCategory'];
if ($event->getPage()->getHeader()) {
if ($careerCategoryId == $event->getPage()->getHeader()->getNavigation()->getActive()->getId()) {
$careers = $this->twigelCareerRepository->search(new Criteria(), Context::createDefaultContext())->getElements();
$event->getPage()->assign(['careers' => $careers]);
}
}
}
if (array_key_exists('countryCategory', $config)) {
$countryCategoryIds = $config['countryCategory'];
if ($event->getPage()->getHeader()) {
if (in_array($event->getPage()->getHeader()->getNavigation()->getActive()->getId(), $countryCategoryIds)) {
$countries = $this->countryRepository->search(new Criteria(), $event->getContext())->getElements();
$event->getPage()->assign(['countries' => $countries]);
}
}
}
if (array_key_exists('model', $config) && array_key_exists('year', $config) && array_key_exists('make', $config)) {
$event->getPage()->assign(['search' => ['model' => $config['model'], 'year' => $config['year'], 'make' => $config['make']]]);
}
if (array_key_exists('mainCategory', $config)){
$mainCategoryId = $config["mainCategory"];
if($event->getPage()->getHeader()){
if($event->getPage()->getHeader()->getNavigation()->getActive()->getId() === $mainCategoryId){
$bundleCategories = $config['bundleCategories'];
$criteria = new Criteria($bundleCategories);
$criteria->addAssociations(['products', 'products.cover',"products.manufacturer"]);
$products = $this->categoryRepository->search($criteria, $event->getSalesChannelContext())->getElements();
if ($products){
$categories = [];
foreach ($products as $product){
$categories[$product->getName()] = $product->getProducts();
}
$event->getPage()->assign(['categories'=> $categories]);
}else{
$event->getPage()->assign(['categories'=> []]);
}
}
}
}
if (array_key_exists('bundelEnable', $config) && $config['bundelEnable'] && array_key_exists('mainCategory', $config)){
$mainCategoryId = $config["mainCategory"];
if($event->getPage()->getHeader()){
if($event->getPage()->getHeader()->getNavigation()->getActive()->getId() === $mainCategoryId){
if (array_key_exists('bundlePromotion', $config)){
$promotionId = $config['bundlePromotion'];
$criteria = new Criteria([$promotionId]);
$criteria->addAssociations(['discounts', 'cartRules', 'orderRules']);
$promotion = $this->promotionRepository->search($criteria, $event->getContext())->first();
if ($promotion){
$event->getPage()->assign(['bundlePromotion' => $promotion]);
}else{
$event->getPage()->assign(['bundlePromotion' => []]);
}
}
}
}
}
}
}