mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-31 03:53:49 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,53 +1,49 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
|
||||
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\TaskBundle\Timeline;
|
||||
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\MainBundle\Timeline\TimelineProviderInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Chill\TaskBundle\Entity\Task\SingleTaskPlaceEvent;
|
||||
use Chill\TaskBundle\Entity\SingleTask;
|
||||
use Chill\MainBundle\Timeline\TimelineSingleQuery;
|
||||
use Chill\PersonBundle\Entity\Person;
|
||||
use Chill\TaskBundle\Entity\SingleTask;
|
||||
use Chill\TaskBundle\Entity\Task\SingleTaskPlaceEvent;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Component\Workflow\Registry;
|
||||
use Symfony\Component\Workflow\Workflow;
|
||||
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
|
||||
use Chill\ActivityBundle\Security\Authorization\ActivityVoter;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Chill\MainBundle\Timeline\TimelineSingleQuery;
|
||||
use UnexpectedValueException;
|
||||
use function array_combine;
|
||||
use function array_fill;
|
||||
use function array_map;
|
||||
use function implode;
|
||||
use function in_array;
|
||||
use function strtr;
|
||||
|
||||
/**
|
||||
* Provide element for timeline for 'person' and 'center' context
|
||||
*
|
||||
* Provide element for timeline for 'person' and 'center' context.
|
||||
*/
|
||||
class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
|
||||
{
|
||||
public const TYPE = 'chill_task.transition';
|
||||
|
||||
protected AuthorizationHelper $authorizationHelper;
|
||||
|
||||
protected EntityManagerInterface $em;
|
||||
|
||||
protected Registry $registry;
|
||||
|
||||
protected AuthorizationHelper $authorizationHelper;
|
||||
|
||||
protected Security $security;
|
||||
|
||||
|
||||
const TYPE = 'chill_task.transition';
|
||||
|
||||
public function __construct(
|
||||
EntityManagerInterface $em,
|
||||
Registry $registry,
|
||||
@@ -68,12 +64,16 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
|
||||
switch ($context) {
|
||||
case 'person':
|
||||
[ $where, $parameters ] = $this->getWhereClauseForPerson($args['person']);
|
||||
|
||||
break;
|
||||
|
||||
case 'center':
|
||||
[ $where, $parameters ] = $this->getWhereClauseForCenter($args['centers']);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \UnexpectedValueException("context {$context} is not supported");
|
||||
throw new UnexpectedValueException("context {$context} is not supported");
|
||||
}
|
||||
|
||||
return TimelineSingleQuery::fromArray([
|
||||
@@ -82,160 +82,26 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
|
||||
'date' => $metadata->getColumnName('datetime'),
|
||||
'FROM' => $this->getFromClause($context),
|
||||
'WHERE' => $where,
|
||||
'parameters' => $parameters
|
||||
'parameters' => $parameters,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getWhereClauseForCenter(array $centers): array
|
||||
{
|
||||
$taskEvent = $this->em->getClassMetadata(SingleTaskPlaceEvent::class);
|
||||
$singleTask = $this->em->getClassMetadata(SingleTask::class);
|
||||
$person = $this->em->getClassMetadata(Person::class);
|
||||
$personFkCenter = $person->getAssociationMapping('center')['joinColumns'][0]['name'];
|
||||
$taskFkCircle = $singleTask->getAssociationMapping('circle')['joinColumns'][0]['name'];
|
||||
|
||||
// the parameters
|
||||
$parameters = [];
|
||||
|
||||
// the clause that we will repeat for each center, joined by 'OR'
|
||||
$clause = "{person}.{center_id} = ? AND {task}.{circle} IN ({circle_ids})";
|
||||
|
||||
// array to gather clauses
|
||||
$clauses = [];
|
||||
|
||||
// loop over centers
|
||||
foreach ($this->authorizationHelper->getReachableCenters(
|
||||
$this->security->getUser(), new Role(ActivityVoter::SEE_DETAILS)) as $center) {
|
||||
|
||||
if (FALSE === \in_array($center, $centers)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// fill center parameter
|
||||
$parameters[] = $center->getId();
|
||||
|
||||
// we loop over circles
|
||||
$circles = $this->authorizationHelper->getReachableCircles(
|
||||
$this->security->getUser(), new Role(ActivityVoter::SEE_DETAILS), $center);
|
||||
$circleIds = [];
|
||||
|
||||
foreach ($circles as $circle) {
|
||||
$parameters[] = $circleIds[] = $circle->getId();
|
||||
}
|
||||
|
||||
$clauses[] = \strtr(
|
||||
$clause,
|
||||
[
|
||||
'{person}' => $person->getTableName(),
|
||||
'{center_id}' => $personFkCenter,
|
||||
'{task}' => $singleTask->getSchemaName().".".$singleTask->getTableName(),
|
||||
'{circle}' => $taskFkCircle,
|
||||
'{circle_ids}' => \implode(', ', \array_fill(0, count($circleIds), '?'))
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if (0 === \count($clauses)) {
|
||||
return [ 'FALSE = TRUE' , [] ];
|
||||
}
|
||||
|
||||
return [
|
||||
\implode(' OR ', $clauses),
|
||||
$parameters
|
||||
];
|
||||
}
|
||||
|
||||
private function getWhereClauseForPerson(Person $personArg): array
|
||||
{
|
||||
$taskEvent = $this->em->getClassMetadata(SingleTaskPlaceEvent::class);
|
||||
$singleTask = $this->em->getClassMetadata(SingleTask::class);
|
||||
$person = $this->em->getClassMetadata(Person::class);
|
||||
$eventFkTask = $taskEvent->getAssociationMapping('task')['joinColumns'][0]['name'];
|
||||
$taskFkPerson = $singleTask->getAssociationMapping('person')['joinColumns'][0]['name'];
|
||||
$personPk = $singleTask->getAssociationMapping('person')['joinColumns'][0]['referencedColumnName'];
|
||||
$taskFkCircle = $singleTask->getAssociationMapping('circle')['joinColumns'][0]['name'];
|
||||
|
||||
|
||||
// the parameters
|
||||
$parameters = $circleIds = [];
|
||||
|
||||
// the clause that we will fill
|
||||
$clause = "{person}.{person_id} = ? AND {task}.{circle} IN ({circle_ids})";
|
||||
|
||||
// person is the first parameter
|
||||
$parameters[] = $personArg->getId();
|
||||
|
||||
// we loop over circles
|
||||
$circles = $this->authorizationHelper->getReachableCircles(
|
||||
$this->security->getUser(), new Role(ActivityVoter::SEE_DETAILS), $personArg->getCenter());
|
||||
|
||||
if (0 === count($circles)) {
|
||||
// go fast to block access to every tasks
|
||||
return [ "FALSE = TRUE", [] ];
|
||||
}
|
||||
|
||||
foreach ($circles as $circle) {
|
||||
$parameters[] = $circleIds[] = $circle->getId();
|
||||
}
|
||||
|
||||
return [
|
||||
\strtr(
|
||||
$clause,
|
||||
[
|
||||
'{person}' => $person->getTableName(),
|
||||
'{person_id}' => $person->getColumnName('id'),
|
||||
'{task}' => $singleTask->getSchemaName().".".$singleTask->getTableName(),
|
||||
'{circle}' => $taskFkCircle,
|
||||
'{circle_ids}' => \implode(', ', \array_fill(0, count($circleIds), '?'))
|
||||
]
|
||||
),
|
||||
$parameters
|
||||
];
|
||||
}
|
||||
|
||||
private function getFromClause(string $context)
|
||||
{
|
||||
$taskEvent = $this->em->getClassMetadata(SingleTaskPlaceEvent::class);
|
||||
$singleTask = $this->em->getClassMetadata(SingleTask::class);
|
||||
$person = $this->em->getClassMetadata(Person::class);
|
||||
$eventFkTask = $taskEvent->getAssociationMapping('task')['joinColumns'][0]['name'];
|
||||
$taskFkPerson = $singleTask->getAssociationMapping('person')['joinColumns'][0]['name'];
|
||||
$personPk = $singleTask->getAssociationMapping('person')['joinColumns'][0]['referencedColumnName'];
|
||||
|
||||
$from = "{single_task_event} ".
|
||||
"JOIN {single_task} ON {single_task}.{task_pk} = {single_task_event}.{event_fk_task} ".
|
||||
"JOIN {person} ON {single_task}.{task_person_fk} = {person}.{person_pk}";
|
||||
|
||||
return \strtr(
|
||||
$from,
|
||||
[
|
||||
'{single_task}' => sprintf('%s.%s', $singleTask->getSchemaName(), $singleTask->getTableName()),
|
||||
'{single_task_event}' => sprintf('%s.%s', $taskEvent->getSchemaName(), $taskEvent->getTableName()),
|
||||
'{task_pk}' => $singleTask->getColumnName('id'),
|
||||
'{event_fk_task}' => $eventFkTask,
|
||||
'{person}' => $person->getTableName(),
|
||||
'{task_person_fk}' => $taskFkPerson,
|
||||
'{person_pk}' => $personPk
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
public function getEntities(array $ids)
|
||||
{
|
||||
$events = $this->em
|
||||
->getRepository(SingleTaskPlaceEvent::class)
|
||||
->findBy([ 'id' => $ids ])
|
||||
;
|
||||
->findBy(['id' => $ids]);
|
||||
|
||||
return \array_combine(
|
||||
\array_map(function($e) { return $e->getId(); }, $events ),
|
||||
return array_combine(
|
||||
array_map(function ($e) { return $e->getId(); }, $events),
|
||||
$events
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
public function getEntityTemplate($entity, $context, array $args)
|
||||
{
|
||||
$workflow = $this->registry->get($entity->getTask(),
|
||||
$workflow = $this->registry->get(
|
||||
$entity->getTask(),
|
||||
(isset($entity->getData()['workflow'])) ? $entity->getData()['workflow'] : null
|
||||
);
|
||||
// sf4 check: prevent error message:
|
||||
@@ -251,15 +117,19 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
|
||||
'context' => $context,
|
||||
'event' => $entity,
|
||||
'task' => $entity->getTask(),
|
||||
'transition' => $transition
|
||||
]
|
||||
'transition' => $transition,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function supportsType($type): bool
|
||||
{
|
||||
return self::TYPE === $type;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $name
|
||||
* @param Workflow $workflow
|
||||
*
|
||||
* @return \Symfony\Component\Workflow\Transition
|
||||
*/
|
||||
protected function getTransitionByName($name, Workflow $workflow)
|
||||
@@ -271,8 +141,143 @@ class TaskLifeCycleEventTimelineProvider implements TimelineProviderInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function supportsType($type): bool
|
||||
private function getFromClause(string $context)
|
||||
{
|
||||
return $type === self::TYPE;
|
||||
$taskEvent = $this->em->getClassMetadata(SingleTaskPlaceEvent::class);
|
||||
$singleTask = $this->em->getClassMetadata(SingleTask::class);
|
||||
$person = $this->em->getClassMetadata(Person::class);
|
||||
$eventFkTask = $taskEvent->getAssociationMapping('task')['joinColumns'][0]['name'];
|
||||
$taskFkPerson = $singleTask->getAssociationMapping('person')['joinColumns'][0]['name'];
|
||||
$personPk = $singleTask->getAssociationMapping('person')['joinColumns'][0]['referencedColumnName'];
|
||||
|
||||
$from = '{single_task_event} ' .
|
||||
'JOIN {single_task} ON {single_task}.{task_pk} = {single_task_event}.{event_fk_task} ' .
|
||||
'JOIN {person} ON {single_task}.{task_person_fk} = {person}.{person_pk}';
|
||||
|
||||
return strtr(
|
||||
$from,
|
||||
[
|
||||
'{single_task}' => sprintf('%s.%s', $singleTask->getSchemaName(), $singleTask->getTableName()),
|
||||
'{single_task_event}' => sprintf('%s.%s', $taskEvent->getSchemaName(), $taskEvent->getTableName()),
|
||||
'{task_pk}' => $singleTask->getColumnName('id'),
|
||||
'{event_fk_task}' => $eventFkTask,
|
||||
'{person}' => $person->getTableName(),
|
||||
'{task_person_fk}' => $taskFkPerson,
|
||||
'{person_pk}' => $personPk,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function getWhereClauseForCenter(array $centers): array
|
||||
{
|
||||
$taskEvent = $this->em->getClassMetadata(SingleTaskPlaceEvent::class);
|
||||
$singleTask = $this->em->getClassMetadata(SingleTask::class);
|
||||
$person = $this->em->getClassMetadata(Person::class);
|
||||
$personFkCenter = $person->getAssociationMapping('center')['joinColumns'][0]['name'];
|
||||
$taskFkCircle = $singleTask->getAssociationMapping('circle')['joinColumns'][0]['name'];
|
||||
|
||||
// the parameters
|
||||
$parameters = [];
|
||||
|
||||
// the clause that we will repeat for each center, joined by 'OR'
|
||||
$clause = '{person}.{center_id} = ? AND {task}.{circle} IN ({circle_ids})';
|
||||
|
||||
// array to gather clauses
|
||||
$clauses = [];
|
||||
|
||||
// loop over centers
|
||||
foreach ($this->authorizationHelper->getReachableCenters(
|
||||
$this->security->getUser(),
|
||||
new Role(ActivityVoter::SEE_DETAILS)
|
||||
) as $center) {
|
||||
if (false === in_array($center, $centers)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// fill center parameter
|
||||
$parameters[] = $center->getId();
|
||||
|
||||
// we loop over circles
|
||||
$circles = $this->authorizationHelper->getReachableCircles(
|
||||
$this->security->getUser(),
|
||||
new Role(ActivityVoter::SEE_DETAILS),
|
||||
$center
|
||||
);
|
||||
$circleIds = [];
|
||||
|
||||
foreach ($circles as $circle) {
|
||||
$parameters[] = $circleIds[] = $circle->getId();
|
||||
}
|
||||
|
||||
$clauses[] = strtr(
|
||||
$clause,
|
||||
[
|
||||
'{person}' => $person->getTableName(),
|
||||
'{center_id}' => $personFkCenter,
|
||||
'{task}' => $singleTask->getSchemaName() . '.' . $singleTask->getTableName(),
|
||||
'{circle}' => $taskFkCircle,
|
||||
'{circle_ids}' => implode(', ', array_fill(0, count($circleIds), '?')),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
if (0 === \count($clauses)) {
|
||||
return ['FALSE = TRUE', []];
|
||||
}
|
||||
|
||||
return [
|
||||
implode(' OR ', $clauses),
|
||||
$parameters,
|
||||
];
|
||||
}
|
||||
|
||||
private function getWhereClauseForPerson(Person $personArg): array
|
||||
{
|
||||
$taskEvent = $this->em->getClassMetadata(SingleTaskPlaceEvent::class);
|
||||
$singleTask = $this->em->getClassMetadata(SingleTask::class);
|
||||
$person = $this->em->getClassMetadata(Person::class);
|
||||
$eventFkTask = $taskEvent->getAssociationMapping('task')['joinColumns'][0]['name'];
|
||||
$taskFkPerson = $singleTask->getAssociationMapping('person')['joinColumns'][0]['name'];
|
||||
$personPk = $singleTask->getAssociationMapping('person')['joinColumns'][0]['referencedColumnName'];
|
||||
$taskFkCircle = $singleTask->getAssociationMapping('circle')['joinColumns'][0]['name'];
|
||||
|
||||
// the parameters
|
||||
$parameters = $circleIds = [];
|
||||
|
||||
// the clause that we will fill
|
||||
$clause = '{person}.{person_id} = ? AND {task}.{circle} IN ({circle_ids})';
|
||||
|
||||
// person is the first parameter
|
||||
$parameters[] = $personArg->getId();
|
||||
|
||||
// we loop over circles
|
||||
$circles = $this->authorizationHelper->getReachableCircles(
|
||||
$this->security->getUser(),
|
||||
new Role(ActivityVoter::SEE_DETAILS),
|
||||
$personArg->getCenter()
|
||||
);
|
||||
|
||||
if (0 === count($circles)) {
|
||||
// go fast to block access to every tasks
|
||||
return ['FALSE = TRUE', []];
|
||||
}
|
||||
|
||||
foreach ($circles as $circle) {
|
||||
$parameters[] = $circleIds[] = $circle->getId();
|
||||
}
|
||||
|
||||
return [
|
||||
strtr(
|
||||
$clause,
|
||||
[
|
||||
'{person}' => $person->getTableName(),
|
||||
'{person_id}' => $person->getColumnName('id'),
|
||||
'{task}' => $singleTask->getSchemaName() . '.' . $singleTask->getTableName(),
|
||||
'{circle}' => $taskFkCircle,
|
||||
'{circle_ids}' => implode(', ', array_fill(0, count($circleIds), '?')),
|
||||
]
|
||||
),
|
||||
$parameters,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user