mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
add accompanying period opening/closing to global timeline
This commit is contained in:
@@ -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]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -21,6 +21,7 @@ namespace Chill\PersonBundle\Timeline;
|
||||
|
||||
use Chill\MainBundle\Timeline\TimelineProviderInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
|
||||
/**
|
||||
* Provide information for opening periods to timeline
|
||||
@@ -46,22 +47,27 @@ class TimelineAccompanyingPeriodClosing extends AbstractTimelineAccompanyingPeri
|
||||
public function fetchQuery($context, array $args)
|
||||
{
|
||||
$metadata = $this->em
|
||||
->getClassMetadata('ChillPersonBundle:AccompanyingPeriod');
|
||||
->getClassMetadata(AccompanyingPeriod::class);
|
||||
|
||||
$data = $this->basicFetchQuery($context, $args);
|
||||
|
||||
$data['type'] = 'accompanying_period_closing';
|
||||
$data['date'] = $metadata->getColumnName('closingDate');
|
||||
$data['WHERE'] = sprintf('%s = %d AND %s IS NOT NULL',
|
||||
$metadata
|
||||
->getAssociationMapping('person')['joinColumns'][0]['name'],
|
||||
$args['person']->getId(),
|
||||
$metadata->getColumnName('closingDate'))
|
||||
;
|
||||
$data['WHERE'] = $this->buildWhereClause($context, $args);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function buildWhereClause($context, array $args): array
|
||||
{
|
||||
list($query, $params) = parent::buildWhereClause($context, $args);
|
||||
$period = $this->em->getClassMetadata(AccompanyingPeriod::class);
|
||||
|
||||
$query .= " AND {$period->getColumnName('closingDate')} IS NOT NULL ";
|
||||
|
||||
return [ $query, $params ];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
|
@@ -21,11 +21,10 @@ namespace Chill\PersonBundle\Timeline;
|
||||
|
||||
use Chill\MainBundle\Timeline\TimelineProviderInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Chill\PersonBundle\Entity\AccompanyingPeriod;
|
||||
|
||||
/**
|
||||
* Provide information for opening periods to timeline
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
*/
|
||||
class TimelineAccompanyingPeriodOpening extends AbstractTimelineAccompanyingPeriod
|
||||
{
|
||||
@@ -46,7 +45,7 @@ class TimelineAccompanyingPeriodOpening extends AbstractTimelineAccompanyingPeri
|
||||
public function fetchQuery($context, array $args)
|
||||
{
|
||||
$metadata = $this->em
|
||||
->getClassMetadata('ChillPersonBundle:AccompanyingPeriod');
|
||||
->getClassMetadata(AccompanyingPeriod::class);
|
||||
|
||||
$data = $this->basicFetchQuery($context, $args);
|
||||
|
||||
|
Reference in New Issue
Block a user