src/Controller/IndexController.php line 25
<?phpnamespace App\Controller;use App\Entity\Entreprise;use App\Entity\Etablissement;use App\Entity\HistoriqueAnalyse;use App\Repository\EntrepriseRepository;use Doctrine\Persistence\ManagerRegistry;use Doctrine\Persistence\ObjectManager;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\BinaryFileResponse;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpFoundation\ResponseHeaderBag;class IndexController extends AbstractController{private ObjectManager $em;public function __construct(ManagerRegistry $doctrine){$this->em = $doctrine->getManager();}public function index(EntrepriseRepository $entrepriseRepository): Response{if(isset($_POST['entreprise'])){return $_POST['entreprise'] !== "" ? $this->redirectToRoute('synthese', ['entreprise' => $_POST['entreprise']]) : $this->redirectToRoute('homepage');}$listeEntreprise = $this->isGranted('ROLE_ADMINISTRATEUR') ? $entrepriseRepository->findBy([], ['nom' => 'asc']) : $entrepriseRepository->findAllFromUser($this->getUser());if(count($listeEntreprise) === 1){return $this->redirectToRoute('synthese', ['entreprise' => $listeEntreprise[0]->getId()]);}return $this->render('index/index.html.twig', ['listeEntreprise' => $listeEntreprise,]);}public function synthese(EntrepriseRepository $entrepriseRepository, $entreprise): Response{$entreprise = $entrepriseRepository->find($entreprise);$result = $this->forward('App\Controller\RedirectionController::redirectEntrepriseLecture', ['entreprise' => $entreprise]);if($result->getStatusCode() != 200) return $result;if(isset($_POST['entreprise'])){return $_POST['entreprise'] !== "" ? $this->redirectToRoute('synthese', ['entreprise' => $_POST['entreprise']]) : $this->redirectToRoute('homepage');}elseif(isset($_POST['pdf'])){$historique = $this->em->getRepository(HistoriqueAnalyse::class)->find($_POST['pdf']);if(!$historique){$this->addFlash('danger', "Une erreur a été détectée, veuillez réessayer");}else{return $this->downloadPDF($historique);}}elseif(isset($_POST['pdfPlanAction'])){$historique = $this->em->getRepository(HistoriqueAnalyse::class)->find($_POST['pdfPlanAction']);if(!$historique){$this->addFlash('danger', "Une erreur a été détectée, veuillez réessayer");}else{return $this->downloadPDFPlanAction($historique);}}$nbZoneStockage = $entrepriseRepository->countZoneStockage($entreprise);$nbAgentEmis = $entrepriseRepository->countAgentEmis($entreprise);$nbProduitEtiquete = $entrepriseRepository->countProduitEtiquete($entreprise);$nbProduitEtiqueteSansFDS = $entrepriseRepository->countProduitEtiquete($entreprise, false);$listeEntreprise = $this->isGranted('ROLE_ADMINISTRATEUR') ? $entrepriseRepository->findBy([], ['nom' => 'asc']) : $entrepriseRepository->findAllFromUser($this->getUser());return $this->render('index/index.html.twig', ['entreprise' => $entreprise,'listeEntreprise' => $listeEntreprise,'nbZoneStockage' => $nbZoneStockage,'nbAgentEmis' => $nbAgentEmis,'nbProduitEtiquete' => $nbProduitEtiquete,'nbProduitEtiqueteSansFDS' => $nbProduitEtiqueteSansFDS,'historique' => $this->em->getRepository(HistoriqueAnalyse::class)->getHistoriqueFromEntreprise($entreprise),]);}public function downloadPDF(HistoriqueAnalyse $historique){$etablissement = $historique->getEtablissement();$entreprise = $etablissement->getEntreprise();$pdf = "{$this->getParameter('dossier_entreprise')}{$entreprise->getId()}/etablissements/{$etablissement->getId()}/{$historique->getId()}/analyse.pdf";if(!file_exists($pdf)){$this->addFlash('danger', "Document introuvable.");return $this->redirectToRoute('synthese', ['entreprise' => $entreprise->getId()]);}$response = new BinaryFileResponse($pdf);$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, "analyse_{$etablissement->getNom()}_{$historique->getDate()->format('d_m_Y')}.pdf");return $response;}public function downloadPDFPlanAction(HistoriqueAnalyse $historique){$etablissement = $historique->getEtablissement();$entreprise = $etablissement->getEntreprise();$pdf = "{$this->getParameter('dossier_entreprise')}{$entreprise->getId()}/etablissements/{$etablissement->getId()}/{$historique->getId()}/plan_action.pdf";if(!file_exists($pdf)){$this->addFlash('danger', "Document introuvable.");return $this->redirectToRoute('synthese', ['entreprise' => $entreprise->getId()]);}$response = new BinaryFileResponse($pdf);$response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, "plan_action_{$etablissement->getNom()}_{$historique->getDate()->format('d_m_Y')}.pdf");return $response;}}