src/Repository/AlertesRepository.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Alertes;
  4. use DateTime;
  5. use Doctrine\ORM\ORMException;
  6. use Doctrine\ORM\QueryBuilder;
  7. use Doctrine\ORM\OptimisticLockException;
  8. use Doctrine\Persistence\ManagerRegistry;
  9. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  10. /**
  11.  * @method Alertes|null find($id, $lockMode = null, $lockVersion = null)
  12.  * @method Alertes|null findOneBy(array $criteria, array $orderBy = null)
  13.  * @method Alertes[]    findAll()
  14.  * @method Alertes[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  */
  16. class AlertesRepository extends ServiceEntityRepository
  17. {
  18.     public function __construct(ManagerRegistry $registry)
  19.     {
  20.         parent::__construct($registryAlertes::class);
  21.     }
  22.     /**
  23.      * @throws ORMException
  24.      * @throws OptimisticLockException
  25.      */
  26.     public function add(Alertes $entitybool $flush true): void
  27.     {
  28.         $this->_em->persist($entity);
  29.         if ($flush) {
  30.             $this->_em->flush();
  31.         }
  32.     }
  33.     /**
  34.      * @throws ORMException
  35.      * @throws OptimisticLockException
  36.      */
  37.     public function remove(Alertes $entitybool $flush true): void
  38.     {
  39.         $this->_em->remove($entity);
  40.         if ($flush) {
  41.             $this->_em->flush();
  42.         }
  43.     }
  44.     protected function genericRequestOrder(QueryBuilder $queryBuilder)
  45.     {
  46.         $response $queryBuilder->select('a')
  47.             ->getQuery()
  48.             ->getResult();
  49.         return $response;
  50.     }
  51.     public function findNotifications($id$user): array
  52.     {
  53.         $today = new DateTime();
  54.         $alertes =  $this->genericRequestOrder(
  55.             ($this->createQueryBuilder('a')
  56.                 ->addSelect('t')
  57.                 ->leftJoin('a.idTypeAlerte''t')
  58.                 ->where('a.idContact = :id')
  59.                 ->andWhere('a.dateAlerte <= :today')
  60.                 ->andWhere("t.alerte != 'Groupe'")
  61.                 ->andWhere('a.traite = false')
  62.                 ->setParameter('id'$id)
  63.                 ->setParameter('today'$today->format('Y-m-d'))
  64.             )
  65.         );
  66.         foreach (($user->getRoles()) as $role) {
  67.             $alertersGroup =  $this->genericRequestOrder(
  68.                 ($this->createQueryBuilder('a')
  69.                     ->addSelect('t')
  70.                     ->leftJoin('a.idTypeAlerte''t')
  71.                     ->where('a.dateAlerte <= :today')
  72.                     ->andWhere("t.alerte = 'Groupe'")
  73.                     ->andWhere('a.traite = false')
  74.                     ->andWhere('a.roleGroup = :role')
  75.                     ->setParameter('role'$role)
  76.                     ->setParameter('today'$today->format('Y-m-d'))
  77.                 )
  78.             );
  79.             $alertes =  array_merge($alertes$alertersGroup);
  80.         }
  81.         return $alertes;
  82.     }
  83. }