<?php
namespace Crehler\ListingBanner\Core\Content\ListingBanners\SalesChannel;
use DateTime;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
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\RangeFilter;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
use Shopware\Core\Framework\Routing\Annotation\Entity;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route(defaults={"_routeScope"={"store-api"}})
*/
class ListingBannersRoute extends AbstractListingBannersRoute
{
protected EntityRepositoryInterface $categoryRepository;
protected EntityRepositoryInterface $bannerRepository;
/**
* @param EntityRepositoryInterface $categoryRepository
* @param EntityRepositoryInterface $bannerRepository
*/
public function __construct(EntityRepositoryInterface $categoryRepository, EntityRepositoryInterface $bannerRepository)
{
$this->categoryRepository = $categoryRepository;
$this->bannerRepository = $bannerRepository;
}
public function getDecorated(): AbstractListingBannersRoute
{
throw new DecorationPatternException(self::class);
}
/**
* @Entity("crehler_banner")
* @Route("/store-api/crehler/listing-banners/{categoryId}", name="store-api.crehler.listing-banners", methods={"GET"})
*/
public function load(string $categoryId, Criteria $criteria, SalesChannelContext $context): ListingBannerRouteResponse
{
$path = explode('|', $this->getPath($categoryId, $context->getContext()));
$path = array_filter($path, static function ($el) {
return $el !== "";
});
$date = (new DateTime())
->format(Defaults::STORAGE_DATE_TIME_FORMAT);
$criteria
->getAssociation('media')
->addSorting(new FieldSorting('position', FieldSorting::ASCENDING));
$criteria
->addFilter(new EqualsFilter('active', true))
->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR,
[
new MultiFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('dateFrom', null), new EqualsFilter('dateTo', null)]),
new MultiFilter(MultiFilter::CONNECTION_AND, [new RangeFilter('dateFrom', [RangeFilter::LT => $date]), new RangeFilter('dateTo', [RangeFilter::GT => $date])])
]
))
->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR,
[
new EqualsFilter('categories.id', $categoryId),
new MultiFilter(MultiFilter::CONNECTION_AND, [new EqualsFilter('inheritCategory', true), new EqualsAnyFilter('categories.id', $path)])
]
))
->addSorting(new FieldSorting('priority', FieldSorting::DESCENDING));
return new ListingBannerRouteResponse($this->bannerRepository->search($criteria, $context->getContext()));
}
protected function getPath(string $id, Context $context): ?string
{
return $this->categoryRepository->search(new Criteria([$id]), $context)->first()->getPath();
}
}