63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\BookingSeries;
|
|
use App\Entity\ResourceType;
|
|
use App\Entity\User;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<BookingSeries>
|
|
*/
|
|
class BookingSeriesRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, BookingSeries::class);
|
|
}
|
|
|
|
/**
|
|
* @return list<BookingSeries>
|
|
*/
|
|
public function findActiveForUser(User $user, \DateTimeImmutable $on): array
|
|
{
|
|
return $this->findActive($on, $user);
|
|
}
|
|
|
|
/**
|
|
* Every series still running on the given day, optionally narrowed to one person or one kind
|
|
* of resource. The admin list is the caller that needs it across everybody.
|
|
*
|
|
* The user is eager-selected because the admin list names the holder of every row -- without
|
|
* it the page fires a query per series.
|
|
*
|
|
* @return list<BookingSeries>
|
|
*/
|
|
public function findActive(\DateTimeImmutable $on, ?User $user = null, ?ResourceType $type = null): array
|
|
{
|
|
$qb = $this->createQueryBuilder('s')
|
|
->addSelect('u', 'r', 'g')
|
|
->join('s.user', 'u')
|
|
->join('s.resource', 'r')
|
|
->leftJoin('r.group', 'g')
|
|
->andWhere('s.endDate >= :on')
|
|
->setParameter('on', $on->setTime(0, 0))
|
|
->orderBy('s.startDate', 'ASC')
|
|
->addOrderBy('u.name', 'ASC');
|
|
|
|
if (null !== $user) {
|
|
$qb->andWhere('s.user = :user')->setParameter('user', $user);
|
|
}
|
|
|
|
if (null !== $type) {
|
|
$qb->andWhere('r.type = :type')->setParameter('type', $type);
|
|
}
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
}
|