From 2bdc06397e20bcd578ab9c583339099267ae8227 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 2 Nov 2022 11:55:36 +0100 Subject: [PATCH] creator job filter --- .../CreatorJobFilter.php | 55 ++++++++++++++++--- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CreatorJobFilter.php b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CreatorJobFilter.php index 6f094fe97..71fe3250f 100644 --- a/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CreatorJobFilter.php +++ b/src/Bundle/ChillPersonBundle/Export/Filter/AccompanyingCourseFilters/CreatorJobFilter.php @@ -11,13 +11,26 @@ declare(strict_types=1); namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; +use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Export\FilterInterface; +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 function in_array; class CreatorJobFilter implements FilterInterface { + private TranslatableStringHelper $translatableStringHelper; + + public function __construct( + TranslatableStringHelper $translatableStringHelper + ) { + $this->translatableStringHelper = $translatableStringHelper; + } + public function addRole(): ?string { return null; @@ -25,11 +38,21 @@ class CreatorJobFilter implements FilterInterface public function alterQuery(QueryBuilder $qb, $data) { - $qb - ->andWhere( - $qb->expr()->in('', ':') - ) - ->setParameter('', $data[]); + if (!in_array('acp_creator', $qb->getAllAliases(), true)) { + $qb->join('acp.createdBy', 'acp_creator'); + } + + $where = $qb->getDQLPart('where'); + $clause = $qb->expr()->in('acp_creator.userJob', ':creator_job'); + + if ($where instanceof Andx) { + $where->add($clause); + } else { + $where = $qb->expr()->andX($clause); + } + + $qb->add('where', $where); + $qb->setParameter('creator_job', $data['creator_job']); } public function applyOn(): string @@ -39,12 +62,30 @@ class CreatorJobFilter implements FilterInterface public function buildForm(FormBuilderInterface $builder) { - $builder->add(); + $builder->add('creator_job', EntityType::class, [ + 'class' => UserJob::class, + 'choice_label' => function (UserJob $j) { + return $this->translatableStringHelper->localize( + $j->getLabel() + ); + }, + 'multiple' => true, + 'expanded' => true, + ]); } public function describeAction($data, $format = 'string'): array { - return ['', [ + $creatorJobs = []; + + foreach ($data['creator_job'] as $j) { + $creatorJobs[] = $this->translatableStringHelper->localize( + $j->getLabel() + ); + } + + return ['Filtered by creator job: only %jobs%', [ + '%jobs%' => implode(', ', $creatorJobs), ]]; }