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

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

View File

@ -14,9 +14,8 @@ namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\UserJob; use Chill\MainBundle\Entity\UserJob;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
class UserJobRepository implements ObjectRepository class UserJobRepository implements UserJobRepositoryInterface
{ {
private EntityRepository $repository; private EntityRepository $repository;
@ -38,6 +37,11 @@ class UserJobRepository implements ObjectRepository
return $this->repository->findAll(); return $this->repository->findAll();
} }
public function findAllActive(): array
{
return $this->repository->findBy(['active' => true]);
}
/** /**
* @param mixed|null $limit * @param mixed|null $limit
* @param mixed|null $offset * @param mixed|null $offset
@ -49,12 +53,12 @@ class UserJobRepository implements ObjectRepository
return $this->repository->findBy($criteria, $orderBy, $limit, $offset); return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
} }
public function findOneBy(array $criteria) public function findOneBy(array $criteria): ?UserJob
{ {
return $this->repository->findOneBy($criteria); return $this->repository->findOneBy($criteria);
} }
public function getClassName() public function getClassName(): string
{ {
return UserJob::class; return UserJob::class;
} }

View File

@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Repository;
use Chill\MainBundle\Entity\UserJob;
use Doctrine\Persistence\ObjectRepository;
interface UserJobRepositoryInterface extends ObjectRepository
{
public function find($id): ?UserJob;
/**
* @return array|UserJob[]
*/
public function findAll(): array;
/**
* @return array|UserJob[]
*/
public function findAllActive(): array;
/**
* @param mixed|null $limit
* @param mixed|null $offset
*
* @return array|object[]|UserJob[]
*/
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null);
public function findOneBy(array $criteria): ?UserJob;
public function getClassName(): string;
}

View File

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

View File

@ -12,7 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters; namespace Chill\PersonBundle\Tests\Export\Filter\AccompanyingCourseFilters;
use Chill\MainBundle\Test\Export\AbstractFilterTest; use Chill\MainBundle\Test\Export\AbstractFilterTest;
use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\CurrentUserJobFilter; use Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\UserJobFilter;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
/** /**
@ -21,7 +21,7 @@ use Doctrine\ORM\EntityManagerInterface;
*/ */
final class CurrentUserJobFilterTest extends AbstractFilterTest final class CurrentUserJobFilterTest extends AbstractFilterTest
{ {
private CurrentUserJobFilter $filter; private UserJobFilter $filter;
protected function setUp(): void protected function setUp(): void
{ {

View File

@ -22,12 +22,11 @@ services:
tags: tags:
- { name: chill.export_filter, alias: accompanyingcourse_userscope_filter } - { name: chill.export_filter, alias: accompanyingcourse_userscope_filter }
chill.person.export.filter_current_userjob: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\UserJobFilter:
class: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\CurrentUserJobFilter
autowire: true autowire: true
autoconfigure: true autoconfigure: true
tags: tags:
- { name: chill.export_filter, alias: accompanyingcourse_current_userjob_filter } - { name: chill.export_filter, alias: accompanyingcourse_userjob_filter }
chill.person.export.filter_socialissue: chill.person.export.filter_socialissue:
class: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\SocialIssueFilter class: Chill\PersonBundle\Export\Filter\AccompanyingCourseFilters\SocialIssueFilter