Feature: allow to filter courses by user's job #export #acp

This commit is contained in:
2022-10-10 21:22:22 +02:00
parent 2f77deaa60
commit aba3b33fd0
5 changed files with 82 additions and 26 deletions

View File

@@ -14,25 +14,30 @@ namespace Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Entity\UserJob;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Repository\UserJobRepositoryInterface;
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;
class CurrentUserJobFilter implements FilterInterface
class UserJobFilter implements FilterInterface
{
private Security $security;
private TranslatableStringHelper $translatableStringHelper;
private UserJobRepositoryInterface $userJobRepository;
public function __construct(
Security $security,
TranslatableStringHelper $translatableStringHelper,
Security $security
UserJobRepositoryInterface $userJobRepository
) {
$this->translatableStringHelper = $translatableStringHelper;
$this->security = $security;
$this->translatableStringHelper = $translatableStringHelper;
$this->userJobRepository = $userJobRepository;
}
public function addRole(): ?string
@@ -42,17 +47,11 @@ class CurrentUserJobFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->eq('acp.job', ':userjob');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('userjob', $this->getUserJob());
$qb
->andWhere(
$qb->expr()->in('acp.job', ':acp_user_job_filter_j')
)
->setParameter('acp_user_job_filter_j', $data['jobs']);
}
public function applyOn()
@@ -62,14 +61,26 @@ class CurrentUserJobFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('jobs', EntityType::class, [
'class' => UserJob::class,
'choices' => $this->userJobRepository->findAllActive(),
'multiple' => true,
'expanded' => true,
'choice_label' => fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()),
'label' => 'Job',
]);
}
public function describeAction($data, $format = 'string')
{
return [
'Filtered by user job: only %job%', [
'%job%' => $this->translatableStringHelper->localize(
$this->getUserJob()->getLabel()
'%job%' => implode(
', ',
array_map(
fn (UserJob $job) => $this->translatableStringHelper->localize($job->getLabel()),
$data['jobs']->toArray()
)
),
],
];