← Back
Editing: PrestataireController.php
<?php namespace App\Controller; use App\Entity\Adresse; use App\Entity\AgencyPrestataire; use App\Entity\Prestataire; use App\Entity\User; use App\Form\AgencyPrestataireType; use App\Form\PrestataireType; use App\Repository\AgencyPrestataireRepository; use App\Repository\PrestataireRepository; use Doctrine\ORM\EntityManagerInterface; use Dompdf\Dompdf; use Dompdf\Options; 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\Session\SessionInterface; use Symfony\Component\Routing\Annotation\Route; /** * Class PrestataireController. */ #[Route(path: '/prestataire')] class PrestataireController extends AbstractController { /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/', name: 'prestataire_index', methods: ['GET', 'POST'])] public function index( Request $request, SessionInterface $session, EntityManagerInterface $em, AgencyPrestataireRepository $agencyPrestataireRepository ): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { return $this->redirect('prestataire_index_admin'); } else { if ($request->get('order_val')) { $order = $request->get('order_val'); } elseif ($session->get('order')) { $result = $session->get('order'); $order = $result; } else { $order = 'asc'; } if ('asc' === $order) { $prestataires = $agencyPrestataireRepository->getPrestatairesByManager( $this->getUser(), 'ASC' ); $session->set('order', 'asc'); } elseif ('desc' === $order) { $prestataires = $agencyPrestataireRepository->getPrestatairesByManager( $this->getUser(), 'DESC' ); $session->set('order', 'desc'); } $firstElem = reset($prestataires); $agencyPrestataire = $firstElem; if ($agencyPrestataire) { $formCp = $this->createForm(AgencyPrestataireType::class, $agencyPrestataire); $formCp->handleRequest($request); if ($formCp->isSubmitted() && $formCp->isValid()) { $em->flush(); } } return $this->render( 'prestataire/index.html.twig', [ 'order' => $order, 'first_presta' => $firstElem, 'prestataires' => $prestataires, ] ); } } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/listeAdmin', name: 'prestataire_index_admin', methods: ['GET'])] public function indexAdmin(PrestataireRepository $prestataireRepository): Response { if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { return $this->render( 'prestataire/indexAdmin.html.twig', [ 'prestataires' => $prestataireRepository->findAll(), ] ); } else { return $this->redirectToRoute('prestataire_index'); } } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/export/pdf', name: 'prestataire_export_pdf_index', methods: ['GET'])] public function exportPdfIndex( PrestataireRepository $prestaireRepository, AgencyPrestataireRepository $agencyPrestataireRepository ): Response { /** @var User $user */ $user = $this->getUser(); if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $prestataireList = $prestaireRepository->findAll(); } elseif ($this->container->get('security.authorization_checker')->isGranted('ROLE_MANAGER')) { $prestataireList = $agencyPrestataireRepository->getPrestatairesByAgency( $user->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_prestataire.html.twig', [ 'prestataireList' => $prestataireList, '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( 'prestataires.pdf', [ 'Attachment' => true, ] ); return new Response(); } #[Route(path: '/new', name: 'prestataire_new', methods: ['GET', 'POST'])] public function new(Request $request, EntityManagerInterface $em): Response { /** @var User $user */ $user = $this->getUser(); $prestataire = new Prestataire(); $agencyPrestataire = new AgencyPrestataire(); $agency = $user->getAgency(); $agencyPrestataire->setAgency($agency); $agencyPrestataire->setPrestataire($prestataire); $formCp = $this->createForm(AgencyPrestataireType::class, $agencyPrestataire); $formCp->handleRequest($request); if ($formCp->isSubmitted() && $formCp->isValid()) { $addresses = $formCp['adresses']->getData(); $prestataire->getAdresses()->clear(); foreach ($addresses as $address) { /* @var Adresse $address */ $address->getPrestataires()->clear(); $address->addPrestataire($prestataire); $em->persist($address); } $em->persist($prestataire); $em->persist($agencyPrestataire); $em->flush(); return $this->redirectToRoute('prestataire_index'); } return $this->render( 'prestataire/new.html.twig', [ 'prestataire' => $prestataire, 'formCp' => $formCp->createView(), ] ); } #[Route(path: '/{id}', name: 'prestataire_show', methods: ['GET'])] public function show(Prestataire $prestataire): Response { return $this->render( 'prestataire/show.html.twig', [ 'prestataire' => $prestataire, ] ); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ #[Route(path: '/{id}/edit', name: 'prestataire_edit', methods: ['GET', 'POST'])] public function edit( Request $request, Prestataire $prestataire, SessionInterface $session, EntityManagerInterface $em, AgencyPrestataireRepository $agencyPrestataireRepository ): Response { $agencyPrestataire = $agencyPrestataireRepository->findByPrestataire($prestataire->getId())[0]; $formCp = $this->createForm(AgencyPrestataireType::class, $agencyPrestataire); $formCp->handleRequest($request); if ($formCp->isSubmitted() && $formCp->isValid()) { $em->flush(); return $this->redirectToRoute( 'prestataire_edit', [ 'id' => $prestataire->getId(), ] ); } if ($session->get('order')) { $order = $session->get('order'); } else { $order = 'asc'; } if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $formP = $this->createForm(PrestataireType::class, $prestataire); return $this->render( 'prestataire/editAdmin.html.twig', [ 'selectedPrestataire' => $agencyPrestataire, 'formCp' => $formCp->createView(), 'formP' => $formP->createView(), ] ); } else { $prestataires = $agencyPrestataireRepository->getPrestatairesByManager( $this->getUser() ); $prestatairesDESC = $agencyPrestataireRepository->getPrestatairesByAgencyDesc( $this->getUser()->getAgency() ); return $this->render( 'prestataire/edit.html.twig', [ 'prestataires' => $prestataires, 'unsorted_prestataires' => $prestatairesDESC, 'selectedPrestataire' => $agencyPrestataire, 'formCp' => $formCp->createView(), 'order' => $order, ] ); } } #[Route(path: '/{id}', name: 'prestataire_delete', methods: ['DELETE', 'POST'])] public function delete( Request $request, Prestataire $prestataire, EntityManagerInterface $em, AgencyPrestataireRepository $agencyPrestataireRepository ): Response { $agencyPrestataire = $agencyPrestataireRepository->findByPrestataire($prestataire->getId())[0]; if ($this->isCsrfTokenValid('delete'.$prestataire->getId(), $request->request->get('_token'))) { $em->remove($agencyPrestataire); $em->remove($prestataire); $em->flush(); } return $this->redirectToRoute('prestataire_index'); } #[Route(path: '/first/{id}', name: 'prestataire_first', methods: ['GET', 'POST'])] public function first( Request $request, Prestataire $prestataire, EntityManagerInterface $em, AgencyPrestataireRepository $agencyPrestataireRepository ): Response { $agencyPrestataire = $agencyPrestataireRepository->findByPrestataire($prestataire->getId())[0]; $formCp = $this->createForm(AgencyPrestataireType::class, $agencyPrestataire); $formCp->handleRequest($request); if ($formCp->isSubmitted() && $formCp->isValid()) { $em->flush(); return $this->redirectToRoute( 'prestataire_edit', [ 'id' => $prestataire->getId(), ] ); } if ($this->container->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { $formP = $this->createForm(PrestataireType::class, $prestataire); return $this->render( 'prestataire/editAdmin.html.twig', [ 'selectedPrestataire' => $agencyPrestataire, 'formCp' => $formCp->createView(), 'formP' => $formP->createView(), ] ); } else { $prestataires = $agencyPrestataireRepository->getPrestatairesByAgency( $this->getUser()->getAgency() ); return $this->render( 'prestataire/_first.html.twig', [ 'prestataires' => $prestataires, 'selectedPrestataire' => $agencyPrestataire, 'formCp' => $formCp->createView(), ] ); } } #[Route(path: '/sort', name: 'prestataire_sort_opt', methods: ['GET', 'POST'])] public function order(Request $request, Prestataire $prestataire): Response { $order = $request->get('order_val'); return $this->render( 'prestataire/index.html.twig', [ 'order' => $order, ] ); } }
Save File
Cancel