chill-bundles/src/Bundle/ChillTaskBundle/Repository/SingleTaskAclAwareRepository.php

128 lines
3.7 KiB
PHP

<?php
namespace Chill\TaskBundle\Repository;
use Chill\TaskBundle\Entity\SingleTask;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\QueryBuilder;
use Symfony\Component\Security\Core\Security;
final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepositoryInterface
{
private EntityManagerInterface $em;
private Security $security;
public function __construct(EntityManagerInterface $em, Security $security)
{
$this->em = $em;
$this->security = $security;
}
public function findByCurrentUsersTasks(
?string $pattern = null,
?array $flags = [],
?int $start = 0,
?int $limit = 50,
?array $orderBy = []
): array {
$qb = $this->buildQueryMyTasks($pattern, $flags);
$qb->select('t');
$qb
->setFirstResult($start)
->setMaxResults($limit)
;
foreach ($orderBy as $field => $direction) {
$qb->addOrderBy('t.'.$field, $direction);
}
return $qb->getQuery()->getResult();
}
public function countByCurrentUsersTasks(
?string $pattern = null,
?array $flags = []
): int {
$qb = $this->buildQueryMyTasks($pattern, $flags);
$qb->select('COUNT(t)');
return $qb->getQuery()->getSingleScalarResult();
}
public function buildQueryMyTasks(
?string $pattern = null,
?array $flags = []
): QueryBuilder {
$qb = $this->em->createQueryBuilder();
$qb
->from(SingleTask::class, 't')
->where($qb->expr()->eq('t.assignee', ':user'))
->setParameter('user', $this->security->getUser())
;
if (!empty($pattern)) {
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(t.title))', 'LOWER(UNACCENT(:pattern))'))
->setParameter('pattern', $pattern)
;
}
if (count($flags) > 0) {
$orX = $qb->expr()->orX();
$now = new \DateTime();
if (\in_array('no-alert', $flags)) {
$orX
->add(
$qb->expr()->orX(
$qb->expr()->isNull('t.endDate'),
$qb->expr()->gte('t.endDate - COALESCE(t.warningInterval, :intervalBlank)', ':now')
)
);
$qb
->setParameter('intervalBlank', new \DateInterval('P0D'))
->setParameter('now', $now)
;
}
if (\in_array('warning', $flags)) {
$orX
->add(
$qb->expr()->andX(
$qb->expr()->eq('t.closed', "'FALSE'"),
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
$qb->expr()->not($qb->expr()->isNull('t.warningInterval')),
$qb->expr()->lte('t.endDate - t.warningInterval', ':now')
)
)
;
$qb
->setParameter('now', $now)
;
}
if (\in_array('alert', $flags)) {
$orX
->add(
$qb->expr()->andX(
$qb->expr()->eq('t.closed', "'FALSE'"),
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
$qb->expr()->lte('t.endDate', ':now')
)
)
;
$qb
->setParameter('now', $now)
;
}
$qb->andWhere($orX);
}
return $qb;
}
}