cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,33 +1,42 @@
<?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\ReportBundle\Timeline;
use Chill\MainBundle\Timeline\TimelineProviderInterface;
use Doctrine\ORM\EntityManager;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Role\Role;
use Doctrine\ORM\Mapping\ClassMetadata;
use Chill\PersonBundle\Entity\Person;
use Chill\MainBundle\Entity\Scope;
use Chill\CustomFieldsBundle\Service\CustomFieldsHelper;
use Chill\ReportBundle\Entity\Report;
use Symfony\Component\Security\Core\Security;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Chill\MainBundle\Timeline\TimelineProviderInterface;
use Chill\MainBundle\Timeline\TimelineSingleQuery;
use Chill\PersonBundle\Entity\Person;
use Chill\ReportBundle\Entity\Report;
use Doctrine\ORM\EntityManager;
use LogicException;
use Symfony\Component\Security\Core\Security;
use UnexpectedValueException;
use function array_fill;
use function array_merge;
use function implode;
use function strtr;
/**
* Provide report for inclusion in timeline
* Provide report for inclusion in timeline.
*/
class TimelineReportProvider implements TimelineProviderInterface
{
protected CustomFieldsHelper $customFieldsHelper;
protected EntityManager $em;
protected AuthorizationHelper $helper;
protected CustomFieldsHelper $customFieldsHelper;
protected bool $showEmptyValues;
private Security $security;
@@ -46,10 +55,6 @@ class TimelineReportProvider implements TimelineProviderInterface
$this->showEmptyValues = $showEmptyValues;
}
/**
*
* {@inheritDoc}
*/
public function fetchQuery($context, array $args)
{
$this->checkContext($context);
@@ -58,15 +63,140 @@ class TimelineReportProvider implements TimelineProviderInterface
[$where, $parameters] = $this->getWhereClause($context, $args);
return TimelineSingleQuery::fromArray([
'id' => $report->getTableName()
.'.'.$report->getColumnName('id'),
'type' => 'report',
'date' => $report->getTableName()
.'.'.$report->getColumnName('date'),
'FROM' => $this->getFromClause($context),
'WHERE' => $where,
'parameters' => $parameters
]);
'id' => $report->getTableName()
. '.' . $report->getColumnName('id'),
'type' => 'report',
'date' => $report->getTableName()
. '.' . $report->getColumnName('date'),
'FROM' => $this->getFromClause($context),
'WHERE' => $where,
'parameters' => $parameters,
]);
}
public function getEntities(array $ids)
{
$reports = $this->em->getRepository('ChillReportBundle:Report')
->findBy(['id' => $ids]);
$result = [];
foreach ($reports as $report) {
$result[$report->getId()] = $report;
}
return $result;
}
public function getEntityTemplate($entity, $context, array $args)
{
$this->checkContext($context);
return [
'template' => 'ChillReportBundle:Timeline:report.html.twig',
'template_data' => [
'report' => $entity,
'context' => $context,
'custom_fields_in_summary' => $this->getFieldsToRender($entity, $context),
],
];
}
public function supportsType($type)
{
return 'report' === $type;
}
protected function getFieldsToRender(Report $entity, $context, array $args = [])
{
//gather all custom fields which should appears in summary
$gatheredFields = [];
if (array_key_exists('summary_fields', $entity->getCFGroup()->getOptions())) {
// keep in memory title
$title = null;
$subtitle = null;
foreach ($entity->getCFGroup()->getCustomFields() as $customField) {
if (in_array(
$customField->getSlug(),
$entity->getCFGroup()->getOptions()['summary_fields']
)) {
// if we do not want to show empty values
if (false === $this->showEmptyValues) {
if ($customField->getType() === 'title') {
$options = $customField->getOptions();
switch ($options['type']) {
case 'title': $title = $customField;
break;
case 'subtitle': $subtitle = $customField;
break;
}
} else {
if ($this->customFieldsHelper->isEmptyValue($entity->getCFData(), $customField)
=== false) {
if (null !== $title) {
$gatheredFields[] = $title;
$title = null;
}
if (null !== $subtitle) {
$gatheredFields[] = $subtitle;
$subtitle = null;
}
$gatheredFields[] = $customField;
}
}
} else {
$gatheredFields[] = $customField;
}
}
}
}
return $gatheredFields;
}
/**
* check if the context is supported.
*
* @param string $context
*
* @throws LogicException if the context is not supported
*/
private function checkContext($context)
{
if ('person' !== $context && 'center' !== $context) {
throw new LogicException("The context '{$context}' is not "
. "supported. Currently only 'person' and 'center' is supported");
}
}
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,
]
);
}
private function getWhereClause(string $context, array $args): array
@@ -74,10 +204,12 @@ class TimelineReportProvider implements TimelineProviderInterface
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");
throw new UnexpectedValueException("This context {$context} is not implemented");
}
}
@@ -96,14 +228,15 @@ class TimelineReportProvider implements TimelineProviderInterface
$parameters = [];
// the clause, that will be joined with an "OR"
$centerScopesClause = "({person}.{center_id} = ? ".
"AND {report}.{scopes_id} IN ({scopes_ids}))";
$centerScopesClause = '({person}.{center_id} = ? ' .
'AND {report}.{scopes_id} IN ({scopes_ids}))';
// container for formatted clauses
$formattedClauses = [];
$askedCenters = $args['centers'];
foreach ($reachableCenters as $center) {
if (FALSE === \in_array($center, $askedCenters)) {
if (false === \in_array($center, $askedCenters)) {
continue;
}
@@ -111,6 +244,7 @@ class TimelineReportProvider implements TimelineProviderInterface
$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;
@@ -118,20 +252,20 @@ class TimelineReportProvider implements TimelineProviderInterface
$scopeIds[] = $scope->getId();
}
$formattedClauses[] = \strtr($centerScopesClause, [
'{scopes_ids}' => \implode(', ', \array_fill(0, \count($scopeIds), '?'))
$formattedClauses[] = strtr($centerScopesClause, [
'{scopes_ids}' => implode(', ', array_fill(0, \count($scopeIds), '?')),
]);
// append $scopeIds to parameters
$parameters = \array_merge($parameters, $scopeIds);
$parameters = array_merge($parameters, $scopeIds);
}
if (0 === count($formattedClauses)) {
return [ 'FALSE = TRUE', [] ];
return ['FALSE = TRUE', []];
}
return [
\strtr(
\implode(' OR ', $formattedClauses),
strtr(
implode(' OR ', $formattedClauses),
[
'{person}' => $person->getTableName(),
'{center_id}' => $personCenterId,
@@ -139,7 +273,7 @@ class TimelineReportProvider implements TimelineProviderInterface
'{scopes_id}' => $reportScopeId,
]
),
$parameters
$parameters,
];
}
@@ -152,10 +286,10 @@ class TimelineReportProvider implements TimelineProviderInterface
$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() ];
$parameters = [$args['person']->getId()];
// this is the final clause that we are going to fill
$clause = "{report}.{person_id} = ? AND {report}.{scopes_id} IN ({scopes_ids})";
$clause = '{report}.{person_id} = ? AND {report}.{scopes_id} IN ({scopes_ids})';
// iterate over reachable scopes
$scopes = $this->helper->getReachableScopes($this->security->getUser(), $role, $args['person']->getCenter());
@@ -169,150 +303,23 @@ class TimelineReportProvider implements TimelineProviderInterface
if (1 === count($parameters)) {
// nothing change, we simplify the clause
$clause = "{report}.{person_id} = ? AND FALSE = TRUE";
$clause = '{report}.{person_id} = ? AND FALSE = TRUE';
}
return [
\strtr(
strtr(
$clause,
[
'{report}' => $report->getTableName(),
'{person_id}' => $reportPersonId,
'{scopes_id}' => $reportScopeId,
'{scopes_ids}' => \implode(', ',
\array_fill(0, \count($parameters)-1, '?'))
'{scopes_ids}' => implode(
', ',
array_fill(0, \count($parameters) - 1, '?')
),
]
),
$parameters
$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
]
);
}
/**
*
* {@inheritDoc}
*/
public function getEntities(array $ids)
{
$reports = $this->em->getRepository('ChillReportBundle:Report')
->findBy(array('id' => $ids));
$result = array();
foreach($reports as $report) {
$result[$report->getId()] = $report;
}
return $result;
}
/**
*
* {@inheritDoc}
*/
public function getEntityTemplate($entity, $context, array $args)
{
$this->checkContext($context);
return array(
'template' => 'ChillReportBundle:Timeline:report.html.twig',
'template_data' => array(
'report' => $entity,
'context' => $context,
'custom_fields_in_summary' => $this->getFieldsToRender($entity, $context),
)
);
}
protected function getFieldsToRender(Report $entity, $context, array $args = array())
{
//gather all custom fields which should appears in summary
$gatheredFields = array();
if (array_key_exists('summary_fields', $entity->getCFGroup()->getOptions())) {
// keep in memory title
$title = null;
$subtitle = null;
foreach ($entity->getCFGroup()->getCustomFields() as $customField) {
if (in_array($customField->getSlug(),
$entity->getCFGroup()->getOptions()['summary_fields'])) {
// if we do not want to show empty values
if ($this->showEmptyValues === false) {
if ($customField->getType() === 'title') {
$options = $customField->getOptions();
switch($options['type']) {
case 'title': $title = $customField; break;
case 'subtitle': $subtitle = $customField; break;
}
} else {
if ($this->customFieldsHelper->isEmptyValue($entity->getCFData(), $customField)
=== false) {
if ($title !== NULL) {
$gatheredFields[] = $title;
$title = null;
}
if ($subtitle !== NULL) {
$gatheredFields[] = $subtitle;
$subtitle = null;
}
$gatheredFields[] = $customField;
}
}
} else {
$gatheredFields[] = $customField;
}
}
}
}
return $gatheredFields;
}
/**
*
* {@inheritDoc}
*/
public function supportsType($type)
{
return $type === 'report';
}
/**
* check if the context is supported
*
* @param string $context
* @throws \LogicException if the context is not supported
*/
private function checkContext($context)
{
if ($context !== 'person' && $context !== 'center') {
throw new \LogicException("The context '$context' is not "
. "supported. Currently only 'person' and 'center' is supported");
}
}
}