From b3a6098b99b7dbdadf9d5ce11fbf95bbcf90506d Mon Sep 17 00:00:00 2001 From: martinlagler Date: Tue, 27 Feb 2024 14:41:18 +0100 Subject: [PATCH] Add audience targeting groups to data provider for smart contents (#663) --- .github/workflows/test-application.yaml | 2 +- Content/ArticleDataProvider.php | 21 ++++++++++++++++++- Controller/ArticleController.php | 1 + Document/ExcerptViewObject.php | 7 +++++++ Document/Index/Factory/ExcerptFactory.php | 1 + Document/Subscriber/ArticlePageSubscriber.php | 4 +++- Resources/config/services.xml | 1 + phpstan-baseline.neon | 5 ----- 8 files changed, 34 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test-application.yaml b/.github/workflows/test-application.yaml index 2290cb30b..32033a84f 100644 --- a/.github/workflows/test-application.yaml +++ b/.github/workflows/test-application.yaml @@ -92,7 +92,7 @@ jobs: options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=5 jackrabbit: - image: sulu/jackrabbit:2.20-standalone + image: sulu/jackrabbit:2.20-tomcat-filesystem env: DATABASE_HOST: mysql DATABASE_PORT: 3306 diff --git a/Content/ArticleDataProvider.php b/Content/ArticleDataProvider.php index 4ae5b546c..eb1ed07d4 100644 --- a/Content/ArticleDataProvider.php +++ b/Content/ArticleDataProvider.php @@ -85,6 +85,11 @@ class ArticleDataProvider implements DataProviderInterface, DataProviderAliasInt */ private $tokenStorage; + /** + * @var bool + */ + private $hasAudienceTargeting; + public function __construct( Manager $searchManager, DocumentManagerInterface $documentManager, @@ -94,7 +99,8 @@ public function __construct( string $articleDocumentClass, int $defaultLimit, MetadataProviderInterface $formMetadataProvider = null, - TokenStorageInterface $tokenStorage = null + TokenStorageInterface $tokenStorage = null, + bool $hasAudienceTargeting = false ) { $this->searchManager = $searchManager; $this->documentManager = $documentManager; @@ -105,6 +111,7 @@ public function __construct( $this->defaultLimit = $defaultLimit; $this->formMetadataProvider = $formMetadataProvider; $this->tokenStorage = $tokenStorage; + $this->hasAudienceTargeting = $hasAudienceTargeting; } public function getConfiguration() @@ -137,6 +144,10 @@ protected function getConfigurationBuilder(): BuilderInterface $builder->enableTypes($this->getTypes()); } + if ($this->hasAudienceTargeting) { + $builder->enableAudienceTargeting(); + } + return $builder; } @@ -298,6 +309,14 @@ private function getSearchResult(array $filters, ?int $limit, int $page, ?int $p $search->addQuery($segmentQuery); } + $targetGroup = $filters['targetGroupId'] ?? null; + + if ($targetGroup) { + $targetGroupQuery = new BoolQuery(); + $targetGroupQuery->add(new TermQuery('excerpt.audience_targeting_groups', $targetGroup), BoolQuery::MUST); + $search->addQuery($targetGroupQuery); + } + return $repository->findDocuments($search); } diff --git a/Controller/ArticleController.php b/Controller/ArticleController.php index 806edcbcf..3516a17c4 100644 --- a/Controller/ArticleController.php +++ b/Controller/ArticleController.php @@ -605,6 +605,7 @@ public function postTriggerAction(string $id, Request $request): Response case 'copy': /** @var ArticleDocument $document */ $document = $this->documentManager->find($id, $locale); + /** @var string $copiedPath */ $copiedPath = $this->documentManager->copy($document, \dirname($document->getPath())); $this->documentManager->flush(); diff --git a/Document/ExcerptViewObject.php b/Document/ExcerptViewObject.php index edef0e341..299994404 100644 --- a/Document/ExcerptViewObject.php +++ b/Document/ExcerptViewObject.php @@ -89,6 +89,13 @@ class ExcerptViewObject */ public $segments; + /** + * @var int[] + * + * @Property(type="integer") + */ + public $audienceTargetingGroups; + /** * @var MediaViewObject[]|Collection * diff --git a/Document/Index/Factory/ExcerptFactory.php b/Document/Index/Factory/ExcerptFactory.php index 9780b1bfc..99fbbeaf5 100644 --- a/Document/Index/Factory/ExcerptFactory.php +++ b/Document/Index/Factory/ExcerptFactory.php @@ -70,6 +70,7 @@ public function create(array $data, string $locale): ExcerptViewObject $excerpt->tags = $this->tagCollectionFactory->create($data['tags']); $excerpt->categories = $this->categoryCollectionFactory->create($data['categories'], $locale); $excerpt->segments = $this->segmentCollectionFactory->create($data['segments'] ?? []); + $excerpt->audienceTargetingGroups = $data['audience_targeting_groups'] ?? []; $excerpt->icon = $this->mediaCollectionFactory->create($data['icon'] ?? [], $locale); $excerpt->images = $this->mediaCollectionFactory->create($data['images'] ?? [], $locale); diff --git a/Document/Subscriber/ArticlePageSubscriber.php b/Document/Subscriber/ArticlePageSubscriber.php index 385dc62e7..265854b5b 100644 --- a/Document/Subscriber/ArticlePageSubscriber.php +++ b/Document/Subscriber/ArticlePageSubscriber.php @@ -158,10 +158,12 @@ public function setWorkflowStageOnArticle($event): void } $document->getParent()->setWorkflowStage(WorkflowStage::TEST); + /** @var array $options */ + $options = $event instanceof PersistEvent ? $event->getOptions() : []; $this->documentManager->persist( $document->getParent(), $this->documentInspector->getLocale($document), - $event instanceof PersistEvent ? $event->getOptions() : [] + $options ); } diff --git a/Resources/config/services.xml b/Resources/config/services.xml index 959dce0f1..33199f268 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -341,6 +341,7 @@ %sulu_article.smart_content.default_limit% + container.hasParameter('sulu_audience_targeting.enabled') diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 0768ecb6f..d064cd2b8 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1580,11 +1580,6 @@ parameters: count: 1 path: Document/Subscriber/ArticlePageSubscriber.php - - - message: "#^Parameter \\#3 \\$options of method Sulu\\\\Component\\\\DocumentManager\\\\DocumentManagerInterface\\:\\:persist\\(\\) expects array, array\\|Symfony\\\\Component\\\\OptionsResolver\\\\OptionsResolver given\\.$#" - count: 1 - path: Document/Subscriber/ArticlePageSubscriber.php - - message: "#^Right side of \\|\\| is always true\\.$#" count: 1