src/Controller/DashboardController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Constant\RouteTitleConstant;
  4. use App\DTO\ExportLogDateForm;
  5. use App\DTO\ImportLogDateForm;
  6. use App\Form\ExportLogType;
  7. use App\Form\ImportLogType;
  8. use App\Manager\ExportLogManager;
  9. use App\Manager\LogManager;
  10. use App\Message\ImportLogMessage;
  11. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Messenger\MessageBusInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. class DashboardController extends AbstractController
  19. {
  20.   /**
  21.    * @Route("/", name="app_dashboard")
  22.    * @Security("is_granted('ROLE_CP')")
  23.    * @param Request $request
  24.    * @param LogManager $logManager
  25.    * @param ExportLogManager $exportLogManager
  26.    * @param MessageBusInterface $messageBus
  27.    * @return Response
  28.    * @throws \PhpOffice\PhpSpreadsheet\Exception
  29.    * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
  30.    */
  31.   public function dashboard(Request $requestLogManager $logManagerExportLogManager $exportLogManagerMessageBusInterface $messageBus): Response
  32.   {
  33.     $title RouteTitleConstant::DASHBOARD;
  34.     $lastLog $logManager->getLastDateLogUpdate();
  35.     $dateImport = new ImportLogDateForm();
  36.     // creation du form pour l'importation
  37.     $formImport =  $this->createForm(ImportLogType::class, $dateImport);
  38.     $formImport->handleRequest($request);
  39.     if($formImport->isSubmitted() && $formImport->isValid()){
  40.       /**
  41.        * @var \DateTime $date
  42.        */
  43.       $date $dateImport->getDate();
  44.       $this->addFlash('success'"L'import des logs est en cours, vous pouvez continuer à utiliser l'application !");
  45.       $messageBus->dispatch(new ImportLogMessage($date->format('Y-m-d'), $this->getUser()));
  46.     }
  47.     // creation du form d'export des log
  48.     $dateExport = new ExportLogDateForm();
  49.     $formExport $this->createForm(ExportLogType::class,$dateExport);
  50.     $formExport->handleRequest($request);
  51.     if($formExport->isSubmitted() && $formExport->isValid()){
  52.       $spreadsheet $exportLogManager->exportYearLog($dateExport->getDate()->format('Y'));
  53.       $writer = new Xlsx($spreadsheet);
  54.       header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  55.       header('Content-Disposition: attachment;filename="kromi_occupation_'.$dateExport->getDate()->format('Y').'.xlsx"');
  56.       header('Cache-Control: max-age=0');
  57.       $writer->save('php://output');
  58.       // ajout d'un exit sinon le fichier est corrompu
  59.       exit();
  60.     }
  61.     // recuperation info mois en cours
  62.     $currentDate = new \DateTime('now');
  63.     $currentDate->setTime(0,0,0);
  64.     $titleCurrentMonth $currentDate->format('m/Y');
  65.     return $this->render('dashboard/index.html.twig', [
  66.       'formImport'=>$formImport->createView(),
  67.       'formExport'=>$formExport->createView(),
  68.       'lastLog'=> $lastLog,
  69.       'title'=>$title,
  70.       'titleCurrentMonth'=>$titleCurrentMonth
  71.     ]);
  72.   }
  73.   /**
  74.    * @Route("/occupation", name="app_dashboard.occupation")
  75.    * @Security("is_granted('ROLE_CP')")
  76.    * @param LogManager $logManager
  77.    * @return Response
  78.    * @throws \Exception
  79.    */
  80.   public function occupationDataCurrentMonth(LogManager $logManager): Response
  81.   {
  82.     $currentDate = new \DateTime('now');
  83.     $currentDate->setTime(0,0,0);
  84.     $occupationCurrentMonth $logManager->getOccupationForChart($currentDatenulltrue);
  85.     $response = new Response(json_encode($occupationCurrentMonth));
  86.     $response->headers->set('Content-Type''application/json');
  87.     return $response;
  88.   }
  89.   /**
  90.    * * @Route("/total", name="app_dashboard.total")
  91.    * @Security("is_granted('ROLE_CP')")
  92.    * @param LogManager $logManager
  93.    * @return Response
  94.    * @throws \Exception
  95.    */
  96.   public function occupationGlobalDataCurrentMonth(LogManager $logManager): Response
  97.   {
  98.     $currentDate = new \DateTime('now');
  99.     $currentDate->setTime(0,0,0);
  100.     $occupationTotCurrentMonth $logManager->getTotalOccupationForChart($currentDatenulltrue);
  101.     $response = new Response(json_encode($occupationTotCurrentMonth));
  102.     $response->headers->set('Content-Type''application/json');
  103.     return $response;
  104.   }
  105. }