Files
myep-team/src/Repository/DestinationRepository.php
T
2025-08-21 14:20:43 +02:00

128 lines
3.8 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Destination;
use App\Model\DestinationFilterDto;
use App\Repository\Traits\QueryHelperTrait;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Destination>
*
* @method Destination|null find($id, $lockMode = null, $lockVersion = null)
* @method Destination|null findOneBy(array $criteria, array $orderBy = null)
* @method Destination[] findAll()
* @method Destination[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class DestinationRepository extends ServiceEntityRepository
{
use QueryHelperTrait;
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Destination::class);
}
public function getListQuery(DestinationFilterDto $filterDto): Query
{
$qb = $this->createQueryBuilder('destination');
$qb->where(
$qb->expr()->andX(
$qb->expr()->isNull('destination.deletedAt'),
$qb->expr()->isNull('destination.busProId')
)
);
if (0 < count($filterDto->getHotels())) {
$constraints = [];
foreach ($filterDto->getHotels() as $index => $hotel) {
$constraints[] = $qb->expr()->like('destination.hotel', ':hotel'.$index);
}
$qb->andWhere($qb->expr()->orX(...$constraints));
foreach ($filterDto->getHotels() as $index => $hotel) {
$qb->setParameter('hotel'.$index, '%'.$hotel.'%');
}
}
return $qb->getQuery();
}
public function getListQueryForCustom(): Query
{
$qb = $this->createQueryBuilder('destination');
return $qb
->where(
$qb->expr()->andX(
$qb->expr()->isNull('destination.deletedAt'),
$qb->expr()->isNull('destination.busProId'))
)
->getQuery()
;
}
public function getAutocompletionData(string $search, bool $upcomingOnly = true): array
{
$qb = $this->createQueryBuilder('destination');
$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')
))
)
->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()
->getResult()
;
$data = [
[
'value' => '',
'text' => '...',
],
];
foreach ($dates as $date) {
$data[] = [
'value' => $date->getId(),
'text' => (string) $date,
];
}
return $data;
}
public function getHotels(): array
{
$qb = $this->createQueryBuilder('destination');
return $qb
->select('destination.hotel', 'destination.hotelCode', 'destination.hotelBusProId')
->groupBy('destination.hotelBusProId')
->orderBy('destination.hotel', 'ASC')
->getQuery()
->getArrayResult()
;
}
}