From b2796467809cb11e0624c72218ac08b8efe67f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Fromme?= Date: Sat, 13 Jan 2024 11:44:58 +0100 Subject: [PATCH] fix: limit destination autocomplete results to upcoming dates --- src/Repository/DestinationRepository.php | 33 +++++++++++++++--------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Repository/DestinationRepository.php b/src/Repository/DestinationRepository.php index 38c7b1a..8ac8afb 100644 --- a/src/Repository/DestinationRepository.php +++ b/src/Repository/DestinationRepository.php @@ -24,22 +24,31 @@ class DestinationRepository extends ServiceEntityRepository parent::__construct($registry, Destination::class); } - public function getAutocompletionData(string $search): array + public function getAutocompletionData(string $search, bool $upcomingOnly = true): array { $qb = $this->createQueryBuilder('destination'); - $dates = $qb - ->where( - $qb->expr()->andX( - $qb->expr()->isNull('destination.deletedAt'), - $qb->expr()->orX( - $qb->expr()->like('destination.product', ':search'), - $qb->expr()->like('destination.hotel', ':search'), - $qb->expr()->like('DATE_FORMAT(destination.dateFrom, \'%d.%m.%y\')', ':search'), - $qb->expr()->like('DATE_FORMAT(destination.dateTo, \'%d.%m.%y\')', ':search') - )) - ) + $qb->where( + $qb->expr()->andX( + $qb->expr()->isNull('destination.deletedAt'), + $qb->expr()->orX( + $qb->expr()->like('destination.product', ':search'), + $qb->expr()->like('destination.hotel', ':search'), + $qb->expr()->like('DATE_FORMAT(destination.dateFrom, \'%d.%m.%y\')', ':search'), + $qb->expr()->like('DATE_FORMAT(destination.dateTo, \'%d.%m.%y\')', ':search') + )) + ) ->setParameter('search', '%'.$this->escapeLikeWildcards($search).'%') + ; + + if (true === $upcomingOnly) { + $qb + ->andWhere($qb->expr()->gt('destination.dateFrom', ':now')) + ->setParameter('now', new \DateTimeImmutable()) + ; + } + + $dates = $qb ->orderBy('destination.dateFrom', 'ASC') ->addOrderBy('destination.product', 'ASC') ->getQuery()