From 54fbaa8c0df69de6b3bd4cae7a2bf00ca32bbf9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 22 Aug 2017 22:33:30 +0200 Subject: [PATCH] add advanced search for person --- Resources/translations/messages.fr.yml | 1 + Resources/views/Person/list.html.twig | 9 ++- Search/PersonSearch.php | 81 +++++++++++++++++++++++++- 3 files changed, 88 insertions(+), 3 deletions(-) diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index 39ab18d5b..b1bb9ec4d 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -6,6 +6,7 @@ firstName: prénom lastname: nom lastName: nom Name: Nom +First name or Last name: Prénom ou nom id: identifiant Birthdate: 'Date de naissance' birthdate: date de naissance diff --git a/Resources/views/Person/list.html.twig b/Resources/views/Person/list.html.twig index 59b735130..30676268b 100644 --- a/Resources/views/Person/list.html.twig +++ b/Resources/views/Person/list.html.twig @@ -61,17 +61,22 @@ {% endif %} diff --git a/Search/PersonSearch.php b/Search/PersonSearch.php index e2678cb6c..2181e8332 100644 --- a/Search/PersonSearch.php +++ b/Search/PersonSearch.php @@ -30,8 +30,14 @@ use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Symfony\Component\Security\Core\Role\Role; use Chill\MainBundle\Pagination\PaginatorFactory; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Chill\MainBundle\Form\Type\ChillDateType; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; +use Symfony\Component\Form\FormBuilderInterface; +use Chill\MainBundle\Search\HasAdvancedSearchFormInterface; -class PersonSearch extends AbstractSearch implements ContainerAwareInterface +class PersonSearch extends AbstractSearch implements ContainerAwareInterface, + HasAdvancedSearchFormInterface { use ContainerAwareTrait; @@ -249,5 +255,78 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface return clone $qb; } + + public function buildForm(FormBuilderInterface $builder) + { + $builder + ->add('_default', TextType::class, [ + 'label' => 'First name or Last name', + 'required' => false + ]) + ->add('firstname', TextType::class, [ + 'label' => 'First name', + 'required' => false + ]) + ->add('lastname', TextType::class, [ + 'label' => 'Last name', + 'required' => false + ]) + ->add('birthdate', ChillDateType::class, [ + 'label' => 'Birthdate', + 'required' => false + ]) + ->add('gender', ChoiceType::class, [ + 'choices' => [ + 'Man' => Person::MALE_GENDER, + 'Woman' => Person::FEMALE_GENDER + ], + '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]) + .' '; + } + + $string .= empty($data['birthdate']) ? + '' + : + 'birthdate:'.$data['birthdate']->format('Y-m-d').' ' + ; + + return $string; + } + + public function convertTermsToFormData(array $terms) + { + foreach(['firstname', 'lastname', 'gender', '_default'] + as $key) { + $data[$key] = $terms[$key] ?? null; + } + + // parse birthdate + if (\array_key_exists('birthdate', $terms)) { + try { + $date = new \DateTime($terms['birthdate']); + } catch (\Exception $ex) { + throw new ParsingException('The date is ' + . 'not parsable', 0, $ex); + } + } + $data['birthdate'] = $date ?? null; + + return $data; + } } \ No newline at end of file