<?php declare(strict_types=1);
namespace ImnxxLandingpageNavi\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Framework\Context;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\Content\Category\CategoryEntity;
class LandingpageNaviSubscriber implements EventSubscriberInterface {
/**
* @var EnityRepository
*/
private $categoryRepository;
public function __construct(EntityRepository $categoryRepository) {
$this->categoryRepository = $categoryRepository;
}
public static function getSubscribedEvents(): array {
return [
GenericPageLoadedEvent::class => 'onPageLoaded',
];
}
public function onPageLoaded(GenericPageLoadedEvent $event) {
$page = $event->getPage();
$context = $event->getContext();
$header = $page->getHeader();
if ($header != null) {
$active_category = $header
->getNavigation()
->getActive();
$parent_category = $this->getParentCategory(
$context,
$active_category->getParentId(),
);
$parent_customs_fields = $parent_category->getCustomFields();
if (
isset($parent_customs_fields['imnxx_landingpage_navi_show']) &&
$parent_customs_fields['imnxx_landingpage_navi_show']
) {
// $customs_fields = $header
// ->getNavigation()
// ->getActive()
// ->getCustomFields();
$landingpage_navi_elements = $this->getCategoriesByParentId(
$context,
$parent_category->getId(),
);
if (count($landingpage_navi_elements) > 0) {
$event->getPage()->assign($landingpage_navi_elements);
$event->getPage()->assign( ['parent_customs_fields' => $parent_customs_fields] );
}
} else if(
isset($active_category->getCustomFields()['imnxx_landingpage_navi_show']) &&
$active_category->getCustomFields()['imnxx_landingpage_navi_show']
) {
// $customs_fields = $header
// ->getNavigation()
// ->getActive()
// ->getCustomFields();
$landingpage_navi_elements = $this->getCategoriesByParentId(
$context,
$active_category->getId(),
);
if (count($landingpage_navi_elements) > 0) {
$event->getPage()->assign($landingpage_navi_elements);
$event->getPage()->assign( ['parent_customs_fields' => $active_category->getCustomFields()] );
}
}
}
}
public function getParentCategory(Context $context, string $id = null): object {
$criteria = new Criteria();
$criteria->addAssociation('customFields');
if ($id != null) {
$criteria->addFilter(new EqualsFilter('id', $id));
}
$category = $this->categoryRepository->search($criteria, $context)->first();
if ($category->getParentId() != null && ( !isset($category->getCustomFields()['imnxx_landingpage_navi_show']) || $category->getCustomFields()['imnxx_landingpage_navi_show']!= true ) ) {
$criteria = new Criteria();
$criteria->addAssociation('customFields');
// $criteria->addFilter(new EqualsFilter('customFields.imnxx_landingpage_navi_show', true));
$criteria->addFilter(new EqualsFilter('id', $category->getParentId()));
$categoryParent = $this->categoryRepository->search($criteria, $context)->first();
}
if(!isset($categoryParent))
return $category;
return $categoryParent;
}
public function getCategoriesByParentId(Context $context, string $parentId = null): array {
$criteria = new Criteria();
$criteria->addAssociation('customFields');
$criteria->addAssociation('children');
if ($parentId == null) {
return [];
}
//Landingpages category ID 32d6d2a9e08146369d513a8dcd7636de
$criteria->addFilter(new EqualsFilter('parentId', $parentId));
$unsorted_categories = $this->categoryRepository->search($criteria, $context)->getElements();
$sorted_categories = [];
$last_id = '';
for($i=0; $i<count($unsorted_categories); $i++) {
foreach($unsorted_categories as $category) {
if($category->afterCategoryId == $last_id) {
$last_id = $category->getId();
$sorted_categories[$category->getId()] = $category;
break;
}
}
}
$categories['landingpage_navi_elements'] = $sorted_categories;
return $categories;
}
}