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) }} -
{{ 'thirdparty.a_company_explanation'|trans }}
+{{ 'thirdparty.a_contact_explanation'|trans }}
+{{ 'Any contacts associated'|trans }}
- {% else %} -{{ 'Any contacts associated'|trans }}
+ {% else %} +