<?php
declare(strict_types=1);
namespace Wbfk\CmsHideParts\Service;
use JetBrains\PhpStorm\ArrayShape;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\AndFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\Request;
use Wbfk\CmsHideParts\DataAbstractionLayer\HiddenCmsPartsEntity;
use Wbfk\CmsHideParts\DataAbstractionLayer\HiddenEntityPartsEntity;
class SalesChannelCmsPageLoaderHideParts implements SalesChannelCmsPageLoaderInterface
{
/**
* @var SalesChannelCmsPageLoaderInterface
*/
private SalesChannelCmsPageLoaderInterface $inner;
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $hiddenEntityPartsRepository;
/**
* @var EntityRepositoryInterface
*/
private EntityRepositoryInterface $hiddenCmsPartsRepository;
public function __construct(
SalesChannelCmsPageLoaderInterface $inner,
EntityRepositoryInterface $hiddenEntityPartsRepository,
EntityRepositoryInterface $hiddenCmsPartsRepository
) {
$this->inner = $inner;
$this->hiddenEntityPartsRepository = $hiddenEntityPartsRepository;
$this->hiddenCmsPartsRepository = $hiddenCmsPartsRepository;
}
public function load(Request $request, Criteria $criteria, SalesChannelContext $context, ?array $config = null, ?ResolverContext $resolverContext = null): EntitySearchResult
{
$hiddenParts = $this->getHiddenParts($criteria->getIds(), $context, $resolverContext);
if (!empty($hiddenParts['slot'])) {
$criteria->getAssociation('sections.blocks.slots')->addFilter(
new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsAnyFilter('id', array_values($hiddenParts['slot']))])
);
}
if (!empty($hiddenParts['block'])) {
$criteria->getAssociation('sections.blocks')->addFilter(
new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsAnyFilter('id', array_values($hiddenParts['block']))])
);
}
if (!empty($hiddenParts['section'])) {
$criteria->getAssociation('sections')->addFilter(
new NotFilter(MultiFilter::CONNECTION_AND, [new EqualsAnyFilter('id', array_values($hiddenParts['section']))])
);
}
return $this->inner->load($request, $criteria, $context, $config, $resolverContext);
}
/**
* @param array $ids
* @param SalesChannelContext $context
* @param ResolverContext|null $resolverContext
* @return array|array[]
*/
#[ArrayShape(['slot' => "array", 'block' => "array", 'section' => "array"])]
private function getHiddenParts(array $ids, SalesChannelContext $context, ?ResolverContext $resolverContext = null): array
{
/*
* Load all hidden parts of the template/CMS-Page
* We will sort them into 'slot', 'block' and 'section'
*/
$hiddenParts = ['slot' => [], 'block' => [], 'section' => []];
//$criteria = new Criteria($ids);
$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('pageId', $ids[0]));
$hiddenCmsParts = $this->hiddenCmsPartsRepository->search($criteria, $context->getContext());
foreach ($hiddenCmsParts->getElements() as $hiddenCmsParts) {
/** @var HiddenCmsPartsEntity $hiddenCmsParts */
$cmsId = $hiddenCmsParts->getCmsId();
$hiddenParts[$hiddenCmsParts->getCmsType()][$cmsId] = $cmsId;
}
/*
* Load all hidden parts of the entity
* We will also sort them into 'slot', 'block' and 'section'
* and therefore merge/override the hidden parts set by the template
*/
if ($resolverContext instanceof EntityResolverContext) {
$entityName = $resolverContext->getDefinition()->getEntityName();
$entityId = $resolverContext->getEntity()->getUniqueIdentifier();
$criteria = new Criteria();
$criteria->addFilter(
new AndFilter([
new EqualsFilter('entity', $entityName),
new EqualsFilter('entityId', $entityId)
])
);
$hiddenEntityParts = $this->hiddenEntityPartsRepository->search($criteria, $context->getContext());
foreach ($hiddenEntityParts->getElements() as $hiddenEntityParts) {
/** @var HiddenEntityPartsEntity $hiddenEntityParts */
$cmsId = $hiddenEntityParts->getCmsId();
$cmsType = $hiddenEntityParts->getCmsType();
if ($hiddenEntityParts->isHidden()) {
$hiddenParts[$cmsType][$cmsId] = $cmsId;
} else {
// Unset, if it was set by the template
unset($hiddenParts[$cmsType][$cmsId]);
}
}
}
return $hiddenParts;
}
}