fix missing lieux in stat page

This commit is contained in:
Tykayn 2025-08-08 19:04:42 +02:00 committed by tykayn
parent a81112a018
commit 7527d5cf6c

View file

@ -132,8 +132,8 @@ final class AdminController extends AbstractController
$stats = $stats_exist;
} else {
$stats = new Stats();
dump('nouvelle stat', $insee_code);
die();
// dump('nouvelle stat', $insee_code);
// die();
$stats->setZone($insee_code);
}
@ -322,13 +322,26 @@ final class AdminController extends AbstractController
$followups = $stats->getCityFollowUps();
}
$commerces = $stats->getPlacesCount();
if (!$commerces) {
// labourer
$places_found = $this->motocultrice->labourer($insee_code);
if (count($places_found)) {
var_dump(count($places_found));
// Remove existing duplicates
$this->removeDuplicatePlaces($stats);
foreach ($places_found as $placeData) {
// labourer
$places_found = $this->motocultrice->labourer($insee_code);
if (count($places_found)) {
// var_dump(count($places_found));
$placeRepo = $this->entityManager->getRepository(Place::class);
$addedCount = 0;
foreach ($places_found as $placeData) {
// Check if a place with the same osm_id and osm_kind already exists
$existingPlace = $placeRepo->findOneBy([
'osmId' => $placeData['id'],
'osm_kind' => $placeData['type'],
'stats' => $stats
]);
if (!$existingPlace) {
// Create a new place only if it doesn't already exist
$newPlace = new Place();
$newPlace->setOsmId($placeData['id'])
->setOsmKind($placeData['type'])
@ -353,10 +366,15 @@ final class AdminController extends AbstractController
$stats->addPlace($newPlace);
$newPlace->setStats($stats);
$this->entityManager->persist($newPlace);
$addedCount++;
}
}
// Update the places_count property
$stats->setPlacesCount($stats->getPlaces()->count());
$this->entityManager->persist($stats);
}
$this->entityManager->persist($newPlace);
$this->entityManager->flush();
$this->actionLogger->log('stats_de_ville', ['insee_code' => $insee_code, 'nom' => $stats->getZone()]);
@ -381,7 +399,10 @@ final class AdminController extends AbstractController
// Données pour le graphique des modifications par trimestre
$modificationsByQuarter = [];
if (isset($commerces) && count($commerces) > 0) {
$commerces = $stats->getPlaces();
if (isset($commerces) && count($commerces) > 0 && is_iterable($commerces)) {
foreach ($commerces as $commerce) {
if ($commerce->getOsmDataDate()) {
@ -403,23 +424,25 @@ final class AdminController extends AbstractController
'features' => []
];
foreach ($commerces as $commerce) {
if ($commerce->getLat() && $commerce->getLon()) {
$geojson['features'][] = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$commerce->getLon(), $commerce->getLat()]
],
'properties' => [
'id' => $commerce->getOsmId(),
'name' => $commerce->getName(),
'main_tag' => $commerce->getMainTag(),
'address' => $commerce->getStreet() . ' ' . $commerce->getHousenumber(),
'note' => $commerce->getNoteContent(),
'osm_url' => 'https://www.openstreetmap.org/' . $commerce->getOsmKind() . '/' . $commerce->getOsmId()
]
];
if (isset($commerces) && is_iterable($commerces)) {
foreach ($commerces as $commerce) {
if ($commerce->getLat() && $commerce->getLon()) {
$geojson['features'][] = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$commerce->getLon(), $commerce->getLat()]
],
'properties' => [
'id' => $commerce->getOsmId(),
'name' => $commerce->getName(),
'main_tag' => $commerce->getMainTag(),
'address' => $commerce->getStreet() . ' ' . $commerce->getHousenumber(),
'note' => $commerce->getNoteContent(),
'osm_url' => 'https://www.openstreetmap.org/' . $commerce->getOsmKind() . '/' . $commerce->getOsmId()
]
];
}
}
}
@ -2110,4 +2133,48 @@ final class AdminController extends AbstractController
$this->addFlash('success', 'Email envoyé avec succès');
return $this->redirectToRoute('app_admin_contacted_places');
}
/**
* Remove duplicate places from a Stats entity
* Duplicates are identified by having the same osm_id and osm_kind
*/
private function removeDuplicatePlaces(Stats $stats): void
{
$placeRepo = $this->entityManager->getRepository(Place::class);
// Get all places for this stats
$places = $placeRepo->findBy(['stats' => $stats]);
// Group places by osm_id and osm_kind
$groupedPlaces = [];
foreach ($places as $place) {
$key = $place->getOsmKind() . '_' . $place->getOsmId();
if (!isset($groupedPlaces[$key])) {
$groupedPlaces[$key] = [];
}
$groupedPlaces[$key][] = $place;
}
// For each group with more than one place, keep the first one and remove the rest
$removedCount = 0;
foreach ($groupedPlaces as $key => $group) {
if (count($group) > 1) {
// Keep the first place
$keepPlace = array_shift($group);
// Remove the rest
foreach ($group as $duplicatePlace) {
$stats->removePlace($duplicatePlace);
$this->entityManager->remove($duplicatePlace);
$removedCount++;
}
}
}
// If places were removed, update the places_count
if ($removedCount > 0) {
$stats->setPlacesCount($stats->getPlaces()->count());
$this->entityManager->persist($stats);
}
}
}