Merge branch 'export_vue_multiselect' into 111_exports_suite

This commit is contained in:
2022-09-08 14:23:22 +02:00
6 changed files with 472 additions and 80 deletions

View File

@@ -13,105 +13,114 @@ namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\PersonBundle\Entity\SocialWork\Goal;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Entity\SocialWork\Goal;
use Chill\PersonBundle\Entity\SocialWork\Result;
use Chill\PersonBundle\Export\Declarations;
use Chill\PersonBundle\Repository\SocialWork\SocialActionRepository;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
class SocialWorkTypeFilter implements FilterInterface
{
private SocialActionRender $socialActionRender;
private SocialActionRepository $socialActionRepository;
private TranslatableStringHelper $translatableStringHelper;
public function __construct(
private EntityManagerInterface $em;
public function __construct
(
SocialActionRender $socialActionRender,
TranslatableStringHelper $translatableStringHelper,
SocialActionRepository $socialActionRepository
) {
EntityManagerInterface $em
)
{
$this->socialActionRender = $socialActionRender;
$this->translatableStringHelper = $translatableStringHelper;
$this->socialActionRepository = $socialActionRepository;
}
public function addRole(): ?string
{
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('r', ':referrers');
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameter('referrers', $data['referrers']);
}
public function applyOn(): string
{
return Declarations::SOCIAL_WORK_ACTION_TYPE;
$this->em = $em;
}
public function buildForm(FormBuilderInterface $builder)
{
$socialActions = $this->socialActionRepository->findAll();
$builder->add('actionType', ChoiceType::class, [
'choices' => $socialActions,
'choice_label' => function (SocialAction $sa) {
return $this->socialActionRender->renderString($sa, []);
},
'multiple' => true,
'expanded' => true,
]);
/*
$refreshGoals = function (FormInterface $form, SocialAction $actionType = null) {
$goals = null === $actionType ? [] : $actionType->getGoals();
$form->add('goal', ChoiceType::class, [
'placeholder' => '',
'choices' => $goals,
'choice_label' => function (Goal $g) {
return $this->translatableStringHelper->localize($g->getTitle());
$builder
->add('actionType', HiddenType::class)
->get('actionType')
->addModelTransformer(new CallbackTransformer(
static function (?iterable $actionsAsIterable): string {
if (null === $actionsAsIterable) { return ''; }
$actionIds = [];
foreach ($actionsAsIterable as $value) {
$actionIds[] = $value->getId();
}
return implode(',', $actionIds);
},
]);
};
function (?string $actionsAsString): array {
if (null === $actionsAsString) { return []; }
return array_map(
fn (string $id): ?SocialAction
=> $this->socialActionRepository->findOneBy(['id' => (int) $id]),
explode(',', $actionsAsString)
);
}
))
;
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($refreshGoals) {
$data = $event->getData();
dump($data);
$builder
->add('goal', HiddenType::class)
->get('goal')
->addModelTransformer(new CallbackTransformer(
static function (?iterable $goalsAsIterable): string {
if (null === $goalsAsIterable) { return ''; }
$goalIds = [];
foreach ($goalsAsIterable as $value) {
$goalIds[] = $value->getId();
}
return implode(',', $goalIds);
},
function (?string $goalsAsString): array {
if (null === $goalsAsString) { return []; }
return array_map(
fn (string $id): ?Goal
=> $this->socialActionRepository->findOneBy(['id' => (int) $id]),
explode(',', $goalsAsString)
);
}
))
;
$refreshGoals($event->getForm(), $data);
});
$builder
->add('result', HiddenType::class)
->get('result')
->addModelTransformer(new CallbackTransformer(
static function (?iterable $resultsAsIterable): string {
if (null === $resultsAsIterable) { return ''; }
$resultIds = [];
foreach ($resultsAsIterable as $value) {
$resultIds[] = $value->getId();
}
return implode(',', $resultIds);
},
function (?string $resultsAsString): array {
if (null === $resultsAsString) { return []; }
return array_map(
fn (string $id): ?Result
=> $this->socialActionRepository->findOneBy(['id' => (int) $id]),
explode(',', $resultsAsString)
);
}
))
;
$builder->get('actionType')->addEventListener(
FormEvents::POST_SUBMIT,
function (FormEvent $event) use ($refreshGoals) {
$actionType = $event->getForm()->getData();
dump($actionType);
$refreshGoals($event->getForm()->getParent(), $actionType);
}
);
*/
}
public function getTitle(): string
{
return 'Filter by type of action, objectives and results';
}
public function describeAction($data, $format = 'string'): array
@@ -123,22 +132,52 @@ class SocialWorkTypeFilter implements FilterInterface
foreach ($data['actionType'] as $at) {
$actionTypes[] = $at->getTitle();
}
foreach ($data['objectives'] as $o) {
$objectives[] = $o->getTitle();
}
foreach ($data['results'] as $r) {
$results[] = $r->getTitle();
}
return ['Filtered by referrers: only %actionTypes%', [
'%actionTypes%' => implode(', ou ', $actionTypes),
'%actionTypes%' => implode(', ou ', $actionTypes)
]];
}
public function getTitle(): string
public function addRole(): ?string
{
return 'Filter by type of action, objectives and results';
return null;
}
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->eq('acpw.socialAction',':actionType');
/*
if (!empty($data['goal'])) {
$clause
$qb->expr()->in('acpw.goals', ':goals');
}
$qb->expr()->in('acpw.results', ':results');
*/
if ($where instanceof Andx) {
$where->add($clause);
} else {
$where = $qb->expr()->andX($clause);
}
$qb->add('where', $where);
$qb->setParameters([
'actionType' => $data['actionType'],
'goal' => $data['goal'],
'result' => $data['result'],
]);
}
public function applyOn(): string
{
return Declarations::SOCIAL_WORK_ACTION_TYPE;
}
}