exports: create origin filter

This commit is contained in:
2022-07-25 12:36:22 +02:00
parent 04ca61be81
commit e56520f4b1
3 changed files with 106 additions and 0 deletions

View File

@@ -0,0 +1,95 @@
<?php
namespace Chill\PersonBundle\Export\Filter;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin;
use Chill\PersonBundle\Export\Declarations;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use phpDocumentor\Reflection\Types\Boolean;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
class OriginFilter implements FilterInterface
{
/**
* @var TranslatableStringHelper
*/
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
TranslatableStringHelper $translatableStringHelper
) {
$this->translatableStringHelper = $translatableStringHelper;
}
/**
* @inheritDoc
*/
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_origins', EntityType::class, [
'class' => Origin::class,
'choice_label' => function (Origin $o) {
return $this->translatableStringHelper->localize($o->getLabel());
},
'multiple' => false,
'expanded' => true
]);
}
/**
* @inheritDoc
*/
public function getTitle(): string
{
return 'Filter by origin';
}
/**
* @inheritDoc
*/
public function describeAction($data, $format = 'string'): array
{
return ['Filtered by origins: only %origin%', [
'%origin%' => $this->translatableStringHelper->localize(
$data['accepted_origins']->getLabel())
]];
}
/**
* @inheritDoc
*/
public function addRole()
{
return null;
}
/**
* @inheritDoc
*/
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->eq('acp.origin', ':origin');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('origin', $data['accepted_origins']);
}
/**
* @inheritDoc
*/
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
}