← Back
Editing: ZoneController.php
<?php namespace App\Controller; use App\Entity\Zone; use App\Form\ZoneType; use App\Repository\ZoneRepository; use Doctrine\ORM\EntityManagerInterface; use Dompdf\Dompdf; use Dompdf\Options; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\Routing\Annotation\Route; #[Route(path: '/zone')] class ZoneController extends AbstractController { /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/', name: 'zone_index', methods: ['GET'])] public function index(ZoneRepository $zoneRepository): Response { if (!$this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { return $this->redirect('/'); } return $this->render('zone/index.html.twig', [ 'zones' => $zoneRepository->findAll(), ]); } /** * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/export/index', name: 'zone_export_index', methods: ['GET'])] public function exportIndex(ZoneRepository $zoneRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $zoneList = $zoneRepository->findAll(); } elseif ($this->container->get('security.authorization_checker')->isGranted('ROLE_MANAGER')) { $zoneList = $zoneRepository->findByAgency($this->getUser()->getAgency()); } else { $zoneList = $zoneRepository->findByAgency($this->getUser()->getAgency()); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Zone'); $sheet->setCellValue('A1', 'Nom'); for ($i = 0; $i < count($zoneList); ++$i) { $sheet->setCellValue('A'.($i + 2), $zoneList[$i]->getNom()); } $writer = new Xlsx($spreadsheet); $fileName = 'zone.xlsx'; $tmpFile = tempnam(sys_get_temp_dir(), $fileName); // Create the excel file in the tmp directory of the system $writer->save($tmpFile); // Return the excel file as an attachment return $this->file($tmpFile, $fileName, ResponseHeaderBag::DISPOSITION_INLINE); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/export/pdf/index', name: 'zone_export_pdf_index', methods: ['GET'])] public function exportPdfIndex(ZoneRepository $zoneRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $zoneList = $zoneRepository->findAll(); } elseif ($this->container->get('security.authorization_checker')->isGranted('ROLE_MANAGER')) { $zoneList = $zoneRepository->findByAgency($this->getUser()->getAgency()); } // Configure Dompdf according to your needs $pdfOptions = new Options(); $pdfOptions->set('defaultFont', 'Arial'); // Instantiate Dompdf with our options $dompdf = new Dompdf($pdfOptions); $dompdf->set_option('isHtml5ParserEnabled', true); // Retrieve the HTML generated in our twig file $html = $this->renderView('export/pdf_zone.html.twig', [ 'zoneList' => $zoneList, 'user' => $this->getUser(), ]); // Load HTML to Dompdf $dompdf->loadHtml($html); // (Optional) Setup the paper size and orientation 'portrait' or 'landscape' $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser (force download) $dompdf->stream('zones.pdf', [ 'Attachment' => true, ]); return new Response(); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/new', name: 'zone_new', methods: ['GET', 'POST'])] public function new(Request $request, EntityManagerInterface $em): Response { if (!$this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { return $this->redirect('/'); } $zone = new Zone(); $form = $this->createForm(ZoneType::class, $zone); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($zone); $em->flush(); return $this->redirectToRoute('zone_index'); } return $this->render('zone/new.html.twig', [ 'zone' => $zone, 'form' => $form->createView(), ]); } #[Route(path: '/{id}', name: 'zone_show', methods: ['GET'])] public function show(Zone $zone): Response { return $this->render('zone/show.html.twig', [ 'zone' => $zone, ]); } #[Route(path: '/{id}/edit', name: 'zone_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Zone $zone, EntityManagerInterface $em): Response { $form = $this->createForm(ZoneType::class, $zone); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->flush(); return $this->redirectToRoute('zone_index', [ 'id' => $zone->getId(), ]); } return $this->render('zone/edit.html.twig', [ 'zone' => $zone, 'form' => $form->createView(), ]); } #[Route(path: '/{id}', name: 'zone_delete', methods: ['DELETE'])] public function delete(Request $request, Zone $zone, EntityManagerInterface $em): Response { if ($this->isCsrfTokenValid('delete'.$zone->getId(), $request->request->get('_token'))) { $em->remove($zone); $em->flush(); } return $this->redirectToRoute('zone_index'); } }
Save File
Cancel