mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-29 11:03:50 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\TaskBundle\Repository;
|
||||
|
||||
/**
|
||||
* AbstractTaskRepository
|
||||
* AbstractTaskRepository.
|
||||
*
|
||||
* This class was generated by the Doctrine ORM. Add your own custom
|
||||
* repository methods below.
|
||||
|
@@ -1,9 +1,16 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\TaskBundle\Repository;
|
||||
|
||||
/**
|
||||
* RecurringTaskRepository
|
||||
* RecurringTaskRepository.
|
||||
*
|
||||
* This class was generated by the Doctrine ORM. Add your own custom
|
||||
* repository methods below.
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?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\TaskBundle\Repository;
|
||||
@@ -10,17 +17,24 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\TaskBundle\Entity\SingleTask;
|
||||
use Chill\TaskBundle\Security\Authorization\TaskVoter;
|
||||
use DateInterval;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use LogicException;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use function substr;
|
||||
|
||||
final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepositoryInterface
|
||||
{
|
||||
private AuthorizationHelperInterface $authorizationHelper;
|
||||
private EntityManagerInterface $em;
|
||||
private Security $security;
|
||||
|
||||
private CenterResolverDispatcherInterface $centerResolverDispatcher;
|
||||
|
||||
private EntityManagerInterface $em;
|
||||
|
||||
private Security $security;
|
||||
|
||||
public function __construct(
|
||||
CenterResolverDispatcherInterface $centerResolverDispatcher,
|
||||
EntityManagerInterface $em,
|
||||
@@ -33,41 +47,150 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
}
|
||||
|
||||
public function findByCurrentUsersTasks(
|
||||
public function buildBaseQuery(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryMyTasks($pattern, $flags);
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$qb
|
||||
->from(SingleTask::class, 't');
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
if (!empty($pattern)) {
|
||||
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(t.title))', 'LOWER(UNACCENT(:pattern))'))
|
||||
->setParameter('pattern', '%' . $pattern . '%');
|
||||
}
|
||||
|
||||
if (count($flags) > 0) {
|
||||
$orXDate = $qb->expr()->orX();
|
||||
$orXState = $qb->expr()->orX();
|
||||
$now = new DateTime();
|
||||
|
||||
foreach ($flags as $key => $flag) {
|
||||
switch ($flag) {
|
||||
case 'no-alert':
|
||||
$orXDate
|
||||
->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);
|
||||
|
||||
break;
|
||||
|
||||
case 'warning':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$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->expr()->gt('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
|
||||
break;
|
||||
|
||||
case 'alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->lte('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
|
||||
break;
|
||||
|
||||
case 'state_new':
|
||||
$orXState
|
||||
->add(
|
||||
'JSONB_ARRAY_LENGTH(t.currentStates) = 0'
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case substr($flag, 0, 6) === 'state_':
|
||||
$state = substr($flag, 6);
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_EXISTS_IN_ARRAY(t.currentStates, :state_{$key}) = 'TRUE'"
|
||||
);
|
||||
$qb->setParameter("state_{$key}", $state);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new LogicException("this flag is not supported: {$flag}");
|
||||
}
|
||||
}
|
||||
|
||||
if ($orXDate->count() > 0) {
|
||||
$qb->andWhere($orXDate);
|
||||
}
|
||||
|
||||
if ($orXState->count() > 0) {
|
||||
$qb->andWhere($orXState);
|
||||
}
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function countByCurrentUsersTasks(
|
||||
public function buildQueryByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.course', ':course'))
|
||||
->setParameter('course', $course);
|
||||
}
|
||||
|
||||
public function buildQueryByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.person', ':person'))
|
||||
->setParameter('person', $person);
|
||||
}
|
||||
|
||||
public function buildQueryMyTasks(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.assignee', ':user'))
|
||||
->setParameter('user', $this->security->getUser());
|
||||
}
|
||||
|
||||
public function countByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): int {
|
||||
return $this->buildQueryMyTasks($pattern, $flags)
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $this
|
||||
->addACLGlobal($qb)
|
||||
->select('COUNT(t)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByCourse($course, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $course);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function countByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
@@ -81,18 +204,13 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findByPerson(
|
||||
Person $person,
|
||||
public function countByCurrentUsersTasks(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByPerson($person, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $person);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
?array $flags = []
|
||||
): int {
|
||||
return $this->buildQueryMyTasks($pattern, $flags)
|
||||
->select('COUNT(t)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function countByPerson(
|
||||
@@ -108,18 +226,6 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function countByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): int {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $this
|
||||
->addACLGlobal($qb)
|
||||
->select('COUNT(t)')
|
||||
->getQuery()->getSingleScalarResult();
|
||||
}
|
||||
|
||||
public function findByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
@@ -133,42 +239,44 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function buildQueryByCourse(
|
||||
public function findByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
) : QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByCourse($course, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $course);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.course', ':course'))
|
||||
->setParameter('course', $course)
|
||||
;
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function buildQueryByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder
|
||||
{
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.person', ':person'))
|
||||
->setParameter('person', $person);
|
||||
}
|
||||
|
||||
public function buildQueryMyTasks(
|
||||
public function findByCurrentUsersTasks(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->buildBaseQuery($pattern, $flags);
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryMyTasks($pattern, $flags);
|
||||
|
||||
return $qb
|
||||
->andWhere($qb->expr()->eq('t.assignee', ':user'))
|
||||
->setParameter('user', $this->security->getUser())
|
||||
;
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function findByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array {
|
||||
$qb = $this->buildQueryByPerson($person, $pattern, $flags);
|
||||
$qb = $this->addACL($qb, $person);
|
||||
|
||||
return $this->getResult($qb, $start, $limit, $orderBy);
|
||||
}
|
||||
|
||||
public function getResult(
|
||||
@@ -181,11 +289,10 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
|
||||
$qb
|
||||
->setFirstResult($start)
|
||||
->setMaxResults($limit)
|
||||
;
|
||||
->setMaxResults($limit);
|
||||
|
||||
foreach ($orderBy as $field => $direction) {
|
||||
$qb->addOrderBy('t.'.$field, $direction);
|
||||
$qb->addOrderBy('t.' . $field, $direction);
|
||||
}
|
||||
|
||||
return $qb->getQuery()->getResult();
|
||||
@@ -195,11 +302,14 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
QueryBuilder $qb,
|
||||
$entity
|
||||
): QueryBuilder {
|
||||
$scopes = $this->authorizationHelper->getReachableScopes($this->security->getUser(),
|
||||
TaskVoter::SHOW, $this->centerResolverDispatcher->resolveCenter($entity));
|
||||
$scopes = $this->authorizationHelper->getReachableScopes(
|
||||
$this->security->getUser(),
|
||||
TaskVoter::SHOW,
|
||||
$this->centerResolverDispatcher->resolveCenter($entity)
|
||||
);
|
||||
|
||||
return $qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
|
||||
->setParameter('scopes', $scopes);
|
||||
return $qb->andWhere($qb->expr()->in('t.circle', ':scopes'))
|
||||
->setParameter('scopes', $scopes);
|
||||
}
|
||||
|
||||
private function addACLGlobal(
|
||||
@@ -217,121 +327,35 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
|
||||
$qb->leftJoin('t.person', 'person')
|
||||
->leftJoin('t.course', 'course')
|
||||
->leftJoin('course.participations', 'participation')
|
||||
->leftJoin('participation.person', 'person_p')
|
||||
;
|
||||
->leftJoin('participation.person', 'person_p');
|
||||
$qb->distinct(true);
|
||||
|
||||
$k = 0;
|
||||
$orX = $qb->expr()->orX();
|
||||
|
||||
foreach ($allowedCenters as $center) {
|
||||
$allowedScopes = $this->authorizationHelper->getReachableScopes($this->security->getUser(),
|
||||
TaskVoter::SHOW, $center);
|
||||
$allowedScopes = $this->authorizationHelper->getReachableScopes(
|
||||
$this->security->getUser(),
|
||||
TaskVoter::SHOW,
|
||||
$center
|
||||
);
|
||||
|
||||
$and = $qb->expr()->andX(
|
||||
$qb->expr()->orX(
|
||||
$qb->expr()->eq('person.center', ':center_'.$k),
|
||||
$qb->expr()->eq('person_p.center', ':center_'.$k)
|
||||
$qb->expr()->eq('person.center', ':center_' . $k),
|
||||
$qb->expr()->eq('person_p.center', ':center_' . $k)
|
||||
),
|
||||
$qb->expr()->in('t.circle', ':scopes_'.$k)
|
||||
$qb->expr()->in('t.circle', ':scopes_' . $k)
|
||||
);
|
||||
$qb
|
||||
->setParameter('center_'.$k, $center)
|
||||
->setParameter('scopes_'.$k, $allowedScopes);
|
||||
->setParameter('center_' . $k, $center)
|
||||
->setParameter('scopes_' . $k, $allowedScopes);
|
||||
$orX->add($and);
|
||||
|
||||
$k++;
|
||||
++$k;
|
||||
}
|
||||
$qb->andWhere($orX);
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
public function buildBaseQuery (
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): QueryBuilder {
|
||||
$qb = $this->em->createQueryBuilder();
|
||||
$qb
|
||||
->from(SingleTask::class, 't')
|
||||
;
|
||||
|
||||
if (!empty($pattern)) {
|
||||
$qb->andWhere($qb->expr()->like('LOWER(UNACCENT(t.title))', 'LOWER(UNACCENT(:pattern))'))
|
||||
->setParameter('pattern', '%'.$pattern.'%')
|
||||
;
|
||||
}
|
||||
|
||||
if (count($flags) > 0) {
|
||||
$orXDate = $qb->expr()->orX();
|
||||
$orXState = $qb->expr()->orX();
|
||||
$now = new \DateTime();
|
||||
|
||||
foreach ($flags as $key => $flag) {
|
||||
switch ($flag) {
|
||||
case 'no-alert':
|
||||
$orXDate
|
||||
->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);
|
||||
break;
|
||||
case 'warning':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$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->expr()->gt('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'alert':
|
||||
$orXDate
|
||||
->add(
|
||||
$qb->expr()->andX(
|
||||
$qb->expr()->not($qb->expr()->isNull('t.endDate')),
|
||||
$qb->expr()->lte('t.endDate', ':now')
|
||||
)
|
||||
);
|
||||
$qb
|
||||
->setParameter('now', $now);
|
||||
break;
|
||||
case 'state_new':
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_ARRAY_LENGTH(t.currentStates) = 0"
|
||||
);
|
||||
break;
|
||||
case \substr($flag, 0, 6) === 'state_':
|
||||
$state = \substr($flag, 6);
|
||||
$orXState
|
||||
->add(
|
||||
"JSONB_EXISTS_IN_ARRAY(t.currentStates, :state_$key) = 'TRUE'"
|
||||
);
|
||||
$qb->setParameter("state_$key", $state);
|
||||
break;
|
||||
default:
|
||||
throw new \LogicException("this flag is not supported: $flag");
|
||||
}
|
||||
}
|
||||
|
||||
if ($orXDate->count() > 0) {
|
||||
$qb->andWhere($orXDate);
|
||||
}
|
||||
if ($orXState->count() > 0) {
|
||||
$qb->andWhere($orXState);
|
||||
}
|
||||
}
|
||||
|
||||
return $qb;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,5 +1,12 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\TaskBundle\Repository;
|
||||
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
@@ -7,18 +14,10 @@ use Chill\PersonBundle\Entity\Person;
|
||||
|
||||
interface SingleTaskAclAwareRepositoryInterface
|
||||
{
|
||||
public function findByCurrentUsersTasks(?string $pattern = null, ?array $flags = [], ?int $start = 0, ?int $limit = 50, ?array $orderBy = []): array;
|
||||
|
||||
public function countByCurrentUsersTasks(?string $pattern = null, ?array $flags = []): int;
|
||||
|
||||
public function findByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
public function countByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array;
|
||||
?array $flags = []
|
||||
): int;
|
||||
|
||||
public function countByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
@@ -26,14 +25,7 @@ interface SingleTaskAclAwareRepositoryInterface
|
||||
?array $flags = []
|
||||
): int;
|
||||
|
||||
public function findByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array;
|
||||
public function countByCurrentUsersTasks(?string $pattern = null, ?array $flags = []): int;
|
||||
|
||||
public function countByPerson(
|
||||
Person $person,
|
||||
@@ -41,11 +33,6 @@ interface SingleTaskAclAwareRepositoryInterface
|
||||
?array $flags = []
|
||||
): int;
|
||||
|
||||
public function countByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = []
|
||||
): int;
|
||||
|
||||
public function findByAllViewable(
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
@@ -53,4 +40,24 @@ interface SingleTaskAclAwareRepositoryInterface
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array;
|
||||
|
||||
public function findByCourse(
|
||||
AccompanyingPeriod $course,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array;
|
||||
|
||||
public function findByCurrentUsersTasks(?string $pattern = null, ?array $flags = [], ?int $start = 0, ?int $limit = 50, ?array $orderBy = []): array;
|
||||
|
||||
public function findByPerson(
|
||||
Person $person,
|
||||
?string $pattern = null,
|
||||
?array $flags = [],
|
||||
?int $start = 0,
|
||||
?int $limit = 50,
|
||||
?array $orderBy = []
|
||||
): array;
|
||||
}
|
||||
|
@@ -1,58 +1,65 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\TaskBundle\Repository;
|
||||
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\TaskBundle\Security\Authorization\TaskVoter;
|
||||
use DateTime;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\EntityRepository;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use LogicException;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\TaskBundle\Security\Authorization\TaskVoter;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Chill\MainBundle\Entity\Center;
|
||||
use UnexpectedValueException;
|
||||
use function array_key_exists;
|
||||
|
||||
/**
|
||||
* Class SingleTaskRepository
|
||||
*
|
||||
* @package Chill\TaskBundle\Repository
|
||||
* Class SingleTaskRepository.
|
||||
*/
|
||||
class SingleTaskRepository extends EntityRepository
|
||||
{
|
||||
public const DATE_STATUS_CURRENT = 'current';
|
||||
|
||||
const DATE_STATUS_ENDED = 'ended';
|
||||
const DATE_STATUS_WARNING = 'warning';
|
||||
const DATE_STATUS_CURRENT = 'current';
|
||||
const DATE_STATUS_NOT_STARTED = 'not_started';
|
||||
public const DATE_STATUS_ENDED = 'ended';
|
||||
|
||||
const DATE_STATUSES = [
|
||||
public const DATE_STATUS_NOT_STARTED = 'not_started';
|
||||
|
||||
public const DATE_STATUS_WARNING = 'warning';
|
||||
|
||||
public const DATE_STATUSES = [
|
||||
self::DATE_STATUS_ENDED,
|
||||
self::DATE_STATUS_WARNING,
|
||||
self::DATE_STATUS_CURRENT,
|
||||
self::DATE_STATUS_NOT_STARTED
|
||||
self::DATE_STATUS_NOT_STARTED,
|
||||
];
|
||||
|
||||
/**
|
||||
*
|
||||
* @var AuthorizationHelper
|
||||
*/
|
||||
protected $authorizationHelper;
|
||||
|
||||
public function setAuthorizationHelper(AuthorizationHelper $authorizationHelper)
|
||||
{
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the tasks for given parameters.
|
||||
*
|
||||
* The parameters are describe in @see SingleTaskRepository::filterByParameters.
|
||||
*
|
||||
* @see SingleTaskRepository::filterByParameters
|
||||
*
|
||||
* @param array $params
|
||||
* @param User $currentUser
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countByParameters($params, User $currentUser = null)
|
||||
public function countByParameters($params, ?User $currentUser = null)
|
||||
{
|
||||
$qb = $this->createQueryBuilder('st')
|
||||
->select('COUNT(st)');
|
||||
@@ -61,8 +68,7 @@ class SingleTaskRepository extends EntityRepository
|
||||
|
||||
return (int) $qb
|
||||
->getQuery()
|
||||
->getSingleScalarResult()
|
||||
;
|
||||
->getSingleScalarResult();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,9 +86,7 @@ class SingleTaskRepository extends EntityRepository
|
||||
* - `types`: string[] an array of task types
|
||||
*
|
||||
* @param type $params
|
||||
* @param User $currentUser
|
||||
* @param int $firstResult
|
||||
* @param int $maxResults
|
||||
*
|
||||
* @return type
|
||||
*/
|
||||
public function findByParameters($params, User $currentUser, int $firstResult = 0, int $maxResults = 50)
|
||||
@@ -93,68 +97,16 @@ class SingleTaskRepository extends EntityRepository
|
||||
|
||||
$qb
|
||||
->setMaxResults($maxResults)
|
||||
->setFirstResult($firstResult)
|
||||
;
|
||||
->setFirstResult($firstResult);
|
||||
|
||||
return $qb
|
||||
->getQuery()
|
||||
->getResult()
|
||||
;
|
||||
->getResult();
|
||||
}
|
||||
|
||||
protected function buildQuery(QueryBuilder $qb, $params, User $currentUser = null)
|
||||
public function setAuthorizationHelper(AuthorizationHelper $authorizationHelper)
|
||||
{
|
||||
if (NULL !== $currentUser) {
|
||||
$this->buildACLQuery($qb, $currentUser);
|
||||
}
|
||||
|
||||
if (\array_key_exists('person', $params) and !empty($params['person'])) {
|
||||
$qb->andWhere($qb->expr()->eq('st.person', ':person'));
|
||||
$qb->setParameter('person', $params['person']);
|
||||
} elseif (\array_key_exists('center', $params)) {
|
||||
if ($params['center'] instanceof Center) {
|
||||
$qb->join('st.person', 'person');
|
||||
$qb->andWhere($qb->expr()->eq('person.center', ':center'));
|
||||
$qb->setParameter('center', $params['center']);
|
||||
} else {
|
||||
throw new \UnexpectedValueException("params 'center' should be an instance of ".Center::class);
|
||||
}
|
||||
}
|
||||
|
||||
if (\array_key_exists('unassigned', $params) and $params['unassigned'] === true) {
|
||||
if (\array_key_exists('user', $params) and !empty($params['user'])) {
|
||||
throw new \UnexpectedValueException("You should not require for "
|
||||
. "unassigned tasks and tasks assigned to some user.");
|
||||
}
|
||||
|
||||
$qb->andWhere($qb->expr()->isNull('st.assignee'));
|
||||
}
|
||||
|
||||
if (\array_key_exists('user', $params) and !empty($params['user'])) {
|
||||
$qb->andWhere($qb->expr()->eq('st.assignee', ':user'));
|
||||
$qb->setParameter('user', $params['user']);
|
||||
}
|
||||
|
||||
if (\array_key_exists('scope', $params) and !empty($params['scope'])) {
|
||||
$qb->andWhere($qb->expr()->eq('st.circle', ':scope'));
|
||||
$qb->setParameter('scope', $params['scope']);
|
||||
}
|
||||
|
||||
if (\array_key_exists('types', $params) && $params['types'] !== NULL) {
|
||||
if (count($params['types']) > 0) {
|
||||
$qb->andWhere($qb->expr()->in('st.type', ':types'));
|
||||
$qb->setParameter('types', $params['types']);
|
||||
}
|
||||
}
|
||||
|
||||
if (\array_key_exists('date_status', $params) and !empty($params['date_status'])) {
|
||||
$this->addTypeFilter($qb, $params);
|
||||
}
|
||||
|
||||
if (\array_key_exists('is_closed', $params)) {
|
||||
$qb->andWhere($this->buildIsClosed($qb, !$params['is_closed']));
|
||||
}
|
||||
|
||||
$this->authorizationHelper = $authorizationHelper;
|
||||
}
|
||||
|
||||
protected function addTypeFilter(QueryBuilder $qb, $params)
|
||||
@@ -164,15 +116,15 @@ class SingleTaskRepository extends EntityRepository
|
||||
switch ($params['date_status']) {
|
||||
case self::DATE_STATUS_ENDED:
|
||||
$andWhere
|
||||
->add($this->buildNowIsAfterEndDate($qb))
|
||||
;
|
||||
->add($this->buildNowIsAfterEndDate($qb));
|
||||
|
||||
break;
|
||||
|
||||
case self::DATE_STATUS_WARNING:
|
||||
$andWhere
|
||||
->add($this->buildNowIsAfterEndDate($qb, true))
|
||||
->add($this->buildNowIsAfterWarningDate($qb))
|
||||
;
|
||||
->add($this->buildNowIsAfterWarningDate($qb));
|
||||
|
||||
break;
|
||||
|
||||
case self::DATE_STATUS_CURRENT:
|
||||
@@ -180,125 +132,174 @@ class SingleTaskRepository extends EntityRepository
|
||||
$andWhere
|
||||
->add($this->buildNowIsAfterEndDate($qb, true))
|
||||
->add($this->buildNowIsAfterWarningDate($qb, true))
|
||||
->add($this->buildNowIsAfterStartDate($qb, false))
|
||||
;
|
||||
->add($this->buildNowIsAfterStartDate($qb, false));
|
||||
|
||||
break;
|
||||
|
||||
case self::DATE_STATUS_NOT_STARTED:
|
||||
$andWhere
|
||||
->add($this->buildNowIsAfterEndDate($qb, true))
|
||||
->add($this->buildNowIsAfterWarningDate($qb, true))
|
||||
->add($this->buildNowIsAfterStartDate($qb, true))
|
||||
;
|
||||
->add($this->buildNowIsAfterStartDate($qb, true));
|
||||
}
|
||||
$qb->setParameter('now', new \DateTime('today'), Types::DATE_MUTABLE);
|
||||
$qb->setParameter('now', new DateTime('today'), Types::DATE_MUTABLE);
|
||||
$qb->andWhere($andWhere);
|
||||
}
|
||||
|
||||
private function buildNowIsAfterEndDate(QueryBuilder $qb, $negative = false)
|
||||
{
|
||||
if ($negative === false) {
|
||||
return $qb->expr()->andX()
|
||||
->add($qb->expr()->isNotNull('st.endDate'))
|
||||
->add($qb->expr()->lte('st.endDate', ':now'))
|
||||
;
|
||||
} else {
|
||||
return $qb->expr()->orX()
|
||||
->add(
|
||||
$qb->expr()->andX()
|
||||
->add($qb->expr()->isNotNull('st.endDate'))
|
||||
->add($qb->expr()->gt('st.endDate', ':now'))
|
||||
)
|
||||
->add($qb->expr()->isNull('st.endDate'))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
private function buildNowIsAfterWarningDate(QueryBuilder $qb, bool $negative = false)
|
||||
{
|
||||
if ($negative === false) {
|
||||
return $qb->expr()->andX()
|
||||
->add($qb->expr()->lte(
|
||||
$qb->expr()->diff('st.endDate', 'st.warningInterval'), ':now'
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return $qb->expr()->orX()
|
||||
->add(
|
||||
$qb->expr()->andX()
|
||||
->add($qb->expr()->isNotNull('st.endDate'))
|
||||
->add($qb->expr()->isNotNull('st.warningInterval'))
|
||||
->add($qb->expr()->gt(
|
||||
$qb->expr()->diff('st.endDate', 'st.warningInterval'),
|
||||
':now'
|
||||
)
|
||||
)
|
||||
)
|
||||
->add($qb->expr()->isNull('st.endDate'))
|
||||
->add($qb->expr()->isNull('st.warningInterval'))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
private function buildNowIsAfterStartDate(QueryBuilder $qb, bool $negative = false)
|
||||
{
|
||||
if ($negative === false) {
|
||||
return $qb->expr()->orX()
|
||||
->add($qb->expr()->lte('st.startDate', ':now'))
|
||||
->add($qb->expr()->isNull('st.startDate'))
|
||||
;
|
||||
} else {
|
||||
return
|
||||
$qb->expr()->andX()
|
||||
->add($qb->expr()->gt('st.startDate', ':now'))
|
||||
->add($qb->expr()->isNotNull('st.startDate'))
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
private function buildIsClosed(QueryBuilder $qb, bool $negative = false)
|
||||
{
|
||||
if ($negative === false) {
|
||||
return $qb->expr()->eq('st.closed', "'TRUE'");
|
||||
} else {
|
||||
return $qb->expr()->eq('st.closed', "'FALSE'");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
protected function buildACLQuery(QueryBuilder $qb, User $currentUser)
|
||||
{
|
||||
if (NULL === $this->authorizationHelper) {
|
||||
throw new \LogicException("Injecting the authorization helper is "
|
||||
. "required to run this query. Please use dependency injection "
|
||||
. "to initialize this repository or use the method "
|
||||
. "`setAuthorizationHelper`");
|
||||
if (null === $this->authorizationHelper) {
|
||||
throw new LogicException('Injecting the authorization helper is '
|
||||
. 'required to run this query. Please use dependency injection '
|
||||
. 'to initialize this repository or use the method '
|
||||
. '`setAuthorizationHelper`');
|
||||
}
|
||||
|
||||
$role = new Role(TaskVoter::SHOW);
|
||||
$qb->join('st.person', 'p');
|
||||
|
||||
$centers = $this->authorizationHelper
|
||||
->getReachableCenters($currentUser, $role)
|
||||
;
|
||||
->getReachableCenters($currentUser, $role);
|
||||
|
||||
$i = 0;
|
||||
$where = $qb->expr()->orX();
|
||||
|
||||
foreach($centers as $center) {
|
||||
foreach ($centers as $center) {
|
||||
$circles = $this->authorizationHelper
|
||||
->getReachableCircles($currentUser, $role, $center);
|
||||
|
||||
$centerWhere = $qb->expr()->andX();
|
||||
|
||||
$centerWhere->add($qb->expr()->eq('p.center', ':center_'.$i));
|
||||
$qb->setParameter('center_'.$i, $center);
|
||||
$centerWhere->add($qb->expr()->in('st.circle', ':circles_'.$i));
|
||||
$qb->setParameter('circles_'.$i, $circles);
|
||||
$centerWhere->add($qb->expr()->eq('p.center', ':center_' . $i));
|
||||
$qb->setParameter('center_' . $i, $center);
|
||||
$centerWhere->add($qb->expr()->in('st.circle', ':circles_' . $i));
|
||||
$qb->setParameter('circles_' . $i, $circles);
|
||||
$where->add($centerWhere);
|
||||
$i ++;
|
||||
++$i;
|
||||
}
|
||||
|
||||
$qb->where($where);
|
||||
}
|
||||
|
||||
protected function buildQuery(QueryBuilder $qb, $params, ?User $currentUser = null)
|
||||
{
|
||||
if (null !== $currentUser) {
|
||||
$this->buildACLQuery($qb, $currentUser);
|
||||
}
|
||||
|
||||
if (array_key_exists('person', $params) and !empty($params['person'])) {
|
||||
$qb->andWhere($qb->expr()->eq('st.person', ':person'));
|
||||
$qb->setParameter('person', $params['person']);
|
||||
} elseif (array_key_exists('center', $params)) {
|
||||
if ($params['center'] instanceof Center) {
|
||||
$qb->join('st.person', 'person');
|
||||
$qb->andWhere($qb->expr()->eq('person.center', ':center'));
|
||||
$qb->setParameter('center', $params['center']);
|
||||
} else {
|
||||
throw new UnexpectedValueException("params 'center' should be an instance of " . Center::class);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('unassigned', $params) and true === $params['unassigned']) {
|
||||
if (array_key_exists('user', $params) and !empty($params['user'])) {
|
||||
throw new UnexpectedValueException('You should not require for '
|
||||
. 'unassigned tasks and tasks assigned to some user.');
|
||||
}
|
||||
|
||||
$qb->andWhere($qb->expr()->isNull('st.assignee'));
|
||||
}
|
||||
|
||||
if (array_key_exists('user', $params) and !empty($params['user'])) {
|
||||
$qb->andWhere($qb->expr()->eq('st.assignee', ':user'));
|
||||
$qb->setParameter('user', $params['user']);
|
||||
}
|
||||
|
||||
if (array_key_exists('scope', $params) and !empty($params['scope'])) {
|
||||
$qb->andWhere($qb->expr()->eq('st.circle', ':scope'));
|
||||
$qb->setParameter('scope', $params['scope']);
|
||||
}
|
||||
|
||||
if (array_key_exists('types', $params) && null !== $params['types']) {
|
||||
if (count($params['types']) > 0) {
|
||||
$qb->andWhere($qb->expr()->in('st.type', ':types'));
|
||||
$qb->setParameter('types', $params['types']);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists('date_status', $params) and !empty($params['date_status'])) {
|
||||
$this->addTypeFilter($qb, $params);
|
||||
}
|
||||
|
||||
if (array_key_exists('is_closed', $params)) {
|
||||
$qb->andWhere($this->buildIsClosed($qb, !$params['is_closed']));
|
||||
}
|
||||
}
|
||||
|
||||
private function buildIsClosed(QueryBuilder $qb, bool $negative = false)
|
||||
{
|
||||
if (false === $negative) {
|
||||
return $qb->expr()->eq('st.closed', "'TRUE'");
|
||||
}
|
||||
|
||||
return $qb->expr()->eq('st.closed', "'FALSE'");
|
||||
}
|
||||
|
||||
private function buildNowIsAfterEndDate(QueryBuilder $qb, $negative = false)
|
||||
{
|
||||
if (false === $negative) {
|
||||
return $qb->expr()->andX()
|
||||
->add($qb->expr()->isNotNull('st.endDate'))
|
||||
->add($qb->expr()->lte('st.endDate', ':now'));
|
||||
}
|
||||
|
||||
return $qb->expr()->orX()
|
||||
->add(
|
||||
$qb->expr()->andX()
|
||||
->add($qb->expr()->isNotNull('st.endDate'))
|
||||
->add($qb->expr()->gt('st.endDate', ':now'))
|
||||
)
|
||||
->add($qb->expr()->isNull('st.endDate'));
|
||||
}
|
||||
|
||||
private function buildNowIsAfterStartDate(QueryBuilder $qb, bool $negative = false)
|
||||
{
|
||||
if (false === $negative) {
|
||||
return $qb->expr()->orX()
|
||||
->add($qb->expr()->lte('st.startDate', ':now'))
|
||||
->add($qb->expr()->isNull('st.startDate'));
|
||||
}
|
||||
|
||||
return
|
||||
$qb->expr()->andX()
|
||||
->add($qb->expr()->gt('st.startDate', ':now'))
|
||||
->add($qb->expr()->isNotNull('st.startDate'));
|
||||
}
|
||||
|
||||
private function buildNowIsAfterWarningDate(QueryBuilder $qb, bool $negative = false)
|
||||
{
|
||||
if (false === $negative) {
|
||||
return $qb->expr()->andX()
|
||||
->add(
|
||||
$qb->expr()->lte(
|
||||
$qb->expr()->diff('st.endDate', 'st.warningInterval'),
|
||||
':now'
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return $qb->expr()->orX()
|
||||
->add(
|
||||
$qb->expr()->andX()
|
||||
->add($qb->expr()->isNotNull('st.endDate'))
|
||||
->add($qb->expr()->isNotNull('st.warningInterval'))
|
||||
->add(
|
||||
$qb->expr()->gt(
|
||||
$qb->expr()->diff('st.endDate', 'st.warningInterval'),
|
||||
':now'
|
||||
)
|
||||
)
|
||||
)
|
||||
->add($qb->expr()->isNull('st.endDate'))
|
||||
->add($qb->expr()->isNull('st.warningInterval'));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user