exports: fix logic in userScope and userJob filter

This commit is contained in:
Mathieu Jaumotte 2022-07-25 11:51:23 +02:00
parent 97e8b3c9c1
commit bf0ca7b777
4 changed files with 48 additions and 62 deletions

View File

@ -61,7 +61,9 @@ class SocialIssueFilter implements FilterInterface
{ {
$issues = []; $issues = [];
foreach ($data['accepted_socialissue'] as $i) { $socialissues = $this->addParentIssues($data['accepted_socialissue']);
foreach ($socialissues as $i) {
if ('null' === $i) { if ('null' === $i) {
$issues[] = $this->translator->trans('Not given'); $issues[] = $this->translator->trans('Not given');
} else { } else {
@ -71,7 +73,7 @@ class SocialIssueFilter implements FilterInterface
return [ return [
'Filtered by socialissues: only %socialissues%', [ 'Filtered by socialissues: only %socialissues%', [
'%socialissues%' => implode(', ', $issues) '%socialissues%' => implode(', ou ', $issues)
]]; ]];
} }

View File

@ -22,6 +22,7 @@ use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 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\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
class UserJobFilter implements FilterInterface class UserJobFilter implements FilterInterface
@ -31,6 +32,8 @@ class UserJobFilter implements FilterInterface
*/ */
protected $translator; protected $translator;
private Security $security;
/** /**
* @var TranslatableStringHelper * @var TranslatableStringHelper
*/ */
@ -38,27 +41,22 @@ class UserJobFilter implements FilterInterface
public function __construct( public function __construct(
TranslatorInterface $translator, TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper TranslatableStringHelper $translatableStringHelper,
Security $security
) { ) {
$this->translator = $translator; $this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->security = $security;
} }
public function describeAction($data, $format = 'string') public function describeAction($data, $format = 'string')
{ {
$jobs = [];
foreach ($data['accepted_userjob'] as $j) {
if ('null' === $j) {
$jobs[] = $this->translator->trans('Not given');
} else {
$jobs[] = $this->translatableStringHelper->localize($j->getLabel());
}
}
return [ return [
'Filtered by user jobs: only %jobs%', 'Filtered by user job: only %job%', [
['%jobs%' => implode(', ', $jobs)], '%job%' => $this->translatableStringHelper->localize(
$this->getUserJob()->getLabel()
)
],
]; ];
} }
@ -69,13 +67,8 @@ class UserJobFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data) public function alterQuery(QueryBuilder $qb, $data)
{ {
$qb
->join('acp.user', 'u')
->join('u.userJob', 'j')
;
$where = $qb->getDQLPart('where'); $where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('u.userJob', ':userjob'); $clause = $qb->expr()->eq('acp.job', ':userjob');
if ($where instanceof Andx) { if ($where instanceof Andx) {
$where->add($clause); $where->add($clause);
@ -84,8 +77,7 @@ class UserJobFilter implements FilterInterface
} }
$qb->add('where', $where); $qb->add('where', $where);
$qb->setParameter('userjob', $data['accepted_userjob']); $qb->setParameter('userjob', $this->getUserJob());
} }
public function applyOn() public function applyOn()
@ -95,20 +87,18 @@ class UserJobFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder
->add('accepted_userjob', EntityType::class, [
'class' => UserJob::class,
'choice_label' => function(UserJob $j) {
return $this->translatableStringHelper->localize($j->getLabel());
},
'multiple' => true,
'expanded' => true,
])
;
} }
public function getTitle() public function getTitle()
{ {
return 'Filter by user job'; return 'Filter by user job';
} }
private function getUserJob():UserJob
{
/** @var User $user */
$user = $this->security->getUser();
return $user->getUserJob();
}
} }

View File

@ -21,6 +21,7 @@ use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 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\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
class UserScopeFilter implements FilterInterface class UserScopeFilter implements FilterInterface
@ -30,6 +31,8 @@ class UserScopeFilter implements FilterInterface
*/ */
protected $translator; protected $translator;
private Security $security;
/** /**
* @var TranslatableStringHelper * @var TranslatableStringHelper
*/ */
@ -37,27 +40,22 @@ class UserScopeFilter implements FilterInterface
public function __construct( public function __construct(
TranslatorInterface $translator, TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper TranslatableStringHelper $translatableStringHelper,
Security $security
) { ) {
$this->translator = $translator; $this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->security = $security;
} }
public function describeAction($data, $format = 'string') public function describeAction($data, $format = 'string')
{ {
$scopes = [];
foreach ($data['accepted_userscope'] as $s) {
if ('null' === $s) {
$scopes[] = $this->translator->trans('Not given');
} else {
$scopes[] = $this->translatableStringHelper->localize($s->getName());
}
}
return [ return [
'Filtered by user scopes: only %scopes%', 'Filtered by user main scope: only %scope%', [
['%scopes%' => implode(', ', $scopes)], '%scope%' => $this->translatableStringHelper->localize(
$this->getUserMainScope()->getName()
)
]
]; ];
} }
@ -71,7 +69,7 @@ class UserScopeFilter implements FilterInterface
$qb->join('acp.scopes', 's'); $qb->join('acp.scopes', 's');
$where = $qb->getDQLPart('where'); $where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('s.id', ':userscope'); $clause = $qb->expr()->eq('s.id', ':userscope');
if ($where instanceof Andx) { if ($where instanceof Andx) {
$where->add($clause); $where->add($clause);
@ -80,7 +78,7 @@ class UserScopeFilter implements FilterInterface
} }
$qb->add('where', $where); $qb->add('where', $where);
$qb->setParameter('userscope', $data['accepted_userscope']); $qb->setParameter('userscope', $this->getUserMainScope());
} }
@ -91,20 +89,18 @@ class UserScopeFilter implements FilterInterface
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder
->add('accepted_userscope', EntityType::class, [
'class' => Scope::class,
'choice_label' => function(Scope $s) {
return $this->translatableStringHelper->localize($s->getName());
},
'multiple' => true,
'expanded' => true,
])
;
} }
public function getTitle() public function getTitle()
{ {
return 'Filter by user scope'; return 'Filter by user scope';
} }
private function getUserMainScope():Scope
{
/** @var User $user */
$user = $this->security->getUser();
return $user->getMainScope();
}
} }

View File

@ -377,12 +377,10 @@ Having an accompanying period closed after this date: Ayant une période d'accom
"Filtered by accompanying period: persons having an accompanying period closed between the %date_from% and %date_to%": "Filtrer par période d'accompagnement: ayant une période fermée entre le %date_from% et le %date_to%" "Filtered by accompanying period: persons having an accompanying period closed between the %date_from% and %date_to%": "Filtrer par période d'accompagnement: ayant une période fermée entre le %date_from% et le %date_to%"
Filter by user scope: Filtrer par service du référent Filter by user scope: Filtrer par service du référent
Accepted userscope: Services "Filtered by user main scope: only %scope%": "Filtré par service du référent: uniquement %scope%"
"Filtered by user scopes: only %scopes%": "Filtré par service du référent: uniquement %scopes%"
Filter by user job: Filtrer par métier du référent Filter by user job: Filtrer par métier du référent
Accepted userjob: Métiers "Filtered by user job: only %job%": "Filtré par métier du référent: uniquement %job%"
"Filtered by user jobs: only %jobs%": "Filtré par métier du référent: uniquement %jobs%"
Filter by social issue: Filtrer par problématiques sociales Filter by social issue: Filtrer par problématiques sociales
Accepted socialissue: Problématiques sociales Accepted socialissue: Problématiques sociales