mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-27 10:03: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:
104
Form/ChoiceLoader/PersonChoiceLoader.php
Normal file
104
Form/ChoiceLoader/PersonChoiceLoader.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs-Libres <info@champs-libres.coop>
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
namespace Chill\PersonBundle\Form\ChoiceLoader;
|
||||
|
||||
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
|
||||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class PersonChoiceLoader implements ChoiceLoaderInterface
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var EntityRepository
|
||||
*/
|
||||
protected $personRepository;
|
||||
|
||||
protected $lazyLoadedPersons = [];
|
||||
|
||||
protected $centers = [];
|
||||
|
||||
public function __construct(
|
||||
EntityRepository $personRepository,
|
||||
array $centers = null
|
||||
) {
|
||||
$this->personRepository = $personRepository;
|
||||
if (NULL !== $centers) {
|
||||
$this->centers = $centers;
|
||||
}
|
||||
}
|
||||
|
||||
protected function hasCenterFilter()
|
||||
{
|
||||
return count($this->centers) > 0;
|
||||
}
|
||||
|
||||
public function loadChoiceList($value = null): ChoiceListInterface
|
||||
{
|
||||
$list = new \Symfony\Component\Form\ChoiceList\ArrayChoiceList(
|
||||
$this->lazyLoadedPersons,
|
||||
function(Person $p) use ($value) {
|
||||
return \call_user_func($value, $p);
|
||||
});
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
public function loadChoicesForValues(array $values, $value = null)
|
||||
{
|
||||
$choices = [];
|
||||
|
||||
foreach($values as $value) {
|
||||
$person = $this->personRepository->find($value);
|
||||
|
||||
if ($this->hasCenterFilter() &&
|
||||
!\in_array($person->getCenter(), $this->centers)) {
|
||||
throw new \RuntimeException("chosen a person not in correct center");
|
||||
}
|
||||
|
||||
$choices[] = $person;
|
||||
}
|
||||
|
||||
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->lazyLoadedPersons[$id] = $choice;
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
}
|
@@ -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