mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-28 18:43:49 +00:00
allow to load person interactively
- return results as json in PersonSearch - update PickPersonType to load peoples with ChoiceLoader
This commit is contained in:
@@ -26,12 +26,16 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInt
|
||||
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Chill\MainBundle\Entity\GroupCenter;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\PersonBundle\Entity\PersonRepository;
|
||||
use Chill\PersonBundle\Search\PersonSearch;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Chill\PersonBundle\Form\ChoiceLoader\PersonChoiceLoader;
|
||||
use Symfony\Component\OptionsResolver\Options;
|
||||
|
||||
/**
|
||||
* This type allow to pick a person.
|
||||
@@ -67,22 +71,36 @@ class PickPersonType extends AbstractType
|
||||
* @var AuthorizationHelper
|
||||
*/
|
||||
protected $authorizationHelper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
protected $urlGenerator;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
public function __construct(
|
||||
PersonRepository $personRepository,
|
||||
TokenStorageInterface $tokenStorage,
|
||||
AuthorizationHelper $authorizationHelper
|
||||
AuthorizationHelper $authorizationHelper,
|
||||
UrlGeneratorInterface $urlGenerator,
|
||||
TranslatorInterface $translator
|
||||
)
|
||||
{
|
||||
$this->personRepository = $personRepository;
|
||||
$this->user = $tokenStorage->getToken()->getUser();
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
protected function filterCentersfom(Options $options)
|
||||
{
|
||||
$qb = $options['query_builder'];
|
||||
|
||||
if ($options['role'] === NULL) {
|
||||
$centers = array_map(function (GroupCenter $g) {
|
||||
|
||||
@@ -98,10 +116,10 @@ class PickPersonType extends AbstractType
|
||||
$selectedCenters = $centers;
|
||||
} else {
|
||||
$selectedCenters = array();
|
||||
$options['centers'] = is_array($options['centers']) ?
|
||||
$optionsCenters = is_array($options['centers']) ?
|
||||
$options['centers'] : array($options['centers']);
|
||||
|
||||
foreach ($options['centers'] as $c) {
|
||||
foreach ($optionsCenters as $c) {
|
||||
// check that every member of the array is a center
|
||||
if (!$c instanceof Center) {
|
||||
throw new \RuntimeException('Every member of the "centers" '
|
||||
@@ -115,14 +133,8 @@ class PickPersonType extends AbstractType
|
||||
$selectedCenters[] = $c;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$qb
|
||||
->orderBy('p.firstName', 'ASC')
|
||||
->orderBy('p.lastName', 'ASC')
|
||||
->where($qb->expr()->in('p.center', ':centers'))
|
||||
->setParameter('centers', $selectedCenters)
|
||||
;
|
||||
|
||||
return $selectedCenters;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
@@ -151,7 +163,11 @@ class PickPersonType extends AbstractType
|
||||
);
|
||||
},
|
||||
'attr' => array('class' => 'select2 '),
|
||||
'query_builder' => $this->personRepository->createQueryBuilder('p')
|
||||
'choice_loader' => function(Options $options) {
|
||||
$centers = $this->filterCentersfom($options);
|
||||
|
||||
return new PersonChoiceLoader($this->personRepository, $centers);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
@@ -159,5 +175,17 @@ class PickPersonType extends AbstractType
|
||||
{
|
||||
return EntityType::class;
|
||||
}
|
||||
|
||||
public function buildView(\Symfony\Component\Form\FormView $view, \Symfony\Component\Form\FormInterface $form, array $options)
|
||||
{
|
||||
$view->vars['attr']['data-person-picker'] = true;
|
||||
$view->vars['attr']['data-select-interactive-loading'] = true;
|
||||
$view->vars['attr']['data-search-url'] = $this->urlGenerator
|
||||
->generate('chill_main_search', [ 'name' => PersonSearch::NAME, '_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