<?php
namespace App\Controller;
use App\Entity\EmailBlacklist;
use App\Entity\TousEnLigneContact;
use App\Repository\AccountingFirmRepository;
use App\Repository\AuthorizedDomainRepository;
use App\Services\WidgetMailing;
use Doctrine\ORM\EntityManagerInterface;
use Qferrer\Mjml\Twig\MjmlExtension;
use Response;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
class WidgetTousEnLigneController extends AbstractController
{
protected MjmlExtension $mjmlRenderer;
protected Environment $twig;
public function __construct(MailerInterface $mailer, MjmlExtension $mjmlRenderer, Environment $twig)
{
$this->mjmlRenderer = $mjmlRenderer;
$this->twig = $twig;
}
#[Route('/check-tous-en-ligne', name: 'w_tous_en_ligne_check')]
public function ctrl_checkTousEnLigne(Request $request, AccountingFirmRepository $repository, string $prefix = null): JsonResponse
{
$token = $request->query->get('token');
$host = $request->get('host');
if ($token && $host) {
$accountingFirm = $repository->findOneBy(['tousEnLigneToken' => $token]);
$isAuthorized = false;
if ($accountingFirm && in_array($host, $accountingFirm->getListAuthorizedDomains())) {
$isAuthorized = true;
}
$url = $isAuthorized ? $accountingFirm->getTousEnLigneWidgetUrl() : null;
} else {
$accountingFirm = $repository->findOneBy(['host' => $host]);
$url = $accountingFirm ? $prefix . $this->generateUrl('w_tous_en_ligne_get') : null;
}
return $this->json($url);
}
#[Route('/widget-tous-en-ligne', name: 'w_tous_en_ligne_get')]
public function ctrl_widgetTousEnLigne(Request $request, AccountingFirmRepository $repository, AuthorizedDomainRepository $domains)
{
$token = $request->query->get('token');
$host = $request->get('host');
if ($host != "preview") {
$accountingFirm = $repository->findOneBy(['host' => $host]);
if (is_null($accountingFirm)) {
$domains = $domains->findBy(['name' => $host]);
if (is_null($domains)) {
throw new NotFoundHttpException();
}
foreach ($domains as $dom) {
$tmp_ac = $dom->getAccountingFirm();
if ($tmp_ac->getTousEnLigneToken() == $token) {
$accountingFirm = $tmp_ac;
break;
}
}
}
if (is_null($accountingFirm)) {
throw new NotFoundHttpException();
}
}
return $this->render('widget_tous_en_ligne/content.html.twig', [
'cabinet' => $accountingFirm,
]);
}
#[Route('/embed', name: 'w_tous_en_ligne_embed')]
public function embed()
{
$root = $this->getParameter('kernel.project_dir');
$path = $root . '/public/widgets/widget_tous_en_ligne/widget_tous_en_ligne.js';
return new BinaryFileResponse($path);
}
#[Route('/{filename}.{format}', name: 'w_tous_en_ligne_files')]
public function tous_en_ligne_files(string $filename, string $format)
{
$allowed = array('png', 'jpg', 'jpeg', 'gif', 'js', 'svg', 'eot', 'ttf', 'woff');
if (in_array($format, $allowed)) {
$root = $this->getParameter('kernel.project_dir');
$path = $root . '/public/widget_tous_en_ligne/' . $filename . '.' . $format;
if (!file_exists($path)) {
$path = $root . '/public/widgets/widget_tous_en_ligne/' . $filename . '.' . $format;
}
return new BinaryFileResponse($path);
}
return false;
}
//si formulaire présent dans le widget, sinon supprimé
#[Route('/send', name: 'tous_en_ligne_send_form')]
public function tous_en_ligne_send(Request $request, EntityManagerInterface $em, AccountingFirmRepository $accountingFirmRepository, WidgetMailing $mailing)
{
$prenom = $request->get('prenom');
$nom = $request->get('nom');
$email = $request->get('email');
$tel = $request->get('phone');
$nameCompany = $request->get('nameCompany');
$siret = $request->get('siret');
$fonction = $request->get('fonction');
$emailDirigeant = $request->get('emailDirigeant');
$address = $request->get('address');
$code_postal = $request->get('code_postal');
$ville = $request->get('ville');
$secteur = $request->get('secteur');
$taille = $request->get('taille');
$siteInternet = $request->get('siteInternet');
$taille = $request->get('taille');
$outilsUsed = explode(";", $request->get('outilsUsed'));
$outilsWant = explode(";", $request->get('outilsWant'));
$idCabinet = $request->get('cabinet');
$cabinet = $accountingFirmRepository->findById($idCabinet);
// return new JsonResponse([$prenom,$nom,$email,$tel,$nameCompany,$siret,$fonction,$address,$code_postal, $ville]);
if (!empty($prenom) && !empty($nom) && !empty($email) && !empty($tel) && !empty($nameCompany) && !empty($siret) && !empty($fonction) && !empty($address) && !empty($code_postal) && !empty($ville)) {
// Vérifier si l'email est dans la blacklist
$blacklistRepo = $em->getRepository(EmailBlacklist::class);
if ($blacklistRepo->isEmailBlacklisted($email)) {
// Incrémenter le compteur de tentatives
$blacklistedEmail = $blacklistRepo->findByEmail($email);
if ($blacklistedEmail) {
$blacklistedEmail->incrementBlockCount();
$em->persist($blacklistedEmail);
$em->flush();
}
return new JsonResponse(['status' => 'error', 'message' => 'Votre email est dans notre liste noire. Impossible d\'envoyer la demande.'], 403);
}
$widgetNameContact = new TousEnLigneContact;
$widgetNameContact->setPrenom($prenom);
$widgetNameContact->setNom($nom);
$widgetNameContact->setPhone($tel);
$widgetNameContact->setEmail($email);
$widgetNameContact->setNameCompany($nameCompany);
$widgetNameContact->setSiret($siret);
$widgetNameContact->setFonction($fonction);
if (!empty($emailDirigeant)) {
$widgetNameContact->setEmailDirigeant($emailDirigeant);
}
$widgetNameContact->setAddress($address);
$widgetNameContact->setCodePostal($code_postal);
$widgetNameContact->setVille($ville);
$widgetNameContact->setSecteur($secteur);
$widgetNameContact->setTaille($taille);
if ($siteInternet == "true") {
$widgetNameContact->setSiteInternet(true);
} else {
$widgetNameContact->setSiteInternet(false);
}
$widgetNameContact->setOutilsUsed($outilsUsed);
$widgetNameContact->setOutilsWant($outilsWant);
$widgetNameContact->setCabinet($cabinet);
$widgetNameContact->updateTimestamps();
$em->persist($widgetNameContact);
$em->flush();
$data = [
'name' => $nom,
'prenom' => $prenom,
'email' => $email,
'tel' => $tel,
'nameCompany' => $nameCompany,
'siret' => $siret,
'fonction' => $fonction,
'emailDirigeant' => $emailDirigeant,
'address' => $address,
'code_postal' => $code_postal,
'ville' => $ville,
'secteur' => $secteur,
'taille' => $taille,
'siteInternet' => $siteInternet,
'outilsUsed' => $outilsUsed,
'outilsWant' => $outilsWant,
'nameExpert' => "Camille",
'emailExpert' => "camille@lesfoliweb.fr",
];
// send mail exemple
$mailing->sendTelAgence($cabinet, $data);
$mailing->sendTelClient($cabinet, $data);
$mailing->sendTelExpert($cabinet, $data);
$mailing->sendTelCabinet($cabinet, $data);
return new JsonResponse('success');
}
return new JsonResponse('error');
}
}