src/Controller/DefaultController.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BasePage;
  4. use App\Entity\Notification;
  5. use App\Entity\Region;
  6. use App\Form\ContactShowCaseType;
  7. use App\Form\Filter\FilterStatisticsType;
  8. use App\Form\ListingType;
  9. use App\Service\CheckRecaptcha;
  10. use App\Service\ExcelExport;
  11. use App\Service\ExcelExportDc;
  12. use App\Service\Notifier;
  13. use App\Service\StatisticsProvider;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use PhpOffice\PhpSpreadsheet\Exception;
  16. use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
  17. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  18. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  19. use Symfony\Component\Asset\Packages;
  20. use Symfony\Component\Form\Form;
  21. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\Response;
  24. use Symfony\Component\HttpFoundation\UrlHelper;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. class DefaultController extends AbstractController
  27. {
  28.     #[Route('/page/{slug}'name'base_page')]
  29.     public function page(BasePage $basePage): Response
  30.     {
  31.         return $this->render('page/view.html.twig', ['basePage' => $basePage]);
  32.     }
  33.     #[Route('/mark-email-{id}-as-read.png'name'notifications_mark_email_as_read')]
  34.     public function readEmailNotifications(EntityManagerInterface $emPackages $assetsManagerNotification $notificationUrlHelper $urlHelper): BinaryFileResponse
  35.     {
  36.         if ($notification->getState() != Notification::STATE_SEEN) {
  37.             $notification->setState(Notification::STATE_SEEN);
  38.             $em->persist($notification);
  39.             $em->flush();
  40.         }
  41.         return new BinaryFileResponse($urlHelper->getRelativePath($assetsManager->getUrl('images/pxl.png')));
  42.     }
  43.     #[Route("/admin/bao"name"toolbox")]
  44.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT') or is_granted('ROLE_SUPPORT')")]
  45.     public function bao(): Response
  46.     {
  47.         return $this->render('documents/bao.html.twig', [
  48.             'edit' => $this->getUser()->hasRole('ROLE_ADMIN'),
  49.         ]);
  50.     }
  51.     /**
  52.      * @throws Exception
  53.      * @throws WriterException
  54.      */
  55.     #[Route("/admin/export-dgefp"name"export.dgefp")]
  56.     #[Security("is_granted('ROLE_ADMIN')")]
  57.     public function exportCollector(ExcelExport $excelExport): BinaryFileResponse
  58.     {
  59.         return $this->file($excelExport->export());
  60.     }
  61.     /**
  62.      * @throws Exception
  63.      * @throws WriterException
  64.      */
  65.     #[Route("/admin/export-dgefp-dc"name"export.dgefp.dc")]
  66.     #[Security("is_granted('ROLE_ADMIN')")]
  67.     public function exportCollectorDc(ExcelExportDc $excelExportDc): BinaryFileResponse
  68.     {
  69.         return $this->file($excelExportDc->export());
  70.     }
  71.     #[Route("/"name"showcase")]
  72.     public function showCase(Request $requestCheckRecaptcha $checkRecaptchaNotifier $notifierEntityManagerInterface $em): Response
  73.     {
  74.         $dataRegions $em->getRepository(Region::class)->findAllByArray();
  75.         $regions = [];
  76.         foreach ($dataRegions as $region) {
  77.             $regions[$region['code']][] = $region;
  78.         }
  79.         $form $this->createForm(ContactShowCaseType::class, null, []);
  80.         $form->handleRequest($request);
  81.         if ($form->isSubmitted() && $form->isValid()) {
  82.             $token $form->get('recaptcha_token')->getData();
  83.             $checkRecaptcha $checkRecaptcha->check($token);
  84.             if ($checkRecaptcha) {
  85.                 $data $request->request->get('contact_show_case');
  86.                 $notifier->createMail($this->getParameter('global_mail'), 'contact_form', (array)$data);
  87.                 // On notifie également l'auteur du message
  88.                 $notifier->createMail($data['email'], 'contact_form_response');
  89.                 $this->addFlash('success''Votre message a bien été transmis à nos équipes qui reviendront vers vous dans les meilleurs délais.');
  90.             } else {
  91.                 $this->addFlash('danger''Message bloqué par le système anti spam, merci de réessayer depuis un autre poste, ou une autre connexion internet.');
  92.             }
  93.             return $this->redirectToRoute('showcase');
  94.         }
  95.         return $this->render('page/showcase.html.twig', [
  96.             'regions' => $regions,
  97.             'form' => $form->createView()
  98.         ]);
  99.     }
  100.     #[Route('/admin/statistiques'name'statistics')]
  101.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT')")]
  102.     public function statistics(StatisticsProvider $statisticsProviderRequest $request): Response
  103.     {
  104.         /** @var Form $form */
  105.         $form $this->createForm(ListingType::class, null, [
  106.             'filtersForm' => FilterStatisticsType::class
  107.         ]);
  108.         $form->handleRequest($request);
  109.         if ($form->isSubmitted() && $form->isValid()) {
  110.             $statisticsProvider->setDatas($form->get('filters')->getData());
  111.         }
  112.         return $this->render('statistics/index.html.twig', ['stats' => $statisticsProvider->getStats(), 'listingForm' => $form->createView()]);
  113.     }
  114.     #[Route('/admin/statistiques/csv'name'statistics.export')]
  115.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT')")]
  116.     public function statisticsExport(StatisticsProvider $statisticsProviderRequest $request): Response
  117.     {
  118.         /** @var Form $form */
  119.         $form $this->createForm(ListingType::class, null, [
  120.             'filtersForm' => FilterStatisticsType::class
  121.         ]);
  122.         $form->handleRequest($request);
  123.         if ($form->get('filters')->getData()) {
  124.             $statisticsProvider->setDatas($form->get('filters')->getData());
  125.         }
  126.         $stats $statisticsProvider->getStats();
  127.         $render $this->render('statistics/index.csv.twig', [
  128.             'stats' => $stats,
  129.         ]);
  130.         if ($request->get('debughtml')) {
  131.             return $render;
  132.         }
  133.         // Compat EXCEL
  134.         $body $render->getContent();
  135.         $body iconv('UTF-8''WINDOWS-1252//IGNORE'$body);
  136.         $body str_replace('&#039;'"'"$body); // Support des single quotes
  137.         return new Response(
  138.             $body,
  139.             Response::HTTP_OK,
  140.             [
  141.                 'content-type' => 'text/csv',
  142.                 'Content-Disposition' => 'attachment; filename="Statistiques-'.array_key_first($stats).'-'.date('Y-m-d').'.csv"',
  143.             ]
  144.         );
  145.     }
  146. }