Feature: add thirdParty choice in docgen person context

This commit is contained in:
nobohan 2023-05-23 23:02:00 +02:00
parent db33fab097
commit d0867f9aa3
4 changed files with 74 additions and 4 deletions

View File

@ -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,
]);;
]);
}
/**

View File

@ -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, [

View File

@ -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,
];
}

View File

@ -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: