36 lines
1.1 KiB
PHP
36 lines
1.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\GraphQLBundle\ApiPublic\Util;
|
|
|
|
use KupShop\ContentBundle\Page\FragmentPage;
|
|
use KupShop\GraphQLBundle\ApiPublic\Types\Content\Fragment\Fragment;
|
|
use KupShop\GraphQLBundle\Exception\GraphQLNotFoundException;
|
|
use Query\Operator;
|
|
|
|
class FragmentUtil
|
|
{
|
|
public function getFragment(?string $code, ?int $id): Fragment
|
|
{
|
|
$qb = sqlQueryBuilder()->select('*,b.name as block_name,b.id as block_id, GROUP_CONCAT(b.content ORDER BY b.position ASC SEPARATOR "") as content')
|
|
->from('pages', 'p')
|
|
->leftJoin('p', 'blocks', 'b', 'b.id_root = p.id_block')
|
|
->andWhere(Operator::equals(['type' => FragmentPage::getType()]))
|
|
->groupBy('p.id');
|
|
|
|
if ($code) {
|
|
$qb->andWhere(Operator::equals(['p.code' => $code]));
|
|
}
|
|
if ($id) {
|
|
$qb->andWhere(Operator::equals(['p.id' => $id]));
|
|
}
|
|
|
|
if (!($fragment = $qb->execute()->fetchAssociative())) {
|
|
throw new GraphQLNotFoundException('Fragment not found');
|
|
}
|
|
|
|
return new Fragment($fragment);
|
|
}
|
|
}
|