diff --git a/src/Command/LinkDemandesPlacesCommand.php b/src/Command/LinkDemandesPlacesCommand.php index 9d2e2d0..656e353 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 0000000..8265da0 --- /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 5e3ab5f..bee6d4f 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 96925ba..fd8008c 100644 --- a/templates/admin/demandes/list.html.twig +++ b/templates/admin/demandes/list.html.twig @@ -96,6 +96,7 @@