export new ActivityTypeFilter

This commit is contained in:
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;
}
}