src/Controller/WidgetCirController.php line 114

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CirContact;
  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 WidgetCirController 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-cir'name'w_cir_check')]
  29.   public function ctrl_checkCir(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(['cirToken' => $token]);
  35.       $isAuthorized false;
  36.       if ($accountingFirm && in_array($host$accountingFirm->getListAuthorizedDomains())) {
  37.         $isAuthorized true;
  38.       }
  39.       $url $isAuthorized $accountingFirm->getCirWidgetUrl() : null;
  40.     } else {
  41.       $accountingFirm $repository->findOneBy(['host' => $host]);
  42.       $url $accountingFirm $prefix $this->generateUrl('w_cir_get') : null;
  43.     }
  44.     return $this->json($url);
  45.   }
  46.   #[Route('/widget-cir'name'w_cir_get')]
  47.   public function ctrl_widgetCir(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->getCirToken() == $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_cir/content.html.twig', [
  71.       'cabinet' => $accountingFirm,
  72.     ]);
  73.   }
  74.   #[Route('/embed'name'w_cir_embed')]
  75.   public function embed()
  76.   {
  77.     $root $this->getParameter('kernel.project_dir');
  78.     $path $root '/public/widgets/widget_cir/widget_cir.js';
  79.     return new BinaryFileResponse($path);
  80.   }
  81.   #[Route('/{filename}.{format}'name'w_cir_files')]
  82.   public function name_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_cir/' $filename '.' $format;
  88.           if (!file_exists($path)) {
  89.             $path $root '/public/widgets/widget_cir/' $filename '.' $format;
  90.           }
  91.           return new BinaryFileResponse($path);
  92.       }
  93.       return false;
  94.   }
  95.   //si formulaire présent dans le widget, sinon supprimé
  96.   #[Route('/send'name'cir_send_form')]
  97.   public function name_send(Request $requestEntityManagerInterface $emAccountingFirmRepository $accountingFirmRepositoryWidgetMailing $mailing)
  98.   {
  99.     $email $request->get('email');
  100.     $tel $request->get('tel');
  101.     $name $request->get('name');
  102.     $idCabinet $request->get('cabinet');
  103.     $cabinet $accountingFirmRepository->findById($idCabinet);
  104.     if (!empty($email) && !empty($name) && !empty($tel) && !empty($cabinet)) {
  105.       // Vérifier si l'email est dans la blacklist
  106.       $blacklistRepo $em->getRepository(EmailBlacklist::class);
  107.       if ($blacklistRepo->isEmailBlacklisted($email)) {
  108.         // Incrémenter le compteur de tentatives
  109.         $blacklistedEmail $blacklistRepo->findByEmail($email);
  110.         if ($blacklistedEmail) {
  111.           $blacklistedEmail->incrementBlockCount();
  112.           $em->persist($blacklistedEmail);
  113.           $em->flush();
  114.         }
  115.         return new JsonResponse(['status' => 'error''message' => 'Votre email est dans notre liste noire. Impossible d\'envoyer le message.'], 403);
  116.       }
  117.       $cirContact = new CirContact;
  118.       $cirContact->setEmail($email);
  119.       $cirContact->setPhone($tel);
  120.       $cirContact->setName($name);
  121.       $cirContact->setAccountingFirm($cabinet);
  122.       $cirContact->updateTimestamps();
  123.       $em->persist($cirContact);
  124.       $em->flush();
  125.       //send mail
  126.       $mailing->sendCirClient($cabinet, [ //Envoie au client
  127.         'name' => $name,
  128.         'email' => $email,
  129.       ]);
  130.       if ($cabinet->isCirExpertExterneUse()) {  // si le cabinet utilise son propre expert
  131.         $mailing->sendCirAgenceExpertExterne($cabinet, [ // envoie à lagence.expert
  132.           'name' => $name,
  133.           'email' => $email,
  134.           'tel' => $tel,
  135.         ]);
  136.         $mailing->sendCirExpertExterne($cabinet, [ //envoie à l'expert du cabinet
  137.           'name' => $name,
  138.           'email' => $email,
  139.           'tel' => $tel,
  140.         ]);
  141.       } else { //si c'est un partenaire de lagence.expert
  142.         $mailing->sendCirAgenceExpertInterne($cabinet, [  // envoie à lagence.expert
  143.           'name' => $name,
  144.           'email' => $email,
  145.           'tel' => $tel,
  146.         ]);
  147.         $mailing->sendCirExpertInterne($cabinet, [ // envoie a partenaire de lagence.expert
  148.           'name' => $name,
  149.           'email' => $email,
  150.           'tel' => $tel,
  151.         ]);
  152.         $mailing->sendCirCabinetInterne($cabinet, [ // envoie au cabinet concerné
  153.           'name' => $name,
  154.           'email' => $email,
  155.           'tel' => $tel,
  156.         ]);
  157.       }
  158.       return new JsonResponse('success');
  159.     }
  160.     return new JsonResponse('error');
  161.   }
  162. }