src/Controller/DefaultController.php line 80

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.     }
  49.     /**
  50.      * @throws Exception
  51.      * @throws WriterException
  52.      */
  53.     #[Route("/admin/export-dgefp"name"export.dgefp")]
  54.     #[Security("is_granted('ROLE_ADMIN')")]
  55.     public function exportCollector(ExcelExport $excelExport): BinaryFileResponse
  56.     {
  57.         return $this->file($excelExport->export());
  58.     }
  59.     /**
  60.      * @throws Exception
  61.      * @throws WriterException
  62.      */
  63.     #[Route("/admin/export-dgefp-dc"name"export.dgefp.dc")]
  64.     #[Security("is_granted('ROLE_ADMIN')")]
  65.     public function exportCollectorDc(ExcelExportDc $excelExportDc): BinaryFileResponse
  66.     {
  67.         return $this->file($excelExportDc->export());
  68.     }
  69.     #[Route("/"name"showcase")]
  70.     public function showCase(Request $requestCheckRecaptcha $checkRecaptchaNotifier $notifierEntityManagerInterface $em): Response
  71.     {
  72.         $dataRegions $em->getRepository(Region::class)->findAllByArray();
  73.         $regions = [];
  74.         foreach ($dataRegions as $region) {
  75.             $regions[$region['code']][] = $region;
  76.         }
  77.         $form $this->createForm(ContactShowCaseType::class, null, []);
  78.         $form->handleRequest($request);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             $token $form->get('recaptcha_token')->getData();
  81.             $checkRecaptcha $checkRecaptcha->check($token);
  82.             if ($checkRecaptcha) {
  83.                 $data $request->request->get('contact_show_case');
  84.                 $notifier->createMail($this->getParameter('global_mail'), 'contact_form', (array)$data);
  85.                 // On notifie également l'auteur du message
  86.                 $notifier->createMail($data['email'], 'contact_form_response');
  87.                 $this->addFlash('success''Votre message a bien été transmis à nos équipes qui reviendront vers vous dans les meilleurs délais.');
  88.             } else {
  89.                 $this->addFlash('danger''Message bloqué par le système anti spam, merci de réessayer depuis un autre poste, ou une autre connexion internet.');
  90.             }
  91.             return $this->redirectToRoute('showcase');
  92.         }
  93.         return $this->render('page/showcase.html.twig', [
  94.             'regions' => $regions,
  95.             'form' => $form->createView()
  96.         ]);
  97.     }
  98.     #[Route('/admin/statistiques'name'statistics')]
  99.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT')")]
  100.     public function statistics(StatisticsProvider $statisticsProviderRequest $request): Response
  101.     {
  102.         /** @var Form $form */
  103.         $form $this->createForm(ListingType::class, null, [
  104.             'filtersForm' => FilterStatisticsType::class
  105.         ]);
  106.         $form->handleRequest($request);
  107.         if ($form->isSubmitted() && $form->isValid()) {
  108.             $statisticsProvider->setDatas($form->get('filters')->getData());
  109.         }
  110.         return $this->render('statistics/index.html.twig', ['stats' => $statisticsProvider->getStats(), 'listingForm' => $form->createView()]);
  111.     }
  112.     #[Route('/admin/statistiques/csv'name'statistics.export')]
  113.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT')")]
  114.     public function statisticsExport(StatisticsProvider $statisticsProviderRequest $request): Response
  115.     {
  116.         /** @var Form $form */
  117.         $form $this->createForm(ListingType::class, null, [
  118.             'filtersForm' => FilterStatisticsType::class
  119.         ]);
  120.         $form->handleRequest($request);
  121.         if ($form->get('filters')->getData()) {
  122.             $statisticsProvider->setDatas($form->get('filters')->getData());
  123.         }
  124.         $stats $statisticsProvider->getStats();
  125.         $render $this->render('statistics/index.csv.twig', [
  126.             'stats' => $stats,
  127.         ]);
  128.         if ($request->get('debughtml')) {
  129.             return $render;
  130.         }
  131.         // Compat EXCEL
  132.         $body $render->getContent();
  133.         $body iconv('UTF-8''WINDOWS-1252//IGNORE'$body);
  134.         $body str_replace('&#039;'"'"$body); // Support des single quotes
  135.         return new Response(
  136.             $body,
  137.             Response::HTTP_OK,
  138.             [
  139.                 'content-type' => 'text/csv',
  140.                 'Content-Disposition' => 'attachment; filename="Statistiques-'.array_key_first($stats).'-'.date('Y-m-d').'.csv"',
  141.             ]
  142.         );
  143.     }
  144. }