<?php
namespace App\Repository;
use App\Entity\Form;
use App\Entity\User;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use function Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Security;
/**
* @method Form|null find($id, $lockMode = null, $lockVersion = null)
* @method Form|null findOneBy(array $criteria, array $orderBy = null)
* @method Form[] findAll()
* @method Form[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class FormRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry, private readonly Security $security, private readonly UserEntityRepository $userEntities)
{
parent::__construct($registry, Form::class);
}
public function findAllByUser(User $user, bool $hasObjects = false): array
{
if (in_array('ROLE_SUPER_ADMIN', $user->getRoles())) {
$qb = $this->createQueryBuilder('f');
return $qb->getQuery()->getResult();
} else {
$profiles = $user->getProfiles();
$qbExpr = $this->getEntityManager()->createQueryBuilder();
$qb = $this->createQueryBuilder('f')
->innerJoin('f.entity', 'e')
->innerJoin('e.userEntities', 'ue')
->innerJoin('ue.user', 'u')
->innerJoin('e.admins', 'a')
->andWhere(
$qbExpr->expr()->orX(
$qbExpr->expr()->andX(
$qbExpr->expr()->eq('u.id', ":userId"),
$qbExpr->expr()->isMemberOf(":profiles", 'f.profiles')
),
$qbExpr->expr()->andX(
$qbExpr->expr()->eq('u.id', ":userId"),
$qbExpr->expr()->eq('size(f.profiles)', 0)
),
$qbExpr->expr()->eq('a.id', ":userId"),
)
)
->andWhere($qbExpr->expr()->orX(
$qbExpr->expr()->eq('f.isBooking', ":isBookingfalse"),
$qbExpr->expr()->isNull('f.isBooking')
));
if (!$hasObjects){
$qb->andWhere("f.isLinkedToObject = 0");
}
$qb->andWhere("f.isActive = 1")
->andWhere("f.publicationDate <= :now")
//TODO: publicationEnd request
->andWhere(
$qbExpr->expr()->orX(
$qbExpr->expr()->gte("f.publicationEnd",":now"),
$qbExpr->expr()->isNull('f.publicationEnd')
)
)
->setParameter('now', new \DateTime('now'))
->setParameter('userId', $user->getId())
->setParameter("profiles", $profiles)
->setParameter("isBookingfalse", 0);
return $qb->getQuery()->getResult();
}
}
public function findByEntitySelected($selectedEntity, $isActive, $isBooking): array
{
return $this->createQueryBuilder('f')
->innerJoin("f.entity", "e")
->andWhere("e.id = :selectedEntity")
->andWhere('f.isActive = :archive')
->andWhere('f.isBooking = :booking')
->setParameter('selectedEntity', $selectedEntity)
->setParameter('archive', $isActive)
->setParameter('booking', $isBooking)
->getQuery()
->getResult();
}
public function findBooking()
{
$user = $this->security->getUser();
$userEntities = $this->userEntities;
$entities = $userEntities->findAllEntitiesIsAccepted($user);
$idEntities = [];
foreach ($entities as $entity) {
$idEntities[] = $entity->getEntity()->getId();
}
$date = new \DateTime();
$date = $date->format('Y-m-d');
$qb = $this->createQueryBuilder('f')
->andWhere("f.entity IN (:entities)")
->andWhere("f.publicationDate <= :date")
->andWhere("f.bookingDate >= :date")
->andWhere("f.isActive = 1")
->andWhere("f.isBooking = 1")
->setParameter('date', $date)
->setParameter('entities',$idEntities);
return $qb->getQuery()->getResult();
}
public function findFormsByIdAnimals($animals, $entities, $profiles)
{
$qbExpr = $this->getEntityManager()->createQueryBuilder();
$qb = $this->createQueryBuilder('f')
->andWhere(
$qbExpr->expr()->in('f.entity',":entities"))
->setParameter('entities', $entities);
if ($animals) {
$qb->andWhere(
$qbExpr->expr()->in(
'f.animal',':animals'))
->setParameter('animals', $animals);
}
if ($profiles) {
$qb->andWhere(
$qbExpr->expr()->isMemberOf(
':profiles','f.profiles'))
->setParameter('profiles', $profiles);
}
return $qb->getQuery()->getResult();
}
public function add(Form $entity, bool $flash = false): void
{
$this->getEntityManager()->persist($entity);
if ($flash) {
$this->getEntityManager()->flush();
}
}
public function remove(Form $entity, bool $flush = false): void
{
$this->getEntityManager()->remove($entity);
if ($flush) {
$this->getEntityManager()->flush();
}
}
}