tp: prepare address to be managed by vue (adapt formType) + add aliases methods isChild and isParent

This commit is contained in:
Mathieu Jaumotte 2021-09-15 11:50:03 +02:00
parent a4f446c6b9
commit 910016f722
3 changed files with 50 additions and 10 deletions

View File

@ -152,6 +152,7 @@ class ThirdParty
private $address; private $address;
/** /**
* Soft-delete flag
* @var boolean * @var boolean
* @ORM\Column(name="active", type="boolean", options={"defaut": true}) * @ORM\Column(name="active", type="boolean", options={"defaut": true})
*/ */
@ -498,11 +499,28 @@ class ThirdParty
return $this; return $this;
} }
/**
* Mechanism to differentiate children and parents
*/
public function isLeaf(): bool public function isLeaf(): bool
{ {
return $this->children->count() !== 0; return $this->children->count() !== 0;
} }
/**
* isLeaf aliases
*/
public function isChild():bool
{
return $this->isLeaf();
}
public function isParent():bool
{
return !$this->isLeaf();
}
/** /**
* @return Collection * @return Collection
*/ */
@ -527,7 +545,8 @@ class ThirdParty
*/ */
public function removeChild(ThirdParty $child): self public function removeChild(ThirdParty $child): self
{ {
$this->categories->removeElement($child); $this->children->removeElement($child);
$this->active = false;
return $this; return $this;
} }

View File

@ -2,6 +2,7 @@
namespace Chill\ThirdPartyBundle\Form; namespace Chill\ThirdPartyBundle\Form;
use Chill\MainBundle\Entity\Address;
use Chill\MainBundle\Form\Type\ChillTextareaType; use Chill\MainBundle\Form\Type\ChillTextareaType;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory; use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
@ -9,7 +10,9 @@ use Chill\ThirdPartyBundle\Entity\ThirdPartyCivility;
use Chill\ThirdPartyBundle\Entity\ThirdPartyProfession; use Chill\ThirdPartyBundle\Entity\ThirdPartyProfession;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
use Doctrine\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolver;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper; 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\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Chill\MainBundle\Form\Type\AddressType;
class ThirdPartyType extends AbstractType class ThirdPartyType extends AbstractType
@ -35,16 +37,20 @@ class ThirdPartyType extends AbstractType
protected TranslatableStringHelper $translatableStringHelper; protected TranslatableStringHelper $translatableStringHelper;
protected ObjectManager $om;
public function __construct( public function __construct(
AuthorizationHelper $authorizationHelper, AuthorizationHelper $authorizationHelper,
TokenStorageInterface $tokenStorage, TokenStorageInterface $tokenStorage,
ThirdPartyTypeManager $typesManager, ThirdPartyTypeManager $typesManager,
TranslatableStringHelper $translatableStringHelper TranslatableStringHelper $translatableStringHelper,
ObjectManager $om
) { ) {
$this->authorizationHelper = $authorizationHelper; $this->authorizationHelper = $authorizationHelper;
$this->tokenStorage = $tokenStorage; $this->tokenStorage = $tokenStorage;
$this->typesManager = $typesManager; $this->typesManager = $typesManager;
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->om = $om;
} }
/** /**
@ -88,11 +94,6 @@ class ThirdPartyType extends AbstractType
->add('email', EmailType::class, [ ->add('email', EmailType::class, [
'required' => false 'required' => false
]) ])
->add('address', AddressType::class, [
'has_valid_from' => false,
'null_if_empty' => true,
'required' => false
])
->add('active', ChoiceType::class, [ ->add('active', ChoiceType::class, [
'label' => 'thirdparty.Status', 'label' => 'thirdparty.Status',
'choices' => [ 'choices' => [
@ -112,9 +113,28 @@ class ThirdPartyType extends AbstractType
'attr' => ['class' => 'select2'] '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) // Contact Person ThirdParty (child)
if ($options['data']->isLeaf()) { if ($options['data']->isChild()) {
$builder $builder
->add('civility', EntityType::class, [ ->add('civility', EntityType::class, [
'label' => 'thirdparty.Civility', 'label' => 'thirdparty.Civility',

View File

@ -5,6 +5,7 @@ services:
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface' $tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
$typesManager: '@Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager' $typesManager: '@Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager'
$translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper' $translatableStringHelper: '@Chill\MainBundle\Templating\TranslatableStringHelper'
$om: '@doctrine.orm.entity_manager'
tags: tags:
- { name: form.type } - { name: form.type }