src/Controller/IndexController.php line 25

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Entreprise;
  4. use App\Entity\Etablissement;
  5. use App\Entity\HistoriqueAnalyse;
  6. use App\Repository\EntrepriseRepository;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Doctrine\Persistence\ObjectManager;
  9. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  10. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  13. class IndexController extends AbstractController
  14. {
  15.     private ObjectManager $em;
  16.     public function __construct(ManagerRegistry $doctrine)
  17.     {
  18.         $this->em = $doctrine->getManager();
  19.     }
  20.     public function index(EntrepriseRepository $entrepriseRepository): Response
  21.     {
  22.         if(isset($_POST['entreprise']))
  23.         {
  24.             return $_POST['entreprise'] !== "" ? $this->redirectToRoute('synthese', ['entreprise' => $_POST['entreprise']]) : $this->redirectToRoute('homepage');
  25.         }
  26.         $listeEntreprise = $this->isGranted('ROLE_ADMINISTRATEUR') ? $entrepriseRepository->findBy([], ['nom' => 'asc']) : $entrepriseRepository->findAllFromUser($this->getUser());
  27.         if(count($listeEntreprise) === 1)
  28.         {
  29.             return $this->redirectToRoute('synthese', ['entreprise' => $listeEntreprise[0]->getId()]);
  30.         }
  31.         return $this->render('index/index.html.twig', [
  32.             'listeEntreprise' => $listeEntreprise,
  33.         ]);
  34.     }
  35.     public function synthese(EntrepriseRepository $entrepriseRepository, $entreprise): Response
  36.     {
  37.         $entreprise = $entrepriseRepository->find($entreprise);
  38.         $result = $this->forward('App\Controller\RedirectionController::redirectEntrepriseLecture', ['entreprise' => $entreprise]);
  39.         if($result->getStatusCode() != 200) return $result;
  40.         if(isset($_POST['entreprise']))
  41.         {
  42.             return $_POST['entreprise'] !== "" ? $this->redirectToRoute('synthese', ['entreprise' => $_POST['entreprise']]) : $this->redirectToRoute('homepage');
  43.         }
  44.         elseif(isset($_POST['pdf']))
  45.         {
  46.             $historique = $this->em->getRepository(HistoriqueAnalyse::class)->find($_POST['pdf']);
  47.             if(!$historique)
  48.             {
  49.                 $this->addFlash('danger', "Une erreur a été détectée, veuillez réessayer");
  50.             }
  51.             else
  52.             {
  53.                 return $this->downloadPDF($historique);
  54.             }
  55.         }
  56.         elseif(isset($_POST['pdfPlanAction']))
  57.         {
  58.             $historique = $this->em->getRepository(HistoriqueAnalyse::class)->find($_POST['pdfPlanAction']);
  59.             if(!$historique)
  60.             {
  61.                 $this->addFlash('danger', "Une erreur a été détectée, veuillez réessayer");
  62.             }
  63.             else
  64.             {
  65.                 return $this->downloadPDFPlanAction($historique);
  66.             }
  67.         }
  68.         $nbZoneStockage = $entrepriseRepository->countZoneStockage($entreprise);
  69.         $nbAgentEmis = $entrepriseRepository->countAgentEmis($entreprise);
  70.         $nbProduitEtiquete = $entrepriseRepository->countProduitEtiquete($entreprise);
  71.         $nbProduitEtiqueteSansFDS = $entrepriseRepository->countProduitEtiquete($entreprise, false);
  72.         $listeEntreprise = $this->isGranted('ROLE_ADMINISTRATEUR') ? $entrepriseRepository->findBy([], ['nom' => 'asc']) : $entrepriseRepository->findAllFromUser($this->getUser());
  73.         return $this->render('index/index.html.twig', [
  74.             'entreprise' => $entreprise,
  75.             'listeEntreprise' => $listeEntreprise,
  76.             'nbZoneStockage' => $nbZoneStockage,
  77.             'nbAgentEmis' => $nbAgentEmis,
  78.             'nbProduitEtiquete' => $nbProduitEtiquete,
  79.             'nbProduitEtiqueteSansFDS' => $nbProduitEtiqueteSansFDS,
  80.             'historique' => $this->em->getRepository(HistoriqueAnalyse::class)->getHistoriqueFromEntreprise($entreprise),
  81.         ]);
  82.     }
  83.     public function downloadPDF(HistoriqueAnalyse $historique)
  84.     {
  85.         $etablissement = $historique->getEtablissement();
  86.         $entreprise = $etablissement->getEntreprise();
  87.         $pdf = "{$this->getParameter('dossier_entreprise')}{$entreprise->getId()}/etablissements/{$etablissement->getId()}/{$historique->getId()}/analyse.pdf";
  88.         if(!file_exists($pdf))
  89.         {
  90.             $this->addFlash('danger', "Document introuvable.");
  91.             return $this->redirectToRoute('synthese', ['entreprise' => $entreprise->getId()]);
  92.         }
  93.         $response = new BinaryFileResponse($pdf);
  94.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, "analyse_{$etablissement->getNom()}_{$historique->getDate()->format('d_m_Y')}.pdf");
  95.         return $response;
  96.     }
  97.     public function downloadPDFPlanAction(HistoriqueAnalyse $historique)
  98.     {
  99.         $etablissement = $historique->getEtablissement();
  100.         $entreprise = $etablissement->getEntreprise();
  101.         $pdf = "{$this->getParameter('dossier_entreprise')}{$entreprise->getId()}/etablissements/{$etablissement->getId()}/{$historique->getId()}/plan_action.pdf";
  102.         if(!file_exists($pdf))
  103.         {
  104.             $this->addFlash('danger', "Document introuvable.");
  105.             return $this->redirectToRoute('synthese', ['entreprise' => $entreprise->getId()]);
  106.         }
  107.         $response = new BinaryFileResponse($pdf);
  108.         $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, "plan_action_{$etablissement->getNom()}_{$historique->getDate()->format('d_m_Y')}.pdf");
  109.         return $response;
  110.     }
  111. }