From 0471120cdc612edae997e454db97817eb71bac5c Mon Sep 17 00:00:00 2001 From: Johannes Wachter Date: Tue, 25 Oct 2022 11:43:19 +0200 Subject: [PATCH] add list filter configuration --- Controller/ArticleController.php | 35 +++++++-- Metadata/ListMetadataVisitor.php | 112 ++++++++++++++++++++++++++++ Resources/config/lists/articles.xml | 47 +++++++++++- Resources/config/services.xml | 10 +++ 4 files changed, 197 insertions(+), 7 deletions(-) create mode 100644 Metadata/ListMetadataVisitor.php diff --git a/Controller/ArticleController.php b/Controller/ArticleController.php index efd5834ad..56e8b416b 100644 --- a/Controller/ArticleController.php +++ b/Controller/ArticleController.php @@ -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)); } @@ -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) { @@ -251,7 +263,7 @@ 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); @@ -259,11 +271,11 @@ public function cgetAction(Request $request): Response $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); } @@ -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); } @@ -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'); + } } diff --git a/Metadata/ListMetadataVisitor.php b/Metadata/ListMetadataVisitor.php new file mode 100644 index 000000000..8533b8891 --- /dev/null +++ b/Metadata/ListMetadataVisitor.php @@ -0,0 +1,112 @@ + + */ + private $articleTypeConfigurations; + + /** + * @param array $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 + */ + 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']; + } +} diff --git a/Resources/config/lists/articles.xml b/Resources/config/lists/articles.xml index ad1676bb8..0718d6c3f 100644 --- a/Resources/config/lists/articles.xml +++ b/Resources/config/lists/articles.xml @@ -14,6 +14,13 @@ visibility="no" translation="sulu_admin.type" /> + + + + > + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 07065a0e4..ef46fdaf9 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -526,5 +526,15 @@ + + + + %sulu_article.types% + + +