exports: create emergency filter

This commit is contained in:
2022-07-25 16:35:11 +02:00
parent ddac410b2e
commit da224ea4bc
4 changed files with 102 additions and 2 deletions

View File

@@ -45,7 +45,7 @@ class ConfidentialFilter implements FilterInterface
public function describeAction($data, $format = 'string'): array
{
dump($data, self::CHOICES);
//$choice = //array_flip(self::CHOICES)[$data['accepted_confidentials']];
foreach (self::CHOICES as $k => $v) {
if ($v === $data['accepted_confidentials']) {
$choice = $k;

View File

@@ -0,0 +1,87 @@
<?php
namespace Chill\PersonBundle\Export\Filter;
use Chill\MainBundle\Export\FilterInterface;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class EmergencyFilter implements FilterInterface
{
private const CHOICES = [
'is not emergency' => false,
'is emergency' => true,
];
private CONST DEFAULT_CHOICE = false;
private TranslatorInterface $translator;
public function __construct(TranslatorInterface $translator)
{
$this->translator = $translator;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_emergencies', ChoiceType::class, [
'choices' => self::CHOICES,
'multiple' => false,
'expanded' => true,
'empty_data' => self::DEFAULT_CHOICE,
'data' => self::DEFAULT_CHOICE,
]);
}
public function getTitle(): string
{
return 'Filter by emergency';
}
public function describeAction($data, $format = 'string'): array
{
dump($data, self::CHOICES);
foreach (self::CHOICES as $k => $v) {
if ($v === $data['accepted_emergencies']) {
$choice = $k;
}
}
return [
'Filtered by emergency: only %emergency%', [
'%emergency%' => $this->translator->trans($choice)
]
];
}
public function addRole()
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->eq('acp.emergency', ':emergency');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('emergency', $data['accepted_emergencies']);
}
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
}