mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-20 14:43:49 +00:00
fix folder name
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/*
|
||||
*/
|
||||
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 Symfony\Component\Form\ChoiceList\ArrayChoiceList;
|
||||
|
||||
/**
|
||||
* Lazy load third parties.
|
||||
*
|
||||
*/
|
||||
class ThirdPartyChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Chill\ThirdPartyBundle\Entity\ThirdParty[]
|
||||
*/
|
||||
protected $lazyLoadedParties = [];
|
||||
|
||||
/**
|
||||
*
|
||||
* @var Center
|
||||
*/
|
||||
protected $center;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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);
|
||||
}
|
||||
|
||||
public function loadChoicesForValues($values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
$choices[] = $party;
|
||||
}
|
||||
|
||||
return $choices;
|
||||
}
|
||||
|
||||
public function loadValuesForChoices(array $choices, $value = null)
|
||||
{
|
||||
$values = [];
|
||||
|
||||
foreach ($choices as $choice) {
|
||||
if (NULL === $choice) {
|
||||
$values[] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
$id = \call_user_func($value, $choice);
|
||||
$values[] = $id;
|
||||
$this->lazyLoadedParties[$id] = $choice;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
144
src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php
Normal file
144
src/Bundle/ChillThirdPartyBundle/Form/ThirdPartyType.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace Chill\ThirdPartyBundle\Form;
|
||||
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
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 Symfony\Component\Security\Core\Role\Role;
|
||||
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;
|
||||
|
||||
|
||||
class ThirdPartyType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var AuthorizationHelper
|
||||
*/
|
||||
protected $authorizationHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TokenStorageInterface
|
||||
*/
|
||||
protected $tokenStorage;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ThirdPartyTypeManager
|
||||
*/
|
||||
protected $typesManager;
|
||||
|
||||
public function __construct(
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
TokenStorageInterface $tokenStorage,
|
||||
ThirdPartyTypeManager $typesManager
|
||||
) {
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->tokenStorage = $tokenStorage;
|
||||
$this->typesManager = $typesManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$types = [];
|
||||
foreach ($this->typesManager->getProviders() as $key => $provider) {
|
||||
$types['chill_3party.key_label.'.$key] = $key;
|
||||
}
|
||||
|
||||
$builder
|
||||
->add('name', TextType::class, [
|
||||
'required' => true
|
||||
])
|
||||
->add('telephone', TextType::class, [
|
||||
'label' => 'Phonenumber',
|
||||
'required' => false
|
||||
])
|
||||
->add('email', EmailType::class, [
|
||||
'required' => false
|
||||
])
|
||||
->add('comment', TextareaType::class, [
|
||||
'required' => false
|
||||
])
|
||||
->add('type', ChoiceType::class, [
|
||||
'choices' => $types,
|
||||
'expanded' => true,
|
||||
'multiple' => true,
|
||||
'label' => 'thirdparty.Type'
|
||||
])
|
||||
->add('active', ChoiceType::class, [
|
||||
'choices' => [
|
||||
'Active, shown to users' => true,
|
||||
'Inactive, not shown to users' => false
|
||||
],
|
||||
'expanded' => true,
|
||||
'multiple' => false
|
||||
])
|
||||
->add('centers', EntityType::class, [
|
||||
'choices' => $this->getReachableCenters($options),
|
||||
'class' => \Chill\MainBundle\Entity\Center::class,
|
||||
'multiple' => true,
|
||||
'expanded' => true
|
||||
])
|
||||
->add('address', AddressType::class, [
|
||||
'has_valid_from' => false,
|
||||
'null_if_empty' => true,
|
||||
'required' => false
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @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}
|
||||
*/
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
$resolver->setDefaults(array(
|
||||
'data_class' => 'Chill\ThirdPartyBundle\Entity\ThirdParty'
|
||||
));
|
||||
|
||||
$resolver->setRequired('usage')
|
||||
->setAllowedValues('usage', ['create', 'update'])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getBlockPrefix()
|
||||
{
|
||||
return 'chill_thirdpartybundle_thirdparty';
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,112 @@
|
||||
<?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;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
class PickThirdPartyType extends AbstractType
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var EntityManagerInterface
|
||||
*/
|
||||
protected $em;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var ThirdPartyTypeManager
|
||||
*/
|
||||
protected $typesManager;
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
UrlGeneratorInterface $urlGenerator,
|
||||
TranslatorInterface $translator,
|
||||
ThirdPartyTypeManager $typesManager
|
||||
) {
|
||||
$this->em = $em;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->translator = $translator;
|
||||
$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']['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' ]
|
||||
);
|
||||
$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');
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user