src/Repository/CentreRepository.php line 41

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Centre;
  4. use App\Entity\Client;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8.  * @method Centre|null find($id, $lockMode = null, $lockVersion = null)
  9.  * @method Centre|null findOneBy(array $criteria, array $orderBy = null)
  10.  * @method Centre[]    findAll()
  11.  * @method Centre[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12.  */
  13. class CentreRepository extends ServiceEntityRepository
  14. {
  15.     public function __construct(ManagerRegistry $registry)
  16.     {
  17.         parent::__construct($registryCentre::class);
  18.     }
  19.     /**
  20.      * @return Centre[] Returns an array of Centre search
  21.      */
  22.   /*  public function findByCoordonates(float $longitude, float $latitude)
  23.     {
  24.         // gets all the centres in a diameter of 15 km
  25.         $distance = 0.1;
  26.         return $this->createQueryBuilder('c')
  27.             ->where('c.longitude BETWEEN  :min_longitude AND :max_longitude')
  28.             ->andWhere('c.latitude BETWEEN  :min_latitude AND :max_latitude')
  29.             ->setParameter('min_longitude', $longitude - $distance)
  30.             ->setParameter('max_longitude', $longitude + $distance)
  31.             ->setParameter('min_latitude', $latitude - $distance)
  32.             ->setParameter('max_latitude',  $latitude + $distance)
  33.             ->getQuery()
  34.             ->getResult();
  35.     }*/
  36.     public function findByCoordonates(float $longitudefloat $latitude$page 1$limit 10)
  37.     {
  38.    $distance 0.4;
  39.    $qb $this->createQueryBuilder('c');
  40.    $qb->select('c')
  41.        ->addSelect('(
  42.            (c.longitude - :longitude) * (c.longitude - :longitude) +
  43.            (c.latitude - :latitude) * (c.latitude - :latitude)
  44.        ) AS HIDDEN distance')
  45.        ->setParameter('longitude'$longitude)
  46.        ->setParameter('latitude'$latitude)
  47.        ->andWhere('c.longitude BETWEEN :min_longitude AND :max_longitude')
  48.        ->andWhere('c.latitude BETWEEN :min_latitude AND :max_latitude')
  49.        ->setParameter('min_longitude'$longitude $distance)
  50.        ->setParameter('max_longitude'$longitude $distance)
  51.        ->setParameter('min_latitude'$latitude $distance)
  52.        ->setParameter('max_latitude'$latitude $distance)
  53.        ->orderBy('distance''ASC')
  54.        ->setFirstResult(($page 1) * $limit
  55.        ->setMaxResults($limit); 
  56.    return $qb->getQuery()->getResult();
  57.     }
  58.     public function findByDistance(float $longitudefloat $latitudefloat $radiusKm 2int $limit 4)
  59.     {
  60.         $distanceLat $radiusKm 111;
  61.         $distanceLng $radiusKm / (111 cos(deg2rad($latitude)));
  62.     
  63.         $qb $this->createQueryBuilder('c');
  64.     
  65.         $qb->select('c')
  66.             ->andWhere('c.longitude BETWEEN :min_longitude AND :max_longitude')
  67.             ->andWhere('c.latitude BETWEEN :min_latitude AND :max_latitude')
  68.             ->andWhere('c.isValid = :is_valid')
  69.             ->setParameter('min_longitude'$longitude $distanceLng)
  70.             ->setParameter('max_longitude'$longitude $distanceLng)
  71.             ->setParameter('min_latitude'$latitude $distanceLat)
  72.             ->setParameter('max_latitude'$latitude $distanceLat)
  73.             ->setParameter('is_valid'true)
  74.             ->setMaxResults($limit);
  75.     
  76.         return $qb->getQuery()->getResult();
  77.     }
  78.     
  79.     public function findByDistanceKm(float $longitudefloat $latitudeint $limit 4)
  80.    {
  81.     $qb $this->createQueryBuilder('c');
  82.     $qb->addSelect(
  83.         '(6371 * acos(cos(radians(:latitude)) * cos(radians(c.latitude)) 
  84.         * cos(radians(c.longitude) - radians(:longitude)) + sin(radians(:latitude)) 
  85.         * sin(radians(c.latitude)))) AS distance'
  86.     )
  87.     ->having('distance <= c.zoneKm')
  88.     ->andWhere('c.isValid = :is_valid')
  89.     ->setParameter('latitude'$latitude)
  90.     ->setParameter('longitude'$longitude)
  91.     ->setParameter('is_valid'true)
  92.     ->orderBy('distance''ASC'
  93.     ->setMaxResults($limit);
  94.     return $qb->getQuery()->getResult();
  95.    }
  96.   
  97.     
  98.     /**
  99.      * @return Centre[] Returns an array of Centre search
  100.      */
  101.     public function findByAdress(string $address)
  102.     {
  103.         // gets all the centres in a diameter of 15 km
  104.         return $this->createQueryBuilder('c')
  105.         ->Where('c.address LIKE CONCAT(\'%\',:query,\'%\')')
  106.         ->setParameter('query'$address)
  107.         ->getQuery()
  108.         ->getResult();
  109.     }
  110.     /**
  111.      * @return Centre[] Returns an array of Centre search
  112.      */
  113.     public function findByQuery(String $query)
  114.     {
  115.         return $this->createQueryBuilder('c')
  116.             ->where('c.name LIKE CONCAT(\'%\',:query,\'%\')')
  117.             ->orWhere(':query LIKE CONCAT(\'%\',c.name,\'%\')')
  118.             ->orWhere('c.postale LIKE CONCAT(\'%\',:query,\'%\')')
  119.             ->orWhere(':query LIKE CONCAT(\'%\',c.postale,\'%\')')
  120.             ->orWhere('c.city LIKE CONCAT(\'%\',:query,\'%\')')
  121.             ->orWhere(':query LIKE CONCAT(\'%\',c.city,\'%\')')
  122.             ->orWhere('c.address LIKE CONCAT(\'%\',:query,\'%\')')
  123.             ->orWhere(':query LIKE CONCAT(\'%\',c.address,\'%\')')
  124.             ->setParameter('query'$query)
  125.             ->getQuery()
  126.             ->getResult();
  127.     }
  128.     /**
  129.      * @return int[] Returns an array of Centre search
  130.      */
  131.     public function findByName(string $name)
  132.     {
  133.         $conn $this->getEntityManager()->getConnection();
  134.         $sql '
  135.         SELECT audio_centre.id_centre_id
  136.         FROM audio_centre 
  137.         INNER JOIN audio ON audio.id = audio_centre.id_audio_id 
  138.         WHERE (CONCAT(audio.name," ",audio.lastname)  LIKE :name) OR (CONCAT(audio.lastname," ",audio.name)  LIKE :name)
  139.         UNION
  140.         SELECT centre.id
  141.         FROM centre
  142.         WHERE centre.name LIKE :name;
  143.         ';
  144.         $stmt $conn->prepare($sql);
  145.         $result =  $stmt->executeQuery(['name' => '%' $name '%']);
  146.         // returns the number of audio that have the specified 
  147.        return $result->fetchFirstColumn();
  148.         // gets all the centres in a diameter of 15 km
  149.     }
  150.     /**
  151.      * @return int[] Returns an array of Centre search
  152.      */
  153.     public function findByRdvTaken(Client $client)
  154.     {
  155.         $conn $this->getEntityManager()->getConnection();
  156.         $sql '
  157.         SELECT  centre.id
  158.         FROM centre 
  159.         INNER JOIN rdv ON centre.id = rdv.id_centre_id
  160.         WHERE rdv.id_client_id = :client_id
  161.         ';
  162.         $stmt $conn->prepare($sql);
  163.         $result =   $stmt->executeQuery(['client_id' => $client->getId()]);
  164.         // returns the number of audio that have the specified 
  165.         return $result->fetchFirstColumn();
  166.         // gets all the centres in a diameter of 15 km
  167.     }
  168.     /**
  169.      * @return int  Returns an array of Centre search
  170.      */
  171.     public function centreHasMotif(int $centreIdint $motifId): int
  172.     {
  173.         $conn $this->getEntityManager()->getConnection();
  174.         $sql '
  175.             SELECT COUNT(1) 
  176.             FROM `audio_motif` 
  177.             INNER JOIN audio ON audio.id = audio_motif.id_audio_id 
  178.             INNER JOIN audio_centre ON audio.id = audio_centre.id_audio_id 
  179.             WHERE `id_motif_id` = :motifId AND `id_centre_id`=:centreId;
  180.         ';
  181.         $stmt $conn->prepare($sql);
  182.         $result =  $stmt->executeQuery(['centreId' => $centreId'motifId' => $motifId]);
  183.         // returns the number of audio that have the specified 
  184.         return (int) $result->fetchFirstColumn()[0];
  185.     }
  186.     public function findVisibleCenters(): array
  187. {
  188.     return $this->createQueryBuilder('c')
  189.         ->andWhere('c.isVisible IS NULL')
  190.         ->getQuery()
  191.         ->getResult();
  192. }
  193.     // /**
  194.     //  * @return Centre[] Returns an array of Centre objects
  195.     //  */
  196.     /*
  197.     public function findByExampleField($value)
  198.     {
  199.         return $this->createQueryBuilder('c')
  200.             ->andWhere('c.exampleField = :val')
  201.             ->setParameter('val', $value)
  202.             ->orderBy('c.id', 'ASC')
  203.             ->setMaxResults(10)
  204.             ->getQuery()
  205.             ->getResult()
  206.         ;
  207.     }
  208.     */
  209.     /*
  210.     public function findOneBySomeField($value): ?Centre
  211.     {
  212.         return $this->createQueryBuilder('c')
  213.             ->andWhere('c.exampleField = :val')
  214.             ->setParameter('val', $value)
  215.             ->getQuery()
  216.             ->getOneOrNullResult()
  217.         ;
  218.     }
  219.     */
  220.     public function countByPeriod(?\DateTime $from, ?\DateTime $tostring $dateField): int
  221. {
  222.     $qb $this->createQueryBuilder('e');
  223.     if ($from) {
  224.         $qb->andWhere("e.{$dateField} >= :from")->setParameter('from'$from);
  225.     }
  226.     if ($to) {
  227.         $qb->andWhere("e.{$dateField} <= :to")->setParameter('to'$to);
  228.     }
  229.     return (int) $qb->select('COUNT(e.id)')
  230.                     ->getQuery()
  231.                     ->getSingleScalarResult();
  232. }
  233. public function findByFilters(
  234.     ?string $postalCode,
  235.     ?string $subscriptionType,
  236.     ?string $centerStatus,
  237.     ?string $signupDateFrom,
  238.     ?string $signupDateTo,
  239.     ?array $contractTypes// new
  240.     ?string $city // ← add this
  241. ): array
  242. {
  243.     $qb $this->createQueryBuilder('c');
  244.     if ($postalCode) {
  245.         $qb->andWhere('c.postale = :postalCode')
  246.            ->setParameter('postalCode'$postalCode);
  247.     }
  248.     if ($centerStatus) {
  249.         switch ($centerStatus) {
  250.             case 'active':
  251.                 $qb->andWhere('c.isValid = true')
  252.                    ->andWhere('c.isResilied = false')
  253.                    ->andWhere('c.isBlocked = false');
  254.                 break;
  255.             case 'cancelled':
  256.                 $qb->andWhere('c.isResilied = true');
  257.                 break;
  258.             case 'blocked':
  259.                 $qb->andWhere('c.isBlocked = true');
  260.                 break;
  261.         }
  262.     }
  263.         if ($signupDateFrom) {
  264.     $from = new \DateTime($signupDateFrom);
  265.     $from->setTime(000);
  266.     $qb->andWhere('c.signupDate >= :from')
  267.        ->setParameter('from'$from);
  268. }
  269. if ($signupDateTo) {
  270.     $to = new \DateTime($signupDateTo);
  271.     $to->setTime(235959);
  272.     $qb->andWhere('c.signupDate <= :to')
  273.        ->setParameter('to'$to);
  274. }
  275. if ($subscriptionType) {
  276.     $qb->join('c.subscription''s'// Assuming 'subscription' is the relation name in Centre entity
  277.        ->andWhere('s.planInterval = :planInterval')
  278.        ->setParameter('planInterval'$subscriptionType);
  279. }
  280. if ($city) {
  281.     $cities array_map('trim'explode(','$city));
  282.     $qb->andWhere('c.city IN (:cities)')
  283.        ->setParameter('cities'$cities);
  284. }
  285.     if ($contractTypes && is_array($contractTypes)) {
  286.         $qb->join('c.specificSubscription''ss'// adjust if different relation
  287.            ->andWhere('ss.contractCategory IN (:contractTypes)')
  288.            ->setParameter('contractTypes'$contractTypes);
  289.     }
  290.     return $qb->getQuery()->getResult();
  291. }
  292. }