<?php declare(strict_types=1);
namespace Uandi\EFB\Storefront\Subscriber;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\Category\Tree\Tree;
use Shopware\Core\Content\Category\Tree\TreeItem;
use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\Event\NestedEvent;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
use Shopware\Storefront\Page\Checkout\Finish\CheckoutFinishPageLoadedEvent;
use Shopware\Storefront\Page\PageLoadedEvent;
use Shopware\Storefront\Pagelet\Header\HeaderPagelet;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class HeaderSubscriber implements EventSubscriberInterface
{
private const CATEGORY_FIELD_NAMES = [
'custom_fields_sales_channel_cat_downloads',
'custom_fields_sales_channel_cat_contact',
];
private EntityRepository $categoryRepository;
public static function getSubscribedEvents()
{
return [
HeaderPageletLoadedEvent::class => 'onHeaderPageletLoaded'
];
}
public function __construct(EntityRepository $categoryRepository)
{
$this->categoryRepository = $categoryRepository;
}
public function onHeaderPageletLoaded(HeaderPageletLoadedEvent $event) {
$this->fetchTopBarElements($event->getSalesChannelContext(), $event->getPagelet());
$this->correctActive($event->getPagelet()->getNavigation(), $event->getSalesChannelContext());
}
private function fetchTopBarElements(SalesChannelContext $context, HeaderPagelet $pagelet) {
$salesChannelCustomFields = $context->getSalesChannel()->getTranslated()['customFields'];
$ids = [];
foreach (self::CATEGORY_FIELD_NAMES as $index => $fieldName) {
if (array_key_exists($fieldName, $salesChannelCustomFields)) {
$ids[$index] = $salesChannelCustomFields[$fieldName];
}
}
$categories = $this->categoryRepository->search(
new Criteria($ids),
$context->getContext()
);
if ($categories->count() !== count($ids)) {
$ids = $categories->getIds();
}
$pagelet->topbarElements = [
$categories->get(array_shift($ids)),
'language-widget',
$categories->get(array_shift($ids)),
];
}
private function correctActive(Tree $navigation, SalesChannelContext $context)
{
$salesChannel = $context->getSalesChannel();
$activePath = $navigation->getActive()->getPath();
if (null === $activePath) {
return;
}
$activePath = array_filter(explode('|', $activePath));
if (count($activePath) < $salesChannel->getNavigationCategoryDepth()) {
return;
}
$activePath = array_slice($activePath, 1, $salesChannel->getNavigationCategoryDepth());
foreach (array_reverse($activePath) as $category) {
$result = $this->findInTree($category, $navigation->getTree());
if (null === $result) {
continue;
}
$navigation->setActive($result->getCategory());
return;
}
$navigation->setActive($salesChannel->getNavigationCategory());
}
/**
* @param TreeItem[] $tree
*/
private function findInTree(string $categoryId, array $tree): ?TreeItem
{
if (isset($tree[$categoryId])) {
return $tree[$categoryId];
}
foreach ($tree as $item) {
$nested = $this->findInTree($categoryId, $item->getChildren());
if ($nested) {
return $nested;
}
}
return null;
}
}