osm-commerces/src/Controller/FollowUpController.php

151 lines
6.2 KiB
PHP
Raw Normal View History

2025-06-29 15:56:55 +02:00
<?php
namespace App\Controller;
use App\Entity\Stats;
use App\Entity\CityFollowUp;
use App\Service\Motocultrice;
2025-06-29 19:24:00 +02:00
use App\Service\FollowUpService;
2025-06-29 15:56:55 +02:00
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FollowUpController extends AbstractController
{
2025-06-29 19:24:00 +02:00
private FollowUpService $followUpService;
public function __construct(FollowUpService $followUpService)
{
$this->followUpService = $followUpService;
}
2025-06-29 18:32:24 +02:00
#[Route('/admin/followup/{insee_code}/delete', name: 'admin_followup_delete', requirements: ['insee_code' => '\\d+'])]
public function deleteFollowups(string $insee_code, EntityManagerInterface $em): Response {
$stats = $em->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();
foreach ($followups as $fu) {
$em->remove($fu);
}
$em->flush();
$this->addFlash('success', 'Tous les suivis ont été supprimés pour cette ville.');
return $this->redirectToRoute('admin_followup_graph', ['insee_code' => $insee_code]);
}
2025-06-29 18:32:24 +02:00
#[Route('/admin/followup/{insee_code}', name: 'admin_followup', requirements: ['insee_code' => '\\d+'])]
2025-06-29 15:56:55 +02:00
public function followup(
string $insee_code,
Motocultrice $motocultrice,
EntityManagerInterface $em
): Response {
$stats = $em->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');
}
2025-06-29 19:24:00 +02:00
$this->followUpService->generateCityFollowUps($stats, $motocultrice, $em);
2025-06-29 15:56:55 +02:00
$this->addFlash('success', 'Suivi enregistré pour la ville.');
return $this->redirectToRoute('admin_followup_graph', ['insee_code' => $insee_code]);
}
2025-06-29 18:32:24 +02:00
#[Route('/admin/followup/{insee_code}/graph', name: 'admin_followup_graph', requirements: ['insee_code' => '\\d+'])]
2025-06-29 15:56:55 +02:00
public function followupGraph(
string $insee_code,
EntityManagerInterface $em,
Motocultrice $motocultrice
) {
2025-06-29 15:56:55 +02:00
$stats = $em->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();
if ($followups->isEmpty()) {
2025-06-29 19:24:00 +02:00
$this->followUpService->generateCityFollowUps($stats, $motocultrice, $em);
$followups = $stats->getCityFollowUps();
}
2025-06-29 15:56:55 +02:00
$followups = $followups->toArray();
usort($followups, fn($a, $b) => $a->getDate() <=> $b->getDate());
$series = [];
foreach ($followups as $fu) {
$series[$fu->getName()][] = [
'date' => $fu->getDate()->format('c'),
'value' => $fu->getMeasure(),
'name' => $fu->getName(),
];
}
2025-06-29 19:38:54 +02:00
// Tri par date dans chaque série
foreach ($series as &$points) {
usort($points, function($a, $b) {
return strtotime($a['date']) <=> strtotime($b['date']);
});
}
unset($points);
2025-06-29 15:56:55 +02:00
return $this->render('admin/followup_graph.html.twig', [
'stats' => $stats,
2025-06-29 19:24:00 +02:00
'series' => $series,
'followup_labels' => FollowUpService::getFollowUpThemes(),
'followup_icons' => FollowUpService::getFollowUpIcons(),
'followup_overpass' => FollowUpService::getFollowUpOverpassQueries(),
2025-06-29 15:56:55 +02:00
]);
}
#[Route('/admin/followup/all', name: 'admin_followup_all')]
public function followupAll(
EntityManagerInterface $em,
Motocultrice $motocultrice
) {
$statsList = $em->getRepository(Stats::class)->findAll();
foreach ($statsList as $stats) {
2025-06-29 19:24:00 +02:00
$this->followUpService->generateCityFollowUps($stats, $motocultrice, $em);
}
$this->addFlash('success', 'Suivi généré pour toutes les villes.');
return $this->redirectToRoute('app_admin');
}
2025-06-29 18:32:24 +02:00
#[Route('/admin/followup/global', name: 'admin_followup_global')]
public function followupGlobal(EntityManagerInterface $em) {
2025-06-29 19:24:00 +02:00
$this->followUpService->generateGlobalFollowUps($em);
2025-06-29 18:32:24 +02:00
$this->addFlash('success', 'Suivi global généré pour toutes les villes.');
return $this->redirectToRoute('admin_followup_global_graph');
}
#[Route('/admin/followup/global/graph', name: 'admin_followup_global_graph')]
public function followupGlobalGraph(EntityManagerInterface $em) {
$stats = $em->getRepository(Stats::class)->findOneBy(['zone' => '00000']);
if (!$stats) {
$this->addFlash('error', 'Aucun suivi global trouvé.');
return $this->redirectToRoute('app_admin');
}
$followups = $stats->getCityFollowUps();
$followups = $followups->toArray();
usort($followups, fn($a, $b) => $a->getDate() <=> $b->getDate());
$series = [];
foreach ($followups as $fu) {
$series[$fu->getName()][] = [
'date' => $fu->getDate()->format('c'),
'value' => $fu->getMeasure(),
'name' => $fu->getName(),
];
}
2025-06-29 19:38:54 +02:00
// Tri par date dans chaque série
foreach ($series as &$points) {
usort($points, function($a, $b) {
return strtotime($a['date']) <=> strtotime($b['date']);
});
}
unset($points);
2025-06-29 18:32:24 +02:00
return $this->render('admin/followup_global_graph.html.twig', [
'stats' => $stats,
2025-06-29 19:24:00 +02:00
'series' => $series,
'followup_labels' => FollowUpService::getFollowUpThemes(),
'followup_icons' => FollowUpService::getFollowUpIcons(),
'followup_overpass' => FollowUpService::getFollowUpOverpassQueries(),
2025-06-29 18:32:24 +02:00
]);
}
2025-06-29 15:56:55 +02:00
}