Skip to content

Commit

Permalink
Add audience targeting groups to data provider for smart contents (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlagler authored Feb 27, 2024
1 parent 54c3092 commit b3a6098
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion Content/ArticleDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ class ArticleDataProvider implements DataProviderInterface, DataProviderAliasInt
*/
private $tokenStorage;

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

public function __construct(
Manager $searchManager,
DocumentManagerInterface $documentManager,
Expand All @@ -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;
Expand All @@ -105,6 +111,7 @@ public function __construct(
$this->defaultLimit = $defaultLimit;
$this->formMetadataProvider = $formMetadataProvider;
$this->tokenStorage = $tokenStorage;
$this->hasAudienceTargeting = $hasAudienceTargeting;
}

public function getConfiguration()
Expand Down Expand Up @@ -137,6 +144,10 @@ protected function getConfigurationBuilder(): BuilderInterface
$builder->enableTypes($this->getTypes());
}

if ($this->hasAudienceTargeting) {
$builder->enableAudienceTargeting();
}

return $builder;
}

Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
7 changes: 7 additions & 0 deletions Document/ExcerptViewObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,13 @@ class ExcerptViewObject
*/
public $segments;

/**
* @var int[]
*
* @Property(type="integer")
*/
public $audienceTargetingGroups;

/**
* @var MediaViewObject[]|Collection
*
Expand Down
1 change: 1 addition & 0 deletions Document/Index/Factory/ExcerptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion Document/Subscriber/ArticlePageSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,12 @@ public function setWorkflowStageOnArticle($event): void
}

$document->getParent()->setWorkflowStage(WorkflowStage::TEST);
/** @var array<string, mixed> $options */
$options = $event instanceof PersistEvent ? $event->getOptions() : [];
$this->documentManager->persist(
$document->getParent(),
$this->documentInspector->getLocale($document),
$event instanceof PersistEvent ? $event->getOptions() : []
$options
);
}

Expand Down
1 change: 1 addition & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@
<argument type="string">%sulu_article.smart_content.default_limit%</argument>
<argument type="service" id="sulu_admin.form_metadata_provider" on-invalid="null"/>
<argument type="service" id="security.token_storage" on-invalid="null"/>
<argument type="expression">container.hasParameter('sulu_audience_targeting.enabled')</argument>

<tag name="sulu.smart_content.data_provider" alias="articles"/>
</service>
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b3a6098

Please sign in to comment.