export SocialWorkTypeFilter: create dynamic Form Action-goal-result

- initiate new Vue component
- cleaning buildForm in Filter
- add HiddenType with ModelTransformer
- use ObjectToIdTransformer in HiddenType fields
- setting mount() and teleport
- list fetch url in api.js
- html/bootstrap form structure
- add Multiselect actionType
- i18n import
- add multiselect for goals and results
- add and remove options in multiselect (wip)
- fix basic vue add/remove element from data store
- vue cleaning
- add ids in value hiddenFields
- adapt Filter in AlterQuery (wip)
- improve code lisibility
This commit is contained in:
2022-09-01 12:28:54 +02:00
parent c4f0ad01d3
commit 648ec68cfb
6 changed files with 442 additions and 54 deletions

View File

@@ -1,27 +1,22 @@
<?php
declare(strict_types=1);
namespace Chill\PersonBundle\Export\Filter\SocialWorkFilters;
use Chill\MainBundle\Export\FilterInterface;
use Chill\MainBundle\Templating\TranslatableStringHelper;
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 Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Query\Expr\Andx;
use Doctrine\ORM\QueryBuilder;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
use Chill\PersonBundle\Templating\Entity\SocialActionRender;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\Event\PostSetDataEvent;
use Symfony\Component\Form\Event\PostSubmitEvent;
use Symfony\Component\Form\Event\PreSetDataEvent;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
class SocialWorkTypeFilter implements FilterInterface
{
@@ -29,63 +24,91 @@ class SocialWorkTypeFilter implements FilterInterface
private TranslatableStringHelper $translatableStringHelper;
private SocialActionRepository $socialActionRepository;
private EntityManagerInterface $em;
public function __construct
(
SocialActionRender $socialActionRender,
TranslatableStringHelper $translatableStringHelper,
SocialActionRepository $socialActionRepository
EntityManagerInterface $em
)
{
$this->socialActionRender = $socialActionRender;
$this->translatableStringHelper = $translatableStringHelper;
$this->socialActionRepository = $socialActionRepository;
$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
@@ -122,7 +145,15 @@ class SocialWorkTypeFilter implements FilterInterface
public function alterQuery(QueryBuilder $qb, $data)
{
$where = $qb->getDQLPart('where');
$clause = $qb->expr()->in('r', ':referrers');
$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);
@@ -131,7 +162,11 @@ class SocialWorkTypeFilter implements FilterInterface
}
$qb->add('where', $where);
$qb->setParameter('referrers', $data['referrers']);
$qb->setParameters([
'actionType' => $data['actionType'],
'goal' => $data['goal'],
'result' => $data['result'],
]);
}
public function applyOn(): string