From 30b4bdafcdb2fa5de7b5163eb87f22cef5165119 Mon Sep 17 00:00:00 2001 From: vincent Date: Mon, 26 Aug 2024 22:17:43 +0200 Subject: [PATCH] corrige les commentaires par projet --- src/Controller/HomeController.php | 2 +- src/Controller/TaskController.php | 26 ++++++++++++++++++++------ src/Entity/Task.php | 5 +++++ src/Form/TaskType.php | 2 +- src/Repository/CommentRepository.php | 2 +- src/Service/GeoJsonManager.php | 30 +++++++++++++++++++++++------- templates/project/show.html.twig | 2 ++ 7 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index a7f209a..f71e7a7 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -21,7 +21,7 @@ class HomeController extends AbstractController public function error(Request $request, $exception, $logger = null): Response { - $this->addFlash('danger', $exception->getMessage()); + $this->addFlash('danger', $exception->getMessage()); // . ' ' . $exception->getFile() . ':' . $exception->getLine()); if ($request->headers->has('Referer')) { return $this->redirect($request->headers->get('Referer')); diff --git a/src/Controller/TaskController.php b/src/Controller/TaskController.php index 31d8f11..3850fb1 100644 --- a/src/Controller/TaskController.php +++ b/src/Controller/TaskController.php @@ -53,6 +53,9 @@ class TaskController extends AbstractController $task = $createForm->getData(); try { + if (!$task->hasGeojson()) { + $task->setGeojson('{}'); + } $task->setCreatedBy($this->getUser()); $entityManager->persist($task); $entityManager->flush(); @@ -104,9 +107,6 @@ class TaskController extends AbstractController 'label' => 'Commenter', ]); - $geom = \geoPHP::load($task->getGeojson(), 'json'); - $bbox = $geom->getBBox(); - $josmCommands = [ 'imagery' => [ 'id' => $project->hasImagery() ? $project->getImagery() : 'osmfr', @@ -120,7 +120,13 @@ class TaskController extends AbstractController UrlGeneratorInterface::ABSOLUTE_URL ), ], - 'zoom' => [ + ]; + + if ($task->hasGeojson()) { + $geom = \geoPHP::load($task->getGeojson(), 'json'); + $bbox = $geom->getBBox(); + + $josmCommands['zoom'] = [ 'bottom' => $bbox['miny'], 'top' => $bbox['maxy'], 'left' => $bbox['minx'], @@ -128,8 +134,8 @@ class TaskController extends AbstractController 'changeset_comment' => sprintf('%s %s', $project->getName(), $task->getName()), 'changeset_source' => $project->getSource(), 'changeset_hashtags' => $project->getHashtags(), - ], - ]; + ]; + } return $this->render('task/show.html.twig', [ 'task' => $task, @@ -329,6 +335,10 @@ class TaskController extends AbstractController return $this->redirect($request->headers->get('Referer')); } + if (!$task->hasGeojson()) { + return new Response('Not found', 404); + } + $response = JsonResponse::fromJsonString($task->getGeojson()); $response->headers->set('Content-Disposition', HeaderUtils::makeDisposition( @@ -353,6 +363,10 @@ class TaskController extends AbstractController return $this->redirect($request->headers->get('Referer')); } + if (!$task->hasGeojson()) { + return new Response('Not found', 404); + } + $geom = \geoPHP::load($task->getGeojson(), 'json'); $gpx = $geom->out('gpx'); diff --git a/src/Entity/Task.php b/src/Entity/Task.php index b098366..43009b5 100644 --- a/src/Entity/Task.php +++ b/src/Entity/Task.php @@ -162,6 +162,11 @@ class Task return $this; } + public function hasGeojson(): bool + { + return isset($this->geojson); + } + public function getGeojson(): ?string { return $this->geojson; diff --git a/src/Form/TaskType.php b/src/Form/TaskType.php index 06019c8..78ba1f0 100644 --- a/src/Form/TaskType.php +++ b/src/Form/TaskType.php @@ -16,7 +16,7 @@ class TaskType extends AbstractType $builder ->add('name', null, ['label' => 'Nom']) ->add('description', null, ['label' => 'Description']) - ->add('geojson', TextareaType::class, ['label' => 'GeoJSON']) + ->add('geojson', TextareaType::class, ['label' => 'GeoJSON', 'required' => false]) ->add('osm', TextareaType::class, ['label' => 'OSM', 'required' => false]) ->add('status', TaskLifecycleType::class, ['label' => 'État']) ->add('urgent', null, ['label' => 'Urgence', 'required' => false]) diff --git a/src/Repository/CommentRepository.php b/src/Repository/CommentRepository.php index b644660..25efd25 100644 --- a/src/Repository/CommentRepository.php +++ b/src/Repository/CommentRepository.php @@ -24,7 +24,7 @@ class CommentRepository extends ServiceEntityRepository public function findLatestByProject(Project $project): array { return $this->createQueryBuilder('c') - ->join(Task::class, 't') + ->join('c.task', 't') ->andWhere('t.project = :project') ->setParameter('project', $project) ->orderBy('c.createdAt', 'ASC') diff --git a/src/Service/GeoJsonManager.php b/src/Service/GeoJsonManager.php index 1362bb2..1a43cef 100644 --- a/src/Service/GeoJsonManager.php +++ b/src/Service/GeoJsonManager.php @@ -19,16 +19,26 @@ class GeoJsonManager private function getFullGeoJson(Task $task) { - $data = json_decode($task->getGeojson(), true); - if (isset($data['features'][0]['properties'])) { + if (!$task->hasGeojson()) { + return null; + } + + try { + $data = json_decode($task->getGeojson(), true); + } catch (\Exception $exception) { + return null; + } + + if (!isset($data['features'][0]['properties'])) { + return null; + } + $data['features'][0]['properties'] = array_merge($data['features'][0]['properties'], [ 'name' => $task->getName(), 'url' => $this->router->generate('app_task_show', ['slug' => $task->getSlug()], UrlGeneratorInterface::ABSOLUTE_URL), 'color' => $this->taskLifecycleStateMachine->getMetadataStore()->getPlaceMetadata($task->getStatus())['color'], ]); - } else { - dump($data); - } + return $data; } @@ -38,11 +48,17 @@ class GeoJsonManager if ($entity instanceof Task) { $task = $entity; - $geoJsons[] = GeoJson::jsonUnserialize($this->getFullGeojson($task)); + $geoJson = $this->getFullGeojson($task); + if (!is_null($geoJson)) { + $geoJsons[] = GeoJson::jsonUnserialize($geoJson); + } } elseif ($entity instanceof Project) { $project = $entity; foreach ($project->getTasks() as $task) { - $geoJsons[] = GeoJson::jsonUnserialize($this->getFullGeojson($task)); + $geoJson = $this->getFullGeojson($task); + if (!is_null($geoJson)) { + $geoJsons[] = GeoJson::jsonUnserialize($geoJson); + } } } diff --git a/templates/project/show.html.twig b/templates/project/show.html.twig index 3222a66..5d917f4 100644 --- a/templates/project/show.html.twig +++ b/templates/project/show.html.twig @@ -105,6 +105,8 @@
{% for comment in comments %}
+ {{ project.name }} + {{ comment.task.project.name }} {{ comment.content|markdown_to_html }}