Files
ENI-sortir/src/Repository/SortieRepository.php
mepiphana2023 8c065dd886 des trucs
2024-11-26 15:34:54 +01:00

118 lines
3.5 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Site;
use App\Entity\Sortie;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<Sortie>
*/
class SortieRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Sortie::class);
}
public function findWithFilters($search, $siteId, $startDate, $endDate, $organisateur, $inscrit, $nonInscrit, $passees, $userConnect)
{
$qb = $this->createQueryBuilder('s')
->leftJoin('s.etat', 'e')
->addSelect('e')
->leftJoin('s.participants', 'p')
->addSelect('p')
->where('e.libelle != :annulee')
->setParameter('annulee', 'Annulée');
// Filtre par nom
if (!empty($search)) {
$qb->andWhere('s.nom LIKE :search')
->setParameter('search', '%' . $search . '%');
}
// Filtre par site
if (!empty($siteId)) {
$qb->andWhere('s.site = :siteId')
->setParameter('siteId', $siteId);
}
// Filtre par date de début
if (!empty($startDate)) {
$qb->andWhere('s.dateHeureDebut >= :startDate')
->setParameter('startDate', $startDate);
}
// Filtre par date de fin
if (!empty($endDate)) {
$qb->andWhere('s.dateHeureDebut <= :endDate')
->setParameter('endDate', $endDate);
}
// Filtre par organisateur
if ($organisateur && $userConnect) {
$qb->andWhere('s.organisateur = :organisateur')
->setParameter('organisateur', $userConnect);
}
// Filtre par inscription
if ($inscrit && $userConnect) {
$qb->andWhere(':userConnect MEMBER OF s.participants')
->setParameter('userConnect', $userConnect);
}
// Filtre par non-inscription
if ($nonInscrit && $userConnect) {
$qb->andWhere(':userConnect NOT MEMBER OF s.participants')
->setParameter('userConnect', $userConnect);
}
// Filtre par sorties passées
if ($passees) {
$qb->andWhere('s.dateHeureDebut < :now')
->setParameter('now', new \DateTime());
} else {
$qb->andWhere('s.dateHeureDebut >= :now')
->setParameter('now', new \DateTime());
}
return $qb->getQuery()->getResult();
}
public function findAllSites()
{
return $this->getEntityManager()
->getRepository(Site::class)
->findAll();
}
// /**
// * @return Sortie[] Returns an array of Sortie objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('s.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?Sortie
// {
// return $this->createQueryBuilder('s')
// ->andWhere('s.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
}