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

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

View File

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