add accompanying period opening/closing to global timeline

This commit is contained in:
2021-05-17 13:24:50 +02:00
parent 73653744d7
commit ea477a9842
10 changed files with 155 additions and 67 deletions

View File

@@ -21,6 +21,13 @@ namespace Chill\PersonBundle\Timeline;
use Chill\MainBundle\Timeline\TimelineProviderInterface;
use Doctrine\ORM\EntityManager;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation;
use Chill\MainBundle\Entity\Center;
use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Symfony\Component\Security\Core\Security;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
/**
* Provide method to build timeline for accompanying periods
@@ -28,19 +35,22 @@ use Doctrine\ORM\EntityManager;
* This class is resued by TimelineAccompanyingPeriodOpening (for opening)
* and TimelineAccompanyingPeriodClosing (for closing)
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
abstract class AbstractTimelineAccompanyingPeriod implements TimelineProviderInterface
{
/**
*
* @var EntityManager
*/
protected $em;
protected EntityManager $em;
private Security $security;
private AuthorizationHelper $authorizationHelper;
private const SUPPORTED_CONTEXTS = [ 'person', 'center' ];
public function __construct(EntityManager $em)
public function __construct(EntityManager $em, Security $security, AuthorizationHelper $authorizationHelper)
{
$this->em = $em;
$this->security = $security;
$this->authorizationHelper = $authorizationHelper;
}
/**
@@ -72,25 +82,74 @@ abstract class AbstractTimelineAccompanyingPeriod implements TimelineProviderInt
*/
protected function basicFetchQuery($context, array $args)
{
if ($context !== 'person') {
if (FALSE === \in_array($context, self::SUPPORTED_CONTEXTS)) {
throw new \LogicException('TimelineAccompanyingPeriod is not able '
. 'to render context '.$context);
}
$metadata = $this->em
->getClassMetadata('ChillPersonBundle:AccompanyingPeriod')
->getClassMetadata(AccompanyingPeriodParticipation::class)
;
return array(
'id' => $metadata->getColumnName('id'),
'FROM' => $metadata->getTableName(),
'WHERE' => sprintf('%s = %d',
$metadata
->getAssociationMapping('person')['joinColumns'][0]['name'],
$args['person']->getId())
'id' => "{$metadata->getTableName()}.{$metadata->getColumnName('id')}",
'FROM' => $this->buildFromClause($context),
'WHERE' => $this->buildWhereClause($context, $args)
);
}
private function buildFromClause($context)
{
$period = $this->em->getClassMetadata(AccompanyingPeriod::class);
$participation = $this->em->getClassMetadata(AccompanyingPeriodParticipation::class);
$person = $this->em->getClassMetadata(Person::class);
$join = $participation->getAssociationMapping('accompanyingPeriod')['joinColumns'][0];
$joinPerson = $participation->getAssociationMapping('person')['joinColumns'][0];
if ($context === 'person') {
return "{$period->getTableName()} ".
"JOIN {$participation->getTableName()} ".
"ON {$participation->getTableName()}.{$join['name']} = ".
"{$period->getTableName()}.{$join['referencedColumnName']}";
} else {
return "{$period->getTableName()} ".
"JOIN {$participation->getTableName()} ".
"ON {$participation->getTableName()}.{$join['name']} = ".
"{$period->getTableName()}.{$join['referencedColumnName']} ".
"JOIN {$person->getTableName()} ".
"ON {$participation->getTableName()}.{$joinPerson['name']} = ".
"{$person->getTableName()}.{$joinPerson['referencedColumnName']}"
;
}
}
protected function buildWhereClause($context, array $args): array
{
$participation = $this->em->getClassMetadata(AccompanyingPeriodParticipation::class);
$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') {
$params = [];
$questionMarks = [];
$query = "{$person->getTableName()}.{$joinCenter['name']} IN (";
foreach ($allowedCenters as $c) {
$questionMarks[] = '?';
$params[] = $c->getId();
}
$query .= \implode(", ", $questionMarks).")";
return [$query, $params];
} elseif ($context === 'person') {
return [ "{$participation->getTableName()}.{$join['name']} = ?", [ $args['person']->getId() ]];
}
throw new \LogicException("this context is not supported: $context");
}
/**
* return the expected response for TimelineProviderInterface::getEntityTemplate
*
@@ -104,7 +163,7 @@ abstract class AbstractTimelineAccompanyingPeriod implements TimelineProviderInt
{
return array(
'template' => $template,
'template_data' => ['person' => $args['person'], 'period' => $entity]
'template_data' => ['period' => $entity, 'context' => $context]
);
}
}