1370 lines
59 KiB
PHP
1370 lines
59 KiB
PHP
<?php
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Routing\Attribute\Route;
|
|
use App\Entity\Place;
|
|
use App\Entity\Stats;
|
|
use App\Entity\StatsHistory;
|
|
use App\Service\Motocultrice;
|
|
use App\Service\BudgetService;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use function uuid_create;
|
|
use Symfony\Component\Filesystem\Filesystem;
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
use Twig\Environment;
|
|
use App\Service\ActionLogger;
|
|
use DateTime;
|
|
use App\Service\FollowUpService;
|
|
|
|
final class AdminController extends AbstractController
|
|
{
|
|
|
|
private FollowUpService $followUpService;
|
|
|
|
public function __construct(
|
|
private EntityManagerInterface $entityManager,
|
|
private Motocultrice $motocultrice,
|
|
private BudgetService $budgetService,
|
|
private Environment $twig,
|
|
private ActionLogger $actionLogger,
|
|
FollowUpService $followUpService
|
|
) {
|
|
$this->followUpService = $followUpService;
|
|
}
|
|
|
|
|
|
#[Route('/admin/labourer-toutes-les-zones', name: 'app_admin_labourer_tout')]
|
|
public function labourer_tout(): Response
|
|
{
|
|
|
|
|
|
|
|
$this->actionLogger->log('labourer_toutes_les_zones', []);
|
|
$updateExisting = true;
|
|
|
|
$stats_all = $this->entityManager->getRepository(Stats::class)->findAll();
|
|
|
|
echo 'on a trouvé ' . count($stats_all) . ' zones à labourer<br>';
|
|
|
|
foreach ($stats_all as $stats) {
|
|
|
|
echo '<br> on laboure la zone ' . $stats->getZone() . ' ';
|
|
|
|
$processedCount = 0;
|
|
$updatedCount = 0;
|
|
$insee_code = $stats->getZone();
|
|
// Vérifier si le code INSEE est un nombre valide
|
|
// Vérifier si les stats ont été modifiées il y a moins de 24h
|
|
if ($stats->getDateModified() !== null) {
|
|
$now = new \DateTime();
|
|
$diff = $now->diff($stats->getDateModified());
|
|
$hours = $diff->h + ($diff->days * 24);
|
|
|
|
if ($hours < 24) {
|
|
echo 'Stats modifiées il y a moins de 24h - on passe au suivant<br>';
|
|
continue;
|
|
}
|
|
}
|
|
if (!is_numeric($insee_code) || $insee_code == 'undefined' || $insee_code == '') {
|
|
echo 'Code INSEE invalide : ' . $insee_code . ' - on passe au suivant<br>';
|
|
continue;
|
|
}
|
|
|
|
$places_overpass = $this->motocultrice->labourer($stats->getZone());
|
|
$places = $places_overpass;
|
|
foreach ($places as $placeData) {
|
|
|
|
|
|
// Vérifier si le lieu existe déjà
|
|
$existingPlace = $this->entityManager->getRepository(Place::class)
|
|
->findOneBy(['osmId' => $placeData['id']]);
|
|
|
|
if (!$existingPlace) {
|
|
$place = new Place();
|
|
$place->setOsmId($placeData['id'])
|
|
->setOsmKind($placeData['type'])
|
|
->setZipCode($insee_code)
|
|
->setUuidForUrl($this->motocultrice->uuid_create())
|
|
->setModifiedDate(new \DateTime())
|
|
->setStats($stats)
|
|
->setDead(false)
|
|
->setOptedOut(false)
|
|
->setMainTag($this->motocultrice->find_main_tag($placeData['tags']) ?? '')
|
|
->setStreet($this->motocultrice->find_street($placeData['tags']) ?? '')
|
|
->setHousenumber($this->motocultrice->find_housenumber($placeData['tags']) ?? '')
|
|
->setSiret($this->motocultrice->find_siret($placeData['tags']) ?? '')
|
|
->setAskedHumainsSupport(false)
|
|
->setLastContactAttemptDate(null)
|
|
->setPlaceCount(0)
|
|
// ->setOsmData($placeData['modified'] ?? null)
|
|
;
|
|
|
|
// Mettre à jour les données depuis Overpass
|
|
$place->update_place_from_overpass_data($placeData);
|
|
|
|
$this->entityManager->persist($place);
|
|
$stats->addPlace($place);
|
|
$processedCount++;
|
|
|
|
// Générer le contenu de l'email avec le template
|
|
$emailContent = $this->twig->render('admin/email_content.html.twig', ['place' => $place]);
|
|
$place->setEmailContent($emailContent);
|
|
} elseif ($updateExisting) {
|
|
// Mettre à jour les données depuis Overpass uniquement si updateExisting est true
|
|
$existingPlace->update_place_from_overpass_data($placeData);
|
|
$stats->addPlace($existingPlace);
|
|
$this->entityManager->persist($existingPlace);
|
|
$updatedCount++;
|
|
}
|
|
}
|
|
// mettre à jour les stats
|
|
// Récupérer tous les commerces de la zone
|
|
$commerces = $this->entityManager->getRepository(Place::class)->findBy(['zip_code' => $insee_code]);
|
|
|
|
// Récupérer les stats existantes pour la zone
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
if (!$stats) {
|
|
$stats = new Stats();
|
|
$stats->setZone($insee_code);
|
|
}
|
|
|
|
$urls = $stats->getAllCTCUrlsMap();
|
|
|
|
$statsHistory = $this->entityManager->getRepository(StatsHistory::class)
|
|
->createQueryBuilder('sh')
|
|
->where('sh.stats = :stats')
|
|
->setParameter('stats', $stats)
|
|
->orderBy('sh.id', 'DESC')
|
|
->setMaxResults(365)
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
// Calculer les statistiques
|
|
$calculatedStats = $this->motocultrice->calculateStats($commerces);
|
|
|
|
// Mettre à jour les stats pour la zone donnée
|
|
$stats->setPlacesCount($calculatedStats['places_count']);
|
|
$stats->setAvecHoraires($calculatedStats['counters']['avec_horaires']);
|
|
$stats->setAvecAdresse($calculatedStats['counters']['avec_adresse']);
|
|
$stats->setAvecSite($calculatedStats['counters']['avec_site']);
|
|
$stats->setAvecAccessibilite($calculatedStats['counters']['avec_accessibilite']);
|
|
$stats->setAvecNote($calculatedStats['counters']['avec_note']);
|
|
|
|
$stats->setCompletionPercent($calculatedStats['completion_percent']);
|
|
|
|
// Associer les stats à chaque commerce
|
|
foreach ($commerces as $commerce) {
|
|
$commerce->setStats($stats);
|
|
$this->entityManager->persist($commerce);
|
|
}
|
|
|
|
$stats->computeCompletionPercent();
|
|
|
|
// Calculer les statistiques de fraîcheur des données OSM
|
|
$timestamps = [];
|
|
foreach ($stats->getPlaces() as $place) {
|
|
if ($place->getOsmDataDate()) {
|
|
$timestamps[] = $place->getOsmDataDate()->getTimestamp();
|
|
}
|
|
}
|
|
|
|
if (!empty($timestamps)) {
|
|
// Date la plus ancienne (min)
|
|
$minTimestamp = min($timestamps);
|
|
$stats->setOsmDataDateMin(new \DateTime('@' . $minTimestamp));
|
|
|
|
// Date la plus récente (max)
|
|
$maxTimestamp = max($timestamps);
|
|
$stats->setOsmDataDateMax(new \DateTime('@' . $maxTimestamp));
|
|
|
|
// Date moyenne
|
|
$avgTimestamp = array_sum($timestamps) / count($timestamps);
|
|
$stats->setOsmDataDateAvg(new \DateTime('@' . (int)$avgTimestamp));
|
|
}
|
|
|
|
if ($stats->getDateCreated() == null) {
|
|
$stats->setDateCreated(new \DateTime());
|
|
}
|
|
|
|
$stats->setDateModified(new \DateTime());
|
|
|
|
// Créer un historique des statistiques
|
|
$statsHistory = new StatsHistory();
|
|
$statsHistory->setDate(new \DateTime())
|
|
->setStats($stats);
|
|
|
|
// Compter les Places avec email et SIRET
|
|
$placesWithEmail = 0;
|
|
$placesWithSiret = 0;
|
|
foreach ($stats->getPlaces() as $place) {
|
|
if ($place->getEmail() && $place->getEmail() !== '') {
|
|
$placesWithEmail++;
|
|
}
|
|
if ($place->getSiret() && $place->getSiret() !== '') {
|
|
$placesWithSiret++;
|
|
}
|
|
}
|
|
|
|
$statsHistory->setPlacesCount($stats->getPlaces()->count())
|
|
->setOpeningHoursCount($stats->getAvecHoraires())
|
|
->setAddressCount($stats->getAvecAdresse())
|
|
->setWebsiteCount($stats->getAvecSite())
|
|
->setSiretCount($placesWithSiret)
|
|
->setEmailsCount($placesWithEmail)
|
|
->setCompletionPercent($stats->getCompletionPercent())
|
|
->setStats($stats);
|
|
|
|
$this->entityManager->persist($statsHistory);
|
|
|
|
|
|
$this->entityManager->persist($stats);
|
|
$this->entityManager->flush();
|
|
|
|
// Générer les contenus d'email après le flush pour éviter les problèmes de mémoire
|
|
$placesToUpdate = $this->entityManager->getRepository(Place::class)->findBy(['zip_code' => $insee_code]);
|
|
foreach ($placesToUpdate as $place) {
|
|
if (!$place->getEmailContent()) {
|
|
$emailContent = $this->twig->render('admin/email_content.html.twig', ['place' => $place]);
|
|
$place->setEmailContent($emailContent);
|
|
$this->entityManager->persist($place);
|
|
}
|
|
}
|
|
$this->entityManager->flush();
|
|
|
|
// Générer les suivis (followups) après la mise à jour des Places
|
|
$this->followUpService->generateCityFollowUps($stats, $this->motocultrice, $this->entityManager);
|
|
|
|
$message = 'Labourage terminé avec succès. ' . $processedCount . ' nouveaux lieux traités.';
|
|
if ($updateExisting) {
|
|
$message .= ' ' . $updatedCount . ' lieux existants mis à jour pour la zone ' . $stats->getName() . ' (' . $stats->getZone() . ').';
|
|
}
|
|
$this->addFlash('success', $message);
|
|
}
|
|
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Labourage des ' . count($stats_all) . ' zones terminé avec succès.');
|
|
return $this->redirectToRoute('app_public_dashboard');
|
|
}
|
|
|
|
#[Route('/admin', name: 'app_admin')]
|
|
public function index(): Response
|
|
{
|
|
return $this->render('admin/index.html.twig', [
|
|
'controller_name' => 'AdminController',
|
|
]);
|
|
}
|
|
|
|
#[Route('/admin/stats/{insee_code}', name: 'app_admin_stats', requirements: ['insee_code' => '\\d+'])]
|
|
public function stats(string $insee_code): Response
|
|
{
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
if (!$stats) {
|
|
$this->addFlash('error', 'Aucune stats trouvée pour ce code INSEE.');
|
|
return $this->redirectToRoute('app_admin');
|
|
}
|
|
$followups = $stats->getCityFollowUps();
|
|
$refresh = false;
|
|
if (!$followups->isEmpty()) {
|
|
$latest = null;
|
|
foreach ($followups as $fu) {
|
|
if ($latest === null || $fu->getDate() > $latest->getDate()) {
|
|
$latest = $fu;
|
|
}
|
|
}
|
|
if ($latest && $latest->getDate() < (new \DateTime('-1 day'))) {
|
|
$refresh = true;
|
|
}
|
|
} else {
|
|
$refresh = true;
|
|
}
|
|
if ($refresh) {
|
|
$this->followUpService->generateCityFollowUps($stats, $this->motocultrice, $this->entityManager);
|
|
$followups = $stats->getCityFollowUps();
|
|
}
|
|
$commerces = $stats->getPlaces();
|
|
$this->actionLogger->log('stats_de_ville', ['insee_code' => $insee_code, 'nom' => $stats->getZone()]);
|
|
// Récupérer tous les commerces de la zone
|
|
// $commerces = $this->entityManager->getRepository(Place::class)->findBy(['zip_code' => $insee_code, 'dead' => false]);
|
|
if (!$stats) {
|
|
// Si aucune stat n'existe, on en crée une vide pour éviter les erreurs, mais sans la sauvegarder
|
|
$stats = new Stats();
|
|
$stats->setZone($insee_code);
|
|
$stats->setName('Nouvelle zone non labourée');
|
|
}
|
|
|
|
$urls = $stats->getAllCTCUrlsMap();
|
|
$statsHistory = $this->entityManager->getRepository(StatsHistory::class)
|
|
->createQueryBuilder('sh')
|
|
->where('sh.stats = :stats')
|
|
->setParameter('stats', $stats)
|
|
->orderBy('sh.id', 'DESC')
|
|
->setMaxResults(100)
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
// Données pour le graphique des modifications par trimestre
|
|
$modificationsByQuarter = [];
|
|
foreach ($commerces as $commerce) {
|
|
if ($commerce->getOsmDataDate()) {
|
|
$date = $commerce->getOsmDataDate();
|
|
$year = $date->format('Y');
|
|
$quarter = ceil($date->format('n') / 3);
|
|
$key = $year . '-Q' . $quarter;
|
|
if (!isset($modificationsByQuarter[$key])) {
|
|
$modificationsByQuarter[$key] = 0;
|
|
}
|
|
$modificationsByQuarter[$key]++;
|
|
}
|
|
}
|
|
ksort($modificationsByQuarter); // Trier par clé (année-trimestre)
|
|
|
|
$geojson = [
|
|
'type' => 'FeatureCollection',
|
|
'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()
|
|
]
|
|
];
|
|
}
|
|
}
|
|
|
|
// Générer le podium local des contributeurs OSM pour cette ville
|
|
$placeRepo = $this->entityManager->getRepository(\App\Entity\Place::class);
|
|
$qb = $placeRepo->createQueryBuilder('p')
|
|
->select(
|
|
'p.osm_user',
|
|
'COUNT(p.id) as nb',
|
|
'AVG((CASE WHEN p.has_opening_hours = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_address = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_website = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_wheelchair = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_note = true THEN 1 ELSE 0 END)) / 5 * 100 as completion_moyen'
|
|
)
|
|
->where('p.osm_user IS NOT NULL')
|
|
->andWhere("p.osm_user != ''")
|
|
->andWhere('p.stats = :stats')
|
|
->setParameter('stats', $stats)
|
|
->groupBy('p.osm_user')
|
|
->orderBy('nb', 'DESC')
|
|
->setMaxResults(100);
|
|
$podium_local = $qb->getQuery()->getResult();
|
|
// Calcul du score pondéré et normalisation locale
|
|
$maxPondere = 0;
|
|
foreach ($podium_local as &$row) {
|
|
$row['completion_moyen'] = $row['completion_moyen'] !== null ? round($row['completion_moyen'], 1) : null;
|
|
$row['completion_pondere'] = ($row['completion_moyen'] !== null && $row['nb'] > 0)
|
|
? round($row['completion_moyen'] * $row['nb'], 1)
|
|
: null;
|
|
if ($row['completion_pondere'] !== null && $row['completion_pondere'] > $maxPondere) {
|
|
$maxPondere = $row['completion_pondere'];
|
|
}
|
|
}
|
|
unset($row);
|
|
|
|
// Normalisation des scores pondérés entre 0 et 100
|
|
foreach ($podium_local as &$row) {
|
|
if ($maxPondere > 0 && $row['completion_pondere'] !== null) {
|
|
$row['completion_pondere_normalisee'] = round($row['completion_pondere'] / $maxPondere * 100, 1);
|
|
} else {
|
|
$row['completion_pondere_normalisee'] = null;
|
|
}
|
|
}
|
|
unset($row);
|
|
|
|
// Tri décroissant sur le score normalisé
|
|
usort($podium_local, function ($a, $b) {
|
|
return ($b['completion_pondere_normalisee'] ?? 0) <=> ($a['completion_pondere_normalisee'] ?? 0);
|
|
});
|
|
|
|
// Récupérer les derniers followups pour chaque type
|
|
$latestFollowups = [];
|
|
$types = array_keys(\App\Service\FollowUpService::getFollowUpThemes());
|
|
foreach ($types as $type) {
|
|
$count = null;
|
|
$completion = null;
|
|
foreach ($stats->getCityFollowUps() as $fu) {
|
|
if ($fu->getName() === $type . '_count') {
|
|
if ($count === null) {
|
|
$count = $fu;
|
|
} else if ($fu->getDate() > $count->getDate()) {
|
|
$count = $fu;
|
|
}
|
|
}
|
|
if ($fu->getName() === $type . '_completion') {
|
|
if ($completion === null) {
|
|
$completion = $fu;
|
|
} else if ($fu->getDate() > $completion->getDate()) {
|
|
$completion = $fu;
|
|
}
|
|
}
|
|
}
|
|
$latestFollowups[$type] = [];
|
|
if ($count) $latestFollowups[$type]['count'] = $count;
|
|
if ($completion) $latestFollowups[$type]['completion'] = $completion;
|
|
}
|
|
// Pour les lieux (places_count et places_completion)
|
|
$count = null;
|
|
$completion = null;
|
|
foreach ($stats->getCityFollowUps() as $fu) {
|
|
if ($fu->getName() === 'places_count') {
|
|
if ($count === null) {
|
|
$count = $fu;
|
|
} else if ($fu->getDate() > $count->getDate()) {
|
|
$count = $fu;
|
|
}
|
|
}
|
|
if ($fu->getName() === 'places_completion') {
|
|
if ($completion === null) {
|
|
$completion = $fu;
|
|
} else if ($fu->getDate() > $completion->getDate()) {
|
|
$completion = $fu;
|
|
}
|
|
}
|
|
}
|
|
$latestFollowups['places'] = [];
|
|
if ($count) $latestFollowups['places']['count'] = $count;
|
|
if ($completion) $latestFollowups['places']['completion'] = $completion;
|
|
|
|
return $this->render('admin/stats.html.twig', [
|
|
'stats' => $stats,
|
|
'commerces' => $commerces,
|
|
'urls' => $urls,
|
|
'geojson' => json_encode($geojson),
|
|
'modificationsByQuarter' => json_encode($modificationsByQuarter),
|
|
'maptiler_token' => $_ENV['MAPTILER_TOKEN'],
|
|
'statsHistory' => $statsHistory,
|
|
'CTC_urls' => $urls,
|
|
'overpass' => '',
|
|
'podium_local' => $podium_local,
|
|
'latestFollowups' => $latestFollowups,
|
|
'followup_labels' => \App\Service\FollowUpService::getFollowUpThemes(),
|
|
'followup_icons' => \App\Service\FollowUpService::getFollowUpIcons(),
|
|
]);
|
|
}
|
|
|
|
#[Route('/admin/placeType/{osm_kind}/{osm_id}', name: 'app_admin_by_osm_id')]
|
|
public function placeType(string $osm_kind, string $osm_id): Response
|
|
{
|
|
|
|
$place = $this->entityManager->getRepository(Place::class)->findOneBy(['osm_kind' => $osm_kind, 'osmId' => $osm_id]);
|
|
if ($place) {
|
|
$this->actionLogger->log('admin/placeType', ['osm_kind' => $osm_kind, 'osm_id' => $osm_id,
|
|
'name' => $place->getName(),
|
|
'code_insee' => $place->getZipCode(),
|
|
'uuid' => $place->getUuidForUrl()
|
|
]);
|
|
return $this->redirectToRoute('app_admin_commerce', ['id' => $place->getId()]);
|
|
} else {
|
|
$this->actionLogger->log('ERROR_admin/placeType', ['osm_kind' => $osm_kind, 'osm_id' => $osm_id,
|
|
'name' => $place->getName(),
|
|
'code_insee' => $place->getZipCode(),
|
|
'uuid' => $place->getUuidForUrl()
|
|
]);
|
|
$this->addFlash('error', 'Le lieu n\'existe pas.');
|
|
$this->actionLogger->log('ERROR_admin/placeType', ['osm_kind' => $osm_kind, 'osm_id' => $osm_id]);
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* rediriger vers l'url unique quand on est admin
|
|
*/
|
|
#[Route('/admin/commerce/{id}', name: 'app_admin_commerce')]
|
|
public function commerce(int $id): Response
|
|
{
|
|
|
|
|
|
// Vérifier si on est en prod
|
|
if ($this->getParameter('kernel.environment') === 'prod') {
|
|
$this->addFlash('error', 'Vous n\'avez pas accès à cette page en production.');
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
$commerce = $this->entityManager->getRepository(Place::class)->find($id);
|
|
|
|
if (!$commerce) {
|
|
throw $this->createNotFoundException('Commerce non trouvé');
|
|
$this->actionLogger->log('ERROR_admin_show_commerce_form_id', ['id' => $id]);
|
|
}
|
|
$this->actionLogger->log('admin_show_commerce_form_id', [
|
|
'id' => $id,
|
|
'name' => $commerce->getName(),
|
|
'code_insee' => $commerce->getZipCode(),
|
|
'uuid' => $commerce->getUuidForUrl()
|
|
]);
|
|
// Redirection vers la page de modification avec les paramètres nécessaires
|
|
return $this->redirectToRoute('app_public_edit', [
|
|
'zipcode' => $commerce->getZipCode(),
|
|
'name' => $commerce->getName() != '' ? $commerce->getName() : '?',
|
|
'uuid' => $commerce->getUuidForUrl()
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* récupérer les commerces de la zone selon le code INSEE, créer les nouveaux lieux, et mettre à jour les existants
|
|
*/
|
|
#[Route('/admin/labourer/{insee_code}', name: 'app_admin_labourer')]
|
|
public function labourer(Request $request, string $insee_code, bool $updateExisting = true): Response
|
|
{
|
|
$deleteMissing = $request->query->getBoolean('deleteMissing', true);
|
|
|
|
$this->actionLogger->log('labourer', ['insee_code' => $insee_code]);
|
|
|
|
// Vérifier si le code INSEE est valide (composé uniquement de chiffres)
|
|
if (!ctype_digit($insee_code) || $insee_code == 'undefined' || $insee_code == '') {
|
|
$this->addFlash('error', 'Code INSEE invalide : il doit être composé uniquement de chiffres.');
|
|
$this->actionLogger->log('ERROR_labourer_bad_insee', ['insee_code' => $insee_code]);
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
try {
|
|
// Récupérer ou créer les stats pour cette zone
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
|
|
$city = $this->motocultrice->get_city_osm_from_zip_code($insee_code);
|
|
if (!$stats) {
|
|
$stats = new Stats();
|
|
$stats->setDateCreated(new \DateTime());
|
|
$stats->setDateModified(new \DateTime());
|
|
$stats->setZone($insee_code)
|
|
->setPlacesCount(0)
|
|
->setAvecHoraires(0)
|
|
->setAvecAdresse(0)
|
|
->setAvecSite(0)
|
|
->setAvecAccessibilite(0)
|
|
->setAvecNote(0)
|
|
->setCompletionPercent(0);
|
|
$this->entityManager->persist($stats);
|
|
$this->entityManager->flush();
|
|
}
|
|
$stats->setName($city);
|
|
|
|
// Récupérer la population via l'API
|
|
$population = null;
|
|
try {
|
|
$apiUrl = 'https://geo.api.gouv.fr/communes/' . $insee_code;
|
|
$response = file_get_contents($apiUrl);
|
|
if ($response !== false) {
|
|
$data = json_decode($response, true);
|
|
if (isset($data['population'])) {
|
|
$population = (int)$data['population'];
|
|
$stats->setPopulation($population);
|
|
}
|
|
if (isset($data['siren'])) {
|
|
$stats->setSiren((int)$data['siren']);
|
|
}
|
|
if (isset($data['codeEpci'])) {
|
|
$stats->setCodeEpci((int)$data['codeEpci']);
|
|
}
|
|
if (isset($data['codesPostaux'])) {
|
|
$stats->setCodesPostaux(implode(';', $data['codesPostaux']));
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->addFlash('error', 'Erreur lors de la récupération des données de l\'API : ' . $e->getMessage());
|
|
|
|
$this->actionLogger->log('ERROR_labourer_geoapi', ['insee_code' => $insee_code, 'message' => $e->getMessage()]);
|
|
}
|
|
|
|
// Récupérer le budget annuel via l'API des finances publiques
|
|
try {
|
|
$budgetAnnuel = $this->budgetService->getBudgetAnnuel($insee_code);
|
|
if ($budgetAnnuel !== null) {
|
|
$stats->setBudgetAnnuel((string) $budgetAnnuel);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Pas de message d'erreur pour le budget, c'est optionnel
|
|
}
|
|
|
|
// OPTIMISATION : Récupérer seulement les osmId et osmKind des lieux existants pour économiser la mémoire
|
|
$existingPlacesQuery = $this->entityManager->getRepository(Place::class)
|
|
->createQueryBuilder('p')
|
|
->select('p.osmId, p.osm_kind, p.id')
|
|
->where('p.zip_code = :zip_code')
|
|
->setParameter('zip_code', $insee_code)
|
|
->getQuery();
|
|
|
|
$existingPlacesResult = $existingPlacesQuery->getResult();
|
|
$placesByOsmKey = [];
|
|
|
|
foreach ($existingPlacesResult as $placeData) {
|
|
|
|
// var_dump($placeData);
|
|
// die( );
|
|
// Clé unique combinant osmId ET osmKind pour éviter les conflits entre node/way
|
|
$osmKey = $placeData['osm_kind'] . '_' . $placeData['osmId'];
|
|
$placesByOsmKey[$osmKey] = $placeData['id'];
|
|
}
|
|
|
|
// Récupérer toutes les données
|
|
$places_overpass = $this->motocultrice->labourer($insee_code);
|
|
$processedCount = 0;
|
|
$updatedCount = 0;
|
|
$deletedCount = 0;
|
|
|
|
$overpass_osm_ids = array_map(fn($place) => $place['id'], $places_overpass);
|
|
|
|
// RÉDUCTION de la taille du batch pour éviter l'explosion mémoire
|
|
$batchSize = 10000;
|
|
$i = 0;
|
|
$notFoundOsmKeys = [];
|
|
foreach ($places_overpass as $placeData) {
|
|
// Vérifier si le lieu existe déjà (optimisé) - utilise osmKind + osmId
|
|
$osmKey = $placeData['type'] . '_' . $placeData['id'];
|
|
$existingPlaceId = $placesByOsmKey[$osmKey] ?? null;
|
|
if (!$existingPlaceId) {
|
|
$place = new Place();
|
|
$place->setOsmId($placeData['id'])
|
|
->setOsmKind($placeData['type'])
|
|
->setZipCode($insee_code)
|
|
->setUuidForUrl($this->motocultrice->uuid_create())
|
|
->setModifiedDate(new \DateTime())
|
|
->setStats($stats)
|
|
->setDead(false)
|
|
->setOptedOut(false)
|
|
->setMainTag($this->motocultrice->find_main_tag($placeData['tags']) ?? '')
|
|
->setStreet($this->motocultrice->find_street($placeData['tags']) ?? '')
|
|
->setHousenumber($this->motocultrice->find_housenumber($placeData['tags']) ?? '')
|
|
->setSiret($this->motocultrice->find_siret($placeData['tags']) ?? '')
|
|
->setAskedHumainsSupport(false)
|
|
->setLastContactAttemptDate(null)
|
|
->setPlaceCount(0)
|
|
// ->setOsmData($placeData['modified'] ?? null)
|
|
;
|
|
$place->update_place_from_overpass_data($placeData);
|
|
$this->entityManager->persist($place);
|
|
$stats->addPlace($place);
|
|
$processedCount++;
|
|
|
|
// Log des objets non trouvés
|
|
$notFoundOsmKeys[] = $osmKey;
|
|
} elseif ($updateExisting) {
|
|
// Charger l'entité existante seulement si nécessaire
|
|
$existingPlace = $this->entityManager->getRepository(Place::class)->find($existingPlaceId);
|
|
if ($existingPlace) {
|
|
$existingPlace->setDead(false);
|
|
$existingPlace->update_place_from_overpass_data($placeData);
|
|
$stats->addPlace($existingPlace);
|
|
$this->entityManager->persist($existingPlace);
|
|
$updatedCount++;
|
|
}
|
|
}
|
|
$i++;
|
|
|
|
// FLUSH/CLEAR plus fréquent pour éviter l'explosion mémoire
|
|
if (($i % $batchSize) === 0) {
|
|
$this->entityManager->flush();
|
|
$this->entityManager->clear();
|
|
|
|
// Forcer le garbage collector
|
|
gc_collect_cycles();
|
|
|
|
// Recharger les stats après clear
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
}
|
|
}
|
|
|
|
$deleteMissing = 1;
|
|
// SUPPRESSION des lieux non corrélés par leur osm kind et osm id avec les données overpass
|
|
if ($deleteMissing) {
|
|
// Créer un ensemble des clés osm présentes dans les données overpass
|
|
$overpassOsmKeys = [];
|
|
foreach ($places_overpass as $placeData) {
|
|
$osmKey = $placeData['type'] . '_' . $placeData['id'];
|
|
$overpassOsmKeys[$osmKey] = true;
|
|
}
|
|
|
|
// ÉLIMINER LES DOUBLONS dans les lieux existants avant suppression
|
|
$uniquePlacesByOsmKey = [];
|
|
$duplicatePlaceIds = [];
|
|
|
|
foreach ($placesByOsmKey as $osmKey => $placeId) {
|
|
if (isset($uniquePlacesByOsmKey[$osmKey])) {
|
|
// Doublon détecté, garder le plus ancien (ID le plus petit)
|
|
if ($placeId < $uniquePlacesByOsmKey[$osmKey]) {
|
|
$duplicatePlaceIds[] = $uniquePlacesByOsmKey[$osmKey];
|
|
$uniquePlacesByOsmKey[$osmKey] = $placeId;
|
|
} else {
|
|
$duplicatePlaceIds[] = $placeId;
|
|
}
|
|
} else {
|
|
$uniquePlacesByOsmKey[$osmKey] = $placeId;
|
|
}
|
|
}
|
|
|
|
// Supprimer les doublons détectés
|
|
if (!empty($duplicatePlaceIds)) {
|
|
$duplicateDeleteQuery = $this->entityManager->createQuery(
|
|
'DELETE FROM App\Entity\Place p WHERE p.id IN (:placeIds)'
|
|
);
|
|
$duplicateDeleteQuery->setParameter('placeIds', $duplicatePlaceIds);
|
|
$duplicateDeletedCount = $duplicateDeleteQuery->execute();
|
|
}
|
|
|
|
// Trouver les lieux existants uniques qui ne sont plus dans overpass
|
|
$placesToDelete = [];
|
|
foreach ($uniquePlacesByOsmKey as $osmKey => $placeId) {
|
|
if (!isset($overpassOsmKeys[$osmKey])) {
|
|
$placesToDelete[] = $placeId;
|
|
}
|
|
}
|
|
|
|
// Supprimer les lieux non trouvés dans overpass en une seule requête
|
|
if (!empty($placesToDelete)) {
|
|
$deleteQuery = $this->entityManager->createQuery(
|
|
'DELETE FROM App\Entity\Place p WHERE p.id IN (:placeIds)'
|
|
);
|
|
$deleteQuery->setParameter('placeIds', $placesToDelete);
|
|
$deletedCount = $deleteQuery->execute();
|
|
}
|
|
}
|
|
|
|
// Flush final
|
|
$this->entityManager->flush();
|
|
$this->entityManager->clear();
|
|
|
|
// Générer les contenus d'email après le flush pour éviter les problèmes de mémoire
|
|
$placesToUpdate = $this->entityManager->getRepository(Place::class)->findBy(['zip_code' => $insee_code]);
|
|
foreach ($placesToUpdate as $place) {
|
|
if (!$place->getEmailContent()) {
|
|
$emailContent = $this->twig->render('admin/email_content.html.twig', ['place' => $place]);
|
|
$place->setEmailContent($emailContent);
|
|
$this->entityManager->persist($place);
|
|
}
|
|
}
|
|
$this->entityManager->flush();
|
|
|
|
// NETTOYAGE D'UNICITÉ des Places après le clear pour éliminer les doublons persistants
|
|
// Approche en deux étapes pour éviter l'erreur MySQL "target table for update in FROM clause"
|
|
|
|
// Étape 1 : Identifier les doublons
|
|
$duplicateIdsQuery = $this->entityManager->createQuery(
|
|
'SELECT p.id FROM App\Entity\Place p
|
|
WHERE p.id NOT IN (
|
|
SELECT MIN(p2.id)
|
|
FROM App\Entity\Place p2
|
|
GROUP BY p2.osmId, p2.osm_kind, p2.zip_code
|
|
)'
|
|
);
|
|
$duplicateIds = $duplicateIdsQuery->getResult();
|
|
|
|
// Étape 2 : Supprimer les doublons identifiés
|
|
if (!empty($duplicateIds)) {
|
|
$duplicateIds = array_column($duplicateIds, 'id');
|
|
$duplicateCleanupQuery = $this->entityManager->createQuery(
|
|
'DELETE App\Entity\Place p WHERE p.id IN (:duplicateIds)'
|
|
);
|
|
$duplicateCleanupQuery->setParameter('duplicateIds', $duplicateIds);
|
|
$duplicateCleanupCount = $duplicateCleanupQuery->execute();
|
|
} else {
|
|
$duplicateCleanupCount = 0;
|
|
}
|
|
|
|
// Récupérer tous les commerces de la zone qui n'ont pas été supprimés
|
|
$commerces = $this->entityManager->getRepository(Place::class)->findBy(['zip_code' => $insee_code]);
|
|
|
|
// Récupérer les stats existantes pour la zone
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
if (!$stats) {
|
|
$stats = new Stats();
|
|
$stats->setZone($insee_code);
|
|
}
|
|
|
|
$urls = $stats->getAllCTCUrlsMap();
|
|
|
|
$statsHistory = $this->entityManager->getRepository(StatsHistory::class)
|
|
->createQueryBuilder('sh')
|
|
->where('sh.stats = :stats')
|
|
->setParameter('stats', $stats)
|
|
->orderBy('sh.id', 'DESC')
|
|
->setMaxResults(365)
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
// Calculer les statistiques
|
|
$calculatedStats = $this->motocultrice->calculateStats($commerces);
|
|
|
|
// Mettre à jour les stats pour la zone donnée
|
|
$stats->setPlacesCount($calculatedStats['places_count']);
|
|
$stats->setAvecHoraires($calculatedStats['counters']['avec_horaires']);
|
|
$stats->setAvecAdresse($calculatedStats['counters']['avec_adresse']);
|
|
$stats->setAvecSite($calculatedStats['counters']['avec_site']);
|
|
$stats->setAvecAccessibilite($calculatedStats['counters']['avec_accessibilite']);
|
|
$stats->setAvecNote($calculatedStats['counters']['avec_note']);
|
|
$stats->setCompletionPercent($calculatedStats['completion_percent']);
|
|
|
|
// Associer les stats à chaque commerce
|
|
foreach ($commerces as $commerce) {
|
|
$commerce->setStats($stats);
|
|
$this->entityManager->persist($commerce);
|
|
}
|
|
|
|
$stats->computeCompletionPercent();
|
|
|
|
// Calculer les statistiques de fraîcheur des données OSM
|
|
$timestamps = [];
|
|
foreach ($stats->getPlaces() as $place) {
|
|
if ($place->getOsmDataDate()) {
|
|
$timestamps[] = $place->getOsmDataDate()->getTimestamp();
|
|
}
|
|
}
|
|
|
|
if (!empty($timestamps)) {
|
|
// Date la plus ancienne (min)
|
|
$minTimestamp = min($timestamps);
|
|
$stats->setOsmDataDateMin(new \DateTime('@' . $minTimestamp));
|
|
|
|
// Date la plus récente (max)
|
|
$maxTimestamp = max($timestamps);
|
|
$stats->setOsmDataDateMax(new \DateTime('@' . $maxTimestamp));
|
|
|
|
// Date moyenne
|
|
$avgTimestamp = array_sum($timestamps) / count($timestamps);
|
|
$stats->setOsmDataDateAvg(new \DateTime('@' . (int)$avgTimestamp));
|
|
}
|
|
|
|
if ($stats->getDateCreated() == null) {
|
|
$stats->setDateCreated(new \DateTime());
|
|
}
|
|
|
|
$stats->setDateModified(new \DateTime());
|
|
|
|
// Créer un historique des statistiques
|
|
$statsHistory = new StatsHistory();
|
|
$statsHistory->setDate(new \DateTime())
|
|
->setStats($stats);
|
|
|
|
// Compter les Places avec email et SIRET
|
|
$placesWithEmail = 0;
|
|
$placesWithSiret = 0;
|
|
$placesWithName = 0;
|
|
|
|
foreach ($stats->getPlaces() as $place) {
|
|
if ($place->getEmail() && $place->getEmail() !== '') {
|
|
$placesWithEmail++;
|
|
}
|
|
if ($place->getSiret() && $place->getSiret() !== '') {
|
|
$placesWithSiret++;
|
|
}
|
|
if ($place->getName() && $place->getName() !== '') {
|
|
$placesWithName++;
|
|
}
|
|
}
|
|
|
|
$statsHistory->setPlacesCount($stats->getPlaces()->count())
|
|
->setOpeningHoursCount($stats->getAvecHoraires())
|
|
->setAddressCount($stats->getAvecAdresse())
|
|
->setWebsiteCount($stats->getAvecSite())
|
|
->setNamesCount($placesWithName)
|
|
->setSiretCount($placesWithSiret)
|
|
->setEmailsCount($placesWithEmail)
|
|
->setCompletionPercent($stats->getCompletionPercent())
|
|
->setStats($stats);
|
|
|
|
$this->entityManager->persist($statsHistory);
|
|
|
|
|
|
$this->entityManager->persist($stats);
|
|
$this->entityManager->flush();
|
|
|
|
// Générer les suivis (followups) après la mise à jour des Places
|
|
$this->followUpService->generateCityFollowUps($stats, $this->motocultrice, $this->entityManager);
|
|
|
|
$message = 'Labourage terminé avec succès. ' . $processedCount . ' nouveaux lieux traités.';
|
|
if ($updateExisting) {
|
|
$message .= ' ' . $updatedCount . ' lieux existants mis à jour.';
|
|
}
|
|
if ($deletedCount > 0) {
|
|
$message .= ' ' . $deletedCount . ' lieux ont été supprimés.';
|
|
}
|
|
$message .= ' Zone : ' . $stats->getName() . ' (' . $stats->getZone() . ').';
|
|
$this->addFlash('success', $message);
|
|
|
|
// Afficher le log des objets non trouvés à la fin
|
|
// if (!empty($notFoundOsmKeys)) {
|
|
// $this->addFlash('info', count($notFoundOsmKeys).' objets OSM non trouvés lors du labourage.');
|
|
// }
|
|
// Rediriger dans tous les cas vers la page de stats de la ville
|
|
return $this->redirectToRoute('app_admin_stats', ['insee_code' => $insee_code]);
|
|
} catch (\Exception $e) {
|
|
$this->addFlash('error', 'Erreur lors du labourage : ' . $e->getMessage());
|
|
die(var_dump($e));
|
|
}
|
|
|
|
// return $this->redirectToRoute('app_public_dashboard');
|
|
return $this->redirectToRoute('app_admin_stats', ['insee_code' => $insee_code]);
|
|
}
|
|
|
|
#[Route('/admin/delete/{id}', name: 'app_admin_delete')]
|
|
public function delete(int $id): Response
|
|
{
|
|
$this->actionLogger->log('admin/delete_place', ['id' => $id]);
|
|
$commerce = $this->entityManager->getRepository(Place::class)->find($id);
|
|
if ($commerce) {
|
|
$this->entityManager->remove($commerce);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Le lieu ' . $commerce->getName() . ' a été supprimé avec succès de OSM Mes commerces, mais pas dans OpenStreetMap.');
|
|
} else {
|
|
$this->addFlash('error', 'Le lieu n\'existe pas.');
|
|
}
|
|
|
|
return $this->redirectToRoute('app_public_dashboard');
|
|
}
|
|
|
|
#[Route('/admin/delete_by_zone/{insee_code}', name: 'app_admin_delete_by_zone')]
|
|
public function delete_by_zone(string $insee_code): Response
|
|
{
|
|
$this->actionLogger->log('admin/delete_by_zone', ['insee_code' => $insee_code]);
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
|
|
if (!$stats) {
|
|
$this->addFlash('error', 'Aucune statistique trouvée pour la zone ' . $insee_code);
|
|
return $this->redirectToRoute('app_public_dashboard');
|
|
}
|
|
|
|
try {
|
|
// 1. Supprimer tous les StatsHistory associés
|
|
foreach ($stats->getStatsHistories() as $history) {
|
|
$this->entityManager->remove($history);
|
|
}
|
|
|
|
// 2. Supprimer tous les Places associées
|
|
foreach ($stats->getPlaces() as $place) {
|
|
$this->entityManager->remove($place);
|
|
}
|
|
|
|
// 3. Supprimer l'objet Stats lui-même
|
|
$this->entityManager->remove($stats);
|
|
|
|
// 4. Appliquer les changements à la base de données
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'La zone ' . $insee_code . ' et toutes les données associées ont été supprimées avec succès.');
|
|
} catch (\Exception $e) {
|
|
$this->addFlash('error', 'Une erreur est survenue lors de la suppression de la zone ' . $insee_code . ': ' . $e->getMessage());
|
|
}
|
|
|
|
return $this->redirectToRoute('app_public_dashboard');
|
|
}
|
|
|
|
|
|
#[Route('/admin/export', name: 'app_admin_export')]
|
|
public function export(): Response
|
|
{
|
|
$this->actionLogger->log('export_all_places', []);
|
|
$places = $this->entityManager->getRepository(Place::class)->findAll();
|
|
|
|
$csvData = [];
|
|
$csvData[] = [
|
|
'Nom',
|
|
'Email',
|
|
'Code postal',
|
|
'ID OSM',
|
|
'Type OSM',
|
|
'Date de modification',
|
|
'Date dernier contact',
|
|
'Note',
|
|
'Désabonné',
|
|
'Inactif',
|
|
'Support humain demandé',
|
|
'A des horaires',
|
|
'A une adresse',
|
|
'A un site web',
|
|
'A accessibilité',
|
|
'A une note'
|
|
];
|
|
|
|
foreach ($places as $place) {
|
|
$csvData[] = [
|
|
$place->getName(),
|
|
$place->getEmail(),
|
|
$place->getZipCode(),
|
|
$place->getOsmId(),
|
|
$place->getOsmKind(),
|
|
$place->getModifiedDate() ? $place->getModifiedDate()->format('Y-m-d H:i:s') : '',
|
|
$place->getLastContactAttemptDate() ? $place->getLastContactAttemptDate()->format('Y-m-d H:i:s') : '',
|
|
$place->getNote(),
|
|
$place->isOptedOut() ? 'Oui' : 'Non',
|
|
$place->isDead() ? 'Oui' : 'Non',
|
|
$place->isAskedHumainsSupport() ? 'Oui' : 'Non',
|
|
$place->hasOpeningHours() ? 'Oui' : 'Non',
|
|
$place->hasAddress() ? 'Oui' : 'Non',
|
|
$place->hasWebsite() ? 'Oui' : 'Non',
|
|
$place->hasWheelchair() ? 'Oui' : 'Non',
|
|
$place->hasNote() ? 'Oui' : 'Non'
|
|
];
|
|
}
|
|
|
|
$response = new Response();
|
|
$response->headers->set('Content-Type', 'text/csv');
|
|
$response->headers->set('Content-Disposition', 'attachment; filename="export_places.csv"');
|
|
|
|
$handle = fopen('php://temp', 'r+');
|
|
foreach ($csvData as $row) {
|
|
fputcsv($handle, $row, ';');
|
|
}
|
|
rewind($handle);
|
|
$response->setContent(stream_get_contents($handle));
|
|
fclose($handle);
|
|
|
|
|
|
|
|
return $response;
|
|
}
|
|
#[Route('/admin/export_csv/{insee_code}', name: 'app_admin_export_csv')]
|
|
public function export_csv(string $insee_code): Response
|
|
{
|
|
$this->actionLogger->log('admin/export_csv', ['insee_code' => $insee_code]);
|
|
|
|
$stats = $this->entityManager->getRepository(Stats::class)->findOneBy(['zone' => $insee_code]);
|
|
$response = new Response($this->motocultrice->export($insee_code));
|
|
$response->headers->set('Content-Type', 'text/csv');
|
|
|
|
$slug_name = str_replace(' ', '-', $stats->getName());
|
|
|
|
$this->actionLogger->log('export_csv', ['insee_code' => $insee_code, 'slug_name' => $slug_name]);
|
|
|
|
$response->headers->set('Content-Disposition', 'attachment; filename="osm-commerces-export_' . $insee_code . '_' . $slug_name . '_' . date('Y-m-d_H-i-s') . '.csv"');
|
|
|
|
return $response;
|
|
}
|
|
|
|
#[Route('/admin/make_email_for_place/{id}', name: 'app_admin_make_email_for_place')]
|
|
public function make_email_for_place(Place $place): Response
|
|
{
|
|
$this->actionLogger->log('admin/make_email_for_place', ['insee_code' => $place->getId()]);
|
|
|
|
return $this->render('admin/view_email_for_place.html.twig', ['place' => $place]);
|
|
}
|
|
|
|
#[Route('/admin/no_more_sollicitation_for_place/{id}', name: 'app_admin_no_more_sollicitation_for_place')]
|
|
public function no_more_sollicitation_for_place(Place $place): Response
|
|
{
|
|
$this->actionLogger->log('no_more_sollicitation_for_place', ['place_id' => $place->getId()]);
|
|
|
|
$place->setOptedOut(true);
|
|
$this->entityManager->persist($place);
|
|
$this->entityManager->flush();
|
|
|
|
$this->addFlash('success', 'Votre lieu ' . $place->getName() . ' ne sera plus sollicité pour mettre à jour ses informations.');
|
|
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
|
|
#[Route('/admin/send_email_to_place/{id}', name: 'app_admin_send_email_to_place')]
|
|
public function send_email_to_place(Place $place, \Symfony\Component\Mailer\MailerInterface $mailer): Response
|
|
{
|
|
$this->actionLogger->log('send_email_to_place', ['place_id' => $place->getId()]);
|
|
|
|
|
|
// Vérifier si le lieu est opted out
|
|
if ($place->isOptedOut()) {
|
|
$this->addFlash('error', 'Ce lieu a demandé à ne plus être sollicité pour mettre à jour ses informations.');
|
|
$this->actionLogger->log('could_not_send_email_to_opted_out_place', ['place_id' => $place->getId()]);
|
|
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
// Vérifier si le lieu a déjà été contacté
|
|
if ($place->getLastContactAttemptDate() !== null) {
|
|
$this->addFlash('error', 'Ce lieu a déjà été contacté le ' . $place->getLastContactAttemptDate()->format('d/m/Y H:i:s'));
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
|
|
// Générer le contenu de l'email avec le template
|
|
$emailContent = $this->renderView('admin/email_content.html.twig', [
|
|
'place' => $place
|
|
]);
|
|
|
|
// Envoyer l'email
|
|
$email = (new \Symfony\Component\Mime\Email())
|
|
->from('contact@openstreetmap.fr')
|
|
->to('contact+send_email@cipherbliss.com')
|
|
->subject('Mise à jour des informations de votre établissement dans OpenStreetMap')
|
|
->html($emailContent);
|
|
|
|
try {
|
|
$mailer->send($email);
|
|
} catch (\Throwable $e) {
|
|
$this->actionLogger->log('ERROR_envoi_email', [
|
|
'place_id' => $place->getId(),
|
|
'message' => $e->getMessage(),
|
|
]);
|
|
$this->addFlash('error', 'Erreur lors de l\'envoi de l\'email : ' . $e->getMessage());
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
|
|
// Mettre à jour la date de dernier contact
|
|
$place->setLastContactAttemptDate(new \DateTime());
|
|
$this->entityManager->persist($place);
|
|
$this->entityManager->flush();
|
|
|
|
$place->setLastContactAttemptDate(new \DateTime());
|
|
|
|
$this->addFlash('success', 'Email envoyé avec succès à ' . $place->getName() . ' le ' . $place->getLastContactAttemptDate()->format('d/m/Y H:i:s'));
|
|
return $this->redirectToRoute('app_public_index');
|
|
}
|
|
|
|
#[Route('/admin/fraicheur/histogramme', name: 'admin_fraicheur_histogramme')]
|
|
public function showFraicheurHistogramme(): Response
|
|
{
|
|
$jsonPath = $this->getParameter('kernel.project_dir') . '/var/fraicheur_osm.json';
|
|
if (!file_exists($jsonPath)) {
|
|
// Générer le fichier si absent
|
|
$this->calculateFraicheur();
|
|
}
|
|
return $this->render('admin/fraicheur_histogramme.html.twig');
|
|
}
|
|
|
|
#[Route('/admin/fraicheur/calculate', name: 'admin_fraicheur_calculate')]
|
|
public function calculateFraicheur(): Response
|
|
{
|
|
// Ajout d'un log d'action avec le service ActionLogger
|
|
$this->actionLogger->log('fraicheur/calculate', []);
|
|
$filesystem = new Filesystem();
|
|
$jsonPath = $this->getParameter('kernel.project_dir') . '/var/fraicheur_osm.json';
|
|
$now = new \DateTime();
|
|
// Si le fichier existe et a moins de 12h, on ne régénère pas
|
|
if ($filesystem->exists($jsonPath)) {
|
|
$fileMTime = filemtime($jsonPath);
|
|
if ($fileMTime && ($now->getTimestamp() - $fileMTime) < 43200) { // 12h = 43200s
|
|
return $this->redirectToRoute('admin_fraicheur_histogramme');
|
|
}
|
|
}
|
|
$places = $this->entityManager->getRepository(Place::class)->findAll();
|
|
$histogram = [];
|
|
$total = 0;
|
|
foreach ($places as $place) {
|
|
$date = $place->getOsmDataDate();
|
|
if ($date) {
|
|
$key = $date->format('Y-m');
|
|
if (!isset($histogram[$key])) {
|
|
$histogram[$key] = 0;
|
|
}
|
|
$histogram[$key]++;
|
|
$total++;
|
|
}
|
|
}
|
|
ksort($histogram);
|
|
$data = [
|
|
'generated_at' => $now->format('c'),
|
|
'total' => $total,
|
|
'histogram' => $histogram
|
|
];
|
|
$filesystem->dumpFile($jsonPath, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
|
|
// --- Distribution villes selon lieux/habitants ---
|
|
$distJsonPath = $this->getParameter('kernel.project_dir') . '/var/distribution_villes_lieux_par_habitant.json';
|
|
// Toujours régénérer
|
|
$statsRepo = $this->entityManager->getRepository(Stats::class);
|
|
$allStats = $statsRepo->findAll();
|
|
$histogram_lieux_par_habitant = [];
|
|
$histogram_habitants_par_lieu = [];
|
|
$totalVilles = 0;
|
|
foreach ($allStats as $stat) {
|
|
$places = $stat->getPlacesCount();
|
|
$population = $stat->getPopulation();
|
|
if ($places && $population && $population > 0) {
|
|
// lieux par habitant (pas de 0.01)
|
|
$ratio_lph = round($places / $population, 4);
|
|
$bin_lph = round(floor($ratio_lph / 0.01) * 0.01, 2);
|
|
if (!isset($histogram_lieux_par_habitant[$bin_lph])) $histogram_lieux_par_habitant[$bin_lph] = 0;
|
|
$histogram_lieux_par_habitant[$bin_lph]++;
|
|
// habitants par lieu (pas de 10)
|
|
$ratio_hpl = ceil($population / $places);
|
|
$bin_hpl = ceil($ratio_hpl / 10) * 10;
|
|
if (!isset($histogram_habitants_par_lieu[$bin_hpl])) $histogram_habitants_par_lieu[$bin_hpl] = 0;
|
|
$histogram_habitants_par_lieu[$bin_hpl]++;
|
|
$totalVilles++;
|
|
}
|
|
}
|
|
ksort($histogram_lieux_par_habitant);
|
|
ksort($histogram_habitants_par_lieu);
|
|
$distData = [
|
|
'generated_at' => $now->format('c'),
|
|
'total_villes' => $totalVilles,
|
|
'histogram_001' => $histogram_lieux_par_habitant,
|
|
'histogram_10' => $histogram_habitants_par_lieu
|
|
];
|
|
$filesystem->dumpFile($distJsonPath, json_encode($distData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
|
|
return $this->redirectToRoute('admin_fraicheur_histogramme');
|
|
}
|
|
|
|
#[Route('/admin/fraicheur/download', name: 'admin_fraicheur_download')]
|
|
public function downloadFraicheur(): JsonResponse
|
|
{
|
|
$jsonPath = $this->getParameter('kernel.project_dir') . '/var/fraicheur_osm.json';
|
|
if (!file_exists($jsonPath)) {
|
|
return new JsonResponse(['error' => 'Fichier non généré'], 404);
|
|
}
|
|
$content = file_get_contents($jsonPath);
|
|
$data = json_decode($content, true);
|
|
return new JsonResponse($data);
|
|
}
|
|
|
|
#[Route('/admin/distribution_villes_lieux_par_habitant_download', name: 'admin_distribution_villes_lieux_par_habitant_download')]
|
|
public function downloadDistributionVillesLieuxParHabitant(): JsonResponse
|
|
{
|
|
$jsonPath = $this->getParameter('kernel.project_dir') . '/var/distribution_villes_lieux_par_habitant.json';
|
|
if (!file_exists($jsonPath)) {
|
|
// Générer à la volée si absent
|
|
$now = new \DateTime();
|
|
$filesystem = new \Symfony\Component\Filesystem\Filesystem();
|
|
$statsRepo = $this->entityManager->getRepository(\App\Entity\Stats::class);
|
|
$allStats = $statsRepo->findAll();
|
|
$distribution = [];
|
|
$histogram = [];
|
|
$totalVilles = 0;
|
|
foreach ($allStats as $stat) {
|
|
$places = $stat->getPlacesCount();
|
|
$population = $stat->getPopulation();
|
|
if ($places && $population && $population > 0) {
|
|
$ratio = round($places / $population, 4); // lieux par habitant
|
|
$bin = round(floor($ratio / 0.01) * 0.01, 2); // pas de 0.01
|
|
if (!isset($histogram[$bin])) $histogram[$bin] = 0;
|
|
$histogram[$bin]++;
|
|
$totalVilles++;
|
|
}
|
|
}
|
|
ksort($histogram);
|
|
$distData = [
|
|
'generated_at' => $now->format('c'),
|
|
'total_villes' => $totalVilles,
|
|
'histogram_001' => $histogram
|
|
];
|
|
$filesystem->dumpFile($jsonPath, json_encode($distData, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
|
}
|
|
$content = file_get_contents($jsonPath);
|
|
$data = json_decode($content, true);
|
|
return new JsonResponse($data);
|
|
}
|
|
|
|
#[Route('/admin/distribution_villes_lieux_par_habitant_villes', name: 'admin_distribution_villes_lieux_par_habitant_villes')]
|
|
public function downloadDistributionVillesLieuxParHabitantVilles(): JsonResponse
|
|
{
|
|
$statsRepo = $this->entityManager->getRepository(\App\Entity\Stats::class);
|
|
$allStats = $statsRepo->findAll();
|
|
$villesByBin = [];
|
|
foreach ($allStats as $stat) {
|
|
$places = $stat->getPlacesCount();
|
|
$population = $stat->getPopulation();
|
|
$name = $stat->getName();
|
|
if ($places && $population && $population > 0 && $name) {
|
|
$ratio = round($places / $population, 4); // lieux par habitant
|
|
$bin = round(floor($ratio / 0.01) * 0.01, 2); // pas de 0.01
|
|
if (!isset($villesByBin[$bin])) $villesByBin[$bin] = [];
|
|
$villesByBin[$bin][] = $name;
|
|
}
|
|
}
|
|
ksort($villesByBin);
|
|
return new JsonResponse(['villes_by_bin' => $villesByBin]);
|
|
}
|
|
|
|
#[Route('/admin/labourer-tous-les-budgets', name: 'app_admin_labourer_tous_les_budgets')]
|
|
public function labourerTousLesBudgets(): Response
|
|
{
|
|
$statsRepo = $this->entityManager->getRepository(Stats::class);
|
|
$query = $statsRepo->createQueryBuilder('s')->getQuery();
|
|
$allStats = $query->toIterable();
|
|
$budgetsMisAJour = 0;
|
|
foreach ($allStats as $stat) {
|
|
if (!$stat->getBudgetAnnuel() && $stat->getZone()) {
|
|
$budget = $this->budgetService->getBudgetAnnuel($stat->getZone());
|
|
if ($budget !== null) {
|
|
$stat->setBudgetAnnuel((string)$budget);
|
|
$this->entityManager->persist($stat);
|
|
$budgetsMisAJour++;
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
if ($budgetsMisAJour > 0) {
|
|
$this->entityManager->flush();
|
|
}
|
|
$this->addFlash('success', $budgetsMisAJour . ' budgets mis à jour.');
|
|
return $this->redirectToRoute('app_admin');
|
|
}
|
|
|
|
#[Route('/admin/podium-contributeurs-osm', name: 'app_admin_podium_contributeurs_osm')]
|
|
public function podiumContributeursOsm(): Response
|
|
{
|
|
// Ajout d'un log d'action avec le service ActionLogger
|
|
$this->actionLogger->log('podium_contributeurs_osm', []);
|
|
// On suppose que le champ "osmUser" existe sur l'entité Place
|
|
$placeRepo = $this->entityManager->getRepository(\App\Entity\Place::class);
|
|
|
|
// Nouvelle requête groupée pour tout calculer d'un coup
|
|
$qb = $placeRepo->createQueryBuilder('p')
|
|
->select(
|
|
'p.osm_user',
|
|
'COUNT(p.id) as nb',
|
|
'AVG((CASE WHEN p.has_opening_hours = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_address = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_website = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_wheelchair = true THEN 1 ELSE 0 END) +'
|
|
. ' (CASE WHEN p.has_note = true THEN 1 ELSE 0 END)) / 5 * 100 as completion_moyen'
|
|
)
|
|
->where('p.osm_user IS NOT NULL')
|
|
->andWhere("p.osm_user != ''")
|
|
->groupBy('p.osm_user')
|
|
->orderBy('nb', 'DESC')
|
|
->setMaxResults(100);
|
|
|
|
$podium = $qb->getQuery()->getResult();
|
|
|
|
// Calcul du score pondéré et normalisation
|
|
$maxPondere = 0;
|
|
foreach ($podium as &$row) {
|
|
$row['completion_moyen'] = $row['completion_moyen'] !== null ? round($row['completion_moyen'], 1) : null;
|
|
$row['completion_pondere'] = ($row['completion_moyen'] !== null && $row['nb'] > 0)
|
|
? round($row['completion_moyen'] * $row['nb'], 1)
|
|
: null;
|
|
if ($row['completion_pondere'] !== null && $row['completion_pondere'] > $maxPondere) {
|
|
$maxPondere = $row['completion_pondere'];
|
|
}
|
|
}
|
|
unset($row);
|
|
|
|
// Normalisation des scores pondérés entre 0 et 100
|
|
foreach ($podium as &$row) {
|
|
if ($maxPondere > 0 && $row['completion_pondere'] !== null) {
|
|
$row['completion_pondere_normalisee'] = round($row['completion_pondere'] / $maxPondere * 100, 1);
|
|
} else {
|
|
$row['completion_pondere_normalisee'] = null;
|
|
}
|
|
}
|
|
unset($row);
|
|
|
|
// Tri décroissant sur le score normalisé
|
|
usort($podium, function ($a, $b) {
|
|
return ($b['completion_pondere_normalisee'] ?? 0) <=> ($a['completion_pondere_normalisee'] ?? 0);
|
|
});
|
|
|
|
return $this->render('admin/podium_contributeurs_osm.html.twig', [
|
|
'podium' => $podium
|
|
]);
|
|
}
|
|
}
|