mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 22:04:23 +00:00
apply timeline for report
This commit is contained in:
parent
9b1a66c992
commit
5350a09951
@ -6,5 +6,5 @@ services:
|
|||||||
- '@chill.main.security.authorization.helper'
|
- '@chill.main.security.authorization.helper'
|
||||||
- '@security.token_storage'
|
- '@security.token_storage'
|
||||||
public: true
|
public: true
|
||||||
tags:
|
# tags:
|
||||||
- { name: chill.timeline, context: 'person' }
|
# - { name: chill.timeline, context: 'person' }
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
<div class="report_entry">
|
<div class="report_entry">
|
||||||
<h3>{{ report.date|format_date('long') }}<span class="report"> / {{ 'Report'|trans }}</span></h3>
|
<h3>{{ report.date|format_date('long') }}<span class="report"> / {{ 'Report'|trans }}</span>{% if 'person' != context %} / {{ report.person|chill_entity_render_box }}{% endif %}</h3>
|
||||||
<div class="statement">
|
<div class="statement">
|
||||||
<span class="statement">{{ '%user% has filled a %report_label% report'|trans(
|
<span class="statement">{{ '%user% has filled a %report_label% report'|trans(
|
||||||
{
|
{
|
||||||
'%user%' : user,
|
'%user%' : report.user,
|
||||||
'%report_label%': report.CFGroup.name|localize_translatable_string,
|
'%report_label%': report.CFGroup.name|localize_translatable_string,
|
||||||
'%date%' : report.date|format_date('long') }
|
'%date%' : report.date|format_date('long') }
|
||||||
) }}</span>
|
) }}</span>
|
||||||
@ -25,13 +25,13 @@
|
|||||||
|
|
||||||
<ul class="record_actions">
|
<ul class="record_actions">
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('report_view', { 'person_id': person.id, 'report_id': report.id} ) }}" class="sc-button bt-view">
|
<a href="{{ path('report_view', { 'person_id': report.person.id, 'report_id': report.id} ) }}" class="sc-button bt-view">
|
||||||
{{ 'View the report'|trans }}
|
{{ 'View the report'|trans }}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
||||||
{% if is_granted('CHILL_REPORT_UPDATE', report) %}
|
{% if is_granted('CHILL_REPORT_UPDATE', report) %}
|
||||||
<li>
|
<li>
|
||||||
<a href="{{ path('report_edit', { 'person_id': person.id, 'report_id': report.id} ) }}" class="sc-button bt-edit">
|
<a href="{{ path('report_edit', { 'person_id': report.person.id, 'report_id': report.id} ) }}" class="sc-button bt-edit">
|
||||||
{{ 'Update the report'|trans }}
|
{{ 'Update the report'|trans }}
|
||||||
</a>
|
</a>
|
||||||
</li>
|
</li>
|
@ -30,71 +30,34 @@ use Chill\PersonBundle\Entity\Person;
|
|||||||
use Chill\MainBundle\Entity\Scope;
|
use Chill\MainBundle\Entity\Scope;
|
||||||
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
|
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
|
||||||
use Chill\ReportBundle\Entity\Report;
|
use Chill\ReportBundle\Entity\Report;
|
||||||
|
use Symfony\Component\Security\Core\Security;
|
||||||
|
use Chill\MainBundle\Timeline\TimelineSingleQuery;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provide report for inclusion in timeline
|
* Provide report for inclusion in timeline
|
||||||
*
|
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
|
||||||
* @author Champs Libres <info@champs-libres.coop>
|
|
||||||
*/
|
*/
|
||||||
class TimelineReportProvider implements TimelineProviderInterface
|
class TimelineReportProvider implements TimelineProviderInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
protected EntityManager $em;
|
||||||
*
|
|
||||||
* @var EntityManager
|
|
||||||
*/
|
|
||||||
protected $em;
|
|
||||||
|
|
||||||
/**
|
protected AuthorizationHelper $helper;
|
||||||
*
|
|
||||||
* @var AuthorizationHelper
|
|
||||||
*/
|
|
||||||
protected $helper;
|
|
||||||
|
|
||||||
/**
|
protected CustomFieldsHelper $customFieldsHelper;
|
||||||
*
|
|
||||||
* @var \Chill\MainBundle\Entity\User
|
|
||||||
*/
|
|
||||||
protected $user;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @var CustomFieldsHelper
|
|
||||||
*/
|
|
||||||
protected $customFieldsHelper;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var
|
|
||||||
*/
|
|
||||||
protected $showEmptyValues;
|
protected $showEmptyValues;
|
||||||
|
|
||||||
/**
|
|
||||||
* TimelineReportProvider constructor.
|
|
||||||
*
|
|
||||||
* @param EntityManager $em
|
|
||||||
* @param AuthorizationHelper $helper
|
|
||||||
* @param TokenStorageInterface $storage
|
|
||||||
* @param CustomFieldsHelper $customFieldsHelper
|
|
||||||
* @param $showEmptyValues
|
|
||||||
*/
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
EntityManager $em,
|
EntityManager $em,
|
||||||
AuthorizationHelper $helper,
|
AuthorizationHelper $helper,
|
||||||
TokenStorageInterface $storage,
|
Security $security,
|
||||||
CustomFieldsHelper $customFieldsHelper,
|
CustomFieldsHelper $customFieldsHelper,
|
||||||
$showEmptyValues
|
$showEmptyValues
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
$this->em = $em;
|
$this->em = $em;
|
||||||
$this->helper = $helper;
|
$this->helper = $helper;
|
||||||
|
$this->security = $security;
|
||||||
if (!$storage->getToken()->getUser() instanceof \Chill\MainBundle\Entity\User)
|
|
||||||
{
|
|
||||||
throw new \RuntimeException('A user should be authenticated !');
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->user = $storage->getToken()->getUser();
|
|
||||||
$this->customFieldsHelper = $customFieldsHelper;
|
$this->customFieldsHelper = $customFieldsHelper;
|
||||||
$this->showEmptyValues = $showEmptyValues;
|
$this->showEmptyValues = $showEmptyValues;
|
||||||
}
|
}
|
||||||
@ -107,70 +70,162 @@ class TimelineReportProvider implements TimelineProviderInterface
|
|||||||
{
|
{
|
||||||
$this->checkContext($context);
|
$this->checkContext($context);
|
||||||
|
|
||||||
$metadataReport = $this->em->getClassMetadata('ChillReportBundle:Report');
|
$report = $this->em->getClassMetadata(Report::class);
|
||||||
$metadataPerson = $this->em->getClassMetadata('ChillPersonBundle:Person');
|
[$where, $parameters] = $this->getWhereClause($context, $args);
|
||||||
|
|
||||||
return array(
|
return TimelineSingleQuery::fromArray([
|
||||||
'id' => $metadataReport->getTableName()
|
'id' => $report->getTableName()
|
||||||
.'.'.$metadataReport->getColumnName('id'),
|
.'.'.$report->getColumnName('id'),
|
||||||
'type' => 'report',
|
'type' => 'report',
|
||||||
'date' => $metadataReport->getTableName()
|
'date' => $report->getTableName()
|
||||||
.'.'.$metadataReport->getColumnName('date'),
|
.'.'.$report->getColumnName('date'),
|
||||||
'FROM' => $this->getFromClause($metadataReport, $metadataPerson),
|
'FROM' => $this->getFromClause($context),
|
||||||
'WHERE' => $this->getWhereClause($metadataReport, $metadataPerson,
|
'WHERE' => $where,
|
||||||
$args['person'])
|
'parameters' => $parameters
|
||||||
);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getWhereClause(ClassMetadata $metadataReport,
|
private function getWhereClause(string $context, array $args): array
|
||||||
ClassMetadata $metadataPerson, Person $person)
|
|
||||||
{
|
{
|
||||||
|
switch ($context) {
|
||||||
|
case 'person':
|
||||||
|
return $this->getWhereClauseForPerson($context, $args);
|
||||||
|
case 'center':
|
||||||
|
return $this->getWhereClauseForCenter($context, $args);
|
||||||
|
default:
|
||||||
|
throw new \UnexpectedValueException("This context $context is not implemented");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getWhereClauseForCenter(string $context, array $args): array
|
||||||
|
{
|
||||||
|
$report = $this->em->getClassMetadata(Report::class);
|
||||||
|
$person = $this->em->getClassMetadata(Person::class);
|
||||||
$role = new Role('CHILL_REPORT_SEE');
|
$role = new Role('CHILL_REPORT_SEE');
|
||||||
$reachableCenters = $this->helper->getReachableCenters($this->user,
|
$reachableCenters = $this->helper->getReachableCenters($this->security->getUser(),
|
||||||
$role);
|
$role);
|
||||||
$associationMapping = $metadataReport->getAssociationMapping('person');
|
$reportPersonId = $report->getAssociationMapping('person')['joinColumns'][0]['name'];
|
||||||
|
$reportScopeId = $report->getAssociationMapping('scope')['joinColumns'][0]['name'];
|
||||||
|
$personCenterId = $person->getAssociationMapping('center')['joinColumns'][0]['name'];
|
||||||
|
// parameters for the query, will be filled later
|
||||||
|
$parameters = [];
|
||||||
|
|
||||||
// we start with reports having the person_id linked to person
|
// the clause, that will be joined with an "OR"
|
||||||
// (currently only context "person" is supported)
|
$centerScopesClause = "({person}.{center_id} = ? ".
|
||||||
$whereClause = sprintf('%s = %d',
|
"AND {report}.{scopes_id} IN ({scopes_ids}))";
|
||||||
$associationMapping['joinColumns'][0]['name'],
|
// container for formatted clauses
|
||||||
$person->getId());
|
$formattedClauses = [];
|
||||||
|
|
||||||
// we add acl (reachable center and scopes)
|
$askedCenters = $args['centers'];
|
||||||
$centerAndScopeLines = array();
|
|
||||||
foreach ($reachableCenters as $center) {
|
foreach ($reachableCenters as $center) {
|
||||||
$reachablesScopesId = array_map(
|
if (FALSE === \in_array($center, $askedCenters)) {
|
||||||
function(Scope $scope) { return $scope->getId(); },
|
continue;
|
||||||
$this->helper->getReachableScopes($this->user, $role,
|
|
||||||
$person->getCenter())
|
|
||||||
);
|
|
||||||
|
|
||||||
$centerAndScopeLines[] = sprintf('(%s = %d AND %s IN (%s))',
|
|
||||||
$metadataPerson->getTableName().'.'.
|
|
||||||
$metadataPerson->getAssociationMapping('center')['joinColumns'][0]['name'],
|
|
||||||
$center->getId(),
|
|
||||||
$metadataReport->getTableName().'.'.
|
|
||||||
$metadataReport->getAssociationMapping('scope')['joinColumns'][0]['name'],
|
|
||||||
implode(',', $reachablesScopesId));
|
|
||||||
|
|
||||||
}
|
|
||||||
$whereClause .= ' AND ('.implode(' OR ', $centerAndScopeLines).')';
|
|
||||||
|
|
||||||
return $whereClause;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getFromClause(ClassMetadata $metadataReport,
|
// add the center id to the parameters
|
||||||
ClassMetadata $metadataPerson)
|
$parameters[] = $center->getId();
|
||||||
|
// loop over scopes
|
||||||
|
$scopeIds = [];
|
||||||
|
foreach ($this->helper->getReachableScopes($this->security->getUser(),
|
||||||
|
$role, $center) as $scope) {
|
||||||
|
if (\in_array($scope->getId(), $scopeIds)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$scopeIds[] = $scope->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
$formattedClauses[] = \strtr($centerScopesClause, [
|
||||||
|
'{scopes_ids}' => \implode(', ', \array_fill(0, \count($scopeIds), '?'))
|
||||||
|
]);
|
||||||
|
// append $scopeIds to parameters
|
||||||
|
$parameters = \array_merge($parameters, $scopeIds);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (0 === count($formattedClauses)) {
|
||||||
|
return [ 'FALSE = TRUE', [] ];
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
\strtr(
|
||||||
|
\implode(' OR ', $formattedClauses),
|
||||||
|
[
|
||||||
|
'{person}' => $person->getTableName(),
|
||||||
|
'{center_id}' => $personCenterId,
|
||||||
|
'{report}' => $report->getTableName(),
|
||||||
|
'{scopes_id}' => $reportScopeId,
|
||||||
|
]
|
||||||
|
),
|
||||||
|
$parameters
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getWhereClauseForPerson(string $context, array $args): array
|
||||||
{
|
{
|
||||||
$associationMapping = $metadataReport->getAssociationMapping('person');
|
$report = $this->em->getClassMetadata(Report::class);
|
||||||
|
$person = $this->em->getClassMetadata(Person::class);
|
||||||
|
$role = new Role('CHILL_REPORT_SEE');
|
||||||
|
$reportPersonId = $report->getAssociationMapping('person')['joinColumns'][0]['name'];
|
||||||
|
$reportScopeId = $report->getAssociationMapping('scope')['joinColumns'][0]['name'];
|
||||||
|
$personCenterId = $person->getAssociationMapping('center')['joinColumns'][0]['name'];
|
||||||
|
// parameters for the query, will be filled later
|
||||||
|
$parameters = [ $args['person']->getId() ];
|
||||||
|
|
||||||
return $metadataReport->getTableName().' JOIN '
|
// this is the final clause that we are going to fill
|
||||||
.$metadataPerson->getTableName().' ON '
|
$clause = "{report}.{person_id} = ? AND {report}.{scopes_id} IN ({scopes_ids})";
|
||||||
.$metadataPerson->getTableName().'.'.
|
// iterate over reachable scopes
|
||||||
$associationMapping['joinColumns'][0]['referencedColumnName']
|
$scopes = $this->helper->getReachableScopes($this->security->getUser(), $role,
|
||||||
.' = '
|
$args['person']->getCenter());
|
||||||
.$associationMapping['joinColumns'][0]['name']
|
|
||||||
|
foreach ($scopes as $scope) {
|
||||||
|
if (\in_array($scope->getId(), $parameters)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$parameters[] = $scope->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1 === count($parameters)) {
|
||||||
|
// nothing change, we simplify the clause
|
||||||
|
$clause = "{report}.{person_id} = ? AND FALSE = TRUE";
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
\strtr(
|
||||||
|
$clause,
|
||||||
|
[
|
||||||
|
'{report}' => $report->getTableName(),
|
||||||
|
'{person_id}' => $reportPersonId,
|
||||||
|
'{scopes_id}' => $reportScopeId,
|
||||||
|
'{scopes_ids}' => \implode(', ',
|
||||||
|
\array_fill(0, \count($parameters)-1, '?'))
|
||||||
|
]
|
||||||
|
),
|
||||||
|
$parameters
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getFromClause(string $context): string
|
||||||
|
{
|
||||||
|
$report = $this->em->getClassMetadata(Report::class);
|
||||||
|
$person = $this->em->getClassMetadata(Person::class);
|
||||||
|
$reportPersonId = $report
|
||||||
|
->getAssociationMapping('person')['joinColumns'][0]['name']
|
||||||
;
|
;
|
||||||
|
$personId = $report
|
||||||
|
->getAssociationMapping('person')['joinColumns'][0]['referencedColumnName']
|
||||||
|
;
|
||||||
|
|
||||||
|
$clause = "{report} ".
|
||||||
|
"JOIN {person} ON {report}.{person_id} = {person}.{id_person} ";
|
||||||
|
|
||||||
|
return \strtr($clause,
|
||||||
|
[
|
||||||
|
'{report}' => $report->getTableName(),
|
||||||
|
'{person}' => $person->getTableName(),
|
||||||
|
'{person_id}' => $reportPersonId,
|
||||||
|
'{id_person}' => $personId
|
||||||
|
]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -199,12 +254,11 @@ class TimelineReportProvider implements TimelineProviderInterface
|
|||||||
$this->checkContext($context);
|
$this->checkContext($context);
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
'template' => 'ChillReportBundle:Timeline:report_person_context.html.twig',
|
'template' => 'ChillReportBundle:Timeline:report.html.twig',
|
||||||
'template_data' => array(
|
'template_data' => array(
|
||||||
'report' => $entity,
|
'report' => $entity,
|
||||||
|
'context' => $context,
|
||||||
'custom_fields_in_summary' => $this->getFieldsToRender($entity, $context),
|
'custom_fields_in_summary' => $this->getFieldsToRender($entity, $context),
|
||||||
'person' => $args['person'],
|
|
||||||
'user' => $entity->getUser()
|
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -272,9 +326,9 @@ class TimelineReportProvider implements TimelineProviderInterface
|
|||||||
*/
|
*/
|
||||||
private function checkContext($context)
|
private function checkContext($context)
|
||||||
{
|
{
|
||||||
if ($context !== 'person') {
|
if ($context !== 'person' && $context !== 'center') {
|
||||||
throw new \LogicException("The context '$context' is not "
|
throw new \LogicException("The context '$context' is not "
|
||||||
. "supported. Currently only 'person' is supported");
|
. "supported. Currently only 'person' and 'center' is supported");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,11 +20,12 @@ services:
|
|||||||
arguments:
|
arguments:
|
||||||
- '@doctrine.orm.entity_manager'
|
- '@doctrine.orm.entity_manager'
|
||||||
- '@chill.main.security.authorization.helper'
|
- '@chill.main.security.authorization.helper'
|
||||||
- '@security.token_storage'
|
- '@Symfony\Component\Security\Core\Security'
|
||||||
- '@chill.custom_field.helper'
|
- '@chill.custom_field.helper'
|
||||||
- '%chill_custom_fields.show_empty_values%'
|
- '%chill_custom_fields.show_empty_values%'
|
||||||
tags:
|
tags:
|
||||||
- { name: chill.timeline, context: 'person' }
|
- { name: chill.timeline, context: 'person' }
|
||||||
|
- { name: chill.timeline, context: 'center' }
|
||||||
|
|
||||||
chill.report.security.authorization.report_voter:
|
chill.report.security.authorization.report_voter:
|
||||||
class: Chill\ReportBundle\Security\Authorization\ReportVoter
|
class: Chill\ReportBundle\Security\Authorization\ReportVoter
|
||||||
|
@ -6,12 +6,12 @@ services:
|
|||||||
$authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper'
|
$authorizationHelper: '@Chill\MainBundle\Security\Authorization\AuthorizationHelper'
|
||||||
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
|
$tokenStorage: '@Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
|
||||||
public: true
|
public: true
|
||||||
tags:
|
# tags:
|
||||||
- { name: 'chill.timeline', context: 'person' }
|
# - { name: 'chill.timeline', context: 'person' }
|
||||||
|
|
||||||
Chill\TaskBundle\Timeline\SingleTaskTaskLifeCycleEventTimelineProvider:
|
Chill\TaskBundle\Timeline\SingleTaskTaskLifeCycleEventTimelineProvider:
|
||||||
arguments:
|
arguments:
|
||||||
$em: '@Doctrine\ORM\EntityManagerInterface'
|
$em: '@Doctrine\ORM\EntityManagerInterface'
|
||||||
$registry: '@Symfony\Component\Workflow\Registry'
|
$registry: '@Symfony\Component\Workflow\Registry'
|
||||||
tags:
|
# tags:
|
||||||
- { name: 'chill.timeline', context: 'task' }
|
#- { name: 'chill.timeline', context: 'task' }
|
||||||
|
Loading…
x
Reference in New Issue
Block a user