mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
handle types and categories in a single select input
This commit is contained in:
@@ -11,9 +11,12 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdPartyProfession;
|
||||
use Chill\ThirdPartyBundle\Form\Type\PickThirdPartyType;
|
||||
use Chill\ThirdPartyBundle\Form\Type\PickThirdPartyTypeCategoryType;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
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\Extension\Core\Type\CheckboxType;
|
||||
@@ -40,14 +43,14 @@ class ThirdPartyType extends AbstractType
|
||||
|
||||
protected TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
protected ObjectManager $om;
|
||||
protected EntityManagerInterface $om;
|
||||
|
||||
public function __construct(
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
TokenStorageInterface $tokenStorage,
|
||||
ThirdPartyTypeManager $typesManager,
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
ObjectManager $om
|
||||
EntityManagerInterface $om
|
||||
) {
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
@@ -171,21 +174,9 @@ class ThirdPartyType extends AbstractType
|
||||
}
|
||||
|
||||
if (ThirdParty::KIND_CHILD !== $options['kind']) {
|
||||
|
||||
$builder
|
||||
->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('typesAndCategories', PickThirdPartyTypeCategoryType::class, [
|
||||
'label' => 'thirdparty.Categories'
|
||||
])
|
||||
->add('active', ChoiceType::class, [
|
||||
'label' => 'thirdparty.Status',
|
||||
@@ -196,42 +187,6 @@ class ThirdPartyType extends AbstractType
|
||||
'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'
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdPartyCategory;
|
||||
use Chill\ThirdPartyBundle\Repository\ThirdPartyCategoryRepository;
|
||||
use Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class PickThirdPartyTypeCategoryType extends \Symfony\Component\Form\AbstractType
|
||||
{
|
||||
private ThirdPartyCategoryRepository $thirdPartyCategoryRepository;
|
||||
private ThirdPartyTypeManager $thirdPartyTypeManager;
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
private const PREFIX_TYPE = 'chill_3party.key_label.';
|
||||
|
||||
public function __construct(
|
||||
ThirdPartyCategoryRepository $thirdPartyCategoryRepository,
|
||||
ThirdPartyTypeManager $thirdPartyTypeManager,
|
||||
TranslatableStringHelper $translatableStringHelper,
|
||||
TranslatorInterface $translator
|
||||
) {
|
||||
$this->thirdPartyCategoryRepository = $thirdPartyCategoryRepository;
|
||||
$this->thirdPartyTypeManager = $thirdPartyTypeManager;
|
||||
$this->translatableStringHelper = $translatableStringHelper;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function getParent()
|
||||
{
|
||||
return ChoiceType::class;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$choices = \array_merge(
|
||||
$this->thirdPartyCategoryRepository->findBy(['active' => true]),
|
||||
$this->thirdPartyTypeManager->getTypes()
|
||||
);
|
||||
|
||||
\uasort($choices, function ($itemA, $itemB) {
|
||||
$strA = $itemA instanceof ThirdPartyCategory ? $this->translatableStringHelper
|
||||
->localize($itemA->getName()) : $this->translator->trans(self::PREFIX_TYPE.$itemA);
|
||||
$strB = $itemB instanceof ThirdPartyCategory ? $this->translatableStringHelper
|
||||
->localize($itemB->getName()) : $this->translator->trans(self::PREFIX_TYPE.$itemB);
|
||||
|
||||
return $strA <=> $strB;
|
||||
});
|
||||
|
||||
|
||||
$resolver->setDefaults([
|
||||
'choices' => $choices,
|
||||
'attr' => [ 'class' => 'select2' ],
|
||||
'multiple' => true,
|
||||
'choice_label' => function($item) {
|
||||
if ($item instanceof ThirdPartyCategory) {
|
||||
return $this->translatableStringHelper->localize($item->getName());
|
||||
}
|
||||
return self::PREFIX_TYPE.$item;
|
||||
},
|
||||
'choice_value' => function($item) {
|
||||
return $this->reverseTransform($item);
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
if ($value === null) {
|
||||
return null;
|
||||
}
|
||||
if (is_array($value)){
|
||||
$r = [];
|
||||
|
||||
foreach ($value as $v) {
|
||||
$r[] = $this->transform($v);
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
if ($value instanceof ThirdPartyCategory) {
|
||||
return 'category:'.$value->getId();
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
return 'type:'.$value;
|
||||
}
|
||||
|
||||
throw new UnexpectedTypeException($value, \implode(' or ', ['array', 'string', ThirdPartyCategory::class]));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user