148 lines
5.0 KiB
PHP
148 lines
5.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\GraphQLBundle\ApiPublic\Util;
|
|
|
|
use KupShop\CatalogBundle\CategorySort;
|
|
use KupShop\CatalogBundle\Entity\Producer;
|
|
use KupShop\CatalogBundle\Entity\Section;
|
|
use KupShop\CatalogBundle\Enum\Ordering;
|
|
use KupShop\CatalogBundle\ProductList\Builder\FilteredProductListBuilder;
|
|
use KupShop\CatalogBundle\ProductList\Builder\FilteredProductListInterface;
|
|
use KupShop\CatalogBundle\Util\FilterUtil;
|
|
use KupShop\CatalogBundle\Util\IndexedFilterUtil;
|
|
use KupShop\CatalogBundle\Util\SectionViewUtil;
|
|
use KupShop\CatalogElasticBundle\Util\ElasticCompileStrategy;
|
|
use KupShop\ComponentsBundle\Twig\DataProvider\SectionDataProvider;
|
|
use KupShop\GraphQLBundle\ApiPublic\Types\Category\BaseFilter;
|
|
use KupShop\GraphQLBundle\ApiPublic\Types\Category\SectionFilter;
|
|
use KupShop\IndexedFilterBundle\Util\FilterUrlGenerator;
|
|
|
|
class SectionUtil
|
|
{
|
|
public function __construct(
|
|
private readonly SectionViewUtil $sectionViewUtil,
|
|
private readonly ?ElasticCompileStrategy $esStrategy,
|
|
private readonly ?SectionDataProvider $sectionDataProvider,
|
|
private readonly IndexedFilterUtil $indexedFilterUtil,
|
|
) {
|
|
}
|
|
|
|
public function getIndexedUrl(\FilterParams $baseFilterParams, \FilterParams $filterParams): string
|
|
{
|
|
$urlGenerator = new FilterUrlGenerator();
|
|
|
|
$filterParams = FilterUtil::diffWithBaseFilterParams(
|
|
$baseFilterParams,
|
|
$filterParams,
|
|
);
|
|
|
|
$sectionIds = $baseFilterParams->getSectionIdsFilter();
|
|
if (!$sectionIds) {
|
|
$sectionIds = $baseFilterParams->getSectionIds();
|
|
}
|
|
|
|
$urlGenerator->setSectionId($sectionIds ? $sectionIds[0] : 0);
|
|
$urlGenerator->setFilterParams($filterParams);
|
|
|
|
if ($baseFilterParams->getProducers()) {
|
|
$urlGenerator->setIgnoreProducer(true);
|
|
}
|
|
|
|
if ($parameters = $baseFilterParams->getParameters()) {
|
|
$urlGenerator->setParameters($parameters);
|
|
}
|
|
|
|
return $urlGenerator->generateUrl()['url'];
|
|
}
|
|
|
|
public function getTitle(BaseFilter $baseFilter, \FilterParams $filterParams, bool $useSeo): string
|
|
{
|
|
$section = $baseFilter->categoryId !== null
|
|
? $this->getSection($baseFilter->categoryId)
|
|
: null;
|
|
|
|
$producer = $baseFilter->producerId !== null
|
|
? $this->getProducer($baseFilter->producerId)
|
|
: null;
|
|
|
|
$filterConfig = $this->sectionViewUtil->getDynamicFilterConfiguration($section, $producer?->getId());
|
|
|
|
return $this->sectionViewUtil->createTitle($section, $producer, $filterParams, $filterConfig, $useSeo);
|
|
}
|
|
|
|
public function getDescription(BaseFilter $baseFilter): string
|
|
{
|
|
$section = $baseFilter->categoryId !== null
|
|
? $this->getSection($baseFilter->categoryId)
|
|
: null;
|
|
|
|
$producer = $baseFilter->producerId !== null
|
|
? $this->getProducer($baseFilter->producerId)
|
|
: null;
|
|
|
|
return $this->sectionViewUtil->createMetaDescription($section, $producer);
|
|
}
|
|
|
|
public function getFilteredSection(SectionFilter $filter, ?\FilterParams &$baseFilterParams = null): FilteredProductListInterface
|
|
{
|
|
$filterData = $filter->filterData;
|
|
$baseFilter = $filter->baseFilter;
|
|
$page = $filter->page;
|
|
$order = $filter->order;
|
|
|
|
$category = match ($baseFilter->categoryId) {
|
|
null => null,
|
|
default => $this->getSection($baseFilter->categoryId),
|
|
};
|
|
|
|
$filterParams = $baseFilter->toFilterParams($category?->displayProductsFromSubsections() ?? true);
|
|
$baseFilterParams = clone $filterParams;
|
|
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
$builder = (new FilteredProductListBuilder())
|
|
->productsAsResult()
|
|
->baseFilterParams($filterParams)
|
|
->paged($page, (int) $dbcfg->cat_show_products);
|
|
|
|
if ($filterData) {
|
|
$builder->dynamicFilter(json_decode($filterData, true));
|
|
}
|
|
|
|
if ($order !== null) {
|
|
$order = Ordering::fromString($order);
|
|
|
|
$builder->orderBy($order->orderBy, $order->orderDir);
|
|
} elseif ($category) {
|
|
$builder->categoryDefaultSort(CategorySort::fromCategory($category));
|
|
}
|
|
|
|
if ($category || $baseFilter->producerId !== null) {
|
|
$filterConfig = $this->sectionViewUtil->getDynamicFilterConfiguration($category, $baseFilter->producerId);
|
|
|
|
$builder->dynamicFilterAttributes($filterConfig);
|
|
}
|
|
|
|
$result = $builder->build($this->esStrategy);
|
|
|
|
return $result;
|
|
}
|
|
|
|
public function getIndexedFilter(int $categoryId, ?int $producerId, \FilterParams $filterParams): array
|
|
{
|
|
return $this->indexedFilterUtil->getIndexedFilter($categoryId, $producerId, $filterParams) ?: [];
|
|
}
|
|
|
|
private function getProducer(int $id): Producer
|
|
{
|
|
return $this->sectionDataProvider->getProducer($id);
|
|
}
|
|
|
|
public function getSection(int $id): Section
|
|
{
|
|
return $this->sectionDataProvider->getSection($id)->getObject();
|
|
}
|
|
}
|