<?php
namespace App\Controller;
use App\Service\VingeanneArchiveService;
use App\Service\ArticleExportFileService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
class VingeanneController extends AbstractController
{
private VingeanneArchiveService $vingeanneService;
private ArticleExportFileService $articleFileService;
public function __construct(
VingeanneArchiveService $vingeanneService,
ArticleExportFileService $articleFileService
) {
$this->vingeanneService = $vingeanneService;
$this->articleFileService = $articleFileService;
}
/**
* Page d'accueil Vingeanne (Dashboard)
*/
public function indexAction(): Response
{
try {
$stats = $this->vingeanneService->getStatistics();
} catch (\Exception $e) {
$stats = [
'total' => 0,
'rea' => 0,
'off' => 0,
'inconnu' => 0,
'error' => $e->getMessage()
];
}
return $this->render('@SyliusAdmin/Vingeanne/index.html.twig', [
'stats' => $stats
]);
}
/**
* Files list page
*/
public function filesListAction(): Response
{
return $this->render('@SyliusAdmin/Vingeanne/files_list.html.twig');
}
/**
* Load files via AJAX (API endpoint)
*/
public function loadFilesAction(Request $request): JsonResponse
{
try {
$filters = [
'type' => $request->query->get('type'),
'name' => $request->query->get('name'),
'dateFrom' => $request->query->get('dateFrom'),
'dateTo' => $request->query->get('dateTo'),
];
$page = max(1, (int) $request->query->get('page', 1));
$limit = max(1, min(100, (int) $request->query->get('limit', 50)));
$offset = ($page - 1) * $limit;
$result = $this->vingeanneService->listFiles($filters, $limit, $offset);
$data = [];
foreach ($result['data'] as $file) {
$data[] = [
'name' => $file['name'],
'type' => $file['type'],
'size' => $file['sizeFormatted'],
'createdAt' => $file['createdAtFormatted'],
'createdAtRaw' => $file['createdAt'] ? $file['createdAt']->format('Y-m-d H:i:s') : null,
];
}
$total = $result['total'];
$pages = $limit > 0 ? (int) ceil($total / $limit) : 0;
return new JsonResponse([
'success' => true,
'data' => $data,
'pagination' => [
'total' => $total,
'page' => $page,
'limit' => $limit,
'pages' => $pages
]
]);
} catch (\Exception $e) {
return new JsonResponse([
'success' => false,
'error' => 'Erreur lors du chargement des fichiers: ' . $e->getMessage()
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* Download a file
*/
public function downloadFileAction(string $filename): Response
{
try {
$result = $this->vingeanneService->downloadFile($filename);
if (!$result) {
return new Response('Fichier introuvable ou erreur lors du téléchargement', Response::HTTP_NOT_FOUND);
}
$response = new Response($result['content']);
$response->headers->set('Content-Type', 'text/plain; charset=UTF-8');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $result['name'] . '"');
return $response;
} catch (\Exception $e) {
return new Response('Erreur: ' . $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* Articles export files list page
*/
public function articlesListAction(): Response
{
try {
$stats = $this->articleFileService->getStatistics('articles/vingeanne');
} catch (\Exception $e) {
$stats = ['total' => 0, 'totalSize' => '0 B', 'error' => $e->getMessage()];
}
return $this->render('@SyliusAdmin/Vingeanne/articles_list.html.twig', [
'stats' => $stats
]);
}
/**
* Load article files via AJAX
*/
public function loadArticleFilesAction(Request $request): JsonResponse
{
try {
$filters = [
'name' => $request->query->get('name'),
'dateFrom' => $request->query->get('dateFrom'),
'dateTo' => $request->query->get('dateTo'),
];
$page = max(1, (int) $request->query->get('page', 1));
$limit = max(1, min(100, (int) $request->query->get('limit', 50)));
$offset = ($page - 1) * $limit;
$result = $this->articleFileService->listArticleFiles('articles/vingeanne', $filters, $limit, $offset);
$total = $result['total'];
$pages = $limit > 0 ? (int) ceil($total / $limit) : 0;
return new JsonResponse([
'success' => true,
'data' => $result['data'],
'pagination' => [
'total' => $total,
'page' => $page,
'limit' => $limit,
'pages' => $pages
]
]);
} catch (\Exception $e) {
return new JsonResponse([
'success' => false,
'error' => 'Erreur lors du chargement des fichiers: ' . $e->getMessage()
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
/**
* Download an article export file
*/
public function downloadArticleFileAction(string $filename): Response
{
try {
$result = $this->articleFileService->downloadFile('articles/vingeanne', $filename);
if (!$result) {
return new Response('Fichier introuvable', Response::HTTP_NOT_FOUND);
}
$response = new Response($result['content']);
$response->headers->set('Content-Type', 'text/plain; charset=UTF-8');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $result['name'] . '"');
return $response;
} catch (\Exception $e) {
return new Response('Erreur: ' . $e->getMessage(), Response::HTTP_INTERNAL_SERVER_ERROR);
}
}
}