From b004c65ec11b7b5e8424dba6ff1bba04dcd5d2a7 Mon Sep 17 00:00:00 2001 From: nobohan Date: Wed, 4 Apr 2018 17:53:11 +0200 Subject: [PATCH] fix deprecations: use choices_as_values=true and flip content of the choice options --- Export/Filter/GenderFilter.php | 5 +- Search/PersonSearch.php | 103 +++++++++++++++++---------------- 2 files changed, 55 insertions(+), 53 deletions(-) diff --git a/Export/Filter/GenderFilter.php b/Export/Filter/GenderFilter.php index 677f807a5..ab551b1f3 100644 --- a/Export/Filter/GenderFilter.php +++ b/Export/Filter/GenderFilter.php @@ -50,9 +50,10 @@ class GenderFilter implements FilterInterface, { $builder->add('accepted_genders', ChoiceType::class, array( 'choices' => array( - Person::FEMALE_GENDER => 'Woman', - Person::MALE_GENDER => 'Man' + 'Woman' => Person::FEMALE_GENDER, + 'Man' => Person::MALE_GENDER ), + 'choices_as_values' => true, 'multiple' => false, 'expanded' => false )); diff --git a/Search/PersonSearch.php b/Search/PersonSearch.php index e3e199763..7e349ba27 100644 --- a/Search/PersonSearch.php +++ b/Search/PersonSearch.php @@ -40,34 +40,34 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, HasAdvancedSearchFormInterface { use ContainerAwareTrait; - + /** * * @var EntityManagerInterface */ private $em; - + /** * * @var \Chill\MainBundle\Entity\User */ private $user; - + /** * * @var AuthorizationHelper */ private $helper; - + /** * * @var PaginatorFactory */ protected $paginatorFactory; - + const NAME = "person_regular"; - - + + public function __construct( EntityManagerInterface $em, TokenStorage $tokenStorage, @@ -78,7 +78,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, $this->user = $tokenStorage->getToken()->getUser(); $this->helper = $helper; $this->paginatorFactory = $paginatorFactory; - + // throw an error if user is not a valid user if (!$this->user instanceof \Chill\MainBundle\Entity\User) { throw new \LogicException('The user provided must be an instance' @@ -103,7 +103,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, { return true; } - + public function supports($domain) { return 'person' === $domain; @@ -117,12 +117,12 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, { $total = $this->count($terms); $paginator = $this->paginatorFactory->create($total); - + return $this->container->get('templating')->render('ChillPersonBundle:Person:list.html.twig', array( 'persons' => $this->search($terms, $start, $limit, $options), 'pattern' => $this->recomposePattern($terms, array('nationality', - 'firstname', 'lastname', 'birthdate', 'gender', + 'firstname', 'lastname', 'birthdate', 'gender', 'birthdate-before','birthdate-after'), $terms['_domain']), 'total' => $total, 'start' => $start, @@ -131,7 +131,7 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, 'paginator' => $paginator )); } - + /** * * @param string $pattern @@ -143,31 +143,31 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, protected function search(array $terms, $start, $limit, array $options = array()) { $qb = $this->createQuery($terms, 'search'); - + $qb->select('p') ->setMaxResults($limit) ->setFirstResult($start); - + //order by firstname, lastname - + $qb->orderBy('p.firstName') ->addOrderBy('p.lastName'); - + return $qb->getQuery()->getResult(); } - + protected function count(array $terms) { $qb = $this->createQuery($terms); - + $qb->select('COUNT(p.id)'); - + return $qb->getQuery()->getSingleScalarResult(); } - - + + private $_cacheQuery = array(); - + /** * * @param array $terms @@ -180,21 +180,21 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, if (array_key_exists($cacheKey, $this->_cacheQuery)) { return clone $this->_cacheQuery[$cacheKey]; } - + $qb = $this->em->createQueryBuilder(); - + $qb->from('ChillPersonBundle:Person', 'p'); - + if (array_key_exists('firstname', $terms)) { $qb->andWhere($qb->expr()->like('UNACCENT(LOWER(p.firstName))', ':firstname')) ->setParameter('firstname', '%'.$terms['firstname'].'%'); } - + if (array_key_exists('lastname', $terms)) { $qb->andWhere($qb->expr()->like('UNACCENT(LOWER(p.lastName))', ':lastname')) ->setParameter('lastname', '%'.$terms['lastname'].'%'); } - + foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) if (array_key_exists($key, $terms)) { try { @@ -220,20 +220,20 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, default: throw new \LogicException("this case $key should not exists"); } - + } - + if (array_key_exists('gender', $terms)) { if (!in_array($terms['gender'], array(Person::MALE_GENDER, Person::FEMALE_GENDER))) { throw new ParsingException('The gender ' .$terms['gender'].' is not accepted. Should be "'.Person::MALE_GENDER .'" or "'.Person::FEMALE_GENDER.'"'); } - + $qb->andWhere($qb->expr()->eq('p.gender', ':gender')) ->setParameter('gender', $terms['gender']); } - + if (array_key_exists('nationality', $terms)) { try { $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'].'" ' . ', used in nationality, is unknow', 0, $ex); } - + $qb->andWhere($qb->expr()->eq('p.nationality', ':nationality')) ->setParameter('nationality', $country); } - + if ($terms['_default'] !== '') { $grams = explode(' ', $terms['_default']); - + foreach($grams as $key => $gram) { $qb->andWhere($qb->expr() ->like('UNACCENT(LOWER(CONCAT(p.firstName, \' \', p.lastName)))', ':default_'.$key)) ->setParameter('default_'.$key, '%'.$gram.'%'); } } - + //restraint center for security $reachableCenters = $this->helper->getReachableCenters($this->user, new Role('CHILL_PERSON_SEE')); @@ -267,12 +267,12 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, ->in('p.center', ':centers')) ->setParameter('centers', $reachableCenters) ; - + $this->_cacheQuery[$cacheKey] = $qb; - + return clone $qb; } - + public function buildForm(FormBuilderInterface $builder) { $builder @@ -305,43 +305,44 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, 'Man' => Person::MALE_GENDER, 'Woman' => Person::FEMALE_GENDER ], + 'choices_as_values' => true, 'label' => 'Gender', 'required' => false ]) ; } - + public function convertFormDataToQuery(array $data) { $string = '@person '; - + $string .= empty($data['_default']) ? '' : $data['_default'].' '; - + foreach(['firstname', 'lastname', 'gender'] as $key) { $string .= empty($data[$key]) ? '' : $key.':'. // add quote if contains spaces (strpos($data[$key], ' ') !== false ? '"'.$data[$key].'"': $data[$key]) .' '; } - + foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) { - $string .= empty($data[$key]) ? + $string .= empty($data[$key]) ? '' : $key.':'.$data[$key]->format('Y-m-d').' ' ; } - + return $string; } - - public function convertTermsToFormData(array $terms) + + public function convertTermsToFormData(array $terms) { - foreach(['firstname', 'lastname', 'gender', '_default'] + foreach(['firstname', 'lastname', 'gender', '_default'] as $key) { $data[$key] = $terms[$key] ?? null; } - + // parse dates foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) { if (\array_key_exists($key, $terms)) { @@ -354,13 +355,13 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface, } $data[$key] = $date ?? null; } - + return $data; } - + public function getAdvancedSearchTitle() { return 'Search within persons'; } -} \ No newline at end of file +}