118 lines
3.8 KiB
PHP
118 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\GraphQLBundle\ApiAdmin\Util\Content;
|
|
|
|
use KupShop\ContentBundle\Util\ArticleList;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Types\Collection\ArticleCollection;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Article;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Input\ArticleFilterInput;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Input\ArticleTranslationInput;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Types\Content\Article\Response\ArticleTranslationMutateResponse;
|
|
use KupShop\GraphQLBundle\ApiAdmin\Types\Parameters;
|
|
use KupShop\GraphQLBundle\ApiShared\ApiUtil;
|
|
use KupShop\GraphQLBundle\Exception\GraphQLNotFoundException;
|
|
use KupShop\I18nBundle\Translations\ArticlesTranslation;
|
|
use KupShop\KupShopBundle\Config;
|
|
use KupShop\KupShopBundle\Util\ObjectUtil;
|
|
use Query\Operator;
|
|
|
|
class ArticleUtil
|
|
{
|
|
public function __construct(
|
|
private ArticleList $articleList,
|
|
private readonly ?ArticlesTranslation $articlesTranslation,
|
|
) {
|
|
}
|
|
|
|
public function getArticle(int $id): ?Article
|
|
{
|
|
$articles = $this->getArticleList()->getArticles(Operator::equals(['a.id' => $id]));
|
|
|
|
if (!($article = reset($articles))) {
|
|
throw new GraphQLNotFoundException('Article not found!');
|
|
}
|
|
|
|
return new Article($article);
|
|
}
|
|
|
|
public function getArticles(int $offset, int $limit, ?Parameters $sort = null, ?ArticleFilterInput $filter = null): ArticleCollection
|
|
{
|
|
$articleList = $this->getArticleList();
|
|
|
|
$specs = [
|
|
ApiUtil::getLimitSpec($offset, $limit),
|
|
ApiUtil::getSortSpec($sort, 'a'),
|
|
];
|
|
|
|
if ($filter?->id) {
|
|
$specs[] = Operator::inIntArray($filter->id, 'a.id');
|
|
}
|
|
|
|
if ($filter?->visible) {
|
|
$specs[] = Operator::equals(['a.figure' => $filter->visible ? 'Y' : 'N']);
|
|
}
|
|
|
|
$articles = $articleList->getArticles(
|
|
Operator::andX($specs)
|
|
);
|
|
|
|
return (new ArticleCollection(ApiUtil::wrapItems($articles, Article::class)))
|
|
->setLimit($limit)
|
|
->setOffset($offset)
|
|
->setItemsTotalCount($articleList->getTotalCount());
|
|
}
|
|
|
|
private function getArticleList(): ArticleList
|
|
{
|
|
$articleList = clone $this->articleList;
|
|
$articleList->setImage(Config::get()['Photo']['id_to_type'][12] ?? 1);
|
|
|
|
return $articleList;
|
|
}
|
|
|
|
public function updateArticleTranslations(ArticleTranslationInput $input): ArticleTranslationMutateResponse
|
|
{
|
|
ApiUtil::validateTranslationLanguage($input->language);
|
|
|
|
$this->getArticle($input->articleId);
|
|
|
|
$result = false;
|
|
$update = [];
|
|
|
|
if (ObjectUtil::isPropertyInitialized($input, 'title')) {
|
|
$update['title'] = $input->title;
|
|
}
|
|
if (ObjectUtil::isPropertyInitialized($input, 'leadIn')) {
|
|
$update['lead_in'] = $input->leadIn;
|
|
}
|
|
|
|
if (ObjectUtil::isPropertyInitialized($input, 'seo') && $input->seo) {
|
|
$seo = $input->seo->toArray();
|
|
if (!empty($seo['meta_keywords'])) {
|
|
$seo['keywords'] = $seo['meta_keywords'];
|
|
unset($seo['meta_keywords']);
|
|
}
|
|
$update = array_merge($update, $seo);
|
|
}
|
|
if (ObjectUtil::isPropertyInitialized($input, 'url')) {
|
|
$update['url'] = $input->url;
|
|
}
|
|
|
|
if (ObjectUtil::isPropertyInitialized($input, 'visibility')) {
|
|
$update['figure'] = $input->visibility ? $input->visibility->value : null;
|
|
}
|
|
|
|
if (!empty($update)) {
|
|
$result = $this->articlesTranslation->saveSingleObject(
|
|
$input->language,
|
|
$input->articleId,
|
|
$update
|
|
);
|
|
}
|
|
|
|
return new ArticleTranslationMutateResponse($result);
|
|
}
|
|
}
|