mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
various improvements on 3party
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace Chill\ThirdPartyBundle\Form;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Form\Type\ChillCollectionType;
|
||||
use Chill\MainBundle\Form\Type\PickCenterType;
|
||||
use Chill\MainBundle\Form\Type\ChillTextareaType;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
@@ -16,6 +17,8 @@ use Doctrine\Persistence\ObjectManager;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
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;
|
||||
@@ -60,58 +63,11 @@ class ThirdPartyType extends AbstractType
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$types = [];
|
||||
foreach ($this->typesManager->getProviders() as $key => $provider) {
|
||||
$types['chill_3party.key_label.'.$key] = $key;
|
||||
}
|
||||
if (count($types) === 1) {
|
||||
$builder
|
||||
->add('types', HiddenType::class, [
|
||||
'data' => array_values($types)
|
||||
])
|
||||
->get('types')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
function (?array $typeArray): ?string {
|
||||
if (null === $typeArray) {
|
||||
return null;
|
||||
}
|
||||
return implode(',', $typeArray);
|
||||
},
|
||||
function (?string $typeStr): ?array {
|
||||
if (null === $typeStr) {
|
||||
return null;
|
||||
}
|
||||
return explode(',', $typeStr);
|
||||
}
|
||||
))
|
||||
;
|
||||
} else {
|
||||
$builder->add('types', ChoiceType::class, [
|
||||
'choices' => $types,
|
||||
'expanded' => true,
|
||||
'multiple' => true,
|
||||
'label' => 'thirdparty.Type'
|
||||
]);
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'required' => true
|
||||
])
|
||||
->add('categories', EntityType::class, [
|
||||
'label' => 'thirdparty.Categories',
|
||||
'class' => ThirdPartyCategory::class,
|
||||
'choice_label' => function (ThirdPartyCategory $category): string {
|
||||
return $this->translatableStringHelper->localize($category->getName());
|
||||
},
|
||||
'query_builder' => function (EntityRepository $er): QueryBuilder {
|
||||
return $er->createQueryBuilder('c')
|
||||
->where('c.active = true');
|
||||
},
|
||||
'required' => true,
|
||||
'multiple' => true,
|
||||
'attr' => ['class' => 'select2']
|
||||
])
|
||||
->add('telephone', TextType::class, [
|
||||
'label' => 'Phonenumber',
|
||||
'required' => false
|
||||
@@ -119,34 +75,17 @@ class ThirdPartyType extends AbstractType
|
||||
->add('email', EmailType::class, [
|
||||
'required' => false
|
||||
])
|
||||
->add('active', ChoiceType::class, [
|
||||
'label' => 'thirdparty.Status',
|
||||
'choices' => [
|
||||
'Active, shown to users' => true,
|
||||
'Inactive, not shown to users' => false
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false
|
||||
])
|
||||
->add('comment', ChillTextareaType::class, [
|
||||
'required' => false
|
||||
])
|
||||
->add('centers', PickCenterType::class, [
|
||||
'role' => $this->om->contains($options['data']) ?
|
||||
'role' => (\array_key_exists('data', $options) && $this->om->contains($options['data'])) ?
|
||||
ThirdPartyVoter::UPDATE : ThirdPartyVoter::CREATE,
|
||||
'choice_options' => [
|
||||
'multiple' => true,
|
||||
'attr' => ['class' => 'select2']
|
||||
]
|
||||
])
|
||||
/*
|
||||
->add('centers', EntityType::class, [
|
||||
'choices' => $this->getReachableCenters($options),
|
||||
'class' => \Chill\MainBundle\Entity\Center::class,
|
||||
'multiple' => true,
|
||||
'attr' => ['class' => 'select2']
|
||||
])
|
||||
*/
|
||||
;
|
||||
|
||||
$builder
|
||||
@@ -171,7 +110,7 @@ class ThirdPartyType extends AbstractType
|
||||
;
|
||||
|
||||
// Contact Person ThirdParty (child)
|
||||
if ($options['data']->isChild()) {
|
||||
if ($options['is_child']) {
|
||||
$builder
|
||||
->add('civility', EntityType::class, [
|
||||
'label' => 'thirdparty.Civility',
|
||||
@@ -184,7 +123,7 @@ class ThirdPartyType extends AbstractType
|
||||
->where('c.active = true');
|
||||
},
|
||||
'placeholder' => 'thirdparty.choose civility',
|
||||
'required' => true
|
||||
'required' => false
|
||||
])
|
||||
->add('profession', EntityType::class, [
|
||||
'label' => 'thirdparty.Profession',
|
||||
@@ -212,27 +151,82 @@ class ThirdPartyType extends AbstractType
|
||||
'label' => 'thirdparty.Acronym',
|
||||
'required' => false
|
||||
])
|
||||
->add('categories', EntityType::class, [
|
||||
'label' => 'thirdparty.Categories',
|
||||
'class' => ThirdPartyCategory::class,
|
||||
'choice_label' => function (ThirdPartyCategory $category): string {
|
||||
return $this->translatableStringHelper->localize($category->getName());
|
||||
},
|
||||
'query_builder' => function (EntityRepository $er): QueryBuilder {
|
||||
return $er->createQueryBuilder('c')
|
||||
->where('c.active = true');
|
||||
},
|
||||
'required' => true,
|
||||
'multiple' => true,
|
||||
'attr' => ['class' => 'select2']
|
||||
])
|
||||
->add('children', ChillCollectionType::class, [
|
||||
'entry_type' => ThirdPartyType::class,
|
||||
'entry_options' => [
|
||||
'is_child' => true,
|
||||
'block_name' => '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' => [
|
||||
'Active, shown to users' => true,
|
||||
'Inactive, not shown to users' => false
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false
|
||||
])
|
||||
;
|
||||
|
||||
// add the types
|
||||
$types = [];
|
||||
foreach ($this->typesManager->getProviders() as $key => $provider) {
|
||||
$types['chill_3party.key_label.'.$key] = $key;
|
||||
}
|
||||
if (count($types) === 1) {
|
||||
$builder
|
||||
->add('types', HiddenType::class, [
|
||||
'data' => array_values($types)
|
||||
])
|
||||
->get('types')
|
||||
->addModelTransformer(new CallbackTransformer(
|
||||
function (?array $typeArray): ?string {
|
||||
if (null === $typeArray) {
|
||||
return null;
|
||||
}
|
||||
return implode(',', $typeArray);
|
||||
},
|
||||
function (?string $typeStr): ?array {
|
||||
if (null === $typeStr) {
|
||||
return null;
|
||||
}
|
||||
return explode(',', $typeStr);
|
||||
}
|
||||
))
|
||||
;
|
||||
} else {
|
||||
$builder
|
||||
->add('types', ChoiceType::class, [
|
||||
'choices' => $types,
|
||||
'expanded' => true,
|
||||
'multiple' => true,
|
||||
'label' => 'thirdparty.Type'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param array $options
|
||||
* @return \Chill\MainBundle\Entity\Center[]
|
||||
*/
|
||||
protected function getReachableCenters(array $options)
|
||||
{
|
||||
switch($options['usage']) {
|
||||
case 'create': $role = new Role(ThirdPartyVoter::CREATE);
|
||||
break;
|
||||
case 'update': $role = new Role(ThirdPartyVoter::UPDATE);
|
||||
break;
|
||||
}
|
||||
|
||||
return $this->authorizationHelper->getReachableCenters(
|
||||
$this->tokenStorage->getToken()->getUser(), $role);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
@@ -240,23 +234,8 @@ class ThirdPartyType extends AbstractType
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => ThirdParty::class
|
||||
'data_class' => ThirdParty::class,
|
||||
'is_child' => false,
|
||||
));
|
||||
|
||||
/*
|
||||
$resolver->setRequired('usage')
|
||||
->setAllowedValues('usage', ['create', 'update'])
|
||||
;
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'chill_thirdpartybundle_thirdparty';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user