From d0867f9aa3d39af5e2f67289849153de82f153cb Mon Sep 17 00:00:00 2001 From: nobohan Date: Tue, 23 May 2023 23:02:00 +0200 Subject: [PATCH] Feature: add thirdParty choice in docgen person context --- .../Service/DocGenerator/ActivityContext.php | 4 +- .../AccompanyingPeriodContext.php | 2 +- .../Service/DocGenerator/PersonContext.php | 71 ++++++++++++++++++- .../translations/messages.fr.yml | 1 + 4 files changed, 74 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php index d3a770490..768a0fabe 100644 --- a/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php +++ b/src/Bundle/ChillActivityBundle/Service/DocGenerator/ActivityContext.php @@ -137,9 +137,9 @@ class ActivityContext implements 'label' => 'docgen.Ask for thirdParty', ]) ->add('thirdPartyLabel', TextType::class, [ - 'label' => 'thirdParty label', + 'label' => 'docgen.thirdParty label', 'required' => true, - ]);; + ]); } /** diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php index 53222b753..29e8c81a3 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/AccompanyingPeriodContext.php @@ -159,7 +159,7 @@ class AccompanyingPeriodContext implements 'label' => 'docgen.Ask for thirdParty', ]) ->add('thirdPartyLabel', TextType::class, [ - 'label' => 'thirdParty label', + 'label' => 'docgen.thirdParty label', 'required' => true, ]) ->add('category', EntityType::class, [ diff --git a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php index 1fdfd23d9..30faa97fd 100644 --- a/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php +++ b/src/Bundle/ChillPersonBundle/Service/DocGenerator/PersonContext.php @@ -26,14 +26,21 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface; use Chill\MainBundle\Security\Resolver\CenterResolverManagerInterface; use Chill\MainBundle\Templating\TranslatableStringHelperInterface; +use Chill\PersonBundle\Entity\Person\PersonResource; use Chill\PersonBundle\Entity\Person; +use Chill\PersonBundle\Entity\Person\ResidentialAddress; use Chill\PersonBundle\Repository\PersonRepository; +use Chill\PersonBundle\Repository\ResidentialAddressRepository; +use Chill\ThirdPartyBundle\Entity\ThirdParty; +use Chill\ThirdPartyBundle\Templating\Entity\ThirdPartyRender; +use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; use DateTime; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityRepository; use LogicException; use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Security\Core\Security; @@ -67,6 +74,12 @@ final class PersonContext implements PersonContextInterface private TranslatorInterface $translator; + private ThirdPartyRender $thirdPartyRender; + + private ThirdPartyRepository $thirdPartyRepository; + + private ResidentialAddressRepository $residentialAddressRepository; + public function __construct( AuthorizationHelperInterface $authorizationHelper, BaseContextData $baseContextData, @@ -78,7 +91,10 @@ final class PersonContext implements PersonContextInterface ScopeRepositoryInterface $scopeRepository, Security $security, TranslatorInterface $translator, - TranslatableStringHelperInterface $translatableStringHelper + TranslatableStringHelperInterface $translatableStringHelper, + ThirdPartyRender $thirdPartyRender, + ThirdPartyRepository $thirdPartyRepository, + ResidentialAddressRepository $residentialAddressRepository ) { $this->authorizationHelper = $authorizationHelper; $this->centerResolverManager = $centerResolverManager; @@ -91,6 +107,9 @@ final class PersonContext implements PersonContextInterface $this->showScopes = $parameterBag->get('chill_main')['acl']['form_show_scopes']; $this->translator = $translator; $this->translatableStringHelper = $translatableStringHelper; + $this->thirdPartyRender = $thirdPartyRender; + $this->thirdPartyRepository = $thirdPartyRepository; + $this->residentialAddressRepository = $residentialAddressRepository; } public function adminFormReverseTransform(array $data): array @@ -110,6 +129,8 @@ final class PersonContext implements PersonContextInterface $r = [ 'mainPerson' => $data['mainPerson'] ?? false, 'mainPersonLabel' => $data['mainPersonLabel'] ?? $this->translator->trans('docgen.Main person'), + 'thirdParty' => $data['thirdParty'] ?? false, + 'thirdPartyLabel' => $data['thirdPartyLabel'] ?? $this->translator->trans('thirdParty'), ]; if (array_key_exists('category', $data)) { @@ -131,6 +152,14 @@ final class PersonContext implements PersonContextInterface ->setParameter('docClass', PersonDocument::class), 'choice_label' => fn ($entity = null) => $entity ? $this->translatableStringHelper->localize($entity->getName()) : '', 'required' => true, + ]) + ->add('thirdParty', CheckboxType::class, [ + 'required' => false, + 'label' => 'docgen.Ask for thirdParty', + ]) + ->add('thirdPartyLabel', TextType::class, [ + 'label' => 'docgen.thirdParty label', + 'required' => true, ]); } @@ -139,12 +168,43 @@ final class PersonContext implements PersonContextInterface */ public function buildPublicForm(FormBuilderInterface $builder, DocGeneratorTemplate $template, $entity): void { + $options = $template->getOptions(); + $builder->add('title', TextType::class, [ 'required' => true, 'label' => 'docgen.Document title', 'data' => $this->translatableStringHelper->localize($template->getName()), ]); + $thirdParties = array_merge( + array_filter(array_map( + fn (ResidentialAddress $r): ?ThirdParty => $r->getHostThirdParty(), + $this + ->residentialAddressRepository + ->findCurrentResidentialAddressByPerson($entity) + ) + ), + array_filter(array_map( + fn (PersonResource $r): ?ThirdParty => $r->getThirdParty(), + $entity->getResources()->filter( + static fn (PersonResource $r): bool => null !== $r->getThirdParty() + )->toArray()) + ) + ); + + if ($options['thirdParty'] ?? false) { + $builder->add('thirdParty', EntityType::class, [ + 'class' => ThirdParty::class, + 'choices' => $thirdParties, + 'choice_label' => fn (ThirdParty $p) => $this->thirdPartyRender->renderString($p, []), + 'multiple' => false, + 'required' => false, + 'expanded' => true, + 'label' => $options['thirdPartyLabel'], + 'placeholder' => $this->translator->trans('Any third party selected'), + ]); + } + if ($this->isScopeNecessary($entity)) { $builder->add('scope', ScopePickerType::class, [ 'center' => $this->centerResolverManager->resolveCenters($entity), @@ -170,6 +230,13 @@ final class PersonContext implements PersonContextInterface 'docgen:person:with-budget' => true, ]); + if ($template->getOptions()['thirdParty']) { + $data['thirdParty'] = $this->normalizer->normalize($contextGenerationData['thirdParty'], 'docgen', [ + 'docgen:expects' => ThirdParty::class, + 'groups' => 'docgen:read' + ]); + } + return $data; } @@ -223,6 +290,7 @@ final class PersonContext implements PersonContextInterface return [ 'title' => $data['title'] ?? '', 'scope_id' => $scope instanceof Scope ? $scope->getId() : null, + 'thirdParty' => null === $data['thirdParty'] ? null : $data['thirdParty']->getId(), ]; } @@ -242,6 +310,7 @@ final class PersonContext implements PersonContextInterface return [ 'title' => $data['title'] ?? '', 'scope' => $scope, + 'thirdParty' => null !== ($id = ($data['thirdParty'] ?? null)) ? $this->thirdPartyRepository->find($id) : null, ]; } diff --git a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml index cf1b6105e..f62a5beff 100644 --- a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml @@ -114,6 +114,7 @@ docgen: A context for person with a third party (for sending mail): Un contexte d'une personne avec un tiers (pour envoyer un courrier à ce tiers, par exemple) Person with third party: Personne avec choix d'un tiers Ask for thirdParty: Demander à l'utilisateur de préciser un tiers + thirdParty label: Libellé du tiers # exports export: