<?php
namespace App\Controller\Services;
use App\Entity\CustomForm;
use App\Entity\Footer;
use App\Entity\FooterSites;
use App\Entity\FormulaireThematique;
use App\Entity\Glossaire;
use App\Entity\InscritNewsletter;
use App\Entity\LogEs;
use App\Entity\PageAccueilService;
use App\Entity\PagesLegales;
use App\Entity\Service;
use App\Entity\ServiceFeedback;
use App\Entity\ServiceGrandProfil;
use App\Entity\ServiceSousProfil;
use App\Entity\ServiceTheme;
use Dompdf\Dompdf;
use Dompdf\Options;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Doctrine\ORM\EntityManagerInterface;
use App\Utilities\Feed;
use App\Service\ElasticsearchService;
use App\Service\EpingleService;
use App\Service\RssService;
use App\Service\SendgridMailService;
use App\Service\SeoService;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
/**
* @method User getUser()
*/
#[Route(host: "serviceslenord.spontaneit.fr")]
//#[Route(host: "services.lenord.fr")]
//#[Route(host:"localhost")]
class MainServicesController extends AbstractController
{
protected mixed $params = [];
private mixed $session;
public function __construct(
private EntityManagerInterface $em,
private Feed $feedUtilities,
private RssService $rss_service,
private ElasticsearchService $es_service,
private EpingleService $epingle_service,
private RequestStack $request_stack,
private SendgridMailService $sendgrid_service,
private SeoService $seo_service
) {
$this->epingle_service = $epingle_service;
$this->rss_service = $rss_service;
$this->es_service = $es_service;
$this->feedUtilities = $feedUtilities;
$this->sendgrid_service = $sendgrid_service;
$this->seo_service = $seo_service;
$this->session = $this->request_stack->getSession();
$this->params['title'] = "";
$this->params['epingles'] = [];
$this->params['is_pdf'] = 0;
$this->params['is_preview'] = 0;
$this->params['breadcrumb'] = [];
$this->params['footer'] = $this->em->getRepository(Footer::class)->find(1);
}
#[Route(path: '/sitemap.xml', name: 'services_plandusite_xml')]
public function services_plandusite_xml(Request $request): Response
{
$date = new \DateTime();
$domain = "https://services.lenord.fr";
$dom = new \DOMDocument('1.0', 'UTF-8');
$rss = $dom->createElement('urlset');
$rss->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$rss_node = $dom->appendChild($rss);
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_home')));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_services', ['slug' => 'particulier'])));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_services', ['slug' => 'professionnel'])));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_services', ['slug' => 'association'])));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_services', ['slug' => 'collectivite'])));
$services = $this->em->getRepository(Service::class)->findBy(['statut' => 5]);
foreach ($services as $service) {
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_detail_service', ['slug' => $service->getAliasUrl()])));
$url_node->appendChild($dom->createElement('lastmod', ($service->getDateUpdate() != null) ? $service->getDateUpdate()->format('Y-m-d') : $date->format('Y-m-d')));
$url_node->appendChild($dom->createElement('changefreq', 'monthly'));
}
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_support')));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_glossaire')));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', 'https://demarches.services.lenord.fr/portals/usagers/forum?page=monCompte'));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_recherche')));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_detail_service', ['slug' => 'mentions-legales-nord-services'])));
$url = $dom->createElement('url');
$url_node = $rss_node->appendChild($url);
$url_node->appendChild($dom->createElement('loc', $domain . $this->generateUrl('services_detail_service', ['slug' => 'accessibilite-nord-services'])));
$xml = $dom->saveXML();
$response = new Response($xml);
$response->headers->set('Content-Type', 'text/xml');
$response->headers->set('Access-Control-Allow-Origin', '*');
return $response;
}
#[Route(path: '/plan-du-site', name: 'services_plandusite')]
public function services_plandusite(Request $request): Response
{
$this->params['title'] = "Plan du site";
$this->params['meta_description'] = "Le plan du site Nord services";
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => $this->params['title']]];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
$this->params['referer'] = $request->headers->get('referer');
$urls = [];
// Home
$urls['index']['parent']['url'] = $this->generateUrl('services_home');
$urls['index']['parent']['name'] = "Accueil";
//Services
//Particuliers
$urls['particuliers']['parent']['url'] = $this->generateUrl('services_services', ['slug' => 'particulier']);
$urls['particuliers']['parent']['name'] = "Particuliers";
//Professionnels
$urls['professionnels']['parent']['url'] = $this->generateUrl('services_services', ['slug' => 'professionnel']);
$urls['professionnels']['parent']['name'] = "Professionnels";
//Associations
$urls['associations']['parent']['url'] = $this->generateUrl('services_services', ['slug' => 'association']);
$urls['associations']['parent']['name'] = "Associations";
//Collectivités
$urls['collectivites']['parent']['url'] = $this->generateUrl('services_services', ['slug' => 'collectivite']);
$urls['collectivites']['parent']['name'] = "Collectivités";
//Services
$themes = $this->em->getRepository(ServiceTheme::class)->findAll();
foreach ($themes as $theme) {
if ($theme->getServices()->count() > 0) {
$urls[$theme->getAliasUrl()]['parent']['url'] = null;
$urls[$theme->getAliasUrl()]['parent']['name'] = $theme->getTitre();
$i = 0;
foreach ($theme->getServices() as $service) {
$urls[$theme->getAliasUrl()]['children'][$i]['url'] = $this->generateUrl('services_detail_service', ['slug' => $service->getAliasUrl()]);
$urls[$theme->getAliasUrl()]['children'][$i]['name'] = $service->getTitre();
$i++;
}
}
}
//Contact / Support
$urls['support']['parent']['url'] = $this->generateUrl('services_support');
$urls['support']['parent']['name'] = "Contact";
//Glossaire
$urls['glossaire']['parent']['url'] = $this->generateUrl('services_glossaire');
$urls['glossaire']['parent']['name'] = "Glossaire";
//Demarches
$urls['glossaire']['parent']['url'] = 'https://demarches.services.lenord.fr/portals/usagers/forum?page=monCompte';
$urls['glossaire']['parent']['name'] = "Nord démarches";
//Recherche
$urls['recherche']['parent']['url'] = $this->generateUrl('services_recherche');
$urls['recherche']['parent']['name'] = "Recherche";
//Mentions légales
$urls['mentionlegale']['parent']['url'] = $this->generateUrl('services_detail_service', ['slug' => 'mentions-legales-nord-services']);
$urls['mentionlegale']['parent']['name'] = "Mentions légales";
//Accessibilité
$urls['accessibilite']['parent']['url'] = $this->generateUrl('services_detail_service', ['slug' => 'accessibilite-nord-services']);
$urls['accessibilite']['parent']['name'] = "Accessibilité";
$this->params['urls'] = $urls;
//Epingle
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
if ($request->get('function') == 'download_pdf') {
$this->params['is_pdf'] = 1;
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
$dompdf = new Dompdf($pdfOptions);
$html = $this->renderView('Services/Main/plandusite.html.twig', $this->params);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$output = $dompdf->output();
$filename = sprintf('le-nord-%s.pdf', $this->params['title']);
return new Response(
$output,
200,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', "$filename"),
]
);
} else {
return $this->render('Services/Main/plandusite.html.twig', $this->params);
}
}
#[Route(path: '/', name: 'services_home')]
public function home(): Response
{
$this->params['title'] = "";
$this->params['meta_description'] = "Tous les services du Département du Nord pour les particuliers, les professionnels, les associations et les collectivités";
$this->params['home'] = $this->em->getRepository(PageAccueilService::class)->find(1);
//
$count_services_home = $this->params['home']->getServices()->count();
$this->params['services_forcomplete'] = [];
if ($count_services_home < 9) {
$limit = 9 - $count_services_home;
$this->params['services_forcomplete'] = $this->em->getRepository(Service::class)->findBy(['statut' => 5], ['view_count' => 'DESC'], 9);
}
$this->params['count_sous_profils'] = count($this->em->getRepository(ServiceSousProfil::class)->findAll());
$this->params['sites'] = $this->em->getRepository(FooterSites::class)->getForFooter(false, true);
$this->params['nordinfo_rss'] = null;
if ($this->params['home']->getUrlRssNordinfo() != null) {
$this->params['nordinfo_rss'] = $this->rss_service->getArrayArticle($this->params['home']->getUrlRssNordinfo());
}
//Epingle
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$epingles_ = $this->epingle_service->getEpinglesSection('home_services');
foreach ($epingles_ as $epingle) {
array_push($this->params['epingles'], $epingle);
}
return $this->render('Services/Main/home.html.twig', $this->params);
}
#[Route(path: '/console2021/apercu_service/{id}', name: 'apercu_service')]
public function apercu_service(Request $request, mixed $id): Response
{
$this->params['service'] = $this->em->getRepository(Service::class)->findOneBy(['alias_url' => $id]);
$this->params['service']->setViewCount($this->params['service']->getViewCount() + 1);
$this->em->persist($this->params['service']);
$this->em->flush();
$this->params['id'] = $this->params['service']->getId();
$this->params['title'] = $this->params['service']->getTitre();
$this->params['meta_description'] = $this->params['service']->getChapo();
$this->params['content_post'] = $this->feedUtilities->jsonToHtml($this->params['service']->getContent(), $request);
$this->params['count_sous_profils'] = count($this->em->getRepository(ServiceSousProfil::class)->findAll());
$this->params['referer'] = $request->headers->get('referer');
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => $this->params['title']]];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
$this->params['openGraph'] = ["title" => $this->params['service']->getTitre(), "url" => "https://services.lenord.fr/" . $id, "image" => "https://services.lenord.fr/img/logos/logo-nord-services.svg", "type" => "website", "description" => $this->params['content_post']];
$this->params['hasVoted'] = $this->session->has('vote_' . $this->params['service']->getId());
$epingles_politiques = $this->epingle_service->getEpinglesSection('services');
foreach ($epingles_politiques as $epingle) {
array_push($this->params['epingles'], $epingle);
}
foreach ($this->params['service']->getEpingles() as $e) {
if ($e->getActive() == 1) {
array_push($this->params['epingles'], $e);
}
}
if ($request->get('function') == 'download_pdf') {
$this->params['is_pdf'] = 1;
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
$dompdf = new Dompdf($pdfOptions);
$html = $this->renderView('Services/Main/service.html.twig', $this->params);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$output = $dompdf->output();
$filename = sprintf('le-nord-%s.pdf', $this->params['title']);
return new Response(
$output,
200,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', "$filename"),
]
);
} else {
return $this->render('Services/Main/service.html.twig', $this->params);
}
}
#[Route(path: '/recherche', name: 'services_recherche')]
public function services_recherche(Request $request): Response
{
$this->params['results'] = null;
if ($request->query->get('search') != null) {
$search_fields = ['combined'];
$results = $this->es_service->searchDocuments('lenord', $search_fields, $request->query->get('search'), ['service', 'glossaire'], [], [], 25, true);
// $results = [];
$this->params['results'] = $results;
//Save log
$logEs = new LogEs;
$logEs->setRecherche($request->query->get('search'))
->setDate(new \DateTime())
->setSite("services");
$this->em->persist($logEs);
$this->em->flush();
}
$this->params['title'] = 'Recherche: "' . $request->query->get('search') . '"';
$this->params['search'] = $request->query->get('search');
$this->params['sites'] = $this->em->getRepository(FooterSites::class)->getForFooter(false, true);
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$this->params['count_sous_profils'] = count($this->em->getRepository(ServiceSousProfil::class)->findAll());
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => 'Recherche']];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
$this->params['referer'] = $request->headers->get('referer');
$epingles_politiques = $this->epingle_service->getEpinglesSection('services');
foreach ($epingles_politiques as $epingle) {
array_push($this->params['epingles'], $epingle);
}
return $this->render('Services/Main/search.html.twig', $this->params);
}
#[Route(path: '/services_recherche/ajax', name: 'services_elastic_search')]
public function services_elastic_search(Request $request): Response
{
$search_fields = ['title', 'content', 'subtitle'];
$results = $this->es_service->searchDocuments('lenord', $search_fields, $request->query->get('search'), 'service');
return new JsonResponse($results['hits']['hits']);
}
private function getCaptcha(): mixed
{
$captchaCh = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$captchaLe = ["zéro", "un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf", "dix"];
$testcap = array_merge($captchaCh, $captchaLe);
$rnd = array_rand($testcap, 2);
$i1 = $rnd[0];
if ($rnd[0] >= 11) {
$i1 = $rnd[0] - 11;
}
$i2 = $rnd[1];
if ($rnd[1] >= 11) {
$i2 = $rnd[1] - 11;
}
$capp = ["valeur1" => ["libelle" => $testcap[$rnd[0]], "valeur" => $captchaCh[$i1]], "valeur2" => ["libelle" => $testcap[$rnd[1]], "valeur" => $captchaCh[$i2]]];
return $capp;
}
#[Route(path: '/glossaire', name: 'services_glossaire')]
public function glossaire(Request $request): Response
{
$this->params['title'] = "Glossaire";
$this->params['meta_description'] = "Le glossaire du site du Département du Nord : acronymes et définitions utiles";
$glossaire = $this->em->getRepository(Glossaire::class)->findBy([], ['terme' => 'ASC']);
$this->params['glossaires'] = $glossaire;
$this->params['letters'] = $this->em->getRepository(Glossaire::class)->getFirstLetters();
$this->params['sites'] = $this->em->getRepository(FooterSites::class)->getForFooter(false, true);
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => 'Glossaire']];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
$this->params['referer'] = $request->headers->get('referer');
//Epingle
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$epingles_ = $this->epingle_service->getEpinglesSection('glossaire_services');
foreach ($epingles_ as $epingle) {
array_push($this->params['epingles'], $epingle);
}
return $this->render('Services/Main/glossaire.html.twig', $this->params);
}
#[Route(path: '/ajax/serviceSaveAvis', name: 'services_save_avis')]
public function serviceSaveAvis(Request $request): Response
{
$service = $this->em->getRepository(Service::class)->find($request->request->get('id'));
if ($request->request->get('valeur') == 'positif') {
$service->setAvisPositif($service->getAvisPositif() + 1);
} else {
$service->setAvisNegatif($service->getAvisNegatif() + 1);
$feedback = new ServiceFeedback;
$feedback->setService($service)
->setDate(new \DateTime())
->setCommentaire($request->request->get('comment'));
$this->em->persist($feedback);
}
$this->em->persist($service);
$this->em->flush();
$this->session->set('vote_' . $request->request->get('id'), true);
return new JsonResponse(['success' => true]);
}
#[Route(path: '/ajax/getServicesByGrandprofil', name: 'services_ajax_getServices')]
public function getServicesByGrandprofil(Request $request): Response
{
$services = $this->em->getRepository(Service::class)->getServicesByGrandprofil($request->request->get('alias'));
return new JsonResponse(['datas' => $services]);
}
#[Route(path: '/ajax/getServicesAll', name: 'services_ajax_getServices')]
public function getServicesAll(Request $request): Response
{
$services = $this->em->getRepository(Service::class)->getServicesByGrandprofil($request->request->get('alias'));
return new JsonResponse(['datas' => $services]);
}
#[Route(path: '/formulaire-de-contact', name: 'services_support')]
public function services_support(Request $request): Response
{
$this->params['title'] = "Formulaire de contact";
$message = $request->query->get('message');
$this->params['_message'] = $message;
$this->params['meta_description'] = "Contacter les services du Département du Nord par courriel";
$this->params['referer'] = $request->headers->get('referer');
$this->params['caps'] = $this->getCaptcha();
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => "Formulaire de contact"]];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
$this->params['thematiques'] = $this->em->getRepository(FormulaireThematique::class)->findBy(['bool_services' => 1], ['name' => 'ASC']);
return $this->render('Services/Main/support.html.twig', $this->params);
}
/*
#[Route(path: '/formulaire-{slug}', name: 'custom_form_services', host: "services.lenord.fr")]
public function custom_form(Request $request, ?string $slug): Response
{
$custom_form = $this->em->getRepository(CustomForm::class)->findOneBy(array('alias' => $slug));
$this->params['caps'] = $this->getCaptcha();
$this->params['form'] = $custom_form;
return $this->render('Services/Main/custom_form.html.twig', $this->params);
}
*/
#[Route(path: '/send_form_custom/{alias}', name: 'send_form_custom_services', host: "services.lenord.fr")]
public function send_form_custom(Request $request, ?string $alias): Response
{
$post = $request->request;
$custom_form = $this->em->getRepository(CustomForm::class)->findOneBy(array('alias' => $alias));
$destinataires_sendgrid = [];
$objet = null;
$objet = "Votre message à " . $custom_form->getDestinataire() . " via le site Nord services";
$emails = explode(";", (string) $custom_form->getEmail());
for ($i = 0; $i < count($emails); $i++) {
array_push($destinataires_sendgrid, $emails[$i]);
}
$message = $post->get('message');
//exp
//destinataire_
//objet_
//message_
//piece_jointe_
$fileName = null;
$auth_extension = ['png', 'jpg', 'jpeg', 'gif', 'pdf'];
$mime_authorized = ['application/pdf', 'image/jpg', 'image/jpeg', 'image/gif', 'image/png'];
$file = null;
$newname = uniqid();
if ($request->files != null && $request->files->get('file') != null) {
$file = $request->files->get('file');
$fileName = $file->getClientOriginalName();
$fileType = $file->getClientMimeType();
if (in_array($file->guessExtension(), $auth_extension) && in_array($fileType, $mime_authorized)) {
/*try {
/*$file->move($uploadDir, $newname.'.'.$file->guessExtension());
array_push($files, array(
'name' => $newname.'.'.$file->guessExtension(),
'type' => $fileType,
'data' => $fileData,
));
} catch (FileException) {
$file = null;
return new JsonResponse(['error' => 'File could not be uploaded']);
}*/
} else {
$file = null;
return new JsonResponse(['error' => "Mauvais format d'image"]);
}
}
$date = new \DateTime;
/*$content = $this->renderView(
'Nordinfo/Mails/dest_signalement2.html.twig',
['expediteur_email_' => $post->get('email'), 'destinataire_' => "agent en charge du support aux usagers", 'objet_' => $objet, 'message_' => $message, 'piece_jointe_' => ($fileName != null) ? $fileName : "Aucune pièce jointe", 'date' => "NI".$date->format('dmYHis')]
);*/
$content = "Le message suivant vient de vous être envoyé via le site Nord services par " . $post->get('nom_prenom') .
" - Merci de répondre directement à " . $post->get('email') . " : " . "\n" . $post->get('message') . "\n\n" . "Nord, le Département est là - https://services.lenord.fr/";
$nom_dest = $custom_form->getDestinataire();
$content_exp = "Bonjour, votre message a été transmis à " . $nom_dest . "." . "\n\n" .
"Message : " . $post->get('message');
if ($fileName !== null) {
$content_exp .= "\n" . "Pièce jointe : " . $fileName;
}
$content_exp .= "\n\n" . "Nord, le Département est là - https://services.lenord.fr/";
//envoi mail
$result_ = $post->get('nb1') + $post->get('nb2');
if ($result_ == $post->get('captcha') && $post->get('raison') == null) {
try {
$this->sendgrid_service->send($destinataires_sendgrid, $content, "Message envoyé par " . $post->get('nom_prenom') . " via le site Nord services", null, $file, false, $post->get('email'));
$this->sendgrid_service->send([$post->get('email')], $content_exp, $objet, null, $file, false);
} catch (\Exception) {
$this->params['message'] = 'Email erreur';
return $this->render('Services/Main/endformerror.html.twig', $this->params);
}
} else {
$this->params['message'] = 'Catcha erreur';
return $this->render('Services/Main/endformerror.html.twig', $this->params);
}
return $this->render('Services/Main/endform.html.twig', $this->params);
}
#[Route(path: '/confirmation', name: 'services_send_form')]
public function sendform(Request $request): Response
{
$post = $request->request;
$result_ = $post->get('nb1') + $post->get('nb2');
if ($result_ == $post->get('captcha')) {
$destinataires_sendgrid = [];
$message = $post->get('message');
//Thematique
$thematique = null;
$formThematique = $this->em->getRepository(FormulaireThematique::class)->find($post->get('thematique'));
$thematique = $formThematique->getName();
$emails = explode(";", (string) $formThematique->getDestinataires());
for ($i = 0; $i < count($emails); $i++) {
array_push($destinataires_sendgrid, $emails[$i]);
}
//exp
//destinataire_
//objet_
//message_
//piece_jointe_
$fileName = null;
$auth_extension = ['png', 'jpg', 'jpeg', 'gif', 'pdf'];
$mime_authorized = ['application/pdf', 'image/jpg', 'image/jpeg', 'image/gif', 'image/png'];
$file = null;
if ($request->files != null && $request->files->get('file') != null) {
$file = $request->files->get('file');
$fileName = $file->getClientOriginalName();
$fileType = $file->getClientMimeType();
if (in_array($file->guessExtension(), $auth_extension) && in_array($fileType, $mime_authorized)) {
//$file->move($uploadDir, $newname.'.'.$file->guessExtension());
/*try {
array_push($files, array(
'name' => $newname.'.'.$file->guessExtension(),
'type' => $fileType,
'data' => $fileData,
));
} catch (FileException) {
$file = null;
return new JsonResponse(['error' => 'File could not be uploaded']);
}*/
} else {
$file = null;
return new JsonResponse(['error' => "Mauvais format d'image"]);
}
}
$date = new \DateTime;
$content = $this->renderView(
'Services/Mails/dest_signalement2.html.twig',
['site' => 'services', 'expediteur_email_' => ($post->get('prenom') != null && $post->get('nom') != null) ? $post->get('prenom') . ' ' . $post->get('nom') : $post->get('email'), 'destinataire_' => "agent en charge du support aux usagers", 'objet_' => 'Nouveau message sur Nord services - ' . $thematique . ' / ' . "NS" . $date->format('dmYHis'), 'thematique' => $thematique, 'email' => $post->get('email'), 'message_' => $message, 'piece_jointe_' => ($fileName != null) ? $fileName : "Aucune pièce jointe", 'date' => "NS" . $date->format('dmYHis')]
);
$nom_dest = 'rédaction du Nord info';
if ($post->get('dest_nom') != null) {
$nom_dest = $post->get('dest_nom');
}
$content_exp = $this->renderView(
'Services/Mails/exp_signalement2.html.twig',
['site' => 'services', 'destinataire_' => $nom_dest, 'objet_' => 'Votre message sur Nord services - ' . $thematique . ' / ' . "NS" . $date->format('dmYHis'), 'thematique' => $thematique, 'email' => $post->get('email'), 'message_' => $message, 'piece_jointe_' => ($fileName != null) ? $fileName : "Aucune pièce jointe", 'date' => "NS" . $date->format('dmYHis')]
);
//Newsletter
if ($result_ == $post->get('captcha')) {
if ($post->get('newsletter') != null) {
$ins = $this->em->getRepository(InscritNewsletter::class)->findOneBy(['email' => $post->get('email')]);
if ($ins == null) {
$ins = new InscritNewsletter;
$ins->setEmail($post->get('email'))
->setPrenom($post->get('prenom'))
->setNom($post->get('nom'))
->setDate(new \DateTime());
$this->em->persist($ins);
$this->em->flush();
}
}
//envoi mail
$result_ = $post->get('nb1') + $post->get('nb2');
try {
$this->sendgrid_service->send($destinataires_sendgrid, $content, 'Nouveau message sur Nord Services - ' . $thematique . ' / ' . "NS" . $date->format('dmYHis'), null, $file);
$this->sendgrid_service->send([$post->get('email')], $content_exp, 'Votre message sur Nord Services - ' . $thematique . ' / ' . "NS" . $date->format('dmYHis'), null, $file);
} catch (\Exception) {
$this->params['message'] = 'Email erreur';
return $this->render('Services/Main/endformerror.html.twig', $this->params);
}
}
} else {
$this->params['message'] = 'Captcha erreur';
return $this->render('Services/Main/endformerror.html.twig', $this->params);
}
return $this->render('Services/Main/endform.html.twig', $this->params);
}
#[Route(path: '/{slug}', name: 'services_services', requirements: ['slug' => 'particulier|professionnel|association|collectivite'])]
public function services(Request $request, ?string $slug): Response
{
$this->params['slug'] = $slug;
$this->params['grp'] = $this->em->getRepository(ServiceGrandProfil::class)->findOneBy(['alias_url' => $slug]);
$sousprofils = [];
$sousprofils_id = [];
$array_fields = [];
$sousprofils_title = [];
$sousprofils_entities = [];
$search_text = null;
$this->params['title'] = ucfirst((string) $slug);
if ($slug == 'collectivite') {
$this->params['title'] = "Collectivité";
}
$this->params['meta_description'] = "Tous les services du Département du Nord pour les " . $slug . "s nordistes";
if ($request->query->get('sp') != null) {
//$array_fields = array('sousprofils.alias');
$i = 0;
foreach ((array)$request->query->get('sp') as $alias) {
array_push($sousprofils, $alias);
$sousp = $this->em->getRepository(ServiceSousProfil::class)->findOneBy(['alias_url' => $alias]);
array_push($sousprofils_entities, $sousp);
array_push($sousprofils_id, $sousp->getId());
array_push($sousprofils_title, $sousp->getTitre());
if ($i > 0) {
$this->params['title'] .= " & ";
}
$this->params['title'] .= ' ' . '"' . $sousp->getTitre() . '"';
$i++;
}
}
$this->params['sousprofils_entities'] = $sousprofils_entities;
$this->params['filters'] = $request->query->get('sp');
$this->params['totalCount'] = $this->em->getRepository(Service::class)->getTotalCount($slug, $sousprofils_id);
$this->params['results'] = [];
$this->params['results'] = $this->es_service->searchDocuments('lenord', $array_fields, $search_text, 'service', [$slug], $sousprofils, $this->params['totalCount']['nb_service']);
$this->params['themes'] = $this->em->getRepository(ServiceTheme::class)->countByTheme($slug, $sousprofils_id);
$all_themes = $this->em->getRepository(ServiceTheme::class)->getAllByGp($slug);
$theme_arr = [];
foreach ($all_themes as $theme) {
$nb_service = 0;
$key = array_search($theme, $this->params['themes']);
if ($key !== false) {
$nb_service = $this->em->getRepository(ServiceTheme::class)->countService($slug, $sousprofils_id, $theme->getId())['nb_service'];
}
array_push($theme_arr, ['nb_service' => $nb_service, 'titre' => $theme->getTitre(), 'alias_url' => $theme->getAliasUrl()]);
}
$this->params['all_themes'] = $theme_arr;
$this->params['sites'] = $this->em->getRepository(FooterSites::class)->getForFooter(false, true);
$this->params['count_sous_profils'] = count($this->em->getRepository(ServiceSousProfil::class)->findAll());
//breadcrumb
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => 'Particulier']];
if ($slug == 'collectivite') {
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => 'Collectivité']];
} else if ($slug == 'association') {
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => 'Association']];
} else if ($slug == 'professionnel') {
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => 'Professionnel']];
}
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
if ($this->params['title'] == "collectivite") {
$this->params['title'] = "Collectivité";
}
foreach ($sousprofils_title as $title_sp) {
$this->params['title'] .= ' "' . $title_sp . '"';
}
//Epingle
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$epingles_politiques = $this->epingle_service->getEpinglesSection('services');
foreach ($epingles_politiques as $epingle) {
array_push($this->params['epingles'], $epingle);
}
//dd($this->params['results']['hits']);
return $this->render('Services/Main/services.html.twig', $this->params);
}
#[Route(path: '/store_preferences', name: 'services_store_preferences')]
public function storeUserPreferences(Request $request): Response
{
$post = $request->request;
$function = $post->get('function');
$user = $this->getUser();
switch ($function) {
case 'store_dark_mode':
$user->setDarkMode((int)$post->get('dark_mode'));
$this->em->persist($user);
$this->em->flush();
break;
case 'store_font_size':
$user->setFontSize($post->get('font_size'));
$this->em->persist($user);
$this->em->flush();
break;
case 'store_font_family':
$user->setFontFamily($post->get('font_family'));
$this->em->persist($user);
$this->em->flush();
break;
}
return new JsonResponse(['success' => true]);
}
/**
* @throws \JsonException
*/
#[Route(path: '/{slug}', name: 'services_detail_service')]
public function service(Request $request, ?string $slug): Response
{
$this->params['service'] = $this->em->getRepository(Service::class)->findOneBy(['alias_url' => $slug]);
if ($this->params['service'] == null) {
return $this->pagelegales($request, $slug);
}
if ($this->params['service']->getStatut() != null && $this->params['service']->getStatut()->getId() == 5) {
$this->params['service']->setViewCount($this->params['service']->getViewCount() + 1);
$this->em->persist($this->params['service']);
$this->em->flush();
$this->params['id'] = $this->params['service']->getId();
$this->params['title'] = $this->params['service']->getTitre();
$this->params['meta_description'] = $this->params['service']->getChapo();
$this->params['content_post'] = $this->feedUtilities->jsonToHtml($this->params['service']->getContent(), $request, $this->params['title']);
$this->params['count_sous_profils'] = count($this->em->getRepository(ServiceSousProfil::class)->findAll());
$this->params['referer'] = $request->headers->get('referer');
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => $this->params['title']]];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
$this->params['openGraph'] = ["title" => $this->params['service']->getTitre(), "url" => "https://services.lenord.fr/" . $slug, "image" => "https://services.lenord.fr/img/logos/logo-nord-services.svg", "type" => "website", "description" => $this->params['content_post']];
$this->params['hasVoted'] = $this->session->has('vote_' . $this->params['service']->getId());
$epingles_politiques = $this->epingle_service->getEpinglesSection('services');
foreach ($epingles_politiques as $epingle) {
array_push($this->params['epingles'], $epingle);
}
foreach ($this->params['service']->getEpingles() as $e) {
if ($e->getActive() == 1) {
array_push($this->params['epingles'], $e);
}
}
if ($request->get('function') == 'download_pdf') {
$this->params['is_pdf'] = 1;
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
$dompdf = new Dompdf($pdfOptions);
$html = $this->renderView('Services/Main/service.html.twig', $this->params);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$output = $dompdf->output();
$filename = sprintf('le-nord-%s.pdf', $this->params['title']);
return new Response(
$output,
200,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', "$filename"),
]
);
} else {
return $this->render('Services/Main/service.html.twig', $this->params);
}
} else {
return $this->pagelegales($request, "erreur404");
}
}
/**
* @throws \JsonException
*/
public function pagelegales(Request $request, ?string $slug): Response
{
$this->params['referer'] = $request->headers->get('referer');
$post = $this->em->getRepository(PagesLegales::class)->findOneBy(['slug' => $slug, 'actif' => 1]);
if (!$post) {
throw $this->createNotFoundException('La page demandée n\'existe pas.');
}
$this->params['post'] = $post;
$this->params['title'] = $this->params['post']->getTitre();
$this->params['meta_description'] = $this->params['post']->getMetaDescription();
$this->params['epingles'] = $this->epingle_service->getEpingles('services');
$epingles_ = $this->epingle_service->getEpinglesSection('legales_services');
foreach ($epingles_ as $epingle) {
array_push($this->params['epingles'], $epingle);
}
$this->params['breadcrumb'] = [['alias' => '/', 'titre' => 'Accueil'], ['alias' => '', 'titre' => $this->params['title']]];
$this->params['seo_breadcrumb'] = json_encode($this->seo_service->getSeoBreadcrumb($this->params['breadcrumb'], $request), JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_HEX_APOS);
if ($request->get('function') === 'download_pdf') {
$this->params['is_pdf'] = 1;
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
$dompdf = new Dompdf($pdfOptions);
$html = $this->renderView('Services/Main/detail_page_legale.html.twig', $this->params);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$output = $dompdf->output();
$filename = sprintf('le-nord-%s.pdf', $this->params['title']);
return new Response(
$output,
200,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', "$filename"),
]
);
} else {
return $this->render('Services/Main/detail_page_legale.html.twig', $this->params);
}
}
}