diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php similarity index 55% rename from src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilter.php rename to src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php index 3accacf2c..c079b2bec 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/UserScopeFilter.php @@ -14,26 +14,30 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Entity\Scope; use Chill\MainBundle\Entity\User; use Chill\MainBundle\Export\FilterInterface; +use Chill\MainBundle\Repository\ScopeRepositoryInterface; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\PersonBundle\Export\Declarations; -use Doctrine\ORM\Query\Expr\Andx; use Doctrine\ORM\QueryBuilder; +use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Security\Core\Security; -use function in_array; -class CurrentUserScopeFilter implements FilterInterface +class UserScopeFilter implements FilterInterface { + private ScopeRepositoryInterface $scopeRepository; + private Security $security; private TranslatableStringHelper $translatableStringHelper; public function __construct( - TranslatableStringHelper $translatableStringHelper, - Security $security + ScopeRepositoryInterface $scopeRepository, + Security $security, + TranslatableStringHelper $translatableStringHelper ) { - $this->translatableStringHelper = $translatableStringHelper; + $this->scopeRepository = $scopeRepository; $this->security = $security; + $this->translatableStringHelper = $translatableStringHelper; } public function addRole(): ?string @@ -43,21 +47,14 @@ class CurrentUserScopeFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - if (!in_array('acpscope', $qb->getAllAliases(), true)) { - $qb->join('acp.scopes', 'acpscope'); + foreach ($data['scopes'] as $key => $scope) { + /** @var Scope $scope */ + $qb + ->andWhere( + $qb->expr()->isMemberOf(':acp_scope_filter_s_' . $key, 'acp.scopes') + ) + ->setParameter('acp_scope_filter_s_' . $key, $scope); } - - $where = $qb->getDQLPart('where'); - $clause = $qb->expr()->eq('acpscope.id', ':userscope'); - - if ($where instanceof Andx) { - $where->add($clause); - } else { - $where = $qb->expr()->andX($clause); - } - - $qb->add('where', $where); - $qb->setParameter('userscope', $this->getUserMainScope()); } public function applyOn() @@ -67,14 +64,25 @@ class CurrentUserScopeFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { + $builder->add('scopes', EntityType::class, [ + 'class' => Scope::class, + 'choices' => $this->scopeRepository->findAllActive(), + 'choice_label' => fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()), + 'multiple' => true, + 'expanded' => true, + ]); } public function describeAction($data, $format = 'string') { return [ 'Filtered by user main scope: only %scope%', [ - '%scope%' => $this->translatableStringHelper->localize( - $this->getUserMainScope()->getName() + '%scope%' => implode( + ', ', + array_map( + fn (Scope $s) => $this->translatableStringHelper->localize($s->getName()), + $data['scopes']->toArray() + ) ), ], ]; diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilterTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilterTest.php index c9353a317..9140d5c41 100644 --- a/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilterTest.php +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Filter/AccompanyingCourseFilters/CurrentUserScopeFilterTest.php @@ -12,7 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters; use Chill\MainBundle\Test\Export\AbstractFilterTest; -use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\CurrentUserScopeFilter; +use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\UserScopeFilter; use Doctrine\ORM\EntityManagerInterface; /** @@ -21,7 +21,7 @@ use Doctrine\ORM\EntityManagerInterface; */ final class CurrentUserScopeFilterTest extends AbstractFilterTest { - private CurrentUserScopeFilter $filter; + private UserScopeFilter $filter; protected function setUp(): void { diff --git a/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml b/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml index 88f6124ea..50fbf65d3 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml @@ -16,12 +16,11 @@ services: - { name: chill.export, alias: avg_accompanyingcourse_duration } ## Filters - chill.person.export.filter_current_userscope: - class: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\CurrentUserScopeFilter + Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\UserScopeFilter: autowire: true autoconfigure: true tags: - - { name: chill.export_filter, alias: accompanyingcourse_current_userscope_filter } + - { name: chill.export_filter, alias: accompanyingcourse_userscope_filter } chill.person.export.filter_current_userjob: class: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\CurrentUserJobFilter