From 7f79ec3a9ffb48164efb074a26115cd6a7b96561 Mon Sep 17 00:00:00 2001 From: Tykayn Date: Wed, 16 Jul 2025 23:01:13 +0200 Subject: [PATCH] linking demandes --- src/Command/LinkDemandesPlacesCommand.php | 3 +- src/Command/LinkDemandesPlacesOsmCommand.php | 116 ++++++++++++++++++ src/Controller/PublicController.php | 14 +++ templates/admin/demandes/list.html.twig | 36 +++++- .../admin/followup_theme_graph.html.twig | 13 +- templates/public/city_demandes.html.twig | 18 ++- 6 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 src/Command/LinkDemandesPlacesOsmCommand.php diff --git a/src/Command/LinkDemandesPlacesCommand.php b/src/Command/LinkDemandesPlacesCommand.php index 9d2e2d0d..656e3536 100644 --- a/src/Command/LinkDemandesPlacesCommand.php +++ b/src/Command/LinkDemandesPlacesCommand.php @@ -107,6 +107,7 @@ class LinkDemandesPlacesCommand extends Command if (!$dryRun) { $demande->setPlace($bestMatch); + $demande->setPlaceUuid($bestMatch->getUuidForUrl()); $demande->setStatus('linked_to_place'); $this->entityManager->persist($demande); $linkedCount++; @@ -199,4 +200,4 @@ class LinkDemandesPlacesCommand extends Command { return str_replace(['%', '_'], ['\%', '\_'], $str); } -} \ No newline at end of file +} diff --git a/src/Command/LinkDemandesPlacesOsmCommand.php b/src/Command/LinkDemandesPlacesOsmCommand.php new file mode 100644 index 00000000..8265da0a --- /dev/null +++ b/src/Command/LinkDemandesPlacesOsmCommand.php @@ -0,0 +1,116 @@ +entityManager = $entityManager; + $this->demandeRepository = $demandeRepository; + $this->placeRepository = $placeRepository; + } + + protected function configure(): void + { + $this + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show matches without linking'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + $io->title('Linking Demandes to Places based on matching OSM type and ID'); + + $dryRun = $input->getOption('dry-run'); + + // Find all Demandes without a UUID but with OSM type and ID + $demandesWithoutUuid = $this->demandeRepository->createQueryBuilder('d') + ->where('d.placeUuid IS NULL') + ->andWhere('d.osmObjectType IS NOT NULL') + ->andWhere('d.osmId IS NOT NULL') + ->getQuery() + ->getResult(); + + if (empty($demandesWithoutUuid)) { + $io->warning('No Demandes without UUID but with OSM type and ID found.'); + return Command::SUCCESS; + } + + $io->info(sprintf('Found %d Demandes without UUID but with OSM type and ID.', count($demandesWithoutUuid))); + + // Process each Demande + $linkedCount = 0; + /** @var Demande $demande */ + foreach ($demandesWithoutUuid as $demande) { + $osmType = $demande->getOsmObjectType(); + $osmId = $demande->getOsmId(); + + // Find Place with matching OSM type and ID + $place = $this->placeRepository->findOneBy([ + 'osm_kind' => $osmType, + 'osmId' => $osmId + ]); + + if ($place) { + $io->text(sprintf( + 'Match found: Demande #%d -> Place #%d (OSM %s/%d)', + $demande->getId(), + $place->getId(), + $osmType, + $osmId + )); + + if (!$dryRun) { + $demande->setPlace($place); + $demande->setPlaceUuid($place->getUuidForUrl()); + $demande->setStatus('linked_to_place'); + $this->entityManager->persist($demande); + $linkedCount++; + } + } else { + $io->text(sprintf( + 'No matching Place found for Demande #%d (OSM %s/%d)', + $demande->getId(), + $osmType, + $osmId + )); + } + } + + if (!$dryRun && $linkedCount > 0) { + $this->entityManager->flush(); + $io->success(sprintf('Linked %d Demandes to Places based on OSM type and ID.', $linkedCount)); + } elseif ($dryRun) { + $io->info('Dry run completed. No changes were made.'); + } else { + $io->info('No Demandes were linked to Places.'); + } + + return Command::SUCCESS; + } +} \ No newline at end of file diff --git a/src/Controller/PublicController.php b/src/Controller/PublicController.php index 5e3ab5fd..bee6d4fe 100644 --- a/src/Controller/PublicController.php +++ b/src/Controller/PublicController.php @@ -151,6 +151,20 @@ class PublicController extends AbstractController $demande->setOsmId((int)$data['osmId']); } + // Check if a Place exists with the same OSM ID and type + if ($demande->getOsmId() && $demande->getOsmObjectType()) { + $existingPlace = $this->entityManager->getRepository(Place::class)->findOneBy([ + 'osm_kind' => $demande->getOsmObjectType(), + 'osmId' => $demande->getOsmId() + ]); + + if ($existingPlace) { + // Link the Place UUID to the Demande + $demande->setPlaceUuid($existingPlace->getUuidForUrl()); + $demande->setPlace($existingPlace); + } + } + $this->entityManager->persist($demande); $this->entityManager->flush(); diff --git a/templates/admin/demandes/list.html.twig b/templates/admin/demandes/list.html.twig index 96925ba0..fd8008cf 100644 --- a/templates/admin/demandes/list.html.twig +++ b/templates/admin/demandes/list.html.twig @@ -96,6 +96,7 @@ Email Date de création Statut + OSM Place UUID Dernière tentative de contact Actions @@ -105,7 +106,15 @@ {% for demande in demandes %} {{ demande.id }} - {{ demande.query }} + + {% if demande.placeUuid and demande.osmObjectType and demande.osmId %} + + {{ demande.query }} + + {% else %} + {{ demande.query }} + {% endif %} + {{ demande.email }} {{ demande.createdAt ? demande.createdAt|date('Y-m-d H:i:s') : '' }} @@ -123,7 +132,28 @@ {{ demande.status }} - {{ demande.placeUuid }} + + {% if demande.osmObjectType and demande.osmId %} + + {{ demande.osmObjectType }}/{{ demande.osmId }} + + {% else %} + - + {% endif %} + + + {% if demande.placeUuid %} + {% if demande.osmObjectType and demande.osmId %} + + {{ demande.placeUuid }} + + {% else %} + {{ demande.placeUuid }} + {% endif %} + {% else %} + {{ demande.placeUuid }} + {% endif %} + {{ demande.lastContactAttempt ? demande.lastContactAttempt|date('Y-m-d H:i:s') : '' }}
@@ -140,7 +170,7 @@ {% else %} - Aucune demande trouvée + Aucune demande trouvée {% endfor %} diff --git a/templates/admin/followup_theme_graph.html.twig b/templates/admin/followup_theme_graph.html.twig index 6e4e643c..7f46d940 100644 --- a/templates/admin/followup_theme_graph.html.twig +++ b/templates/admin/followup_theme_graph.html.twig @@ -508,18 +508,21 @@ // Mettre à jour les statistiques function updateStats() { - if (countData.length > 0) { + if (Array.isArray(countData) && countData.length > 0) { const latestCount = countData[countData.length - 1]; document.getElementById('currentCount').textContent = latestCount.value; document.getElementById('lastUpdate').textContent = new Date(latestCount.date).toLocaleDateString('fr-FR'); } - if (completionData.length > 0) { + if (Array.isArray(completionData) && completionData.length > 0) { const latestCompletion = completionData[completionData.length - 1]; document.getElementById('currentCompletion').textContent = latestCompletion.value + '%'; } - document.getElementById('dataPoints').textContent = Math.max(countData.length, completionData.length); + document.getElementById('dataPoints').textContent = Math.max( + Array.isArray(countData) ? countData.length : 0, + Array.isArray(completionData) ? completionData.length : 0 + ); } // Configuration commune pour les graphiques @@ -571,7 +574,7 @@ datasets: [ { label: "Nombre d'objets", - data: countData?.map(d => ({ x: new Date(d.date), y: d.value })), + data: Array.isArray(countData) ? countData.map(d => ({ x: new Date(d.date), y: d.value })) : [], borderColor: '#0d6efd', backgroundColor: 'rgba(13, 110, 253, 0.1)', borderWidth: 2, @@ -581,7 +584,7 @@ }, { label: 'Pourcentage de complétion', - data: completionData?.map(d => ({ x: new Date(d.date), y: d.value })), + data: Array.isArray(completionData) ? completionData.map(d => ({ x: new Date(d.date), y: d.value })) : [], borderColor: '#198754', backgroundColor: 'rgba(25, 135, 84, 0.1)', borderWidth: 2, diff --git a/templates/public/city_demandes.html.twig b/templates/public/city_demandes.html.twig index f47c7ad9..90415b67 100644 --- a/templates/public/city_demandes.html.twig +++ b/templates/public/city_demandes.html.twig @@ -47,6 +47,7 @@ Nom du commerce Date de création Statut + OSM Dernière tentative de contact {% if is_granted('ROLE_ADMIN') %} Actions @@ -74,6 +75,21 @@ {{ demande.status }} + + {% if demande.osmObjectType and demande.osmId %} + + {{ demande.osmObjectType }}/{{ demande.osmId }} + + {% if demande.placeUuid %} +
+ + Éditer + + {% endif %} + {% else %} + - + {% endif %} + {{ demande.lastContactAttempt ? demande.lastContactAttempt|date('Y-m-d H:i:s') : '' }} {% if is_granted('ROLE_ADMIN') %} @@ -92,7 +108,7 @@ {% else %} - Aucune demande trouvée pour cette ville + Aucune demande trouvée pour cette ville {% endfor %}