You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

327 lines
11 KiB

<?php
namespace App\Controller;
use App\Entity\Comment;
use App\Entity\Project;
use App\Entity\Task;
use App\Form\CommentType;
use App\Form\TaskType;
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\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Workflow\WorkflowInterface;
#[Route('/task')]
class TaskController extends AbstractController
{
#[Route('/{projectSlug}/create', name: 'app_task_create')]
public function create(Request $request, EntityManagerInterface $entityManager, $projectSlug): Response
{
$repository = $entityManager->getRepository(Project::class);
$project = $repository->findOneBySlug($projectSlug);
if (!$project) {
$this->addFlash(
'warning',
'Projet non trouvé !'
);
return $this->redirectToRoute('app_project');
}
$task = new Task();
$task->setProject($project);
$createForm = $this->createForm(TaskType::class, $task);
$createForm->add('submit', SubmitType::class, [
'label' => 'Créer',
]);
$createForm->handleRequest($request);
if ($createForm->isSubmitted() and $createForm->isValid()) {
$task = $createForm->getData();
try {
$entityManager->persist($task);
$entityManager->flush();
$this->addFlash(
'success',
'Tâche créée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
} catch (\Exception $exception) {
$this->addFlash(
'danger',
'Impossible de créer la tâche !'
);
}
}
return $this->render('task/create.html.twig', [
'project' => $project,
'create_form' => $createForm,
]);
}
#[Route('/{projectSlug}/show/{taskSlug}', name: 'app_task_show')]
public function show(EntityManagerInterface $entityManager, GeoJsonManager $geoJsonManager, $projectSlug, $taskSlug): Response
{
$repository = $entityManager->getRepository(Task::class);
$task = $repository->findOneBySlug($taskSlug);
if (!$task) {
$this->addFlash(
'warning',
'Tâche non trouvée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
}
$comment = new Comment();
$commentForm = $this->createForm(CommentType::class, $comment, [
'action' => $this->generateUrl('app_task_comment', ['projectSlug' => $projectSlug, 'taskSlug' => $taskSlug]),
]);
$commentForm->add('submit', SubmitType::class, [
'label' => 'Commenter',
]);
return $this->render('task/show.html.twig', [
'task' => $task,
'project' => $task->getProject(),
'commentForm' => $commentForm,
]);
}
#[Route('/{projectSlug}/comment/{taskSlug}', name: 'app_task_comment')]
public function comment(Request $request, EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
$repository = $entityManager->getRepository(Task::class);
$task = $repository->findOneBySlug($taskSlug);
if (!$task) {
$this->addFlash(
'warning',
'Tâche non trouvée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
}
$comment = new Comment();
$comment->setTask($task);
$commentForm = $this->createForm(CommentType::class, $comment);
$commentForm->add('submit', SubmitType::class, [
'label' => 'Commenter',
]);
$commentForm->handleRequest($request);
if ($commentForm->isSubmitted() and $commentForm->isValid()) {
$comment = $commentForm->getData();
try {
$entityManager->persist($comment);
$entityManager->flush();
$this->addFlash(
'success',
'Commentaire ajouté !'
);
} catch (\Exception $exception) {
$this->addFlash(
'danger',
'Impossible de commenter !'
);
}
}
return $this->redirectToRoute('app_task_show', ['projectSlug' => $projectSlug, 'taskSlug' => $taskSlug]);
}
#[Route('/{projectSlug}/update/{taskSlug}', name: 'app_task_update')]
public function update(Request $request, EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
$repository = $entityManager->getRepository(Task::class);
$task = $repository->findOneBySlug($taskSlug);
if (!$task) {
$this->addFlash(
'warning',
'Tâche non trouvée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
}
$updateForm = $this->createForm(TaskType::class, $task);
$updateForm->add('submit', SubmitType::class, [
'label' => 'Modifier',
]);
$updateForm->handleRequest($request);
if ($updateForm->isSubmitted() and $updateForm->isValid()) {
$task = $updateForm->getData();
try {
$entityManager->persist($task);
$entityManager->flush();
$this->addFlash(
'success',
'Tâche modifiée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
} catch (\Exception $exception) {
$this->addFlash(
'danger',
'Impossible de modifier la tâche !'
);
}
}
return $this->render('task/update.html.twig', [
'project' => $task->getProject(),
'task' => $task,
'update_form' => $updateForm,
]);
}
#[Route('/{projectSlug}/remove/{taskSlug}', name: 'app_task_remove')]
public function remove(EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
$repository = $entityManager->getRepository(Task::class);
$task = $repository->findOneBySlug($taskSlug);
if (!$task) {
$this->addFlash(
'warning',
'Tâche non trouvée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
}
try {
$entityManager->remove($task);
$entityManager->flush();
$this->addFlash(
'success',
'Tâche supprimée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
} catch (\Exception $exception) {
$this->addFlash(
'danger',
'Impossible de supprimer la tâche !'
);
}
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
}
private function transition(WorkflowInterface $taskLifecycleStateMachine, EntityManagerInterface $entityManager, $projectSlug, $taskSlug, $status): Response
{
$repository = $entityManager->getRepository(Task::class);
$task = $repository->findOneBySlug($taskSlug);
if (!$task) {
$this->addFlash(
'warning',
'Tâche non trouvée !'
);
return $this->redirectToRoute('app_project_show', ['projectSlug' => $projectSlug]);
}
try {
$taskLifecycleStateMachine->apply($task, $status);
$entityManager->persist($task);
$entityManager->flush();
$this->addFlash(
'success',
'La tâche est modifiée !'
);
} catch (Exception $exception) {
$this->addFlash(
'warning',
'Impossible de modifier la tâche !'
);
}
return $this->redirectToRoute('app_task_show', ['projectSlug' => $projectSlug, 'taskSlug' => $taskSlug]);
}
#[Route('/{projectSlug}/start/{taskSlug}', name: 'app_task_start')]
public function start(WorkflowInterface $taskLifecycleStateMachine, EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
return $this->transition($taskLifecycleStateMachine, $entityManager, $projectSlug, $taskSlug, Task::TRANSITION_START);
}
#[Route('/{projectSlug}/finish/{taskSlug}', name: 'app_task_finish')]
public function finish(WorkflowInterface $taskLifecycleStateMachine, EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
return $this->transition($taskLifecycleStateMachine, $entityManager, $projectSlug, $taskSlug, Task::TRANSITION_FINISH);
}
#[Route('/{projectSlug}/cancel/{taskSlug}', name: 'app_task_cancel')]
public function cancel(WorkflowInterface $taskLifecycleStateMachine, EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
return $this->transition($taskLifecycleStateMachine, $entityManager, $projectSlug, $taskSlug, Task::TRANSITION_CANCEL);
}
#[Route('/{projectSlug}/reset/{taskSlug}', name: 'app_task_reset')]
public function reset(WorkflowInterface $taskLifecycleStateMachine, EntityManagerInterface $entityManager, $projectSlug, $taskSlug): Response
{
return $this->transition($taskLifecycleStateMachine, $entityManager, $projectSlug, $taskSlug, Task::TRANSITION_RESET);
}
#[Route('/{slug}.geojson', name: 'app_task_geojson')]
public function geojson(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 !'
);
return $this->redirect($request->headers->get('referer'));
}
return JsonResponse::fromJsonString($task->getGeojson());
}
#[Route('/{slug}.osm', name: 'app_task_osm')]
public function osm(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 !'
);
return $this->redirect($request->headers->get('referer'));
}
$xml = new \DOMDocument();
$xml->loadXml($task->getOsm());
$response = new Response($xml->saveXML());
$response->headers->set('Content-Type', 'application/xml');
return $response;
}
}