export new SocialActionFilter

This commit is contained in:
2022-08-01 12:45:04 +02:00
parent 758c56482b
commit b511517a0f
3 changed files with 120 additions and 7 deletions

View File

@@ -0,0 +1,101 @@
<?php
namespace Chill\PersonBundle\Export\Filter;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class SocialActionFilter implements FilterInterface
{
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var TranslatableStringHelper
*/
private TranslatableStringHelper $translatableStringHelper;
/**
* @var SocialActionRender
*/
private SocialActionRender $actionRender;
public function __construct(
TranslatorInterface $translator,
TranslatableStringHelper $translatableStringHelper,
SocialActionRender $actionRender
) {
$this->translator = $translator;
$this->translatableStringHelper = $translatableStringHelper;
$this->actionRender = $actionRender;
}
public function buildForm(FormBuilderInterface $builder)
{
$builder->add('accepted_socialactions', EntityType::class, [
'class' => SocialAction::class,
'choice_label' => function (SocialAction $sa) {
return $this->actionRender->renderString($sa, []);
},
'multiple' => true,
'expanded' => true,
]);
}
public function getTitle(): string
{
return 'Filter by socialaction';
}
public function describeAction($data, $format = 'string'): array
{
$socialactions = [];
foreach ($data['accepted_socialactions'] as $sa) {
$socialactions[] = $this->actionRender->renderString($sa, []);
}
return ['Filtered by socialactions: only %socialactions%', [
'%socialactions%' => implode(", ou ", $socialactions)
]];
}
public function addRole()
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$qb->join('acp.works', 'acpw')
->join('acpw.socialAction', 'sa')
;
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('sa.id', ':socialactions');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('socialactions', $data['accepted_socialactions']);
}
public function applyOn(): string
{
return Declarations::ACP_TYPE;
}
}