Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add List filter configuration #632

Merged
merged 1 commit into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
}
}
117 changes: 117 additions & 0 deletions Metadata/ListMetadataVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?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;

/**
* @final
*
* @internal
*/
class ListMetadataVisitor implements ListMetadataVisitorInterface
wachterjohannes marked this conversation as resolved.
Show resolved Hide resolved
{
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'];
}
}
61 changes: 60 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,58 @@
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>
<property
name="publishedState"
visibility="never"
translation="sulu_article.published_state"
>
<filter type="select">
<params>
<param name="options" type="collection">
<param name="published" value="sulu_article.published" />
<param name="test" value="sulu_article.not_published" />
</param>
</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>
3 changes: 3 additions & 0 deletions Resources/translations/admin.de.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"sulu_article.additional_webspace": "Zusätzliche Webspaces",
"sulu_article.shadow_article": "Shadow Artikel",
"sulu_article.enable_shadow_article": "Shadow Artikel aktivieren",
"sulu_article.published_state": "Status der Veröffentlichung",
"sulu_article.published": "Veröffentlicht",
"sulu_article.not_published": "Nicht Veröffentlicht",
"sulu_activity.resource.articles": "Artikel",
"sulu_activity.resource.articles.translation": "Übersetzung",
"sulu_activity.description.articles.created": "{userFullName} hat den Artikel \"{resourceTitle}\" erstellt",
Expand Down
3 changes: 3 additions & 0 deletions Resources/translations/admin.en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"sulu_article.additional_webspace": "Additional webspaces",
"sulu_article.shadow_article": "Shadow Article",
"sulu_article.enable_shadow_article": "Enable Shadow Article",
"sulu_article.published_state": "Published State",
"sulu_article.published": "Published",
"sulu_article.not_published": "Not Published",
"sulu_activity.resource.articles": "Article",
"sulu_activity.resource.articles.translation": "Translation",
"sulu_activity.description.articles.created": "{userFullName} has created the article \"{resourceTitle}\"",
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"jangregor/phpstan-prophecy": "^1.0",
"massive/build-bundle": "^0.3 || ^0.4 || ^0.5",
"phpcr/phpcr-shell": "^1.1",
"phpspec/prophecy": "^1.15",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-doctrine": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
Expand Down