<?php
namespace App\Controller;
use App\Entity\BasePage;
use App\Entity\Notification;
use App\Entity\Region;
use App\Form\ContactShowCaseType;
use App\Form\Filter\FilterStatisticsType;
use App\Form\ListingType;
use App\Service\CheckRecaptcha;
use App\Service\ExcelExport;
use App\Service\ExcelExportDc;
use App\Service\Notifier;
use App\Service\StatisticsProvider;
use Doctrine\ORM\EntityManagerInterface;
use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\UrlHelper;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
#[Route('/page/{slug}', name: 'base_page')]
public function page(BasePage $basePage): Response
{
return $this->render('page/view.html.twig', ['basePage' => $basePage]);
}
#[Route('/mark-email-{id}-as-read.png', name: 'notifications_mark_email_as_read')]
public function readEmailNotifications(EntityManagerInterface $em, Packages $assetsManager, Notification $notification, UrlHelper $urlHelper): BinaryFileResponse
{
if ($notification->getState() != Notification::STATE_SEEN) {
$notification->setState(Notification::STATE_SEEN);
$em->persist($notification);
$em->flush();
}
return new BinaryFileResponse($urlHelper->getRelativePath($assetsManager->getUrl('images/pxl.png')));
}
#[Route("/admin/bao", name: "toolbox")]
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT') or is_granted('ROLE_SUPPORT')")]
public function bao(): Response
{
return $this->render('documents/bao.html.twig');
}
/**
* @throws Exception
* @throws WriterException
*/
#[Route("/admin/export-dgefp", name: "export.dgefp")]
#[Security("is_granted('ROLE_ADMIN')")]
public function exportCollector(ExcelExport $excelExport): BinaryFileResponse
{
return $this->file($excelExport->export());
}
/**
* @throws Exception
* @throws WriterException
*/
#[Route("/admin/export-dgefp-dc", name: "export.dgefp.dc")]
#[Security("is_granted('ROLE_ADMIN')")]
public function exportCollectorDc(ExcelExportDc $excelExportDc): BinaryFileResponse
{
return $this->file($excelExportDc->export());
}
#[Route("/", name: "showcase")]
public function showCase(Request $request, CheckRecaptcha $checkRecaptcha, Notifier $notifier, EntityManagerInterface $em): Response
{
$dataRegions = $em->getRepository(Region::class)->findAllByArray();
$regions = [];
foreach ($dataRegions as $region) {
$regions[$region['code']][] = $region;
}
$form = $this->createForm(ContactShowCaseType::class, null, []);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$token = $form->get('recaptcha_token')->getData();
$checkRecaptcha = $checkRecaptcha->check($token);
if ($checkRecaptcha) {
$data = $request->request->get('contact_show_case');
$notifier->createMail($this->getParameter('global_mail'), 'contact_form', (array)$data);
// On notifie également l'auteur du message
$notifier->createMail($data['email'], 'contact_form_response');
$this->addFlash('success', 'Votre message a bien été transmis à nos équipes qui reviendront vers vous dans les meilleurs délais.');
} else {
$this->addFlash('danger', 'Message bloqué par le système anti spam, merci de réessayer depuis un autre poste, ou une autre connexion internet.');
}
return $this->redirectToRoute('showcase');
}
return $this->render('page/showcase.html.twig', [
'regions' => $regions,
'form' => $form->createView()
]);
}
#[Route('/admin/statistiques', name: 'statistics')]
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT')")]
public function statistics(StatisticsProvider $statisticsProvider, Request $request): Response
{
/** @var Form $form */
$form = $this->createForm(ListingType::class, null, [
'filtersForm' => FilterStatisticsType::class
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$statisticsProvider->setDatas($form->get('filters')->getData());
}
return $this->render('statistics/index.html.twig', ['stats' => $statisticsProvider->getStats(), 'listingForm' => $form->createView()]);
}
#[Route('/admin/statistiques/csv', name: 'statistics.export')]
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_PILOT')")]
public function statisticsExport(StatisticsProvider $statisticsProvider, Request $request): Response
{
/** @var Form $form */
$form = $this->createForm(ListingType::class, null, [
'filtersForm' => FilterStatisticsType::class
]);
$form->handleRequest($request);
if ($form->get('filters')->getData()) {
$statisticsProvider->setDatas($form->get('filters')->getData());
}
$stats = $statisticsProvider->getStats();
$render = $this->render('statistics/index.csv.twig', [
'stats' => $stats,
]);
if ($request->get('debughtml')) {
return $render;
}
// Compat EXCEL
$body = $render->getContent();
$body = iconv('UTF-8', 'WINDOWS-1252//IGNORE', $body);
$body = str_replace(''', "'", $body); // Support des single quotes
return new Response(
$body,
Response::HTTP_OK,
[
'content-type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="Statistiques-'.array_key_first($stats).'-'.date('Y-m-d').'.csv"',
]
);
}
}