<?php declare(strict_types=1);
namespace DonCarneTheme\Subscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\Struct\ArrayStruct;
class GenericPageLoadedSubscriber implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
private EntityRepository $categoryRepository;
public function __construct(
SystemConfigService $systemConfigService,
EntityRepository $categoryRepository
)
{
$this->systemConfigService = $systemConfigService;
$this->categoryRepository = $categoryRepository;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'addInfo'
];
}
public function addInfo(GenericPageLoadedEvent $event)
{
$this->addTopBar($event);
$this->addMenuSpecialCategories($event);
}
private function addTopBar(GenericPageLoadedEvent $event)
{
$config = $this->systemConfigService->get('DonCarneTheme.config');
if(!$config){
return;
}
$keys = ["topbarCategory1", "topbarCategory2", "topbarCategory3", "topbarCategory4"];
$categoryIds = $this->addConfigCategory($config, $keys);
$topbarCategories = null;
if(!empty($categoryIds)){
$topbarCategories = $this->getCategories($categoryIds, $event->getContext());
}
if($topbarCategories) {
$event->getPage()->addExtension(
'topbar',
new ArrayStruct($topbarCategories)
);
}
}
private function addConfigCategory(array $config, array $keys): array
{
$categoryIds = [];
foreach($keys as $key){
if(isset($config[$key])){
$categoryIds[] = $config[$key];
}
}
return $categoryIds;
}
private function getCategories(array $categoryIds, Context $context): array
{
$criteria = (new Criteria())
->addFilter(new EqualsAnyFilter('id', $categoryIds));
return $this->categoryRepository->search($criteria, $context)->getElements();
}
private function addMenuSpecialCategories(GenericPageLoadedEvent $event)
{
$header = $event->getPage()->getHeader();
if(!$header){
return;
}
$categories = $header->getNavigation()->getTree();
if(!$categories){
return;
}
$resolvedCategories = [];
$extraCategories = false;
$showExtraCategories = false;
foreach ($categories as $category){
foreach($category->getChildren() as $subcategory){
$customFields = $subcategory->getCategory()->getCustomFields();
if($customFields && isset($customFields['doncarne_category_special_menu_display'])){
$customFields = $subcategory->getCategory()->getCustomFields();
$categoryIds = isset($customFields['doncarne_category_custom_fields_special_menu_extra_categories']) ? $customFields['doncarne_category_custom_fields_special_menu_extra_categories'] : [];
if(!empty($categoryIds)){
$extraCategories = $this->getCategories($categoryIds, $event->getContext());
if($extraCategories) {
$resolvedCategories = array_merge($resolvedCategories, $extraCategories);
$showExtraCategories = true;
}
}
}
}
}
if($showExtraCategories){
$event->getPage()->getHeader()->addExtension(
'menuExtraCategories',
new ArrayStruct($resolvedCategories)
);
}
}
}