From 78c5e04a22022314d0f0d4d3a2d0a24f8d9e8645 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 27 Aug 2024 20:58:11 +0200 Subject: [PATCH] =?UTF-8?q?export=20des=20t=C3=A2ches?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Controller/TaskController.php | 50 +++++++++++++++++++++++++++++++++++++++ templates/project/show.html.twig | 1 + 2 files changed, 51 insertions(+) diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 3850fb1..38f3f5e 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\HeaderUtils; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\Routing\Attribute\Route; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Workflow\WorkflowInterface; @@ -409,6 +410,55 @@ class TaskController extends AbstractController return $response; } + #[Route('/download/{slug}.csv', name: 'app_task_csv')] + public function csv(Request $request, EntityManagerInterface $entityManager, $slug): Response + { + $repository = $entityManager->getRepository(Project::class); + $project = $repository->findOneBySlug($slug); + + if (!$project) { + $this->addFlash('warning', 'Projet non trouvé !'); + return $this->redirect($request->headers->get('referer')); + } + + $response = new StreamedResponse(); + + $response->setCallback(function () use ($project): void { + $output = fopen('php://output', 'a'); + fputcsv( + $output, [ + 'name', + 'description', + 'osm', + 'geojson', + 'status', + ] + ); + foreach($project->getTasks() as $task) { + fputcsv( + $output, + [ + $task->getName(), + $task->getDescription(), + $task->getOsm(), + $task->getGeojson(), + $task->getStatus(), + ] + ); + } + fclose($output); + flush(); + }); + + $response->headers->set('Content-Type', 'text/csv'); + $response->headers->set('Content-Disposition', HeaderUtils::makeDisposition( + HeaderUtils::DISPOSITION_ATTACHMENT, + sprintf('%s.csv', $project->getSlug()) + )); + + return $response; + } + #[Route('/{slug}/changesets', name: 'app_task_changesets')] public function changesets(OpenStreetMapClient $osmClient, EntityManagerInterface $entityManager, $slug): Response { diff --git a/templates/project/show.html.twig b/templates/project/show.html.twig index 7b74428..504f85b 100644 --- a/templates/project/show.html.twig +++ b/templates/project/show.html.twig @@ -27,6 +27,7 @@ Requêter Overpass {% endif %} + Exporter les tâches Créer une tâche {% endif %} {% if randomTask %}