diff --git a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php index 75da7ad78..0b0b3e56b 100644 --- a/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php +++ b/src/Bundle/ChillThirdPartyBundle/Entity/ThirdParty.php @@ -152,6 +152,7 @@ class ThirdParty private $address; /** + * Soft-delete flag * @var boolean * @ORM\Column(name="active", type="boolean", options={"defaut": true}) */ @@ -498,11 +499,28 @@ class ThirdParty return $this; } + /** + * Mechanism to differentiate children and parents + */ public function isLeaf(): bool { return $this->children->count() !== 0; } + /** + * isLeaf aliases + */ + public function isChild():bool + { + return $this->isLeaf(); + } + + public function isParent():bool + { + return !$this->isLeaf(); + } + + /** * @return Collection */ @@ -527,7 +545,8 @@ class ThirdParty */ public function removeChild(ThirdParty $child): self { - $this->categories->removeElement($child); + $this->children->removeElement($child); + $this->active = false; return $this; } diff --git a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php index 9db5013c3..30323794e 100644 --- a/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php +++ b/src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php @@ -2,6 +2,7 @@ namespace Chill\ThirdPartyBundle\Form; +use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory; @@ -9,7 +10,9 @@ use Chill\ThirdPartyBundle\Entity\ThirdPartyCivility; use Chill\ThirdPartyBundle\Entity\ThirdPartyProfession; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; +use Doctrine\Persistence\ObjectManager; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\CallbackTransformer; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; @@ -21,8 +24,7 @@ use Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\EmailType; -use Symfony\Component\Form\Extension\Core\Type\TextareaType; -use Chill\MainBundle\Form\Type\AddressType; +use Symfony\Component\Form\Extension\Core\Type\HiddenType; class ThirdPartyType extends AbstractType @@ -35,16 +37,20 @@ class ThirdPartyType extends AbstractType protected TranslatableStringHelper $translatableStringHelper; + protected ObjectManager $om; + public function __construct( AuthorizationHelper $authorizationHelper, TokenStorageInterface $tokenStorage, ThirdPartyTypeManager $typesManager, - TranslatableStringHelper $translatableStringHelper + TranslatableStringHelper $translatableStringHelper, + ObjectManager $om ) { $this->authorizationHelper = $authorizationHelper; $this->tokenStorage = $tokenStorage; $this->typesManager = $typesManager; $this->translatableStringHelper = $translatableStringHelper; + $this->om = $om; } /** @@ -88,11 +94,6 @@ class ThirdPartyType extends AbstractType ->add('email', EmailType::class, [ 'required' => false ]) - ->add('address', AddressType::class, [ - 'has_valid_from' => false, - 'null_if_empty' => true, - 'required' => false - ]) ->add('active', ChoiceType::class, [ 'label' => 'thirdparty.Status', 'choices' => [ @@ -112,9 +113,28 @@ class ThirdPartyType extends AbstractType 'attr' => ['class' => 'select2'] ]) ; + $builder->add('address', HiddenType::class); + $builder->get('address') + ->addModelTransformer(new CallbackTransformer( + function (?Address $address): string { + if (null === $address) { + return ''; + } + return $address->getId(); + }, + function (string $addressId): ?Address { + if ('' === $addressId) { + return null; + } + return $this->om + ->getRepository(Address::class) + ->findOneBy(['id' => (int) $addressId]); + } + )) + ; // Contact Person ThirdParty (child) - if ($options['data']->isLeaf()) { + if ($options['data']->isChild()) { $builder ->add('civility', EntityType::class, [ 'label' => 'thirdparty.Civility', diff --git a/src/Bundle/ChillThirdPartyBundle/config/services/form.yaml b/src/Bundle/ChillThirdPartyBundle/config/services/form.yaml index de62b197c..8ff604769 100644 --- a/src/Bundle/ChillThirdPartyBundle/config/services/form.yaml +++ b/src/Bundle/ChillThirdPartyBundle/config/services/form.yaml @@ -5,6 +5,7 @@ services: $tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface' $typesManager: '@Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager' $translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper' + $om: '@doctrine.orm.entity_manager' tags: - { name: form.type }