<?php
namespace App\Controller;
use App\Constant\RouteTitleConstant;
use App\DTO\ExportLogDateForm;
use App\DTO\ImportLogDateForm;
use App\Form\ExportLogType;
use App\Form\ImportLogType;
use App\Manager\ExportLogManager;
use App\Manager\LogManager;
use App\Message\ImportLogMessage;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Routing\Annotation\Route;
class DashboardController extends AbstractController
{
/**
* @Route("/", name="app_dashboard")
* @Security("is_granted('ROLE_CP')")
* @param Request $request
* @param LogManager $logManager
* @param ExportLogManager $exportLogManager
* @param MessageBusInterface $messageBus
* @return Response
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
public function dashboard(Request $request, LogManager $logManager, ExportLogManager $exportLogManager, MessageBusInterface $messageBus): Response
{
$title = RouteTitleConstant::DASHBOARD;
$lastLog = $logManager->getLastDateLogUpdate();
$dateImport = new ImportLogDateForm();
// creation du form pour l'importation
$formImport = $this->createForm(ImportLogType::class, $dateImport);
$formImport->handleRequest($request);
if($formImport->isSubmitted() && $formImport->isValid()){
/**
* @var \DateTime $date
*/
$date = $dateImport->getDate();
$this->addFlash('success', "L'import des logs est en cours, vous pouvez continuer à utiliser l'application !");
$messageBus->dispatch(new ImportLogMessage($date->format('Y-m-d'), $this->getUser()));
}
// creation du form d'export des log
$dateExport = new ExportLogDateForm();
$formExport = $this->createForm(ExportLogType::class,$dateExport);
$formExport->handleRequest($request);
if($formExport->isSubmitted() && $formExport->isValid()){
$spreadsheet = $exportLogManager->exportYearLog($dateExport->getDate()->format('Y'));
$writer = new Xlsx($spreadsheet);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="kromi_occupation_'.$dateExport->getDate()->format('Y').'.xlsx"');
header('Cache-Control: max-age=0');
$writer->save('php://output');
// ajout d'un exit sinon le fichier est corrompu
exit();
}
// recuperation info mois en cours
$currentDate = new \DateTime('now');
$currentDate->setTime(0,0,0);
$titleCurrentMonth = $currentDate->format('m/Y');
return $this->render('dashboard/index.html.twig', [
'formImport'=>$formImport->createView(),
'formExport'=>$formExport->createView(),
'lastLog'=> $lastLog,
'title'=>$title,
'titleCurrentMonth'=>$titleCurrentMonth
]);
}
/**
* @Route("/occupation", name="app_dashboard.occupation")
* @Security("is_granted('ROLE_CP')")
* @param LogManager $logManager
* @return Response
* @throws \Exception
*/
public function occupationDataCurrentMonth(LogManager $logManager): Response
{
$currentDate = new \DateTime('now');
$currentDate->setTime(0,0,0);
$occupationCurrentMonth = $logManager->getOccupationForChart($currentDate, null, true);
$response = new Response(json_encode($occupationCurrentMonth));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
/**
* * @Route("/total", name="app_dashboard.total")
* @Security("is_granted('ROLE_CP')")
* @param LogManager $logManager
* @return Response
* @throws \Exception
*/
public function occupationGlobalDataCurrentMonth(LogManager $logManager): Response
{
$currentDate = new \DateTime('now');
$currentDate->setTime(0,0,0);
$occupationTotCurrentMonth = $logManager->getTotalOccupationForChart($currentDate, null, true);
$response = new Response(json_encode($occupationTotCurrentMonth));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
}