src/Controller/WidgetCeController.php line 111

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CeContact;
  4. use App\Entity\EmailBlacklist;
  5. use App\Repository\AccountingFirmRepository;
  6. use App\Repository\AuthorizedDomainRepository;
  7. use App\Services\WidgetMailing;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Qferrer\Mjml\Twig\MjmlExtension;
  10. use Response;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. use Symfony\Component\Mailer\MailerInterface;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. use Twig\Environment;
  19. class WidgetCeController extends AbstractController
  20. {
  21.   protected MjmlExtension $mjmlRenderer;
  22.   protected Environment $twig;
  23.   public function __construct(MailerInterface $mailerMjmlExtension $mjmlRendererEnvironment $twig)
  24.     {
  25.         $this->mjmlRenderer $mjmlRenderer;
  26.         $this->twig $twig;
  27.     }
  28.   #[Route('/check-ce'name'w_ce_check')]
  29.   public function ctrl_checkCe(Request $requestAccountingFirmRepository $repositorystring $prefix null): JsonResponse
  30.   {
  31.     $token $request->query->get('token');
  32.     $host $request->get('host');
  33.     if ($token && $host) {
  34.       $accountingFirm $repository->findOneBy(['ceToken' => $token]);
  35.       $isAuthorized false;
  36.       if ($accountingFirm && in_array($host$accountingFirm->getListAuthorizedDomains())) {
  37.         $isAuthorized true;
  38.       }
  39.       $url $isAuthorized $accountingFirm->getCeWidgetUrl() : null;
  40.     } else {
  41.       $accountingFirm $repository->findOneBy(['host' => $host]);
  42.       $url $accountingFirm $prefix $this->generateUrl('w_ce_get') : null;
  43.     }
  44.     return $this->json($url);
  45.   }
  46.   #[Route('/widget-ce'name'w_ce_get')]
  47.   public function ctrl_widgetCe(Request $requestAccountingFirmRepository $repositoryAuthorizedDomainRepository $domains)
  48.   {
  49.     $token $request->query->get('token');
  50.     $host $request->get('host');
  51.     if ($host != "preview") {
  52.       $accountingFirm $repository->findOneBy(['host' => $host]);
  53.       if (is_null($accountingFirm)) {
  54.         $domains $domains->findBy(['name' => $host]);
  55.         if (is_null($domains)) {
  56.           throw new NotFoundHttpException();
  57.         }
  58.         foreach ($domains as $dom) {
  59.           $tmp_ac $dom->getAccountingFirm();
  60.           if ($tmp_ac->getCeToken() == $token) {
  61.             $accountingFirm $tmp_ac;
  62.             break;
  63.           }
  64.         }
  65.       }
  66.       if (is_null($accountingFirm)) {
  67.         throw new NotFoundHttpException();
  68.       }
  69.     }
  70.     return $this->render('widget_ce/content.html.twig', [
  71.       'cabinet' => $accountingFirm,
  72.     ]);
  73.   }
  74.   #[Route('/embed'name'w_ce_embed')]
  75.   public function embed()
  76.   {
  77.     $root $this->getParameter('kernel.project_dir');
  78.     $path $root '/public/widgets/widget_ce/widget_ce.js';
  79.     return new BinaryFileResponse($path);
  80.   }
  81.   #[Route('/{filename}.{format}'name'w_ce_files')]
  82.   public function ce_files(string $filenamestring $format)
  83.   {
  84.       $allowed = array('png''jpg''jpeg''gif''js''svg''eot''ttf''woff');
  85.       if (in_array($format$allowed)) {
  86.           $root $this->getParameter('kernel.project_dir');
  87.           $path $root '/public/widget_ce/' $filename '.' $format;
  88.           return new BinaryFileResponse($path);
  89.       }
  90.       return false;
  91.   }
  92.   #[Route('/send'name'ce_send_form')]
  93.   public function name_send(Request $requestEntityManagerInterface $emAccountingFirmRepository $accountingFirmRepositoryWidgetMailing $mailing)
  94.   {
  95.     $email $request->get('email');
  96.     $tel $request->get('tel');
  97.     $name $request->get('name');
  98.     $idCabinet $request->get('cabinet');
  99.     $cabinet $accountingFirmRepository->findById($idCabinet);
  100.     if (!empty($email) && !empty($name) && !empty($tel) && !empty($cabinet)) {
  101.       // Vérifier si l'email est dans la blacklist
  102.       $blacklistRepo $em->getRepository(EmailBlacklist::class);
  103.       if ($blacklistRepo->isEmailBlacklisted($email)) {
  104.         // Incrémenter le compteur de tentatives
  105.         $blacklistedEmail $blacklistRepo->findByEmail($email);
  106.         if ($blacklistedEmail) {
  107.           $blacklistedEmail->incrementBlockCount();
  108.           $em->persist($blacklistedEmail);
  109.           $em->flush();
  110.         }
  111.         return new JsonResponse(['status' => 'error''message' => 'Votre email est dans notre liste noire. Impossible d\'envoyer le message.'], 403);
  112.       }
  113.       $ceContact = new CeContact;
  114.       $ceContact->setName($name);
  115.       $ceContact->setPhone($tel);
  116.       $ceContact->setEmail($email);
  117.       $ceContact->setAccountingFirm($cabinet);
  118.       $ceContact->updateTimestamps();
  119.       $em->persist($ceContact);
  120.       $em->flush();
  121.       $mailing->sendCeClient($cabinet, [
  122.         'name' => $name,
  123.         'email' => $email,
  124.       ]);
  125.       if ($cabinet->isCeExpertExterneUse()) {
  126.         $mailing->sendCeAgenceExpertExterne($cabinet, [ // envoie à lagence.expert
  127.           'name' => $name,
  128.           'email' => $email,
  129.           'tel' => $tel,
  130.         ]);
  131.         $mailing->sendCeExpertExterne($cabinet, [ //envoie à l'expert du cabinet
  132.           'name' => $name,
  133.           'email' => $email,
  134.           'tel' => $tel,
  135.         ]);
  136.       } else {
  137.         $mailing->sendCeAgenceExpertInterne($cabinet, [  // envoie à lagence.expert
  138.           'name' => $name,
  139.           'email' => $email,
  140.           'tel' => $tel,
  141.         ]);
  142.         $mailing->sendCeExpertInterne($cabinet, [ // envoie a partenaire de lagence.expert
  143.           'name' => $name,
  144.           'email' => $email,
  145.           'tel' => $tel,
  146.         ]);
  147.         $mailing->sendCeCabinetInterne($cabinet, [ // envoie au cabinet concerné
  148.           'name' => $name,
  149.           'email' => $email,
  150.           'tel' => $tel,
  151.         ]);
  152.       }
  153.       return new JsonResponse('success');
  154.     }
  155.     return new JsonResponse('error');
  156.   }
  157. }