From 0633fd812fab044207ff63c8bed4a315012ef2aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 8 Oct 2021 17:51:51 +0200 Subject: [PATCH] handle kind as contacts --- .../Controller/ThirdPartyController.php | 37 +++++++++++++++ .../Entity/ThirdParty.php | 1 + .../Form/ThirdPartyType.php | 45 ++++++++++--------- .../views/ThirdParty/_form.html.twig | 38 ++++++++-------- .../views/ThirdParty/new_pick_kind.html.twig | 37 +++++++++++++++ .../Resources/views/ThirdParty/view.html.twig | 30 +++++++------ .../migrations/Version20211007165001.php | 10 ++--- .../translations/messages.fr.yml | 8 ++++ 8 files changed, 148 insertions(+), 58 deletions(-) create mode 100644 src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new_pick_kind.html.twig diff --git a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php index 42427544c..d6e4c0046 100644 --- a/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php +++ b/src/Bundle/ChillThirdPartyBundle/Controller/ThirdPartyController.php @@ -8,8 +8,12 @@ use Chill\MainBundle\Pagination\PaginatorInterface; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\ThirdPartyBundle\Repository\ThirdPartyACLAwareRepositoryInterface; use Chill\ThirdPartyBundle\Repository\ThirdPartyRepository; +use http\Exception\RuntimeException; use Symfony\Bundle\FrameworkBundle\Controller\Controller; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Annotation\Route; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Component\HttpFoundation\Request; @@ -45,15 +49,19 @@ final class ThirdPartyController extends CRUDController protected ThirdPartyACLAwareRepositoryInterface $thirdPartyACLAwareRepository; + protected RequestStack $requestStack; + public function __construct( AuthorizationHelper $authorizationHelper, TranslatorInterface $translator, PaginatorFactory $paginatorFactory, + RequestStack $requestStack, ThirdPartyACLAwareRepositoryInterface $thirdPartyACLAwareRepository ) { $this->authorizationHelper = $authorizationHelper; $this->translator = $translator; $this->paginatorFactory = $paginatorFactory; + $this->requestStack = $requestStack; $this->thirdPartyACLAwareRepository = $thirdPartyACLAwareRepository; } @@ -82,9 +90,38 @@ final class ThirdPartyController extends CRUDController } } + if ('new' === $action) { + if (!$request->query->has('kind')) { + return $this->render('@ChillThirdParty/ThirdParty/new_pick_kind.html.twig'); + } else { + $kind = $request->query->getAlpha('kind', ''); + + if (!(ThirdParty::KIND_COMPANY === $kind || ThirdParty::KIND_CONTACT === $kind)) { + throw new BadRequestHttpException('This kind is not supported: '.$kind); + } + + $entity->setKind($kind); + } + } + return null; } + protected function createFormFor(string $action, $entity, string $formClass = null, array $formOptions = []): FormInterface + { + if ('new' === $action) { + return parent::createFormFor($action, $entity, $formClass, \array_merge( + $formOptions, [ 'kind' => $this->requestStack->getCurrentRequest()->query->getAlpha('kind')] + )); + } elseif ('edit' === $action) { + return parent::createFormFor($action, $entity, $formClass, \array_merge( + $formOptions, [ 'kind' => $entity->getKind()] + )); + } + + return parent::createFormFor($action, $entity, $formClass, $formOptions); + } + protected function buildFilterOrderHelper(string $action, Request $request): ?FilterOrderHelper { return $this->getFilterOrderHelperFactory() diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 5d7a4cc6e..2bf83bbac 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -61,6 +61,7 @@ class ThirdParty implements TrackCreationInterface, TrackUpdateInterface const KIND_CONTACT = 'contact'; const KIND_COMPANY = 'company'; + const KIND_CHILD = 'child'; /** * @ORM\Column(name="kind", type="string", length="20", options={"default":""}) diff --git a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php index 64c9fabda..057f848cb 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php @@ -18,14 +18,11 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\Form\FormView; use Symfony\Component\OptionsResolver\OptionsResolver; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter; use Symfony\Bridge\Doctrine\Form\Type\EntityType; -use Symfony\Component\Security\Core\Role\Role; use Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; @@ -64,7 +61,6 @@ class ThirdPartyType extends AbstractType */ public function buildForm(FormBuilderInterface $builder, array $options) { - $builder ->add('name', TextType::class, [ 'required' => true @@ -111,7 +107,7 @@ class ThirdPartyType extends AbstractType ; // Contact Person ThirdParty (child) - if ($options['is_child']) { + if (ThirdParty::KIND_CONTACT === $options['kind'] || ThirdParty::KIND_CHILD === $options['kind']) { $builder ->add('civility', EntityType::class, [ 'label' => 'thirdparty.Civility', @@ -156,6 +152,27 @@ class ThirdPartyType extends AbstractType 'label' => 'thirdparty.Acronym', 'required' => false ]) + ->add('activeChildren', ChillCollectionType::class, [ + 'entry_type' => ThirdPartyType::class, + 'entry_options' => [ + 'is_child' => true, + 'block_name' => 'children', + 'kind' => ThirdParty::KIND_CHILD, + ], + 'block_name' => 'active_children', + 'allow_add' => true, + 'allow_delete' => true, + 'by_reference' => false, + 'button_add_label' => "Add a contact", + 'button_remove_label' => "Remove a contact", + 'empty_collection_explain' => "Any contact" + ]) + ; + } + + if (ThirdParty::KIND_CHILD !== $options['kind']) { + + $builder ->add('categories', EntityType::class, [ 'label' => 'thirdparty.Categories', 'class' => ThirdPartyCategory::class, @@ -170,20 +187,6 @@ class ThirdPartyType extends AbstractType 'multiple' => true, 'attr' => ['class' => 'select2'] ]) - ->add('activeChildren', ChillCollectionType::class, [ - 'entry_type' => ThirdPartyType::class, - 'entry_options' => [ - 'is_child' => true, - 'block_name' => 'children' - ], - 'block_name' => 'active_children', - 'allow_add' => true, - 'allow_delete' => true, - 'by_reference' => false, - 'button_add_label' => "Add a contact", - 'button_remove_label' => "Remove a contact", - 'empty_collection_explain' => "Any contact" - ]) ->add('active', ChoiceType::class, [ 'label' => 'thirdparty.Status', 'choices' => [ @@ -192,8 +195,7 @@ class ThirdPartyType extends AbstractType ], 'expanded' => true, 'multiple' => false - ]) - ; + ]); // add the types $types = []; @@ -242,6 +244,7 @@ class ThirdPartyType extends AbstractType $resolver->setDefaults(array( 'data_class' => ThirdParty::class, 'is_child' => false, + 'kind' => null )); } } diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/_form.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/_form.html.twig index 38f1181e8..046e4db0f 100644 --- a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/_form.html.twig +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/_form.html.twig @@ -1,29 +1,31 @@ - - {% if form.civility is defined %} {{ form_row(form.civility) }} {% endif %} - {{ form_row(form.name) }} +{{ form_row(form.name) }} - {% if form.nameCompany is defined %} - {{ form_row(form.nameCompany) }} - {{ form_row(form.acronym) }} - {% endif %} +{% if form.nameCompany is defined %} + {{ form_row(form.nameCompany) }} + {{ form_row(form.acronym) }} +{% endif %} - {% if form.profession is defined %} - {{ form_row(form.profession) }} - {% endif %} +{% if form.profession is defined %} + {{ form_row(form.profession) }} +{% endif %} - {{ form_row(form.types) }} - {{ form_row(form.categories) }} +{{ form_row(form.types) }} +{{ form_row(form.categories) }} - {{ form_row(form.telephone) }} - {{ form_row(form.email) }} +{{ form_row(form.telephone) }} +{{ form_row(form.email) }} -

{{ 'Contacts'|trans }}

- {{ form_widget(form.activeChildren) }} +{{ form_row(form.contactDataAnonymous) }} + +{% if form.activeChildren is defined %} +

{{ 'Contacts'|trans }}

+ {{ form_widget(form.activeChildren) }} +{% endif %}
{{ form_label(form.address) }} @@ -54,6 +56,6 @@
{{ form_row(form.comment) }} - {{ form_row(form.centers) }} +{{ form_row(form.centers) }} - {{ form_row(form.active) }} +{{ form_row(form.active) }} diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new_pick_kind.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new_pick_kind.html.twig new file mode 100644 index 000000000..3ede02bfb --- /dev/null +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/new_pick_kind.html.twig @@ -0,0 +1,37 @@ +{% extends "@ChillMain/layout.html.twig" %} + +{% block title 'thirdparty.Which kind of third party ?'|trans %} + +{% block content %} +
+

{{ block('title') }}

+ +
+
+ +
+

{{ 'thirdparty.a_company_explanation'|trans }}

+
+
+
+ +
+

{{ 'thirdparty.a_contact_explanation'|trans }}

+
+
+
+
+ +{% endblock %} diff --git a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/view.html.twig b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/view.html.twig index 1200c3e83..460468e98 100644 --- a/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/view.html.twig +++ b/src/Bundle/ChillThirdPartyBundle/Resources/views/ThirdParty/view.html.twig @@ -28,7 +28,7 @@ {{ thirdParty.name }} - {% if thirdParty.isLeaf == false %} + {% if thirdParty.kind == 'company' %}
{{ 'thirdparty.NameCompany'|trans }}
{% if thirdParty.nameCompany == null %} @@ -99,20 +99,22 @@ {% endif %}
-
{{ 'Contacts'|trans }}
-
- {% if thirdParty.activeChildren|length == 0 %} -

{{ 'Any contacts associated'|trans }}

- {% else %} -
- {% for tp in thirdParty.activeChildren %} -
- {{ tp|chill_entity_render_box({'render': 'bloc', 'addLink': false}) }} + {% if thirdParty.kind == 'company' %} +
{{ 'Contacts'|trans }}
+
+ {% if thirdParty.activeChildren|length == 0 %} +

{{ 'Any contacts associated'|trans }}

+ {% else %} +
+ {% for tp in thirdParty.activeChildren %} +
+ {{ tp|chill_entity_render_box({'render': 'bloc', 'addLink': false}) }} +
+ {% endfor %}
- {% endfor %} - - {% endif %} -
+ {% endif %} + + {% endif %}
{{ 'Centers'|trans }}
diff --git a/src/Bundle/ChillThirdPartyBundle/migrations/Version20211007165001.php b/src/Bundle/ChillThirdPartyBundle/migrations/Version20211007165001.php index a822f9efc..f58fa1a79 100644 --- a/src/Bundle/ChillThirdPartyBundle/migrations/Version20211007165001.php +++ b/src/Bundle/ChillThirdPartyBundle/migrations/Version20211007165001.php @@ -41,11 +41,11 @@ final class Version20211007165001 extends AbstractMigration NEW.canonicalized = UNACCENT( LOWER( - name || - CASE WHEN COALESCE(name_company, '') <> '' THEN ' ' ELSE '' END || - COALESCE(name_company, '') || - CASE WHEN COALESCE(acronym, '') <> '' THEN ' ' ELSE '' END || - COALESCE(acronym, '') + NEW.name || + CASE WHEN COALESCE(NEW.name_company, '') <> '' THEN ' ' ELSE '' END || + COALESCE(NEW.name_company, '') || + CASE WHEN COALESCE(NEW.acronym, '') <> '' THEN ' ' ELSE '' END || + COALESCE(NEW.acronym, '') ) ) ; diff --git a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml index e370655e4..938117cc1 100644 --- a/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillThirdPartyBundle/translations/messages.fr.yml @@ -29,6 +29,14 @@ thirdparty.UpdateBy.short: ' par ' thirdparty.CreatedAt.long: Date de création thirdparty.UpdatedAt.long: Date de la dernière modification thirdparty.UpdateBy.long: Utilisateur qui a effectué la dernière modification +thirdparty.A company: Une institution +thirdparty.A contact: Une personne physique +thirdparty.a_company_explanation: >- + Les institutions peuvent compter un ou plusieurs contacts, interne à l'instution. Il est également possible de + leur associer un acronyme, et le nom d'un service. +thirdparty.a_contact_explanation: >- + Les personnes physiques ne disposent pas d'acronyme, de service, ou de contacts sous-jacents. +thirdparty.Which kind of third party ?: Quel type de tiers souhaitez-vous créer ? New third party: Ajouter un nouveau tiers Show third party %name%: Tiers "%name%"