fix deprecations: use choices_as_values=true and flip content of the choice options

This commit is contained in:
nobohan 2018-04-04 17:53:11 +02:00
parent be6d3493ed
commit b004c65ec1
2 changed files with 55 additions and 53 deletions

View File

@ -50,9 +50,10 @@ class GenderFilter implements FilterInterface,
{ {
$builder->add('accepted_genders', ChoiceType::class, array( $builder->add('accepted_genders', ChoiceType::class, array(
'choices' => array( 'choices' => array(
Person::FEMALE_GENDER => 'Woman', 'Woman' => Person::FEMALE_GENDER,
Person::MALE_GENDER => 'Man' 'Man' => Person::MALE_GENDER
), ),
'choices_as_values' => true,
'multiple' => false, 'multiple' => false,
'expanded' => false 'expanded' => false
)); ));

View File

@ -40,34 +40,34 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
HasAdvancedSearchFormInterface HasAdvancedSearchFormInterface
{ {
use ContainerAwareTrait; use ContainerAwareTrait;
/** /**
* *
* @var EntityManagerInterface * @var EntityManagerInterface
*/ */
private $em; private $em;
/** /**
* *
* @var \Chill\MainBundle\Entity\User * @var \Chill\MainBundle\Entity\User
*/ */
private $user; private $user;
/** /**
* *
* @var AuthorizationHelper * @var AuthorizationHelper
*/ */
private $helper; private $helper;
/** /**
* *
* @var PaginatorFactory * @var PaginatorFactory
*/ */
protected $paginatorFactory; protected $paginatorFactory;
const NAME = "person_regular"; const NAME = "person_regular";
public function __construct( public function __construct(
EntityManagerInterface $em, EntityManagerInterface $em,
TokenStorage $tokenStorage, TokenStorage $tokenStorage,
@ -78,7 +78,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
$this->user = $tokenStorage->getToken()->getUser(); $this->user = $tokenStorage->getToken()->getUser();
$this->helper = $helper; $this->helper = $helper;
$this->paginatorFactory = $paginatorFactory; $this->paginatorFactory = $paginatorFactory;
// throw an error if user is not a valid user // throw an error if user is not a valid user
if (!$this->user instanceof \Chill\MainBundle\Entity\User) { if (!$this->user instanceof \Chill\MainBundle\Entity\User) {
throw new \LogicException('The user provided must be an instance' throw new \LogicException('The user provided must be an instance'
@ -103,7 +103,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
{ {
return true; return true;
} }
public function supports($domain) public function supports($domain)
{ {
return 'person' === $domain; return 'person' === $domain;
@ -117,12 +117,12 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
{ {
$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', return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig',
array( array(
'persons' => $this->search($terms, $start, $limit, $options), 'persons' => $this->search($terms, $start, $limit, $options),
'pattern' => $this->recomposePattern($terms, array('nationality', 'pattern' => $this->recomposePattern($terms, array('nationality',
'firstname', 'lastname', 'birthdate', 'gender', 'firstname', 'lastname', 'birthdate', 'gender',
'birthdate-before','birthdate-after'), $terms['_domain']), 'birthdate-before','birthdate-after'), $terms['_domain']),
'total' => $total, 'total' => $total,
'start' => $start, 'start' => $start,
@ -131,7 +131,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
'paginator' => $paginator 'paginator' => $paginator
)); ));
} }
/** /**
* *
* @param string $pattern * @param string $pattern
@ -143,31 +143,31 @@ 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');
$qb->select('p') $qb->select('p')
->setMaxResults($limit) ->setMaxResults($limit)
->setFirstResult($start); ->setFirstResult($start);
//order by firstname, lastname //order by firstname, lastname
$qb->orderBy('p.firstName') $qb->orderBy('p.firstName')
->addOrderBy('p.lastName'); ->addOrderBy('p.lastName');
return $qb->getQuery()->getResult(); return $qb->getQuery()->getResult();
} }
protected function count(array $terms) protected function count(array $terms)
{ {
$qb = $this->createQuery($terms); $qb = $this->createQuery($terms);
$qb->select('COUNT(p.id)'); $qb->select('COUNT(p.id)');
return $qb->getQuery()->getSingleScalarResult(); return $qb->getQuery()->getSingleScalarResult();
} }
private $_cacheQuery = array(); private $_cacheQuery = array();
/** /**
* *
* @param array $terms * @param array $terms
@ -180,21 +180,21 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
if (array_key_exists($cacheKey, $this->_cacheQuery)) { if (array_key_exists($cacheKey, $this->_cacheQuery)) {
return clone $this->_cacheQuery[$cacheKey]; return clone $this->_cacheQuery[$cacheKey];
} }
$qb = $this->em->createQueryBuilder(); $qb = $this->em->createQueryBuilder();
$qb->from('ChillPersonBundle:Person', 'p'); $qb->from('ChillPersonBundle:Person', 'p');
if (array_key_exists('firstname', $terms)) { if (array_key_exists('firstname', $terms)) {
$qb->andWhere($qb->expr()->like('UNACCENT(LOWER(p.firstName))', ':firstname')) $qb->andWhere($qb->expr()->like('UNACCENT(LOWER(p.firstName))', ':firstname'))
->setParameter('firstname', '%'.$terms['firstname'].'%'); ->setParameter('firstname', '%'.$terms['firstname'].'%');
} }
if (array_key_exists('lastname', $terms)) { if (array_key_exists('lastname', $terms)) {
$qb->andWhere($qb->expr()->like('UNACCENT(LOWER(p.lastName))', ':lastname')) $qb->andWhere($qb->expr()->like('UNACCENT(LOWER(p.lastName))', ':lastname'))
->setParameter('lastname', '%'.$terms['lastname'].'%'); ->setParameter('lastname', '%'.$terms['lastname'].'%');
} }
foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key)
if (array_key_exists($key, $terms)) { if (array_key_exists($key, $terms)) {
try { try {
@ -220,20 +220,20 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
default: default:
throw new \LogicException("this case $key should not exists"); throw new \LogicException("this case $key should not exists");
} }
} }
if (array_key_exists('gender', $terms)) { if (array_key_exists('gender', $terms)) {
if (!in_array($terms['gender'], array(Person::MALE_GENDER, Person::FEMALE_GENDER))) { if (!in_array($terms['gender'], array(Person::MALE_GENDER, Person::FEMALE_GENDER))) {
throw new ParsingException('The gender ' throw new ParsingException('The gender '
.$terms['gender'].' is not accepted. Should be "'.Person::MALE_GENDER .$terms['gender'].' is not accepted. Should be "'.Person::MALE_GENDER
.'" or "'.Person::FEMALE_GENDER.'"'); .'" or "'.Person::FEMALE_GENDER.'"');
} }
$qb->andWhere($qb->expr()->eq('p.gender', ':gender')) $qb->andWhere($qb->expr()->eq('p.gender', ':gender'))
->setParameter('gender', $terms['gender']); ->setParameter('gender', $terms['gender']);
} }
if (array_key_exists('nationality', $terms)) { if (array_key_exists('nationality', $terms)) {
try { try {
$country = $this->em->createQuery('SELECT c FROM ' $country = $this->em->createQuery('SELECT c FROM '
@ -245,21 +245,21 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
throw new ParsingException('The country code "'.$terms['nationality'].'" ' throw new ParsingException('The country code "'.$terms['nationality'].'" '
. ', used in nationality, is unknow', 0, $ex); . ', used in nationality, is unknow', 0, $ex);
} }
$qb->andWhere($qb->expr()->eq('p.nationality', ':nationality')) $qb->andWhere($qb->expr()->eq('p.nationality', ':nationality'))
->setParameter('nationality', $country); ->setParameter('nationality', $country);
} }
if ($terms['_default'] !== '') { if ($terms['_default'] !== '') {
$grams = explode(' ', $terms['_default']); $grams = explode(' ', $terms['_default']);
foreach($grams as $key => $gram) { foreach($grams as $key => $gram) {
$qb->andWhere($qb->expr() $qb->andWhere($qb->expr()
->like('UNACCENT(LOWER(CONCAT(p.firstName, \' \', p.lastName)))', ':default_'.$key)) ->like('UNACCENT(LOWER(CONCAT(p.firstName, \' \', p.lastName)))', ':default_'.$key))
->setParameter('default_'.$key, '%'.$gram.'%'); ->setParameter('default_'.$key, '%'.$gram.'%');
} }
} }
//restraint center for security //restraint center for security
$reachableCenters = $this->helper->getReachableCenters($this->user, $reachableCenters = $this->helper->getReachableCenters($this->user,
new Role('CHILL_PERSON_SEE')); new Role('CHILL_PERSON_SEE'));
@ -267,12 +267,12 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
->in('p.center', ':centers')) ->in('p.center', ':centers'))
->setParameter('centers', $reachableCenters) ->setParameter('centers', $reachableCenters)
; ;
$this->_cacheQuery[$cacheKey] = $qb; $this->_cacheQuery[$cacheKey] = $qb;
return clone $qb; return clone $qb;
} }
public function buildForm(FormBuilderInterface $builder) public function buildForm(FormBuilderInterface $builder)
{ {
$builder $builder
@ -305,43 +305,44 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
'Man' => Person::MALE_GENDER, 'Man' => Person::MALE_GENDER,
'Woman' => Person::FEMALE_GENDER 'Woman' => Person::FEMALE_GENDER
], ],
'choices_as_values' => true,
'label' => 'Gender', 'label' => 'Gender',
'required' => false 'required' => false
]) ])
; ;
} }
public function convertFormDataToQuery(array $data) public function convertFormDataToQuery(array $data)
{ {
$string = '@person '; $string = '@person ';
$string .= empty($data['_default']) ? '' : $data['_default'].' '; $string .= empty($data['_default']) ? '' : $data['_default'].' ';
foreach(['firstname', 'lastname', 'gender'] as $key) { foreach(['firstname', 'lastname', 'gender'] as $key) {
$string .= empty($data[$key]) ? '' : $key.':'. $string .= empty($data[$key]) ? '' : $key.':'.
// add quote if contains spaces // add quote if contains spaces
(strpos($data[$key], ' ') !== false ? '"'.$data[$key].'"': $data[$key]) (strpos($data[$key], ' ') !== false ? '"'.$data[$key].'"': $data[$key])
.' '; .' ';
} }
foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) { foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) {
$string .= empty($data[$key]) ? $string .= empty($data[$key]) ?
'' ''
: :
$key.':'.$data[$key]->format('Y-m-d').' ' $key.':'.$data[$key]->format('Y-m-d').' '
; ;
} }
return $string; return $string;
} }
public function convertTermsToFormData(array $terms) public function convertTermsToFormData(array $terms)
{ {
foreach(['firstname', 'lastname', 'gender', '_default'] foreach(['firstname', 'lastname', 'gender', '_default']
as $key) { as $key) {
$data[$key] = $terms[$key] ?? null; $data[$key] = $terms[$key] ?? null;
} }
// parse dates // parse dates
foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) { foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) {
if (\array_key_exists($key, $terms)) { if (\array_key_exists($key, $terms)) {
@ -354,13 +355,13 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
} }
$data[$key] = $date ?? null; $data[$key] = $date ?? null;
} }
return $data; return $data;
} }
public function getAdvancedSearchTitle() public function getAdvancedSearchTitle()
{ {
return 'Search within persons'; return 'Search within persons';
} }
} }