<?php
namespace App\Repository;
use App\Entity\Alertes;
use DateTime;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
/**
* @method Alertes|null find($id, $lockMode = null, $lockVersion = null)
* @method Alertes|null findOneBy(array $criteria, array $orderBy = null)
* @method Alertes[] findAll()
* @method Alertes[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class AlertesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Alertes::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(Alertes $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(Alertes $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
protected function genericRequestOrder(QueryBuilder $queryBuilder)
{
$response = $queryBuilder->select('a')
->getQuery()
->getResult();
return $response;
}
public function findNotifications($id, $user): array
{
$today = new DateTime();
$alertes = $this->genericRequestOrder(
($this->createQueryBuilder('a')
->addSelect('t')
->leftJoin('a.idTypeAlerte', 't')
->where('a.idContact = :id')
->andWhere('a.dateAlerte <= :today')
->andWhere("t.alerte != 'Groupe'")
->andWhere('a.traite = false')
->setParameter('id', $id)
->setParameter('today', $today->format('Y-m-d'))
)
);
foreach (($user->getRoles()) as $role) {
$alertersGroup = $this->genericRequestOrder(
($this->createQueryBuilder('a')
->addSelect('t')
->leftJoin('a.idTypeAlerte', 't')
->where('a.dateAlerte <= :today')
->andWhere("t.alerte = 'Groupe'")
->andWhere('a.traite = false')
->andWhere('a.roleGroup = :role')
->setParameter('role', $role)
->setParameter('today', $today->format('Y-m-d'))
)
);
$alertes = array_merge($alertes, $alertersGroup);
}
return $alertes;
}
}