<?php declare(strict_types=1);
namespace SmsBecoTechnicTheme\Subscriber;
use Acris\CategoryCustomerGroup\Components\BlockCategoryService;
use Shopware\Core\Content\Category\CategoryEntity;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeSentEvent;
use Shopware\Core\Content\MailTemplate\Service\Event\MailBeforeValidateEvent;
use Shopware\Core\Framework\Api\Context\AdminApiSource;
use Shopware\Core\Framework\Api\Context\AdminSalesChannelApiSource;
use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CategorySubscriber implements EventSubscriberInterface
{
private EntityRepository $categoryRepository;
private EntityRepository $blockedCategoryRepository;
public function __construct(
EntityRepository $categoryRepository,
EntityRepository $blockedCategoryRepository
)
{
$this->categoryRepository = $categoryRepository;
$this->blockedCategoryRepository = $blockedCategoryRepository;
}
public static function getSubscribedEvents(): array
{
return [
'category.written' => 'onCategoryWrittenEvent',
];
}
public function onCategoryWrittenEvent(EntityWrittenEvent $event)
{
$contextSource = $event->getContext()->getSource();
// Only trigger on Admin changes
if (!$contextSource instanceof AdminApiSource) {
return;
}
ini_set('max_execution_time', "0");
$criteria = new Criteria();
$criteria->addFilter(new EqualsAnyFilter('id', $event->getIds()));
$criteria->addAssociation('children');
/** @var CategoryEntity $category */
$category = $this->categoryRepository->search($criteria, $event->getContext())->first();
$categoryCustomFields = $category->getCustomFields();
if (isset($categoryCustomFields['cdz_use_configuration_for_child_categories']) && $categoryCustomFields['cdz_use_configuration_for_child_categories'] && $category->getChildren()->count() > 0) {
$blockedCustomerGroupsCriteria = new Criteria();
$blockedCustomerGroupsCriteria->addFilter(new EqualsFilter('categoryId', $category->getId()));
$blockedCustomerGroups = $this->blockedCategoryRepository->searchIds($blockedCustomerGroupsCriteria, $event->getContext())->getIds();
if (empty($blockedCustomerGroups)) {
return;
}
$this->updateChildCategories($category, $event, $blockedCustomerGroups);
}
}
private function updateChildCategories(CategoryEntity $category, EntityWrittenEvent $event, $blockedCustomerGroups): void
{
$childCategories = $this->getChildCategories($category, $event);
foreach ($childCategories as $childCategory) {
$this->updateChildCategories($childCategory, $event, $blockedCustomerGroups);
}
foreach ($blockedCustomerGroups as $blockedCustomerGroup) {
$this->blockedCategoryRepository->create([
[
'categoryId' => $category->getId(),
'customerGroupId' => $blockedCustomerGroup['customerGroupId']
]
], $event->getContext());
}
}
private function getChildCategories(CategoryEntity $category, EntityWrittenEvent $event): EntityCollection
{
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('parentId', $category->getId()));
$criteria->addAssociation('children');
return $this->categoryRepository->search($criteria, $event->getContext())->getEntities();
}
}