mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,45 +1,49 @@
|
||||
<?php
|
||||
/*
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Form\ChoiceLoader;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use function call_user_func;
|
||||
use function in_array;
|
||||
|
||||
/**
|
||||
* Lazy load third parties.
|
||||
*
|
||||
*/
|
||||
class ThirdPartyChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\ThirdPartyBundle\Entity\ThirdParty[]
|
||||
*/
|
||||
protected $lazyLoadedParties = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Center
|
||||
*/
|
||||
protected $center;
|
||||
|
||||
|
||||
/**
|
||||
* @var \Chill\ThirdPartyBundle\Entity\ThirdParty[]
|
||||
*/
|
||||
protected $lazyLoadedParties = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $partyRepository;
|
||||
|
||||
|
||||
public function __construct(Center $center, EntityRepository $partyRepository)
|
||||
{
|
||||
$this->center = $center;
|
||||
$this->partyRepository = $partyRepository;
|
||||
}
|
||||
|
||||
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
return new ArrayChoiceList($this->lazyLoadedParties, $value);
|
||||
@@ -48,39 +52,40 @@ class ThirdPartyChoiceLoader implements ChoiceLoaderInterface
|
||||
public function loadChoicesForValues($values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach($values as $value) {
|
||||
|
||||
foreach ($values as $value) {
|
||||
if (empty($value)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
$party = $this->partyRepository->find($value);
|
||||
|
||||
if (FALSE === \in_array($this->center, $party->getCenters()->toArray())) {
|
||||
throw new \RuntimeException("the party's center is not authorized");
|
||||
|
||||
if (false === in_array($this->center, $party->getCenters()->toArray())) {
|
||||
throw new RuntimeException("the party's center is not authorized");
|
||||
}
|
||||
|
||||
|
||||
$choices[] = $party;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (NULL === $choice) {
|
||||
if (null === $choice) {
|
||||
$values[] = null;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = \call_user_func($value, $choice);
|
||||
|
||||
$id = call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedParties[$id] = $choice;
|
||||
}
|
||||
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
||||
|
@@ -1,51 +1,52 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Form;
|
||||
|
||||
use Chill\MainBundle\Entity\Address;
|
||||
use Chill\MainBundle\Entity\Civility;
|
||||
use Chill\MainBundle\Form\Type\ChillCollectionType;
|
||||
use Chill\MainBundle\Form\Type\ChillTextareaType;
|
||||
use Chill\MainBundle\Form\Type\PickAddressType;
|
||||
use Chill\MainBundle\Form\Type\PickCenterType;
|
||||
use Chill\MainBundle\Form\Type\ChillTextareaType;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
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 Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter;
|
||||
use Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\EmailType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Chill\ThirdPartyBundle\Security\Voter\ThirdPartyVoter;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
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\HiddenType;
|
||||
|
||||
use function array_key_exists;
|
||||
|
||||
class ThirdPartyType extends AbstractType
|
||||
{
|
||||
protected AuthorizationHelper $authorizationHelper;
|
||||
|
||||
protected TokenStorageInterface $tokenStorage;
|
||||
protected EntityManagerInterface $om;
|
||||
|
||||
protected ThirdPartyTypeManager $typesManager;
|
||||
protected TokenStorageInterface $tokenStorage;
|
||||
|
||||
protected TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
protected EntityManagerInterface $om;
|
||||
protected ThirdPartyTypeManager $typesManager;
|
||||
|
||||
private bool $askCenter;
|
||||
|
||||
@@ -65,35 +66,32 @@ class ThirdPartyType extends AbstractType
|
||||
$this->askCenter = $parameterBag->get('chill_main')['acl']['form_show_centers'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'required' => true
|
||||
'required' => true,
|
||||
])
|
||||
->add('telephone', TextType::class, [
|
||||
'label' => 'Phonenumber',
|
||||
'required' => false
|
||||
'required' => false,
|
||||
])
|
||||
->add('email', EmailType::class, [
|
||||
'required' => false
|
||||
'required' => false,
|
||||
])
|
||||
->add('comment', ChillTextareaType::class, [
|
||||
'required' => false
|
||||
'required' => false,
|
||||
]);
|
||||
|
||||
if ($this->askCenter) {
|
||||
$builder
|
||||
->add('centers', PickCenterType::class, [
|
||||
'role' => (\array_key_exists('data', $options) && $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']
|
||||
]
|
||||
'attr' => ['class' => 'select2'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -111,7 +109,7 @@ class ThirdPartyType extends AbstractType
|
||||
->where('c.active = true');
|
||||
},
|
||||
'placeholder' => 'thirdparty.choose civility',
|
||||
'required' => false
|
||||
'required' => false,
|
||||
])
|
||||
->add('profession', EntityType::class, [
|
||||
'label' => 'thirdparty.Profession',
|
||||
@@ -124,19 +122,18 @@ class ThirdPartyType extends AbstractType
|
||||
->where('p.active = true');
|
||||
},
|
||||
'placeholder' => 'thirdparty.choose profession',
|
||||
'required' => false
|
||||
'required' => false,
|
||||
])
|
||||
->add('contactDataAnonymous', CheckboxType::class, [
|
||||
'required' => false,
|
||||
'label' => 'thirdparty.Contact data are confidential'
|
||||
])
|
||||
;
|
||||
'label' => 'thirdparty.Contact data are confidential',
|
||||
]);
|
||||
|
||||
// Institutional ThirdParty (parent)
|
||||
} else {
|
||||
$builder
|
||||
->add('address', PickAddressType::class, [
|
||||
'label' => 'Address'
|
||||
'label' => 'Address',
|
||||
])
|
||||
->add('address2', PickAddressType::class, [
|
||||
'label' => 'Address',
|
||||
@@ -146,11 +143,11 @@ class ThirdPartyType extends AbstractType
|
||||
])
|
||||
->add('nameCompany', TextType::class, [
|
||||
'label' => 'thirdparty.NameCompany',
|
||||
'required' => false
|
||||
'required' => false,
|
||||
])
|
||||
->add('acronym', TextType::class, [
|
||||
'label' => 'thirdparty.Acronym',
|
||||
'required' => false
|
||||
'required' => false,
|
||||
])
|
||||
->add('activeChildren', ChillCollectionType::class, [
|
||||
'entry_type' => ThirdPartyType::class,
|
||||
@@ -163,40 +160,35 @@ class ThirdPartyType extends AbstractType
|
||||
'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"
|
||||
])
|
||||
;
|
||||
'button_add_label' => 'Add a contact',
|
||||
'button_remove_label' => 'Remove a contact',
|
||||
'empty_collection_explain' => 'Any contact',
|
||||
]);
|
||||
}
|
||||
|
||||
if (ThirdParty::KIND_CHILD !== $options['kind']) {
|
||||
$builder
|
||||
->add('typesAndCategories', PickThirdPartyTypeCategoryType::class, [
|
||||
'label' => 'thirdparty.Categories'
|
||||
'label' => 'thirdparty.Categories',
|
||||
])
|
||||
->add('active', ChoiceType::class, [
|
||||
'label' => 'thirdparty.Status',
|
||||
'choices' => [
|
||||
'Active, shown to users' => true,
|
||||
'Inactive, not shown to users' => false
|
||||
'Inactive, not shown to users' => false,
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false
|
||||
'multiple' => false,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
$resolver->setDefaults([
|
||||
'data_class' => ThirdParty::class,
|
||||
'is_child' => false,
|
||||
'kind' => null
|
||||
));
|
||||
'kind' => null,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@@ -1,55 +1,55 @@
|
||||
<?php
|
||||
/*
|
||||
*
|
||||
*/
|
||||
namespace Chill\ThirdPartyBundle\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Chill\ThirdPartyBundle\Form\ChoiceLoader\ThirdPartyChoiceLoader;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Chill\ThirdPartyBundle\Search\ThirdPartySearch;
|
||||
use Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager;
|
||||
use Chill\MainBundle\Search\SearchInterface;
|
||||
|
||||
/**
|
||||
*
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Form\Type;
|
||||
|
||||
use Chill\MainBundle\Search\SearchInterface;
|
||||
use Chill\ThirdPartyBundle\Entity\ThirdParty;
|
||||
use Chill\ThirdPartyBundle\Form\ChoiceLoader\ThirdPartyChoiceLoader;
|
||||
use Chill\ThirdPartyBundle\Search\ThirdPartySearch;
|
||||
use Chill\ThirdPartyBundle\ThirdPartyType\ThirdPartyTypeManager;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use function array_diff;
|
||||
use function array_merge;
|
||||
use function is_array;
|
||||
|
||||
class PickThirdPartyType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ThirdPartyTypeManager
|
||||
*/
|
||||
protected $typesManager;
|
||||
|
||||
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
UrlGeneratorInterface $urlGenerator,
|
||||
EntityManagerInterface $em,
|
||||
UrlGeneratorInterface $urlGenerator,
|
||||
TranslatorInterface $translator,
|
||||
ThirdPartyTypeManager $typesManager
|
||||
) {
|
||||
@@ -59,54 +59,55 @@ class PickThirdPartyType extends AbstractType
|
||||
$this->typesManager = $typesManager;
|
||||
}
|
||||
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
->setRequired('center')
|
||||
->setAllowedTypes('center', [ \Chill\MainBundle\Entity\Center::class ])
|
||||
->setDefined('types')
|
||||
->setRequired('types')
|
||||
->setAllowedValues('types', function($types) {
|
||||
if (FALSE === \is_array($types)) {
|
||||
return false;
|
||||
}
|
||||
// return false if one element is not contained in allowed types
|
||||
return count(\array_diff($types, $this->typesManager->getTypes())) === 0;
|
||||
})
|
||||
;
|
||||
|
||||
$resolver
|
||||
->setDefault('class', ThirdParty::class)
|
||||
->setDefault('choice_label', function(ThirdParty $tp) {
|
||||
return $tp->getName();
|
||||
})
|
||||
->setDefault('choice_loader', function(Options $options) {
|
||||
return new ThirdPartyChoiceLoader($options['center'],
|
||||
$this->em->getRepository(ThirdParty::class));
|
||||
})
|
||||
;
|
||||
}
|
||||
|
||||
public function getParent(): string
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
|
||||
public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr']['class'] = \array_merge(['select2 '], $view->vars['attr']['class'] ?? []);
|
||||
$view->vars['attr']['class'] = array_merge(['select2 '], $view->vars['attr']['class'] ?? []);
|
||||
$view->vars['attr']['data-3party-picker'] = true;
|
||||
$view->vars['attr']['data-select-interactive-loading'] = true;
|
||||
$view->vars['attr']['data-search-url'] = $this->urlGenerator
|
||||
->generate('chill_main_search', [
|
||||
'name' => ThirdPartySearch::NAME,
|
||||
SearchInterface::REQUEST_QUERY_KEY_ADD_PARAMETERS => ['t' => $options['types']],
|
||||
'_format' => 'json' ]
|
||||
);
|
||||
->generate(
|
||||
'chill_main_search',
|
||||
[
|
||||
'name' => ThirdPartySearch::NAME,
|
||||
SearchInterface::REQUEST_QUERY_KEY_ADD_PARAMETERS => ['t' => $options['types']],
|
||||
'_format' => 'json', ]
|
||||
);
|
||||
$view->vars['attr']['data-placeholder'] = $this->translator->trans($options['placeholder']);
|
||||
$view->vars['attr']['data-no-results-label'] = $this->translator->trans('select2.no_results');
|
||||
$view->vars['attr']['data-error-load-label'] = $this->translator->trans('select2.error_loading');
|
||||
$view->vars['attr']['data-searching-label'] = $this->translator->trans('select2.searching');
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver
|
||||
->setRequired('center')
|
||||
->setAllowedTypes('center', [\Chill\MainBundle\Entity\Center::class])
|
||||
->setDefined('types')
|
||||
->setRequired('types')
|
||||
->setAllowedValues('types', function ($types) {
|
||||
if (false === is_array($types)) {
|
||||
return false;
|
||||
}
|
||||
// return false if one element is not contained in allowed types
|
||||
return count(array_diff($types, $this->typesManager->getTypes())) === 0;
|
||||
});
|
||||
|
||||
$resolver
|
||||
->setDefault('class', ThirdParty::class)
|
||||
->setDefault('choice_label', function (ThirdParty $tp) {
|
||||
return $tp->getName();
|
||||
})
|
||||
->setDefault('choice_loader', function (Options $options) {
|
||||
return new ThirdPartyChoiceLoader(
|
||||
$options['center'],
|
||||
$this->em->getRepository(ThirdParty::class)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public function getParent(): string
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
}
|
||||
|
@@ -1,27 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
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;
|
||||
use function array_merge;
|
||||
use function implode;
|
||||
use function uasort;
|
||||
|
||||
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.';
|
||||
|
||||
private ThirdPartyCategoryRepository $thirdPartyCategoryRepository;
|
||||
|
||||
private ThirdPartyTypeManager $thirdPartyTypeManager;
|
||||
|
||||
private TranslatableStringHelper $translatableStringHelper;
|
||||
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct(
|
||||
ThirdPartyCategoryRepository $thirdPartyCategoryRepository,
|
||||
ThirdPartyTypeManager $thirdPartyTypeManager,
|
||||
@@ -34,67 +45,68 @@ class PickThirdPartyTypeCategoryType extends \Symfony\Component\Form\AbstractTyp
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
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 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) {
|
||||
if (null === $value) {
|
||||
return null;
|
||||
}
|
||||
if (is_array($value)){
|
||||
|
||||
if (is_array($value)) {
|
||||
$r = [];
|
||||
|
||||
foreach ($value as $v) {
|
||||
$r[] = $this->transform($v);
|
||||
$r[] = $this->transform($v);
|
||||
}
|
||||
|
||||
return $r;
|
||||
}
|
||||
|
||||
if ($value instanceof ThirdPartyCategory) {
|
||||
return 'category:'.$value->getId();
|
||||
return 'category:' . $value->getId();
|
||||
}
|
||||
|
||||
if (is_string($value)) {
|
||||
return 'type:'.$value;
|
||||
return 'type:' . $value;
|
||||
}
|
||||
|
||||
throw new UnexpectedTypeException($value, \implode(' or ', ['array', 'string', ThirdPartyCategory::class]));
|
||||
throw new UnexpectedTypeException($value, implode(' or ', ['array', 'string', ThirdPartyCategory::class]));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user