creator job filter

This commit is contained in:
Julie Lenaerts 2022-11-02 11:55:36 +01:00 committed by Julien Fastré
parent edd9aaa475
commit 2bdc06397e

View File

@ -11,13 +11,26 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters; namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Export\FilterInterface; use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Export\Declarations; use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use function in_array;
class CreatorJobFilter implements FilterInterface class CreatorJobFilter implements FilterInterface
{ {
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
TranslatableStringHelper $translatableStringHelper
) {
$this->translatableStringHelper = $translatableStringHelper;
}
public function addRole(): ?string public function addRole(): ?string
{ {
return null; return null;
@ -25,11 +38,21 @@ class CreatorJobFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
$qb if (!in_array('acp_creator', $qb->getAllAliases(), true)) {
->andWhere( $qb->join('acp.createdBy', 'acp_creator');
$qb->expr()->in('', ':') }
)
->setParameter('', $data[]); $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 public function applyOn(): string
@ -39,12 +62,30 @@ class CreatorJobFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder) 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 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),
]]; ]];
} }