mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +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:
parent
b4801734d3
commit
f2ed32e458
@ -32,6 +32,7 @@ use Symfony\Component\Security\Core\Role\Role;
|
|||||||
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
use Chill\PersonBundle\Security\Authorization\PersonVoter;
|
||||||
use Chill\PersonBundle\Search\SimilarPersonMatcher;
|
use Chill\PersonBundle\Search\SimilarPersonMatcher;
|
||||||
use Symfony\Component\Translation\TranslatorInterface;
|
use Symfony\Component\Translation\TranslatorInterface;
|
||||||
|
use Chill\MainBundle\Search\SearchProvider;
|
||||||
|
|
||||||
class PersonController extends Controller
|
class PersonController extends Controller
|
||||||
{
|
{
|
||||||
@ -46,7 +47,7 @@ class PersonController extends Controller
|
|||||||
* @var TranslatorInterface
|
* @var TranslatorInterface
|
||||||
*/
|
*/
|
||||||
protected $translator;
|
protected $translator;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
SimilarPersonMatcher $similarPersonMatcher,
|
SimilarPersonMatcher $similarPersonMatcher,
|
||||||
TranslatorInterface $translator
|
TranslatorInterface $translator
|
||||||
|
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\Exception\AccessDeniedException;
|
||||||
use Symfony\Component\Security\Core\Role\Role;
|
use Symfony\Component\Security\Core\Role\Role;
|
||||||
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
|
||||||
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||||
use Chill\MainBundle\Entity\GroupCenter;
|
use Chill\MainBundle\Entity\GroupCenter;
|
||||||
use Chill\PersonBundle\Entity\Person;
|
use Chill\PersonBundle\Entity\Person;
|
||||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||||
use Chill\MainBundle\Entity\Center;
|
use Chill\MainBundle\Entity\Center;
|
||||||
use Chill\PersonBundle\Entity\PersonRepository;
|
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.
|
* This type allow to pick a person.
|
||||||
@ -67,22 +71,36 @@ class PickPersonType extends AbstractType
|
|||||||
* @var AuthorizationHelper
|
* @var AuthorizationHelper
|
||||||
*/
|
*/
|
||||||
protected $authorizationHelper;
|
protected $authorizationHelper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var UrlGeneratorInterface
|
||||||
|
*/
|
||||||
|
protected $urlGenerator;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @var TranslatorInterface
|
||||||
|
*/
|
||||||
|
protected $translator;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
PersonRepository $personRepository,
|
PersonRepository $personRepository,
|
||||||
TokenStorageInterface $tokenStorage,
|
TokenStorageInterface $tokenStorage,
|
||||||
AuthorizationHelper $authorizationHelper
|
AuthorizationHelper $authorizationHelper,
|
||||||
|
UrlGeneratorInterface $urlGenerator,
|
||||||
|
TranslatorInterface $translator
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
$this->personRepository = $personRepository;
|
$this->personRepository = $personRepository;
|
||||||
$this->user = $tokenStorage->getToken()->getUser();
|
$this->user = $tokenStorage->getToken()->getUser();
|
||||||
$this->authorizationHelper = $authorizationHelper;
|
$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) {
|
if ($options['role'] === NULL) {
|
||||||
$centers = array_map(function (GroupCenter $g) {
|
$centers = array_map(function (GroupCenter $g) {
|
||||||
|
|
||||||
@ -98,10 +116,10 @@ class PickPersonType extends AbstractType
|
|||||||
$selectedCenters = $centers;
|
$selectedCenters = $centers;
|
||||||
} else {
|
} else {
|
||||||
$selectedCenters = array();
|
$selectedCenters = array();
|
||||||
$options['centers'] = is_array($options['centers']) ?
|
$optionsCenters = is_array($options['centers']) ?
|
||||||
$options['centers'] : 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
|
// check that every member of the array is a center
|
||||||
if (!$c instanceof Center) {
|
if (!$c instanceof Center) {
|
||||||
throw new \RuntimeException('Every member of the "centers" '
|
throw new \RuntimeException('Every member of the "centers" '
|
||||||
@ -115,14 +133,8 @@ class PickPersonType extends AbstractType
|
|||||||
$selectedCenters[] = $c;
|
$selectedCenters[] = $c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $selectedCenters;
|
||||||
$qb
|
|
||||||
->orderBy('p.firstName', 'ASC')
|
|
||||||
->orderBy('p.lastName', 'ASC')
|
|
||||||
->where($qb->expr()->in('p.center', ':centers'))
|
|
||||||
->setParameter('centers', $selectedCenters)
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function configureOptions(OptionsResolver $resolver)
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
@ -151,7 +163,11 @@ class PickPersonType extends AbstractType
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
'attr' => array('class' => 'select2 '),
|
'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;
|
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');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,8 @@ services:
|
|||||||
- "@chill.person.repository.person"
|
- "@chill.person.repository.person"
|
||||||
- "@security.token_storage"
|
- "@security.token_storage"
|
||||||
- "@chill.main.security.authorization.helper"
|
- "@chill.main.security.authorization.helper"
|
||||||
|
- '@Symfony\Component\Routing\Generator\UrlGeneratorInterface'
|
||||||
|
- '@Symfony\Component\Translation\TranslatorInterface'
|
||||||
tags:
|
tags:
|
||||||
- { name: form.type }
|
- { name: form.type }
|
||||||
|
|
||||||
|
@ -35,6 +35,7 @@ use Chill\MainBundle\Form\Type\ChillDateType;
|
|||||||
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
||||||
use Symfony\Component\Form\FormBuilderInterface;
|
use Symfony\Component\Form\FormBuilderInterface;
|
||||||
use Chill\MainBundle\Search\HasAdvancedSearchFormInterface;
|
use Chill\MainBundle\Search\HasAdvancedSearchFormInterface;
|
||||||
|
use Doctrine\ORM\Query;
|
||||||
|
|
||||||
class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
|
class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
|
||||||
HasAdvancedSearchFormInterface
|
HasAdvancedSearchFormInterface
|
||||||
@ -104,7 +105,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function supports($domain)
|
public function supports($domain, $format)
|
||||||
{
|
{
|
||||||
return 'person' === $domain;
|
return 'person' === $domain;
|
||||||
}
|
}
|
||||||
@ -113,23 +114,32 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
|
|||||||
* (non-PHPdoc)
|
* (non-PHPdoc)
|
||||||
* @see \Chill\MainBundle\Search\SearchInterface::renderResult()
|
* @see \Chill\MainBundle\Search\SearchInterface::renderResult()
|
||||||
*/
|
*/
|
||||||
public function renderResult(array $terms, $start = 0, $limit = 50, array $options = array())
|
public function renderResult(array $terms, $start = 0, $limit = 50, array $options = array(), $format = 'html')
|
||||||
{
|
{
|
||||||
$total = $this->count($terms);
|
$total = $this->count($terms);
|
||||||
$paginator = $this->paginatorFactory->create($total);
|
$paginator = $this->paginatorFactory->create($total);
|
||||||
|
|
||||||
return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig',
|
if ($format === 'html') {
|
||||||
array(
|
return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig',
|
||||||
'persons' => $this->search($terms, $start, $limit, $options),
|
array(
|
||||||
'pattern' => $this->recomposePattern($terms, array('nationality',
|
'persons' => $this->search($terms, $start, $limit, $options),
|
||||||
'firstname', 'lastname', 'birthdate', 'gender',
|
'pattern' => $this->recomposePattern($terms, array('nationality',
|
||||||
'birthdate-before','birthdate-after'), $terms['_domain']),
|
'firstname', 'lastname', 'birthdate', 'gender',
|
||||||
'total' => $total,
|
'birthdate-before','birthdate-after'), $terms['_domain']),
|
||||||
'start' => $start,
|
'total' => $total,
|
||||||
'search_name' => self::NAME,
|
'start' => $start,
|
||||||
'preview' => $options[SearchInterface::SEARCH_PREVIEW_OPTION],
|
'search_name' => self::NAME,
|
||||||
'paginator' => $paginator
|
'preview' => $options[SearchInterface::SEARCH_PREVIEW_OPTION],
|
||||||
));
|
'paginator' => $paginator
|
||||||
|
));
|
||||||
|
} elseif ($format === 'json') {
|
||||||
|
return [
|
||||||
|
'results' => $this->search($terms, $start, $limit, \array_merge($options, [ 'simplify' => true ])),
|
||||||
|
'pagination' => [
|
||||||
|
'more' => $paginator->hasNextPage()
|
||||||
|
]
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,17 +153,35 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
|
|||||||
protected function search(array $terms, $start, $limit, array $options = array())
|
protected function search(array $terms, $start, $limit, array $options = array())
|
||||||
{
|
{
|
||||||
$qb = $this->createQuery($terms, 'search');
|
$qb = $this->createQuery($terms, 'search');
|
||||||
|
|
||||||
|
if ($options['simplify'] ?? false) {
|
||||||
|
$qb->select(
|
||||||
|
'p.id',
|
||||||
|
$qb->expr()->concat(
|
||||||
|
'p.firstName',
|
||||||
|
$qb->expr()->literal(' '),
|
||||||
|
'p.lastName'
|
||||||
|
).'AS text'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
$qb->select('p');
|
||||||
|
}
|
||||||
|
|
||||||
$qb->select('p')
|
$qb
|
||||||
->setMaxResults($limit)
|
->setMaxResults($limit)
|
||||||
->setFirstResult($start);
|
->setFirstResult($start);
|
||||||
|
|
||||||
//order by firstname, lastname
|
//order by firstname, lastname
|
||||||
|
|
||||||
$qb->orderBy('p.firstName')
|
$qb
|
||||||
->addOrderBy('p.lastName');
|
->orderBy('p.firstName')
|
||||||
|
->addOrderBy('p.lastName');
|
||||||
return $qb->getQuery()->getResult();
|
|
||||||
|
if ($options['simplify'] ?? false) {
|
||||||
|
return $qb->getQuery()->getResult(Query::HYDRATE_ARRAY);
|
||||||
|
} else {
|
||||||
|
return $qb->getQuery()->getResult();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function count(array $terms)
|
protected function count(array $terms)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user