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 = [];
foreach ($data['accepted_socialissue'] as $i) {
$socialissues = $this->addParentIssues($data['accepted_socialissue']);
foreach ($socialissues as $i) {
if ('null' === $i) {
$issues[] = $this->translator->trans('Not given');
} else {
@ -71,7 +73,7 @@ class SocialIssueFilter implements FilterInterface
return [
'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 Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserJobFilter implements FilterInterface
@ -31,6 +32,8 @@ class UserJobFilter implements FilterInterface
*/
protected $translator;
private Security $security;
/**
* @var TranslatableStringHelper
*/
@ -38,27 +41,22 @@ class UserJobFilter implements FilterInterface
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
TranslatableStringHelper $translatableStringHelper,
Security $security
) {
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->security = $security;
}
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 [
'Filtered by user jobs: only %jobs%',
['%jobs%' => implode(', ', $jobs)],
'Filtered by user job: only %job%', [
'%job%' => $this->translatableStringHelper->localize(
$this->getUserJob()->getLabel()
)
],
];
}
@ -69,13 +67,8 @@ class UserJobFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$qb
->join('acp.user', 'u')
->join('u.userJob', 'j')
;
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('u.userJob', ':userjob');
$clause = $qb->expr()->eq('acp.job', ':userjob');
if ($where instanceof Andx) {
$where->add($clause);
@ -84,8 +77,7 @@ class UserJobFilter implements FilterInterface
}
$qb->add('where', $where);
$qb->setParameter('userjob', $data['accepted_userjob']);
$qb->setParameter('userjob', $this->getUserJob());
}
public function applyOn()
@ -95,20 +87,18 @@ class UserJobFilter implements FilterInterface
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()
{
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 Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
class UserScopeFilter implements FilterInterface
@ -30,6 +31,8 @@ class UserScopeFilter implements FilterInterface
*/
protected $translator;
private Security $security;
/**
* @var TranslatableStringHelper
*/
@ -37,27 +40,22 @@ class UserScopeFilter implements FilterInterface
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper
TranslatableStringHelper $translatableStringHelper,
Security $security
) {
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->security = $security;
}
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 [
'Filtered by user scopes: only %scopes%',
['%scopes%' => implode(', ', $scopes)],
'Filtered by user main scope: only %scope%', [
'%scope%' => $this->translatableStringHelper->localize(
$this->getUserMainScope()->getName()
)
]
];
}
@ -71,7 +69,7 @@ class UserScopeFilter implements FilterInterface
$qb->join('acp.scopes', 's');
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('s.id', ':userscope');
$clause = $qb->expr()->eq('s.id', ':userscope');
if ($where instanceof Andx) {
$where->add($clause);
@ -80,7 +78,7 @@ class UserScopeFilter implements FilterInterface
}
$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)
{
$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()
{
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%"
Filter by user scope: Filtrer par service du référent
Accepted userscope: Services
"Filtered by user scopes: only %scopes%": "Filtré par service du référent: uniquement %scopes%"
"Filtered by user main scope: only %scope%": "Filtré par service du référent: uniquement %scope%"
Filter by user job: Filtrer par métier du référent
Accepted userjob: Métiers
"Filtered by user jobs: only %jobs%": "Filtré par métier du référent: uniquement %jobs%"
"Filtered by user job: only %job%": "Filtré par métier du référent: uniquement %job%"
Filter by social issue: Filtrer par problématiques sociales
Accepted socialissue: Problématiques sociales