<?php
declare(strict_types=1);
namespace MadArticleCustomergroup\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Shopware\Storefront\Pagelet\Menu\Offcanvas\MenuOffcanvasPageletLoadedEvent;
use Shopware\Core\Content\Category\CategoryEvents;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class CategorySubscriber implements EventSubscriberInterface
{
private SystemConfigService $systemConfigService;
public function __construct(
SystemConfigService $systemConfigService
)
{
$this->systemConfigService = $systemConfigService;
}
public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'onHeaderPageletLoadedEvent',
MenuOffcanvasPageletLoadedEvent::class => 'onMenuOffcanvasPageletLoadedEvent',
];
}
public function onMenuOffcanvasPageletLoadedEvent(MenuOffcanvasPageletLoadedEvent $event){
$salesChannelContext = $event->getSalesChannelContext();
if($salesChannelContext->getCustomer() != null){
if($salesChannelContext->getCustomer()->getTags() != null){
$userTags = $salesChannelContext->getCustomer()->getTags()->getElements();
}else{
$userTags = '';
}
$userGroup = $salesChannelContext->getCustomer()->getGroupId();
}else{
$userTags = '';
$userGroup = '';
}
/* for the navigation */
foreach($event->getPagelet()->getNavigation()->getTree() as $nav){
$nav->getCategory()->MadShowCategory = true;
$nav->getCategory()->MadShowTagCategory = true;
if(isset($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
/* Check if User Login */
if($userGroup == ''){
$nav->getCategory()->MadShowCategory = false;
}elseif(!in_array($userGroup, $nav->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
$nav->getCategory()->MadShowCategory = false;
}
}
if(isset($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
/* Check if User Login */
if($userTags == ''){
$nav->getCategory()->MadShowTagCategory = false;
}else{
foreach($nav->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
if(array_key_exists($tag, $userTags)){
$nav->getCategory()->MadShowTagCategory = true;
break;
}else{
$nav->getCategory()->MadShowTagCategory = false;
}
}
}
}
/* Children Category */
if(count($nav->getChildren()) > 0){
foreach($nav->getChildren() as $child){
$child->getCategory()->MadShowCategory = true;
$child->getCategory()->MadShowTagCategory = true;
if(isset($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
/* Check if User Login */
if($userGroup == ''){
$child->getCategory()->MadShowCategory = false;
}elseif(!in_array($userGroup, $child->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
$child->getCategory()->MadShowCategory = false;
}
}
if(isset($child->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($child->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
/* Check if User Login */
if($userTags == ''){
$child->getCategory()->MadShowTagCategory = false;
}else{
foreach($child->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
if(array_key_exists($tag, $userTags)){
$child->getCategory()->MadShowTagCategory = true;
break;
}else{
$child->getCategory()->MadShowTagCategory = false;
}
}
}
}
}
}
}
/* for the active category */
$activeCategory = $event->getPagelet()->getNavigation()->getActive();
$activeCategory->MadShowCategory = true;
$activeCategory->MadShowTagCategory = true;
}
public function onHeaderPageletLoadedEvent(HeaderPageletLoadedEvent $event){
$salesChannelContext = $event->getSalesChannelContext();
if($salesChannelContext->getCustomer() != null){
if($salesChannelContext->getCustomer()->getTags() != null){
$userTags = $salesChannelContext->getCustomer()->getTags()->getElements();
}else{
$userTags = '';
}
$userGroup = $salesChannelContext->getCustomer()->getGroupId();
}else{
$userTags = '';
$userGroup = $this->systemConfigService->getString('MadArticleCustomergroup.config.guestGroup');
}
/* for the navigation */
foreach($event->getPagelet()->getNavigation()->getTree() as $nav){
$nav->getCategory()->MadShowCategory = true;
$nav->getCategory()->MadShowTagCategory = true;
if(isset($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
/* Check if User Login */
if($userGroup == ''){
$nav->getCategory()->MadShowCategory = false;
}elseif(!in_array($userGroup, $nav->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
$nav->getCategory()->MadShowCategory = false;
}
}
if(isset($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($nav->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
/* Check if User Login */
if($userTags == ''){
$nav->getCategory()->MadShowTagCategory = false;
}else{
foreach($nav->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
if(array_key_exists($tag, $userTags)){
$nav->getCategory()->MadShowTagCategory = true;
break;
}else{
$nav->getCategory()->MadShowTagCategory = false;
}
}
}
}
/* Children Category */
if(count($nav->getChildren()) > 0){
foreach($nav->getChildren() as $child){
$child->getCategory()->MadShowCategory = true;
$child->getCategory()->MadShowTagCategory = true;
if(isset($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) and count($child->getCategory()->getCustomFields()['mad_category_usergroups_enable']) > 0){
/* Check if User Login */
if($userGroup == ''){
$child->getCategory()->MadShowCategory = false;
}elseif(!in_array($userGroup, $child->getCategory()->getCustomFields()['mad_category_usergroups_enable'])){
$child->getCategory()->MadShowCategory = false;
}
}
if(isset($child->getCategory()->getCustomFields()['mad_category_tags_enable']) and count($child->getCategory()->getCustomFields()['mad_category_tags_enable']) > 0){
/* Check if User Login */
if($userTags == ''){
$child->getCategory()->MadShowTagCategory = false;
}else{
foreach($child->getCategory()->getCustomFields()['mad_category_tags_enable'] as $tag){
if(array_key_exists($tag, $userTags)){
$child->getCategory()->MadShowTagCategory = true;
break;
}else{
$child->getCategory()->MadShowTagCategory = false;
}
}
}
}
}
}
}
/* for the active category */
$activeCategory = $event->getPagelet()->getNavigation()->getActive();
$activeCategory->MadShowCategory = true;
$activeCategory->MadShowTagCategory = true;
if($userGroup == ''){
$activeCategory->MadLogin = false;
}else{
$activeCategory->MadLogin = true;
}
if(isset($activeCategory->getCustomFields()['mad_category_usergroups_enable']) and count($activeCategory->getCustomFields()['mad_category_usergroups_enable']) > 0){
/* Check if User Login */
if($userGroup == ''){
$activeCategory->MadShowCategory = false;
}elseif(!in_array($userGroup, $activeCategory->getCustomFields()['mad_category_usergroups_enable'])){
$activeCategory->MadShowCategory = false;
}
}
if(isset($activeCategory->getCustomFields()['mad_category_tags_enable']) and count($activeCategory->getCustomFields()['mad_category_tags_enable']) > 0){
/* Check if User Login */
if($userTags == ''){
$activeCategory->MadShowTagCategory = false;
}else{
foreach($activeCategory->getCustomFields()['mad_category_tags_enable'] as $tag){
if(array_key_exists($tag, $userTags)){
$activeCategory->MadShowTagCategory = true;
break;
}else{
$activeCategory->MadShowTagCategory = false;
}
}
}
}
}
}