export new ActivityTypeFilter

This commit is contained in:
Mathieu Jaumotte 2022-08-01 15:45:31 +02:00
parent bc2209319a
commit 4794e5e7b5
3 changed files with 122 additions and 0 deletions

View File

@ -0,0 +1,111 @@
<?php
namespace Chill\PersonBundle\Export\Filter;
use Chill\ActivityBundle\Entity\Activity;
use Chill\ActivityBundle\Entity\ActivityType;
use Chill\MainBundle\Export\FilterInterface;
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;
/**
* TODO merge with ActivityTypeFilter in ChillActivity (!?)
*/
class ActivityTypeFilter implements FilterInterface
{
private TranslatableStringHelper $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper)
{
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_activitytypes', EntityType::class, [
'class' => ActivityType::class,
'choice_label' => function (ActivityType $aty) {
return $this->translatableStringHelper->localize($aty->getName());
},
'multiple' => true,
'expanded' => true
]);
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return 'Filter by activity type';
}
/**
* @inheritDoc
*/
public function describeAction($data, $format = 'string'): array
{
$types = [];
foreach ($data['accepted_activitytypes'] as $aty) {
$types[] = $this->translatableStringHelper->localize($aty->getName());
}
return ['Filtered by activity types: only %activitytypes%', [
'%activitytypes%' => implode(", ou ", $types)
]];
}
/**
* @inheritDoc
*/
public function addRole()
{
return null;
}
/**
* @inheritDoc
*/
public function alterQuery(QueryBuilder $qb, $data)
{
// One2many between activity and accompanyingperiod is not reversed !
// we replace indicator 'from' clause by 'act', and put 'acp' in a join
$qb->resetDQLPart('from');
$qb->from('ChillActivityBundle:Activity', 'act');
$qb
->join('act.accompanyingPeriod', 'acp')
->join('act.activityType', 'aty')
;
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('aty.id', ':activitytypes');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('activitytypes', $data['accepted_activitytypes']);
}
/**
* @inheritDoc
*/
public function applyOn()
{
return Declarations::ACP_TYPE;
}
}

View File

@ -74,6 +74,13 @@ services:
tags:
- { name: chill.export_filter, alias: accompanyingcourse_evaluation_filter }
chill.person.export.filter_activitytype:
class: Chill\PersonBundle\Export\Filter\ActivityTypeFilter
autowire: true
autoconfigure: true
tags:
- { name: chill.export_filter, alias: accompanyingcourse_activitytype_filter }
chill.person.export.filter_origin:
class: Chill\PersonBundle\Export\Filter\OriginFilter
autowire: true

View File

@ -398,6 +398,10 @@ Filter by evaluation: Filtrer par évaluation
Accepted evaluations: Évaluations
"Filtered by evaluations: only %evals%": "Filtré par évaluation: uniquement %evals%"
Filter by activity type: Filtrer par type d'activité
Accepted activitytypes: Types d'activités
"Filtered by activity types: only %activitytypes%": "Filtré par type d'activité: seulement %activitytypes%"
Filter by origin: Filtrer par origine du parcours
Accepted origins: Origines
"Filtered by origins: only %origins%": "Filtré par origine du parcours: uniquement %origins%"