← Back
Editing: ItemController.php
<?php namespace App\Controller; use App\Entity\Item; use App\Form\ItemType; use App\Repository\ItemRepository; use Doctrine\ORM\EntityManagerInterface; use Dompdf\Dompdf; use Dompdf\Options; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; 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: '/item')] class ItemController extends AbstractController { /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ #[Route(path: '/', name: 'item_index', methods: ['GET'])] public function index(ItemRepository $itemRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { return $this->render( 'item/index.html.twig', [ 'items' => $itemRepository->findAll(), ] ); } else { return $this->redirect('/'); } } /** * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ #[Route(path: '/export/index', name: 'item_export_index', methods: ['GET'])] public function exportIndex(ItemRepository $itemRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $itemList = $itemRepository->findAll(); } elseif ($this->container->get('security.authorization_checker')->isGranted('ROLE_MANAGER')) { $itemList = $itemRepository->findByAgency($this->getUser()->getAgency()); } else { $itemList = $itemRepository->findByAgency($this->getUser()->getAgency()); } $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle('Item'); $sheet->setCellValue('A1', 'Nom'); $sheet->setCellValue('B1', 'Zone'); $sheet->setCellValue('C1', 'Prestataire'); for ($i = 0; $i < count($itemList); ++$i) { $sheet->setCellValue('A'.($i + 2), $itemList[$i]->getNom()); $sheet->setCellValue('B'.($i + 2), $itemList[$i]->getZone() ? $itemList[$i]->getZone()->getNom() : ''); $sheet->setCellValue( 'C'.($i + 2), $itemList[$i]->getPrestataire() ? $itemList[$i]->getPrestataire()->getNom() : '' ); } $writer = new Xlsx($spreadsheet); $fileName = 'items.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 \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ #[Route(path: '/export/pdf/index', name: 'item_export_pdf_index', methods: ['GET'])] public function exportPdfIndex(ItemRepository $itemRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $itemList = $itemRepository->findAll(); } elseif ($this->container->get('security.authorization_checker')->isGranted('ROLE_MANAGER')) { $itemList = $itemRepository->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); $title = 'Welcome to our PDF Test'; // Retrieve the HTML generated in our twig file $html = $this->renderView( 'export/pdf_item.html.twig', [ 'itemList' => $itemList, '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( 'entreprises.pdf', [ 'Attachment' => true, ] ); return new Response(); } /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ #[Route(path: '/new', name: 'item_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('/'); } $item = new Item(); $form = $this->createForm(ItemType::class, $item); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->persist($item); $em->flush(); return $this->redirectToRoute('item_index'); } return $this->render( 'item/new.html.twig', [ 'item' => $item, 'form' => $form->createView(), ] ); } #[Route(path: '/{id}', name: 'item_show', methods: ['GET'])] public function show(Item $item): Response { return $this->render( 'item/show.html.twig', [ 'item' => $item, ] ); } /** * @throws \Psr\Container\ContainerExceptionInterface * @throws \Psr\Container\NotFoundExceptionInterface */ #[Route(path: '/{id}/edit', name: 'item_edit', methods: ['GET', 'POST'])] public function edit(Request $request, Item $item, EntityManagerInterface $em): Response { $this->container->get('security.authorization_checker')->isGranted( 'ROLE_ADMIN' ) ? $options['role'] = 'admin' : $options['role'] = 'non-admin'; $options['agency'] = $this->getUser()->getAgency(); $form = $this->createForm(ItemType::class, $item, $options); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em->flush(); return $this->redirectToRoute( 'item_index', [ 'id' => $item->getId(), ] ); } return $this->render( 'item/edit.html.twig', [ 'item' => $item, 'form' => $form->createView(), ] ); } #[Route(path: '/{id}', name: 'item_delete', methods: ['DELETE'])] public function delete(Request $request, Item $item, EntityManagerInterface $em): Response { if ($this->isCsrfTokenValid('delete'.$item->getId(), $request->request->get('_token'))) { $em->remove($item); $em->flush(); } return $this->redirectToRoute('item_index'); } }
Save File
Cancel