@ -11,6 +11,7 @@ use App\Service\GeoJsonManager;
use Doctrine\ORM\EntityManagerInterface ;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ;
use Symfony\Component\Form\Extension\Core\Type\SubmitType ;
use Symfony\Component\HttpFoundation\HeaderUtils ;
use Symfony\Component\HttpFoundation\JsonResponse ;
use Symfony\Component\HttpFoundation\Request ;
use Symfony\Component\HttpFoundation\Response ;
@ -21,17 +22,20 @@ use Symfony\Component\Workflow\WorkflowInterface;
class TaskController extends AbstractController
{
#[Route('/create', name: 'app_task_create')]
public function create ( Request $request , EntityManagerInterface $entityManager , $slug ) : Response
public function create ( Request $request , EntityManagerInterface $entityManager ) : Response
{
if ( ! $request -> query -> has ( 'slug' )) {
$this -> addFlash ( 'warning' , 'Projet non spécifié !' );
return $this -> redirectToRoute ( 'app_project' );
}
$slug = $request -> query -> get ( 'slug' );
$repository = $entityManager -> getRepository ( Project :: class );
$project = $repository -> findOneBySlug ( $slug );
if ( ! $project ) {
$this -> addFlash (
'warning' ,
'Projet non trouvé !'
);
$this -> addFlash ( 'warning' , 'Projet non trouvé !' );
return $this -> redirectToRoute ( 'app_project' );
}
@ -58,10 +62,7 @@ class TaskController extends AbstractController
return $this -> redirectToRoute ( 'app_project_show' , [ 'slug' => $slug ]);
} catch ( \Exception $exception ) {
$this -> addFlash (
'danger' ,
'Impossible de créer la tâche !'
);
$this -> addFlash ( 'danger' , 'Impossible de créer la tâche !' );
}
}
@ -84,9 +85,14 @@ class TaskController extends AbstractController
'Tâche non trouvée !'
);
if ( ! $request -> headers -> has ( 'Referer' )) {
throw $this -> createNotFoundException ( 'Task not found' );
}
return $this -> redirect ( $request -> headers -> get ( 'Referer' ));
}
$nextTask = $repository -> findNextOne ( $task );
$comment = new Comment ();
$commentForm = $this -> createForm ( CommentType :: class , $comment , [
'action' => $this -> generateUrl ( 'app_task_comment' , [ 'slug' => $slug ]),
@ -95,10 +101,15 @@ class TaskController extends AbstractController
'label' => 'Commenter' ,
]);
$geom = \geoPHP :: load ( $task -> getGeojson (), 'json' );
$bbox = $geom -> getBBox ();
return $this -> render ( 'task/show.html.twig' , [
'task' => $task ,
'project' => $task -> getProject (),
'commentForm' => $commentForm ,
'nextTask' => $nextTask ,
'bbox' => $bbox ,
]);
}
@ -118,6 +129,7 @@ class TaskController extends AbstractController
}
$comment = new Comment ();
$comment -> setCreatedBy ( $this -> getUser ());
$comment -> setTask ( $task );
$commentForm = $this -> createForm ( CommentType :: class , $comment );
$commentForm -> add ( 'submit' , SubmitType :: class , [
@ -132,14 +144,10 @@ class TaskController extends AbstractController
$entityManager -> persist ( $comment );
$entityManager -> flush ();
$this -> addFlash (
'success' ,
'Commentaire ajouté !'
);
} catch ( \Exception $exception ) {
$this -> addFlash (
'danger' ,
'Impossible de commenter !'
'Impossible de commenter ! ' . $exception -> getMessage ()
);
}
}
@ -175,11 +183,6 @@ class TaskController extends AbstractController
$entityManager -> persist ( $task );
$entityManager -> flush ();
$this -> addFlash (
'success' ,
'Tâche modifiée !'
);
return $this -> redirectToRoute ( 'app_task_show' , [ 'slug' => $slug ]);
} catch ( \Exception $exception ) {
$this -> addFlash (
@ -217,11 +220,6 @@ class TaskController extends AbstractController
$entityManager -> remove ( $task );
$entityManager -> flush ();
$this -> addFlash (
'success' ,
'Tâche supprimée !'
);
return $this -> redirectToRoute ( 'app_project_show' , [ 'slug' => $project -> getSlug ()]);
} catch ( \Exception $exception ) {
$this -> addFlash (
@ -233,7 +231,7 @@ class TaskController extends AbstractController
return $this -> redirectToRoute ( 'app_project_show' , [ 'slug' => $slug ]);
}
private function transition ( WorkflowInterface $taskLifecycleStateMachine , EntityManagerInterface $entityManager , $slug , $transitionName ) : Response
private function transition ( WorkflowInterface $taskLifecycleStateMachine , EntityManagerInterface $entityManager , $slug , $transitionName , $commentTemplate ) : Response
{
$repository = $entityManager -> getRepository ( Task :: class );
$task = $repository -> findOneBySlug ( $slug );
@ -265,14 +263,18 @@ class TaskController extends AbstractController
} elseif ( $shouldTransitionUnlock ) {
$task -> unlock ();
}
$entityManager -> persist ( $task );
$entityManager -> flush ();
$this -> addFlash (
'success' ,
'La tâche est modifiée !'
);
$comment = new Comment ();
$comment -> setTask ( $task );
$comment -> setCreatedBy ( $this -> getUser ());
$comment -> setContent ( $this -> renderView ( $commentTemplate , [
'user' => $this -> getUser (),
'task' => $task ,
]));
$entityManager -> persist ( $comment );
$entityManager -> flush ();
} catch ( Exception $exception ) {
$this -> addFlash (
'warning' ,
@ -286,28 +288,28 @@ class TaskController extends AbstractController
#[Route('/{slug}/start', name: 'app_task_start')]
public function start ( WorkflowInterface $taskLifecycleStateMachine , EntityManagerInterface $entityManager , $slug ) : Response
{
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_START );
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_START , 'comment/start.md.twig' );
}
#[Route('/{slug}/finish', name: 'app_task_finish')]
public function finish ( WorkflowInterface $taskLifecycleStateMachine , EntityManagerInterface $entityManager , $slug ) : Response
{
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_FINISH );
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_FINISH , 'comment/finish.md.twig' );
}
#[Route('/{slug}/cancel', name: 'app_task_cancel')]
public function cancel ( WorkflowInterface $taskLifecycleStateMachine , EntityManagerInterface $entityManager , $slug ) : Response
{
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_CANCEL );
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_CANCEL , 'comment/cancel.md.twig' );
}
#[Route('/{slug}/reset', name: 'app_task_reset')]
public function reset ( WorkflowInterface $taskLifecycleStateMachine , EntityManagerInterface $entityManager , $slug ) : Response
{
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_RESET );
return $this -> transition ( $taskLifecycleStateMachine , $entityManager , $slug , Task :: TRANSITION_RESET , 'comment/reset.md.twig' );
}
#[Route('/{slug}.geojson', name: 'app_task_geojson')]
#[Route('/download/ {slug}.geojson', name: 'app_task_geojson')]
public function geojson ( Request $request , EntityManagerInterface $entityManager , $slug ) : Response
{
$repository = $entityManager -> getRepository ( Task :: class );
@ -318,13 +320,52 @@ class TaskController extends AbstractController
'warning' ,
'Tâche non trouvée !'
);
// TODO faire pareil ailleurs où il y a des referer
if ( ! $request -> headers -> has ( 'Referer' )) {
throw $this -> createNotFoundException ( 'Task not found' );
}
return $this -> redirect ( $request -> headers -> get ( 'Referer' ));
}
$response = JsonResponse :: fromJsonString ( $task -> getGeojson ());
$response -> headers -> set ( 'Content-Disposition' , HeaderUtils :: makeDisposition (
HeaderUtils :: DISPOSITION_ATTACHMENT ,
sprintf ( '%s.geojson' , $task -> getSlug ())
));
return $response ;
}
#[Route('/download/{slug}.gpx', name: 'app_task_gpx')]
public function gpx ( Request $request , EntityManagerInterface $entityManager , $slug ) : Response
{
$repository = $entityManager -> getRepository ( Task :: class );
$task = $repository -> findOneBySlug ( $slug );
if ( ! $task ) {
$this -> addFlash ( 'warning' , 'Tâche non trouvée !' );
if ( ! $request -> headers -> has ( 'Referer' )) {
throw $this -> createNotFoundException ( 'Task not found' );
}
return $this -> redirect ( $request -> headers -> get ( 'Referer' ));
}
return JsonResponse :: fromJsonString ( $task -> getGeojson ());
$geom = \geoPHP :: load ( $task -> getGeojson (), 'json' );
$gpx = $geom -> out ( 'gpx' );
$response = new Response ( $gpx );
$response -> headers -> set ( 'Content-Type' , 'application/xml' );
$response -> headers -> set ( 'Content-Disposition' , HeaderUtils :: makeDisposition (
HeaderUtils :: DISPOSITION_ATTACHMENT ,
sprintf ( '%s.gpx' , $task -> getSlug ())
));
return $response ;
}
#[Route('/{slug}.osm', name: 'app_task_osm')]
#[Route('/download/ {slug}.osm', name: 'app_task_osm')]
public function osm ( Request $request , EntityManagerInterface $entityManager , $slug ) : Response
{
$repository = $entityManager -> getRepository ( Task :: class );
@ -340,8 +381,15 @@ class TaskController extends AbstractController
$xml = new \DOMDocument ();
$xml -> loadXml ( $task -> getOsm ());
$response = new Response ( $xml -> saveXML ());
$response -> headers -> set ( 'Content-Type' , 'application/xml' );
$response -> headers -> set ( 'Content-Disposition' , HeaderUtils :: makeDisposition (
HeaderUtils :: DISPOSITION_ATTACHMENT ,
sprintf ( '%s.osm' , $task -> getSlug ())
));
return $response ;
}