export new EvaluationFilter

This commit is contained in:
2022-08-01 14:32:11 +02:00
parent b511517a0f
commit bc2209319a
3 changed files with 116 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
<?php
namespace Chill\PersonBundle\Export\Filter;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\SocialWork\Evaluation;
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;
class EvaluationFilter implements FilterInterface
{
/**
* @var TranslatableStringHelper
*/
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
TranslatableStringHelper $translatableStringHelper
) {
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_evaluations', EntityType::class, [
'class' => Evaluation::class,
'choice_label' => function (Evaluation $ev) {
return $this->translatableStringHelper->localize($ev->getTitle());
},
'multiple' => true,
'expanded' => true,
]);
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return 'Filter by evaluation';
}
/**
* @inheritDoc
*/
public function describeAction($data, $format = 'string'): array
{
$evaluations = [];
foreach ($data['accepted_evaluations'] as $ev) {
$evaluations[] = $this->translatableStringHelper->localize($ev->getTitle());
}
return ['Filtered by evaluations: only %evals%', [
'%evals%' => implode(", ou ", $evaluations)
]];
}
/**
* @inheritDoc
*/
public function addRole()
{
return null;
}
/**
* @inheritDoc
*/
public function alterQuery(QueryBuilder $qb, $data)
{
$qb
->join('acp.works', 'acpw')
->join('acpw.accompanyingPeriodWorkEvaluations', 'we')
->join('we.evaluation', 'ev')
;
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('ev.id', ':evaluations');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('evaluations', $data['accepted_evaluations']);
}
/**
* @inheritDoc
*/
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
}