Initial survey bundle import
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Mummert\SurveyBundle\Service;
|
||||
|
||||
use Contao\StringUtil;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Mummert\SurveyBundle\Repository\SurveyEditorRepository;
|
||||
use Mummert\SurveyBundle\Repository\SurveyRepository;
|
||||
|
||||
final class SurveyCategoryAssignmentService
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Connection $connection,
|
||||
private readonly SurveyRepository $surveyRepository,
|
||||
private readonly SurveyEditorRepository $surveyEditorRepository,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
public function getMemberCategoryIds(int $memberId): array
|
||||
{
|
||||
$value = $this->connection->fetchOne('SELECT surveyCategories FROM tl_member WHERE id = ?', [$memberId]);
|
||||
|
||||
return $this->normalizeCategoryIds(false === $value ? [] : $value);
|
||||
}
|
||||
|
||||
public function applyMemberCategoriesToSurvey(int $memberId, int $surveyId): void
|
||||
{
|
||||
if ($memberId <= 0 || $surveyId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->syncSurvey($surveyId, [
|
||||
$memberId => $this->getMemberCategoryIds($memberId),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<int>|null $categoryIds
|
||||
*/
|
||||
public function syncAssignedSurveys(int $memberId, ?array $categoryIds = null): void
|
||||
{
|
||||
if ($memberId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$normalizedCategoryIds = $this->normalizeCategoryIds($categoryIds ?? $this->getMemberCategoryIds($memberId));
|
||||
|
||||
foreach ($this->surveyEditorRepository->findSurveyIdsByMember($memberId) as $surveyId) {
|
||||
$this->syncSurvey($surveyId, [
|
||||
$memberId => $normalizedCategoryIds,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function syncSurvey(int $surveyId, array $memberCategoryOverrides = []): void
|
||||
{
|
||||
if ($surveyId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$categoryIds = [];
|
||||
|
||||
foreach ($this->surveyEditorRepository->findMemberIdsBySurvey($surveyId) as $memberId) {
|
||||
$memberCategoryIds = $memberCategoryOverrides[$memberId] ?? $this->getMemberCategoryIds($memberId);
|
||||
|
||||
foreach ($this->normalizeCategoryIds($memberCategoryIds) as $categoryId) {
|
||||
$categoryIds[] = $categoryId;
|
||||
}
|
||||
}
|
||||
|
||||
$this->surveyRepository->updateCategories($surveyId, $this->normalizeCategoryIds($categoryIds));
|
||||
}
|
||||
|
||||
public function syncSurveyWithoutMember(int $surveyId, int $memberId): void
|
||||
{
|
||||
if ($surveyId <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$categoryIds = [];
|
||||
|
||||
foreach ($this->surveyEditorRepository->findMemberIdsBySurvey($surveyId) as $assignedMemberId) {
|
||||
if ($assignedMemberId === $memberId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->getMemberCategoryIds($assignedMemberId) as $categoryId) {
|
||||
$categoryIds[] = $categoryId;
|
||||
}
|
||||
}
|
||||
|
||||
$this->surveyRepository->updateCategories($surveyId, $this->normalizeCategoryIds($categoryIds));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<int>
|
||||
*/
|
||||
public function normalizeCategoryIds(mixed $value): array
|
||||
{
|
||||
$categoryIds = is_array($value) ? $value : StringUtil::deserialize($value, true);
|
||||
$categoryIds = array_values(array_unique(array_filter(array_map('intval', $categoryIds), static fn (int $id): bool => $id > 0)));
|
||||
sort($categoryIds);
|
||||
|
||||
return $categoryIds;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user