Skip to content

Commit

Permalink
Merge branch '2.4' of https://github.com/sulu/SuluArticleBundle into 2.5
Browse files Browse the repository at this point in the history
 Conflicts:
	Resources/config/services_headless.xml
  • Loading branch information
alexander-schranz committed Dec 2, 2022
2 parents 9e8bbe7 + 5c258a8 commit 02e90f6
Show file tree
Hide file tree
Showing 17 changed files with 1,785 additions and 9 deletions.
41 changes: 33 additions & 8 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ public function cgetAction(Request $request): Response
$limit = (int) $this->restHelper->getLimit();
$page = (int) $this->restHelper->getPage();

/** @var array{
* authored?: array{
* from: string,
* to: string,
* },
* type?: string,
* contactId?: string,
* categoryId?: string,
* tagId?: string,
* pageId?: string,
* publishedState?: string,
* } $filter */
$filter = $request->query->all()['filter'] ?? [];

if (null !== $locale) {
$search->addQuery(new TermQuery('locale', $locale));
}
Expand All @@ -235,7 +249,7 @@ public function cgetAction(Request $request): Response
$search->addQuery($boolQuery);
}

if (null !== ($typeString = $request->get('types'))) {
if (null !== ($typeString = $request->get('types', $filter['type'] ?? null))) {
$types = \explode(',', $typeString);

if (\count($types) > 1) {
Expand All @@ -251,27 +265,27 @@ public function cgetAction(Request $request): Response
}
}

if ($contactId = $request->get('contactId')) {
if ($contactId = $request->get('contactId', $filter['contactId'] ?? null)) {
$boolQuery = new BoolQuery();
$boolQuery->add(new MatchQuery('changer_contact_id', $contactId), BoolQuery::SHOULD);
$boolQuery->add(new MatchQuery('creator_contact_id', $contactId), BoolQuery::SHOULD);
$boolQuery->add(new MatchQuery('author_id', $contactId), BoolQuery::SHOULD);
$search->addQuery($boolQuery);
}

if ($categoryId = $request->get('categoryId')) {
if ($categoryId = $request->get('categoryId', $filter['categoryId'] ?? null)) {
$search->addQuery(new TermQuery('excerpt.categories.id', $categoryId), BoolQuery::MUST);
}

if ($tagId = $request->get('tagId')) {
if ($tagId = $request->get('tagId', $filter['tagId'] ?? null)) {
$search->addQuery(new TermQuery('excerpt.tags.id', $tagId), BoolQuery::MUST);
}

if ($pageId = $request->get('pageId')) {
if ($pageId = $request->get('pageId', $filter['pageId'] ?? null)) {
$search->addQuery(new TermQuery('parent_page_uuid', $pageId), BoolQuery::MUST);
}

if ($workflowStage = $request->get('workflowStage')) {
if ($workflowStage = $request->get('workflowStage', $filter['publishedState'] ?? null)) {
$search->addQuery(new TermQuery('published_state', 'published' === $workflowStage), BoolQuery::MUST);
}

Expand All @@ -283,8 +297,8 @@ public function cgetAction(Request $request): Response
$search->addQuery(new TermQuery('localization_state.state', 'ghost'), BoolQuery::MUST_NOT);
}

$authoredFrom = $request->get('authoredFrom');
$authoredTo = $request->get('authoredTo');
$authoredFrom = $request->get('authoredFrom', $this->convertDateTime($filter['authored']['from'] ?? null));
$authoredTo = $request->get('authoredTo', $this->convertDateTime($filter['authored']['to'] ?? null));
if ($authoredFrom || $authoredTo) {
$search->addQuery($this->getRangeQuery('authored', $authoredFrom, $authoredTo), BoolQuery::MUST);
}
Expand Down Expand Up @@ -683,4 +697,15 @@ private function getSortFieldName(string $sortBy): ?string

return null;
}

private function convertDateTime(?string $dateTimeString): ?string
{
if (!$dateTimeString) {
return null;
}

$dateTime = new \DateTime($dateTimeString);

return $dateTime->format('Y-m-d');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Infrastructure\SuluHeadlessBundle\ContentTypeResolver;

use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\ContentTypeResolverInterface;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Bundle\HeadlessBundle\Content\StructureResolverInterface;
use Sulu\Component\Content\Compat\PropertyInterface;
use Sulu\Component\Content\Compat\PropertyParameter;
use Sulu\Component\Content\Mapper\ContentMapperInterface;
use Sulu\Component\Content\Query\ContentQueryBuilderInterface;

class ArticleSelectionResolver implements ContentTypeResolverInterface
{
public static function getContentType(): string
{
return 'article_selection';
}

/**
* @var StructureResolverInterface
*/
private $structureResolver;

/**
* @var ContentQueryBuilderInterface
*/
private $contentQueryBuilder;

/**
* @var ContentMapperInterface
*/
private $contentMapper;

/**
* @var bool
*/
private $showDrafts;

public function __construct(
StructureResolverInterface $structureResolver,
ContentQueryBuilderInterface $contentQueryBuilder,
ContentMapperInterface $contentMapper,
bool $showDrafts
) {
$this->structureResolver = $structureResolver;
$this->contentQueryBuilder = $contentQueryBuilder;
$this->contentMapper = $contentMapper;
$this->showDrafts = $showDrafts;
}

public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView
{
/** @var string[]|null $ids */
$ids = $data;

if (empty($ids)) {
return new ContentView([], ['ids' => []]);
}

/** @var PropertyParameter[] $params */
$params = $property->getParams();
/** @var PropertyParameter[] $propertiesParamValue */
$propertiesParamValue = isset($params['properties']) ? $params['properties']->getValue() : [];

$this->contentQueryBuilder->init([
'ids' => $ids,
'properties' => $propertiesParamValue,
'published' => !$this->showDrafts,
]);

/** @var array{string, mixed[]} $queryBuilderResult */
$queryBuilderResult = $this->contentQueryBuilder->build($property->getStructure()->getWebspaceKey(), [$locale]);
list($articlesQuery) = $queryBuilderResult;

$articleStructures = $this->contentMapper->loadBySql2(
$articlesQuery,
$locale,
$property->getStructure()->getWebspaceKey()
);

$propertyMap = [
'title' => 'title',
'routePath' => 'routePath',
];

foreach ($propertiesParamValue as $propertiesParamEntry) {
$paramName = $propertiesParamEntry->getName();
$paramValue = $propertiesParamEntry->getValue();
$propertyMap[$paramName] = \is_string($paramValue) ? $paramValue : $paramName;
}

$articles = \array_fill_keys($ids, null);

foreach ($articleStructures as $articleStructure) {
$articles[$articleStructure->getUuid()] = $this->structureResolver->resolveProperties($articleStructure, $propertyMap, $locale);
}

return new ContentView(\array_values(\array_filter($articles)), ['ids' => $ids]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Infrastructure\SuluHeadlessBundle\ContentTypeResolver;

use Sulu\Bundle\HeadlessBundle\Content\ContentTypeResolver\ContentTypeResolverInterface;
use Sulu\Bundle\HeadlessBundle\Content\ContentView;
use Sulu\Component\Content\Compat\PropertyInterface;

class SingleArticleSelectionResolver implements ContentTypeResolverInterface
{
public static function getContentType(): string
{
return 'single_article_selection';
}

/**
* @var ContentTypeResolverInterface
*/
private $articleSelectionResolver;

public function __construct(ContentTypeResolverInterface $articleSelectionResolver)
{
$this->articleSelectionResolver = $articleSelectionResolver;
}

public function resolve($data, PropertyInterface $property, string $locale, array $attributes = []): ContentView
{
/** @var string|null $id */
$id = $data;

if (empty($id)) {
return new ContentView(null, ['id' => null]);
}

$content = $this->articleSelectionResolver->resolve([$id], $property, $locale, $attributes);

/** @var mixed[]|null $contentData */
$contentData = $content->getContent();

return new ContentView($contentData[0] ?? null, ['id' => $id]);
}
}

0 comments on commit 02e90f6

Please sign in to comment.