95 lines
3.3 KiB
PHP
95 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace KupShop\GraphQLBundle\ApiPublic\Controller;
|
|
|
|
use GraphQL\Type\Definition\ResolveInfo;
|
|
use KupShop\ComponentsBundle\Twig\DataProvider\ProductDataProvider;
|
|
use KupShop\ComponentsBundle\Utils\ComponentRenderer;
|
|
use KupShop\ContentBundle\Util\Block;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Annotation\Module;
|
|
use KupShop\GraphQLBundle\ApiShared\Util\ProductListUtil;
|
|
use KupShop\KupShopBundle\Context\UserContext;
|
|
use KupShop\KupShopBundle\Util\Contexts;
|
|
use KupShop\RestrictionsBundle\Utils\Restrictions;
|
|
use Query\QueryBuilder;
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
use TheCodingMachine\GraphQLite\Annotations\Autowire;
|
|
use TheCodingMachine\GraphQLite\Annotations\Query;
|
|
use TheCodingMachine\GraphQLite\Annotations\UseInputType;
|
|
|
|
readonly class BlockController
|
|
{
|
|
public function __construct(
|
|
private Block $blockUtil,
|
|
private ProductListUtil $productListUtil,
|
|
private ?ProductDataProvider $productDataProvider,
|
|
private readonly ?Restrictions $restrictions,
|
|
) {
|
|
}
|
|
|
|
#[Query]
|
|
#[Module(\Modules::COMPONENTS)]
|
|
public function productsBlock(
|
|
ResolveInfo $resolveInfo,
|
|
#[Autowire] ComponentRenderer $renderer,
|
|
#[UseInputType('String')] $data,
|
|
): string {
|
|
if (!$data) {
|
|
throw new NotFoundHttpException('Empty data');
|
|
}
|
|
|
|
$blocekData = json_decode_strict($data, true);
|
|
|
|
$productsList = $this->productListUtil->getProductList();
|
|
|
|
$productsList->andSpec($this->blockUtil->getProductsBlockSpecs($blocekData));
|
|
$productsList->andSpec(function (QueryBuilder $qb) use ($blocekData) {
|
|
if ($blocekData['orderBy'] ?? false) {
|
|
$qb->orderBySql($blocekData['orderBy']);
|
|
}
|
|
if ($blocekData['count'] ?? false) {
|
|
$qb->setMaxResults($blocekData['count']);
|
|
}
|
|
});
|
|
|
|
if (findModule(\Modules::RESTRICTIONS)) {
|
|
$productsList->andSpec($this->restrictions->getRestrictionSpec());
|
|
}
|
|
|
|
$productsCollection = $productsList->getProducts();
|
|
$productsCollection->fetchProductsPhotoIds();
|
|
|
|
$retailType = Contexts::get(UserContext::class)->getRetailPriceType();
|
|
$retailType->prefetchProductCollection($productsCollection);
|
|
$originalType = Contexts::get(UserContext::class)->getOriginalPriceType();
|
|
$originalType->prefetchProductCollection($productsCollection);
|
|
|
|
$this->productDataProvider->addProducts($productsCollection);
|
|
|
|
return $renderer->renderToString('BlocekProducts', [
|
|
'productList' => $productsCollection,
|
|
'itemType' => 'square',
|
|
]);
|
|
}
|
|
|
|
#[Query]
|
|
#[Module(\Modules::COMPONENTS)]
|
|
public function componentBlock(ResolveInfo $resolveInfo, #[Autowire] ComponentRenderer $renderer, #[UseInputType('String')] $data): string
|
|
{
|
|
if (!$data) {
|
|
throw new NotFoundHttpException('Empty data');
|
|
}
|
|
$blocekData = json_decode_strict(html_entity_decode($data), true);
|
|
|
|
try {
|
|
return $renderer->renderToString($blocekData['name'], $blocekData['params'] ?? []);
|
|
} catch (\Exception $e) {
|
|
if (isDevelopment()) {
|
|
return $e->getMessage();
|
|
}
|
|
|
|
return '';
|
|
}
|
|
}
|
|
}
|