Skip to content

Commit

Permalink
add list filter configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Oct 25, 2022
1 parent 2978470 commit 0471120
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 7 deletions.
35 changes: 29 additions & 6 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,18 @@ 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,
* } $filter */
$filter = $request->get('filter');

if (null !== $locale) {
$search->addQuery(new TermQuery('locale', $locale));
}
Expand All @@ -235,7 +247,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,19 +263,19 @@ 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);
}

Expand All @@ -283,8 +295,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 +695,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');
}
}
112 changes: 112 additions & 0 deletions Metadata/ListMetadataVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

/*
* 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\Metadata;

use Sulu\Bundle\AdminBundle\Metadata\ListMetadata\ListMetadata;
use Sulu\Bundle\AdminBundle\Metadata\ListMetadata\ListMetadataVisitorInterface;
use Sulu\Component\Content\Compat\Structure\StructureBridge;
use Sulu\Component\Content\Compat\StructureManagerInterface;

class ListMetadataVisitor implements ListMetadataVisitorInterface
{
use StructureTagTrait;

/**
* @var StructureManagerInterface
*/
private $structureManager;

/**
* @var array<string, array{
* translation_key: string,
* }>
*/
private $articleTypeConfigurations;

/**
* @param array<string, array{
* translation_key: string,
* }> $articleTypeConfigurations
*/
public function __construct(StructureManagerInterface $structureManager, array $articleTypeConfigurations)
{
$this->structureManager = $structureManager;
$this->articleTypeConfigurations = $articleTypeConfigurations;
}

public function visitListMetadata(ListMetadata $listMetadata, string $key, string $locale, array $metadataOptions = []): void
{
if ('articles' !== $key) {
return;
}

$typeField = $listMetadata->getField('type');

$types = $this->getTypes();
if (1 === \count($types)) {
$typeField->setFilterType(null);
$typeField->setFilterTypeParameters(null);

return;
}

$options = [];
foreach ($types as $type) {
$options[$type['type']] = $type['title'];
}

$typeField->setFilterTypeParameters(['options' => $options]);
}

/**
* @return array<string, array{
* type: string,
* title: string,
* }>
*/
private function getTypes(): array
{
$types = [];

// prefill array with keys from configuration to keep order of configuration for tabs
foreach ($this->articleTypeConfigurations as $typeKey => $articleTypeConfiguration) {
$types[$typeKey] = [
'type' => $typeKey,
'title' => $this->getTitle($typeKey),
];
}

/** @var StructureBridge $structure */
foreach ($this->structureManager->getStructures('article') as $structure) {
/** @var string|null $type */
$type = $this->getType($structure->getStructure(), null);
$typeKey = $type ?: 'default';
if (empty($types[$typeKey])) {
$types[$typeKey] = [
'type' => $typeKey,
'title' => $this->getTitle($typeKey),
];
}
}

return $types;
}

private function getTitle(string $type): string
{
if (!\array_key_exists($type, $this->articleTypeConfigurations)) {
return \ucfirst($type);
}

return $this->articleTypeConfigurations[$type]['translation_key'];
}
}
47 changes: 46 additions & 1 deletion Resources/config/lists/articles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
visibility="no"
translation="sulu_admin.type"
/>
<property
name="type"
visibility="never"
translation="sulu_admin.type"
>
<filter type="select"/>
</property>
<property
name="title"
visibility="always"
Expand Down Expand Up @@ -51,6 +58,44 @@
type="date"
visibility="no"
translation="sulu_admin.authored"
/>
>
<filter type="date" />
</property>
<property
name="contactId"
visibility="never"
translation="sulu_contact.people"
>
<filter type="selection">
<params>
<param name="displayProperty" value="fullName" />
<param name="resourceKey" value="contacts" />
</params>
</filter>
</property>
<property
name="categoryId"
visibility="never"
translation="sulu_category.categories"
>
<filter type="selection">
<params>
<param name="displayProperty" value="name" />
<param name="resourceKey" value="categories" />
</params>
</filter>
</property>
<property
name="tagId"
visibility="never"
translation="sulu_tag.tags"
>
<filter type="selection">
<params>
<param name="displayProperty" value="name" />
<param name="resourceKey" value="tags" />
</params>
</filter>
</property>
</properties>
</list>
10 changes: 10 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -526,5 +526,15 @@

<tag name="sulu_document_manager.event_subscriber" />
</service>

<service
id="sulu_article.articles_list_metadata_visitor"
class="Sulu\Bundle\ArticleBundle\Metadata\ListMetadataVisitor"
>
<argument type="service" id="sulu.content.structure_manager"/>
<argument>%sulu_article.types%</argument>

<tag name="sulu_admin.list_metadata_visitor"/>
</service>
</services>
</container>

0 comments on commit 0471120

Please sign in to comment.