Commit c99583b6 authored by Julien Fastré's avatar Julien Fastré

implementing workflow on tasks

parent 4fe9c429
......@@ -18,6 +18,8 @@
namespace Chill\TaskBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Chill\TaskBundle\DependencyInjection\Compiler\TaskWorkflowDefinitionCompilerPass;
/**
*
......@@ -25,5 +27,10 @@ use Symfony\Component\HttpKernel\Bundle\Bundle;
*/
class ChillTaskBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new TaskWorkflowDefinitionCompilerPass());
}
}
......@@ -9,12 +9,30 @@ use Symfony\Component\Workflow\Registry;
use Symfony\Component\HttpFoundation\Response;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class TaskController extends Controller
{
/**
* Apply a transition to a task
*
* @Route(
* "/{_locale}/task/transition/{kind}/{taskId}/{transition}",
* name="chill_task_task_transition"
* )
*
* @param string $kind
* @param int $taskId
* @param string $transition
* @param SingleTaskRepository $singleTaskRepository
* @param Registry $registry
* @param EntityManagerInterface $em
* @param Request $request
* @return Response
*/
public function applyTransitionAction(
$type,
$kind,
$taskId,
$transition,
SingleTaskRepository $singleTaskRepository,
......@@ -22,7 +40,7 @@ class TaskController extends Controller
EntityManagerInterface $em,
Request $request
) {
switch ($type) {
switch ($kind) {
case 'single-task':
$task = $singleTaskRepository
->find($taskId)
......@@ -33,7 +51,7 @@ class TaskController extends Controller
);
break;
default:
return new Response("The type '$type' is not implemented",
return new Response("The type '$kind' is not implemented",
Response::HTTP_BAD_REQUEST);
}
......@@ -44,7 +62,7 @@ class TaskController extends Controller
// we simply check that the user can see the task. Other ACL checks
// should be performed using `guard` events.
$this->denyAccessUnlessGranted($task, TaskVoter::SHOW);
$this->denyAccessUnlessGranted(TaskVoter::SHOW, $task);
$workflow = $registry->get($task);
......
......@@ -30,6 +30,7 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
$loader->load('services/security.yml');
$loader->load('services/repositories.yml');
$loader->load('services/workflow.yml');
$loader->load('services/templating.yml');
}
public function prepend(ContainerBuilder $container)
......@@ -72,7 +73,7 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
'currentStates'
],
],
'type' => 'workflow',
'type' => 'state_machine',
'support_strategy' => TaskWorkflowManager::class,
'places' => [ 'new', 'in_progress', 'closed', 'canceled'],
'initial_place' => 'new',
......@@ -82,7 +83,7 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
'to' => 'in_progress'
],
'close' => [
'from' => 'in_progress',
'from' => ['new', 'in_progress'],
'to' => 'closed'
],
'cancel' => [
......
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* 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/>.
*/
namespace Chill\TaskBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Chill\TaskBundle\Workflow\TaskWorkflowManager;
use Symfony\Component\DependencyInjection\Reference;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TaskWorkflowDefinitionCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(TaskWorkflowManager::class)) {
throw new \LogicException("The service ".TaskWorkflowManager::class." is "
. "not registered");
}
$workflowManagerDefinition = $container->getDefinition(TaskWorkflowManager::class);
foreach ($container->findTaggedServiceIds('chill_task.workflow_definition') as $id => $tags) {
$workflowManagerDefinition
->addMethodCall('addDefinition', [new Reference($id)]);
}
}
}
services:
Chill\TaskBundle\Templating\TaskTwigExtension:
arguments:
$taskWorkflowManager: '@Chill\TaskBundle\Workflow\TaskWorkflowManager'
tags:
- { name: 'twig.extension' }
services:
Chill\TaskBundle\Workflow\TaskWorkflowManager: ~
\ No newline at end of file
Chill\TaskBundle\Workflow\TaskWorkflowManager: ~
Chill\TaskBundle\Workflow\Definition\DefaultTaskDefinition:
tags:
- { name: 'chill_task.workflow_definition' }
\ No newline at end of file
......@@ -48,7 +48,11 @@
<tr>
<td>{{ task.title }}</td>
<td>{{ task.type }}</td>
<td>todo</td>
<td>
{% for transition in workflow_transitions(task) %}
<a href="{{ path('chill_task_task_transition', { 'taskId': task.id, 'transition': transition.name, 'kind': 'single-task', 'return_path': app.request.uri }) }}" style="background-color: {{ task_workflow_metadata(task, 'transition.background-color', transition)|e('html_attr') }}; color: {{ task_workflow_metadata(task, 'transition.text-color', transition)|e('html_attr') }}">{{ task_workflow_metadata(task, 'transition.verb', transition) }}</a>
{% endfor %}
</td>
<td>{% if task.startDate is not null %}{{ task.startDate|localizeddate('medium', 'none') }}{% endif %}</td>
<td>{% if task.warningDate is not null %}{{ task.warningDate|localizeddate('medium', 'none') }}{% endif %}</td>
<td>{% if task.endDate is not null %}{{ task.endDate|localizeddate('medium', 'none') }}{% endif %}</td>
......
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* 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/>.
*/
namespace Chill\TaskBundle\Templating;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;
use Chill\TaskBundle\Entity\AbstractTask;
use Chill\TaskBundle\Workflow\TaskWorkflowManager;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TaskTwigExtension extends \Twig_Extension
{
/**
*
* @var TaskWorkflowManager
*/
protected $taskWorkflowManager;
public function __construct(TaskWorkflowManager $taskWorkflowManager)
{
$this->taskWorkflowManager = $taskWorkflowManager;
}
public function getFunctions()
{
return [
new TwigFunction('task_workflow_metadata', [ $this, 'getWorkflowMetadata' ] )
];
}
public function getWorkflowMetadata(
AbstractTask $task,
string $key,
$metadataSubject = null,
string $name = null
) {
return $this->taskWorkflowManager->getWorkflowMetadata($task, $key, $metadataSubject, $name);
}
}
<?php
namespace Chill\TaskBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Chill\MainBundle\Tests\TestHelper;
use Faker;
use Chill\MainBundle\Entity\Center;
class SingleTaskControllerTest extends WebTestCase
{
/**
*
* @var Faker\Generator
*/
protected $faker;
protected function setUp()
{
self::bootKernel();
$this->faker = Faker\Factory::create('fr');
}
/**
*
* @return \Chill\PersonBundle\Entity\Person
*/
protected function getRandomPerson($centerName)
{
$em = self::$kernel
->getContainer()
->get('doctrine.orm.entity_manager')
;
$centers = $em
->getRepository(Center::class)
->findAll();
$center = \array_filter(
$centers,
function(Center $c) use ($centerName) {
return $centerName === $c->getName();
})[0];
$ids = $em
->createQuery('SELECT p.id FROM ChillPersonBundle:Person p '
. 'WHERE p.center = :center'
)
->setParameter('center', $center)
->getResult()
;
$id = $ids[\array_rand($ids)];
return self::$kernel
->getContainer()
->get('doctrine.orm.entity_manager')
->getRepository(\Chill\PersonBundle\Entity\Person::class)
->find($id)
;
}
public function testNew()
{
$client = static::createClient(
array(),
TestHelper::getAuthenticatedClientOptions()
);
$person = $this->getRandomPerson('Center A');
$crawler = $client->request('GET', '/fr/task/single-task/new', [
'person_id' => $person->getId()
]);
var_dump($crawler->text());
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $crawler->selectButton('Envoi')->form();
$title = $this->faker->sentence;
$circles = $form->get('circle')
->availableOptionsValues()
;
$client->submit($form, [
'title' => $title,
'circle' => $circles[\array_rand($circles)]
]);
$this->assertTrue($client->getResponse()->isRedirect(sprintf(
'/fr/task/task/list/%d', $person->getId())));
$crawler = $client->followRedirect();
$this->assertContains($title, $crawler->text(),
"Assert that newly created task title is shown in list page")
;
}
}
<?php
namespace Chill\TaskBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class TaskControllerTest extends WebTestCase
{
}
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* 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/>.
*/
namespace Chill\TaskBundle\Workflow\Definition;
use Chill\TaskBundle\Entity\AbstractTask;
use Chill\TaskBundle\Entity\SingleTask;
use Symfony\Component\Workflow\Transition;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class DefaultTaskDefinition implements \Chill\TaskBundle\Workflow\TaskWorkflowDefinition
{
const TRANSITION_METADATA = [
'close' => [
'verb' => 'close',
'background-color' => 'var(--yellow)', // css variable, see https://developer.mozilla.org/fr/docs/Web/CSS/Les_variables_CSS
'text-color' => 'black'
],
'cancel' => [
'verb' => 'cancel',
'background-color' => 'var(--red)', // css variable, see https://developer.mozilla.org/fr/docs/Web/CSS/Les_variables_CSS
'text-color' => 'black'
],
'start' => [
'verb' => 'start',
'background-color' => 'var(--green)', // css variable, see https://developer.mozilla.org/fr/docs/Web/CSS/Les_variables_CSS
'text-color' => 'black'
]
];
public function supports(AbstractTask $task)
{
return $task instanceof SingleTask
&& $task->getType() === 'task_default';
}
public function getAssociatedWorkflowName()
{
return 'task_default';
}
public function getWorkflowMetadata(
string $key,
$metadataSubject = null
) {
$keys = \explode('.', $key);
switch($keys[0]) {
case 'transition':
if (!$metadataSubject instanceof Transition) {
throw new \LogicException("You must give a transition as metadatasubject");
}
return $this->getTransitionMetadata(\implode('.', \array_slice($keys, 1)), $metadataSubject);
default:
throw new \LogicException("this key '$key' is not implemented");
}
}
protected function getTransitionMetadata($key, Transition $transition)
{
if (!\array_key_exists($transition->getName(), self::TRANSITION_METADATA)) {
throw new \LogicException("the metadata for this transition are not defined");
}
if (!\array_key_exists($key, self::TRANSITION_METADATA[$transition->getName()])) {
throw new \LogicException("The metadata ".$key." is not "
. "defined for the transition ".$transition.getName());
}
return self::TRANSITION_METADATA[$transition->getName()][$key];
}
}
<?php
/*
* Copyright (C) 2018 Champs Libres Cooperative <info@champs-libres.coop>
*
* 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/>.
*/
namespace Chill\TaskBundle\Workflow;
/**
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
interface TaskWorkflowDefinition
{
}
......@@ -28,14 +28,56 @@ use Symfony\Component\Workflow\Workflow;
*/
class TaskWorkflowManager implements SupportStrategyInterface
{
/**
*
* @var TaskWorkflowDefinition[]
*/
protected $definitions = array();
public function addDefinition(TaskWorkflowDefinition $definition) {
$this->definitions[] = $definition;
}
/**
*
* @param AbstractTask $task
* @return TaskWorkflowDefinition
* @throws \LogicException
*/
public function getTaskWorkflowDefinition(AbstractTask $task)
{
$definitions = array();
foreach($this->definitions as $tested) {
if ($tested->supports($task)) {
$definitions[] = $tested;
}
}
$count = count($definitions);
if ($count > 1) {
throw new \LogicException("More than one TaskWorkflowDefinition supports "
. "this task. This should not happens.");
} elseif ($count === 0) {
throw new \LogicException("No taskWorkflowDefinition supports this task.");
}
return $definitions[0];
}
public function supports(Workflow $workflow, $subject): bool
{
if (!$subject instanceof AbstractTask) {
return false;
}
dump($workflow->getName());
return $workflow->getName() === 'task_default';
return $workflow->getName() === $this
->getTaskWorkflowDefinition($subject)->getAssociatedWorkflowName();
}
public function getWorkflowMetadata(AbstractTask $task, string $key, $metadataSubject = null, string $name = null)
{
return $this->getTaskWorkflowDefinition($task)
->getWorkflowMetadata($key, $metadataSubject);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment