diff --git a/src/Bundle/ChillActivityBundle/Timeline/TimelineActivityProvider.php b/src/Bundle/ChillActivityBundle/Timeline/TimelineActivityProvider.php index ff1d9ffb3..a4dbdfad3 100644 --- a/src/Bundle/ChillActivityBundle/Timeline/TimelineActivityProvider.php +++ b/src/Bundle/ChillActivityBundle/Timeline/TimelineActivityProvider.php @@ -101,7 +101,7 @@ class TimelineActivityProvider implements TimelineProviderInterface $metadataActivity = $this->em->getClassMetadata(Activity::class); [$where, $parameters] = $this->getWhereClauseForPerson($args['person']); - dump($where, $parameters); + return TimelineSingleQuery::fromArray([ 'id' => $metadataActivity->getTableName() .'.'.$metadataActivity->getColumnName('id'), @@ -119,49 +119,33 @@ class TimelineActivityProvider implements TimelineProviderInterface $parameters = []; $metadataActivity = $this->em->getClassMetadata('ChillActivityBundle:Activity'); $associationMapping = $metadataActivity->getAssociationMapping('person'); - $metadataPerson = $this->em->getClassMetadata('ChillPersonBundle:Person'); - $role = new Role('CHILL_ACTIVITY_SEE'); - $reachableCenters = $this->helper->getReachableCenters($this->user, - $role); - - if (count($reachableCenters) === 0) { - return 'FALSE = TRUE'; - } - - // we start with activities having the person_id linked to person - // (currently only context "person" is supported) - $whereClause = sprintf(' {activity.person_id} = ? '); + $reachableScopes = $this->helper->getReachableScopes($this->user, + $role, $person->getCenter()); + $whereClause = sprintf(' {activity.person_id} = ? AND {activity.scope_id} IN ({scopes_ids}) '); + $scopes_ids = []; + + // first parameter: activity.person_id $parameters[] = $person->getId(); - - // we add acl (reachable center and scopes) - $centerAndScopeClauses = []; - foreach ($reachableCenters as $center) { - $parameters[] = $center->getId(); - $scopes_ids = []; - $reachableScopes = $this->helper->getReachableScopes($this->user, $role, $person->getCenter()); - foreach ($reachableScopes as $scope) { - $scopes_ids[] = '?'; - $parameters[] = $scope->getId(); + + // loop on reachable scopes + foreach ($reachableScopes as $scope) { + if (\in_array($scope->getId(), $scopes_ids)) { + continue; } - $centerAndScopeClauses[] = \strtr( - '( {person.center_id} = ? AND {activity.scope_id} IN ({scopes_ids})) ', - [ - '{scopes_ids}' => \implode(", ", $scopes_ids) - ] - ); + $scopes_ids[] = '?'; + $parameters[] = $scope->getId(); } - $whereClause .= ' AND ('.\implode(' OR ', $centerAndScopeClauses).' ) '; return [ \strtr( $whereClause, [ '{activity.person_id}' => $associationMapping['joinColumns'][0]['name'], - '{person.center_id}' => $metadataPerson->getTableName().'.'. - $metadataPerson->getAssociationMapping('center')['joinColumns'][0]['name'], '{activity.scope_id}' => $metadataActivity->getTableName().'.'. $metadataActivity->getAssociationMapping('scope')['joinColumns'][0]['name'], + '{scopes_ids}' => \implode(", ", $scopes_ids) +, ] ), $parameters diff --git a/src/Bundle/ChillMainBundle/Timeline/TimelineBuilder.php b/src/Bundle/ChillMainBundle/Timeline/TimelineBuilder.php index bd1a4cc1a..de2ae9975 100644 --- a/src/Bundle/ChillMainBundle/Timeline/TimelineBuilder.php +++ b/src/Bundle/ChillMainBundle/Timeline/TimelineBuilder.php @@ -28,8 +28,6 @@ use Doctrine\ORM\NativeQuery; /** * Build timeline - * - * @author Julien Fastré */ class TimelineBuilder implements ContainerAwareInterface { diff --git a/src/Bundle/ChillMainBundle/Timeline/TimelineSingleQuery.php b/src/Bundle/ChillMainBundle/Timeline/TimelineSingleQuery.php index cf8dc085e..e7e456a80 100644 --- a/src/Bundle/ChillMainBundle/Timeline/TimelineSingleQuery.php +++ b/src/Bundle/ChillMainBundle/Timeline/TimelineSingleQuery.php @@ -38,12 +38,12 @@ class TimelineSingleQuery public static function fromArray(array $a) { return new TimelineSingleQuery( - $a['id'], - $a['date'], - $a['type'] ?? $a['key'], - $a['FROM'] ?? $a['from'], - $a['WHERE'] ?? $a['where'], - $a['parameters']); + $a['id'] ?? null, + $a['date'] ?? null, + $a['type'] ?? $a['key'] ?? null, + $a['FROM'] ?? $a['from'] ?? null, + $a['WHERE'] ?? $a['where'] ?? null, + $a['parameters'] ?? null); } public function getId(): string diff --git a/src/Bundle/ChillPersonBundle/Timeline/AbstractTimelineAccompanyingPeriod.php b/src/Bundle/ChillPersonBundle/Timeline/AbstractTimelineAccompanyingPeriod.php index 6b5b0cfd8..d5778280a 100644 --- a/src/Bundle/ChillPersonBundle/Timeline/AbstractTimelineAccompanyingPeriod.php +++ b/src/Bundle/ChillPersonBundle/Timeline/AbstractTimelineAccompanyingPeriod.php @@ -28,6 +28,7 @@ use Chill\MainBundle\Entity\Center; use Chill\PersonBundle\Security\Authorization\PersonVoter; use Symfony\Component\Security\Core\Security; use Chill\MainBundle\Security\Authorization\AuthorizationHelper; +use Chill\MainBundle\Timeline\TimelineSingleQuery; /** * Provide method to build timeline for accompanying periods @@ -88,14 +89,16 @@ abstract class AbstractTimelineAccompanyingPeriod implements TimelineProviderInt } $metadata = $this->em - ->getClassMetadata(AccompanyingPeriodParticipation::class) + ->getClassMetadata(AccompanyingPeriod::class) ; - - return array( + [$where, $parameters] = $this->buildWhereClause($context, $args); + + return TimelineSingleQuery::fromArray([ 'id' => "{$metadata->getTableName()}.{$metadata->getColumnName('id')}", 'FROM' => $this->buildFromClause($context), - 'WHERE' => $this->buildWhereClause($context, $args) - ); + 'WHERE' => $where, + 'parameters' => $parameters + ]); } private function buildFromClause($context) @@ -130,9 +133,9 @@ abstract class AbstractTimelineAccompanyingPeriod implements TimelineProviderInt $join = $participation->getAssociationMapping('person')['joinColumns'][0]; $person = $this->em->getClassMetadata(Person::class); $joinCenter = $person->getAssociationMapping('center')['joinColumns'][0]; - $allowedCenters = $this->authorizationHelper->filterReachableCenters($this->security->getUser(), $args['centers'], PersonVoter::SEE); if ($context === 'center') { + $allowedCenters = $this->authorizationHelper->filterReachableCenters($this->security->getUser(), $args['centers'], PersonVoter::SEE); $params = []; $questionMarks = []; $query = "{$person->getTableName()}.{$joinCenter['name']} IN ("; diff --git a/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodClosing.php b/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodClosing.php index 2e3d953fa..c873b6405 100644 --- a/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodClosing.php +++ b/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodClosing.php @@ -25,8 +25,6 @@ use Chill\PersonBundle\Entity\AccompanyingPeriod; /** * Provide information for opening periods to timeline - * - * @author Julien Fastré */ class TimelineAccompanyingPeriodClosing extends AbstractTimelineAccompanyingPeriod { @@ -49,13 +47,15 @@ class TimelineAccompanyingPeriodClosing extends AbstractTimelineAccompanyingPeri $metadata = $this->em ->getClassMetadata(AccompanyingPeriod::class); - $data = $this->basicFetchQuery($context, $args); - - $data['type'] = 'accompanying_period_closing'; - $data['date'] = $metadata->getColumnName('closingDate'); - $data['WHERE'] = $this->buildWhereClause($context, $args); + $query = $this->basicFetchQuery($context, $args); + [$where, $parameters] = $this->buildWhereClause($context, $args); + $query->setKey('accompanying_period_closing') + ->setDate($metadata->getColumnName('closingDate')) + ->setWhere($where) + ->setParameters($parameters) + ; - return $data; + return $query; } protected function buildWhereClause($context, array $args): array diff --git a/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodOpening.php b/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodOpening.php index 5d3222789..04b3df5b8 100644 --- a/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodOpening.php +++ b/src/Bundle/ChillPersonBundle/Timeline/TimelineAccompanyingPeriodOpening.php @@ -47,12 +47,12 @@ class TimelineAccompanyingPeriodOpening extends AbstractTimelineAccompanyingPeri $metadata = $this->em ->getClassMetadata(AccompanyingPeriod::class); - $data = $this->basicFetchQuery($context, $args); + $query = $this->basicFetchQuery($context, $args); - $data['type'] = 'accompanying_period_opening'; - $data['date'] = $metadata->getColumnName('openingDate'); + $query->setKey('accompanying_period_opening') + ->setDate($metadata->getColumnName('openingDate')); - return $data; + return $query; } /**