<?php
namespace App\Controller;
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 WidgetAppelleTonEcController 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-appelle-ton-ec', name: 'w_appelle_ton_ec_check')]
public function ctrl_checkAppelleTonEc(Request $request, AccountingFirmRepository $repository, string $prefix = null): JsonResponse
{
$token = $request->query->get('token');
$host = $request->get('host');
if ($token && $host) {
$accountingFirm = $repository->findOneBy(['appelleTonEcToken' => $token]);
$isAuthorized = false;
if ($accountingFirm && in_array($host, $accountingFirm->getListAuthorizedDomains())) {
$isAuthorized = true;
}
$url = $isAuthorized ? $accountingFirm->getAppelleTonEcWidgetUrl() : null;
} else {
$accountingFirm = $repository->findOneBy(['host' => $host]);
$url = $accountingFirm ? $prefix . $this->generateUrl('w_appelle_ton_ec_get') : null;
}
return $this->json($url);
}
#[Route('/widget-appelle-ton-ec', name: 'w_appelle_ton_ec_get')]
public function ctrl_widgetAppelleTonEc(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->getAppelleTonEcToken() == $token) {
$accountingFirm = $tmp_ac;
break;
}
}
}
if (is_null($accountingFirm)) {
throw new NotFoundHttpException();
}
}
return $this->render('widget_appelle_ton_ec/content.html.twig', [
'cabinet' => $accountingFirm,
'videos' => $accountingFirm->getVideoAppelleTonEcs(),
'contactPath' => $accountingFirm->getActuv2UrlContact()
]);
}
#[Route('/embed', name: 'w_appelle_ton_ec_embed')]
public function embed()
{
$root = $this->getParameter('kernel.project_dir');
$path = $root . '/public/widgets/widget_appelle_ton_ec/widget_appelle_ton_ec.js';
return new BinaryFileResponse($path);
}
#[Route('/{filename}.{format}', name: 'w_appelle_ton_ec_files')]
public function appelle_ton_ec_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_appelle_ton_ec/' . $filename . '.' . $format;
return new BinaryFileResponse($path);
}
return false;
}
}