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;
/**
* 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;
}

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

View File

@ -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 }