<?php
namespace App\Repository;
use App\Entity\BFEdition;
use App\Entity\BFSubscription;
use App\Entity\BFUser;
use App\Object\FestivalSearch;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method BFEdition|null find($id, $lockMode = null, $lockVersion = null)
* @method BFEdition|null findOneBy(array $criteria, array $orderBy = null)
* @method BFEdition[] findAll()
* @method BFEdition[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class BFEditionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, BFEdition::class);
}
/**
* @return BFEdition[]
*/
public function findNearestIsactive(bool $isactive=true, int $maxnumber=9): array
{
$qb = $this->createQueryBuilder('e')
->andwhere('e.isactive = :isactive')
->andWhere('e.startdate >= :now OR :now BETWEEN e.startdate AND e.enddate')
->setParameter('isactive', $isactive)
->setParameter('now', date('Y-m-d'))
->orderBy('e.startdate', 'ASC')
->addOrderBy('e.enddate', 'ASC');
$query = $qb->getQuery();
$query->setMaxResults($maxnumber);
return $query->execute();
}
/**
* @return BFEdition[]
*/
public function findNearestSubscriptions(BFUser $user, bool $isactive=true, int $maxnumber=3): array
{
$qb = $this->createQueryBuilder('e')
->innerJoin(BFSubscription::class, 's', 'WITH', 'e.festival = s.festival')
->andwhere('e.isactive = :isactive')
->andWhere('e.startdate >= :now OR :now BETWEEN e.startdate AND e.enddate')
->andWhere('s.user = :user')
->setParameter('isactive', $isactive)
->setParameter('now', date('Y-m-d'))
->setParameter('user', $user)
->orderBy('e.startdate', 'ASC')
->addOrderBy('e.enddate', 'ASC');
$query = $qb->getQuery();
$query->setMaxResults($maxnumber);
return $query->execute();
}
/**
* @return BFEdition[]
*/
public function findAllSubscriptions(BFUser $user, bool $isactive=true): array
{
$qb = $this->createQueryBuilder('e')
->innerJoin(BFSubscription::class, 's', 'WITH', 'e.festival = s.festival')
->andwhere('e.isactive = :isactive')
->andWhere('e.startdate >= :now OR :now BETWEEN e.startdate AND e.enddate')
->andWhere('s.user = :user')
->setParameter('isactive', $isactive)
->setParameter('now', date('Y-m-d'))
->setParameter('user', $user)
->orderBy('e.startdate', 'ASC')
->addOrderBy('e.enddate', 'ASC');
$query = $qb->getQuery();
return $query->execute();
}
/**
* @return BFEdition[]
*/
public function findAllPossible(FestivalSearch $festivalsearch): array
{
$qb = $this->createQueryBuilder('e')
->andwhere('e.isactive = :isactive')
->andWhere('e.startdate >= :now OR :date BETWEEN e.startdate AND e.enddate')
->orderBy('e.startdate', 'ASC')
->addOrderBy('e.enddate', 'ASC')
->setParameter('isactive', true);
if($festivalsearch->hasLocation())
{
$location = explode(',', $festivalsearch->getLocation())[0];
if($festivalsearch->getPlaceid()!=null)
{
$qb->andwhere('e.placeid is not null OR e.location LIKE :location');
}
else
{
$qb->andwhere('e.location LIKE :location');
}
$qb->setParameter('location', '%'.$location.'%');
}
if($festivalsearch->hasDate())
{
$datetime = \DateTime::createFromFormat('d/m/Y',$festivalsearch->getDate());
$nowdatetime= \DateTime::createFromFormat('d/m/Y', date('d/m/Y'));
if($datetime<$nowdatetime)
{
$qb->setParameter('now', date('Y-m-d'));
}
else
{
$qb->setParameter('now', $datetime->format('Y-m-d'));
}
$qb->setParameter('date', $datetime->format('Y-m-d'));
}
else
{
$qb->setParameter('now', date('Y-m-d'));
$qb->setParameter('date', date('Y-m-d'));
}
$query = $qb->getQuery();
return $query->execute();
}
// /**
// * @return BFEdition[] Returns an array of BFEdition objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('b')
->andWhere('b.exampleField = :val')
->setParameter('val', $value)
->orderBy('b.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?BFEdition
{
return $this->createQueryBuilder('b')
->andWhere('b.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}