add filtering before / after date of birth

This commit is contained in:
2017-08-29 17:01:26 +02:00
parent f5de2d8049
commit 75b4ef5a7d
3 changed files with 74 additions and 27 deletions

View File

@@ -122,7 +122,8 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
array(
'persons' => $this->search($terms, $start, $limit, $options),
'pattern' => $this->recomposePattern($terms, array('nationality',
'firstname', 'lastname', 'birthdate', 'gender'), $terms['_domain']),
'firstname', 'lastname', 'birthdate', 'gender',
'birthdate-before','birthdate-after'), $terms['_domain']),
'total' => $total,
'start' => $start,
'search_name' => self::NAME,
@@ -194,16 +195,32 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
->setParameter('lastname', '%'.$terms['lastname'].'%');
}
if (array_key_exists('birthdate', $terms)) {
foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key)
if (array_key_exists($key, $terms)) {
try {
$date = new \DateTime($terms['birthdate']);
$date = new \DateTime($terms[$key]);
} catch (\Exception $ex) {
throw new ParsingException('The date is '
. 'not parsable', 0, $ex);
}
$qb->andWhere($qb->expr()->eq('p.birthdate', ':birthdate'))
->setParameter('birthdate', $date);
switch($key) {
case 'birthdate':
$qb->andWhere($qb->expr()->eq('p.birthdate', ':birthdate'))
->setParameter('birthdate', $date);
break;
case 'birthdate-before':
$qb->andWhere($qb->expr()->lt('p.birthdate', ':birthdatebefore'))
->setParameter('birthdatebefore', $date);
break;
case 'birthdate-after':
$qb->andWhere($qb->expr()->gt('p.birthdate', ':birthdateafter'))
->setParameter('birthdateafter', $date);
break;
default:
throw new \LogicException("this case $key should not exists");
}
}
if (array_key_exists('gender', $terms)) {
@@ -271,10 +288,18 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
'label' => 'Last name',
'required' => false
])
->add('birthdate-after', ChillDateType::class, [
'label' => 'Birthdate after',
'required' => false
])
->add('birthdate', ChillDateType::class, [
'label' => 'Birthdate',
'required' => false
])
->add('birthdate-before', ChillDateType::class, [
'label' => 'Birthdate before',
'required' => false
])
->add('gender', ChoiceType::class, [
'choices' => [
'Man' => Person::MALE_GENDER,
@@ -299,11 +324,13 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
.' ';
}
$string .= empty($data['birthdate']) ?
''
:
'birthdate:'.$data['birthdate']->format('Y-m-d').' '
;
foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) {
$string .= empty($data[$key]) ?
''
:
$key.':'.$data[$key]->format('Y-m-d').' '
;
}
return $string;
}
@@ -315,18 +342,25 @@ class PersonSearch extends AbstractSearch implements ContainerAwareInterface,
$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);
// parse dates
foreach (['birthdate', 'birthdate-before', 'birthdate-after'] as $key) {
if (\array_key_exists($key, $terms)) {
try {
$date = new \DateTime($terms[$key]);
} catch (\Exception $ex) {
throw new ParsingException("The date for $key is "
. 'not parsable', 0, $ex);
}
}
$data[$key] = $date ?? null;
}
$data['birthdate'] = $date ?? null;
return $data;
}
public function getAdvancedSearchTitle()
{
return 'Search within persons';
}
}