add confirmation page for task

This commit is contained in:
2018-07-06 15:47:14 +02:00
parent 5ada6d913c
commit 52bda7c94f
7 changed files with 268 additions and 26 deletions

View File

@@ -11,6 +11,11 @@ use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Chill\TaskBundle\Event\UI\UIEvent;
use Chill\TaskBundle\Entity\AbstractTask;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Workflow\Transition;
class TaskController extends Controller
@@ -41,7 +46,8 @@ class TaskController extends Controller
Registry $registry,
EntityManagerInterface $em,
Request $request,
TranslatorInterface $translator
TranslatorInterface $translator,
EventDispatcherInterface $eventDispatcher
) {
switch ($kind) {
case 'single-task':
@@ -49,9 +55,12 @@ class TaskController extends Controller
->find($taskId)
;
$defaultReturnPath = $this->generateUrl(
'chill_task_singletask_list',
[ 'person_id' => $task->getPerson() ]
);
'chill_task_single_task_show',
[
'id' => $task->getId(),
'list_params' => $request->query->get('list_params', [])
]);
$defaultTemplate = '@ChillTask/SingleTask/transition.html.twig';
break;
default:
return new Response("The type '$kind' is not implemented",
@@ -59,27 +68,75 @@ class TaskController extends Controller
}
if (NULL === $task) {
$this->createNotFoundException("task with id '$taskId' and type "
throw $this->createNotFoundException("task with id '$taskId' and type "
. "'$type' does not exists");
}
$workflow = $registry->get($task);
if (!$workflow->can($task, $transition)) {
throw $this->createAccessDeniedException('You are not allowed to apply this transition');
}
$transitionInstance = \array_values( // array_values needed to reset keys (array_filter preserves keys)
\array_filter(
$workflow->getEnabledTransitions($task),
function(Transition $t) use ($transition) {
return $t->getName() === $transition;
}
))[0];
// we simply check that the user can see the task. Other ACL checks
// should be performed using `guard` events.
$this->denyAccessUnlessGranted(TaskVoter::SHOW, $task);
$form = $this->createTransitionForm($task);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$workflow = $registry->get($task);
if ($workflow->can($task, $transition)) {
$workflow->apply($task, $transition);
if ($workflow->can($task, $transition)) {
$workflow->apply($task, $transition);
$em->flush();
$em->flush();
$this->addFlash('success', $translator->trans('The transition is successfully applied'));
$this->addFlash('success', $translator->trans('The transition is successfully applied'));
} else {
$this->addFlash('error', $translator->trans('The transition could not be applied'));
}
return $this->redirect($defaultReturnPath);
} else {
$this->addFlash('error', $translator->trans('The transition could not be applied'));
$event = (new UIEvent($kind, $task))
->setForm($form)
->setTransition($transitionInstance)
;
$eventDispatcher->dispatch(UIEvent::SHOW_TRANSITION_PAGE, $event);
if ($event->hasResponse()) {
return $event->getResponse();
} else {
return $this->render($defaultTemplate, [
'task' => $task,
'form' => $form->createView(),
'transition' => $transitionInstance
]);
}
}
return $this->redirect($request->query->get('return_path', $defaultReturnPath));
}
/**
*
* @param \Chill\TaskBundle\Controller\AbstractTask $task
* @return \Symfony\Component\Form\FormInterface
*/
protected function createTransitionForm(AbstractTask $task)
{
$builder = $this->createFormBuilder($task);
$builder->add('submit', SubmitType::class);
return $builder->getForm();
}
}