mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
141 lines
4.2 KiB
PHP
141 lines
4.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Repository\AccompanyingPeriod;
|
|
|
|
use Chill\MainBundle\Entity\User;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
|
use Chill\PersonBundle\Entity\AccompanyingPeriod\AccompanyingPeriodWork;
|
|
use Chill\PersonBundle\Entity\SocialWork\SocialAction;
|
|
use DateTimeImmutable;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Doctrine\ORM\QueryBuilder;
|
|
use Doctrine\Persistence\ObjectRepository;
|
|
|
|
final class AccompanyingPeriodWorkRepository implements ObjectRepository
|
|
{
|
|
private EntityRepository $repository;
|
|
|
|
public function __construct(EntityManagerInterface $entityManager)
|
|
{
|
|
$this->repository = $entityManager->getRepository(AccompanyingPeriodWork::class);
|
|
}
|
|
|
|
public function countByAccompanyingPeriod(AccompanyingPeriod $period): int
|
|
{
|
|
return $this->repository->countByAccompanyingPeriod($period);
|
|
}
|
|
|
|
public function countBySocialActionWithDescendants(SocialAction $action): int
|
|
{
|
|
$qb = $this->buildQueryBySocialActionWithDescendants($action);
|
|
$qb->select('COUNT(g)');
|
|
|
|
return $qb
|
|
->getQuery()
|
|
->getSingleScalarResult();
|
|
}
|
|
|
|
public function countNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): int
|
|
{
|
|
return $this->buildQueryNearEndDateByUser($user, $since, $until)
|
|
->select('count(w)')->getQuery()->getSingleScalarResult();
|
|
}
|
|
|
|
public function find($id): ?AccompanyingPeriodWork
|
|
{
|
|
return $this->repository->find($id);
|
|
}
|
|
|
|
public function findAll(): array
|
|
{
|
|
return $this->repository->findAll();
|
|
}
|
|
|
|
public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array
|
|
{
|
|
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
|
|
}
|
|
|
|
/**
|
|
* @param mixed|null $orderBy
|
|
* @param mixed|null $limit
|
|
* @param mixed|null $offset
|
|
*
|
|
* @return AccompanyingPeriodWork[]
|
|
*/
|
|
public function findByAccompanyingPeriod(AccompanyingPeriod $period, $orderBy = null, $limit = null, $offset = null): array
|
|
{
|
|
return $this->repository->findByAccompanyingPeriod($period, $orderBy, $limit, $offset);
|
|
}
|
|
|
|
public function findNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until, int $limit = 20, int $offset = 0): array
|
|
{
|
|
return $this->buildQueryNearEndDateByUser($user, $since, $until)
|
|
->select('w')
|
|
->setFirstResult($offset)
|
|
->setMaxResults($limit)
|
|
->getQuery()
|
|
->getResult();
|
|
}
|
|
|
|
public function findOneBy(array $criteria): ?AccompanyingPeriodWork
|
|
{
|
|
return $this->repository->findOneBy($criteria);
|
|
}
|
|
|
|
public function getClassName()
|
|
{
|
|
return AccompanyingPeriodWork::class;
|
|
}
|
|
|
|
private function buildQueryBySocialActionWithDescendants(SocialAction $action): QueryBuilder
|
|
{
|
|
$actions = $action->getDescendantsWithThis();
|
|
|
|
$qb = $this->repository->createQueryBuilder('g');
|
|
|
|
$orx = $qb->expr()->orX();
|
|
$i = 0;
|
|
|
|
foreach ($actions as $a) {
|
|
$orx->add(":action_{$i} MEMBER OF g.socialActions");
|
|
$qb->setParameter("action_{$i}", $a);
|
|
}
|
|
$qb->where($orx);
|
|
|
|
return $qb;
|
|
}
|
|
|
|
private function buildQueryNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): QueryBuilder
|
|
{
|
|
$qb = $this->repository->createQueryBuilder('w');
|
|
|
|
$qb
|
|
->join('w.accompanyingPeriod', 'period')
|
|
->where(
|
|
$qb->expr()->andX(
|
|
$qb->expr()->eq('period.user', ':user'),
|
|
$qb->expr()->gte('w.endDate', ':since'),
|
|
$qb->expr()->lte('w.startDate', ':until')
|
|
)
|
|
)
|
|
->setParameters([
|
|
'user' => $user,
|
|
'since' => $since,
|
|
'until' => $until,
|
|
]);
|
|
|
|
return $qb;
|
|
}
|
|
}
|