<?php
namespace App\Controller;
use App\Diffusion\PubDiffusion;
use App\Diffusion\PopupExtraDiffusion;
use App\Entity\Adherent\Magasin;
use App\Entity\Adherent\WebFactoryConsentement;
use App\Entity\ConfigEadh\Popover;
use App\Entity\EspaceFournisseur\Pub;
use App\Entity\Message\Message;
use App\Entity\PopupExtra;
use App\Entity\VenteRechercheMagasins\VenteRechercheMagasins;
use App\Enum\ArticleLocationsEnum;
use App\Form\User\WebFactoryConsentementType;
use App\Manager\Crm\CrmAccessManager;
use App\Manager\OpcManager;
use App\Manager\Progress\ModerationChpSupContact;
use App\Manager\Webfactory\WebfactoryManager;
use App\Repository\PopupExtraMasquerRepository;
use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class HomeController extends AbstractController
{
private ?UserInterface $user;
public function __construct(
Security $security,
private readonly EntityManagerInterface $em,
private readonly TranslatorInterface $translator,
private readonly CrmAccessManager $crmAccessManager,
private readonly WebfactoryManager $webfactoryManager,
private readonly OpcManager $opcManager,
private readonly ModerationChpSupContact $moderationChpSupContact
)
{
$this->user = $security->getUser();
}
#[Route(path: '/', name: 'home')]
public function home(Request $request, PubDiffusion $pubDiffusion, PopupExtraMasquerRepository $popupMasquerRepository, PopupExtraDiffusion $popupDiffusion): Response
{
$dateDuJour = new DateTime('now');
$disabledAnnonces = $this->em->getRepository(VenteRechercheMagasins::class)->createQueryBuilder('vrm')
->where('vrm.disable_at < :date')
->andWhere('vrm.disabled = :disabled')
->setParameters(['date' => $dateDuJour, 'disabled' => 0])
->getQuery()
->getResult();
if (!empty($disabledAnnonces)) {
foreach ($disabledAnnonces as $disabledAnnonce) {
$notif = new Message();
$notif->setAdherent($disabledAnnonce->getCodeAdh());
$notif->setType('Vente_magasins');
$notif->setObjet('Vente et recherche de magasin');
$notif->setContenu('Votre annonce N°' . $disabledAnnonce->getRefAnnonce() . ' a été mis hors ligne !');
$notif->setUrl($this->generateUrl('vente_et_recherche_magasin_dashboard'));
$notif->setLangue($this->user->getLangue());
$disabledAnnonce->setDisabled(1);
$this->em->persist($notif);
$this->em->persist($disabledAnnonce);
}
$this->em->flush();
}
// Je récupère la pop-up du moment (en fonction de la date)
$popupOptions = ['options' => ['active' => 1, 'langue' => $this->user->getLangue()]];
$popupContent = $popupDiffusion->getDatasByEntities(PopupExtra::class, $popupOptions);
$popupContent = (!empty($popupContent)) ? $popupContent[0] : null;
$isAccord = false;
if ($popupContent) {
$accord = $popupMasquerRepository->findOneBy(['K_ADHERENT' => $this->user->getKAdherent(), 'popup' => $popupContent->getId()]);
if (!empty($accord)) {
$isAccord = true;
}
}
$pubRequestOptions = ['options' => ['location' => 'HOMEPAGE', 'langue' => true], 'orderBy' => ['sort' => 'ASC']];
$pubs = $pubDiffusion->getDatasByEntities(Pub::class, $pubRequestOptions);
$popovers = $this->em->getRepository(Popover::class)->getPopoverByCategory('Homepage', $request->getLocale());
return $this->render('home/home.html.twig', [
'article_locations' => ArticleLocationsEnum::toArray(),
'pubs' => $pubs,
'popupContent' => $popupContent,
'isAccord' => $isAccord,
'popovers' => $popovers
]);
}
#[Route(path: '/multimag-{codeAdh}', name: 'multimag_switch')]
public function multimagSwitch($codeAdh): RedirectResponse
{
$multimagSwitch = $this->em->getRepository(Magasin::class)->findOneBy(['k_adherent' => $codeAdh]);
if ($multimagSwitch) {
$this->getUser()->setKadherent($codeAdh);
$this->getUser()->setMagasin($multimagSwitch->getLib());
$this->getUser()->setLib($multimagSwitch->getLib());
$this->getUser()->setCRM($this->crmAccessManager->checkAccessCrm($codeAdh));
$this->getUser()->setESV(($multimagSwitch->getKNIVEAUADH() === 'LOL4G' || $multimagSwitch->getKNIVEAUADH() === 'LOL2G'));
$contratSiteWeb = $this->webfactoryManager->getContratFactory($codeAdh);
$this->getUser()->setContratSiteWeb($contratSiteWeb);
$this->getUser()->setKPA($multimagSwitch->getKPA());
$this->opcManager->initJaugeOpc($multimagSwitch);
}
return $this->redirectToRoute('home');
}
#[Route(path: '/mentions-legales', name: 'mentions')]
public function mentionsLegalesAction(): Response
{
$locale = $this->translator->getLocale();
if ($locale == "nl") {
return $this->render('home/Politique/mentions_BE_NL.html.twig');
} else {
return $this->render('home/Politique/mentions.html.twig');
}
}
#[Route(path: '/politique-de-confidentialite', name: 'politique')]
public function politiqueConfAction(): Response
{
$locale = $this->translator->getLocale();
if ($locale == "nl") {
return $this->render('home/Politique/politique_BE_NL.html.twig');
} else {
return $this->render('home/Politique/politique.html.twig');
}
}
#[Route(path: '/lang/{lang}', name: 'change_lang')]
public function updateLangAction(Request $request, $lang): RedirectResponse
{
$referer = $request->headers->get('referer');
$session = $request->getSession();
if ($this->getUser()) {
$session->set('_locale', strtolower((string)$this->getUser()->getLangue()));
} else {
$session->set('_locale', strtolower((string)$lang));
}
return $this->redirect($referer);
}
#[Route(path: '/redirect/bibliotheque-facebook', name: 'redirection_bibliotheque_fb')]
#[IsGranted(['ROLE_BIBLIOTHEQUE_FB'])]
public function bibliothequeFb(): RedirectResponse
{
$param = 'fr_FR';
if (strtoupper((string)$this->user->getCentrale()) == "BE") {
$param = (strtoupper((string)$this->user->getLangue()) == 'NL') ? 'be_NL' : 'be_FR';
}
return $this->redirect('https://form.groupeall.com/bibliotheque-facebook/' . $param . '/api-login-checkout/' . $this->user->getTokenSSO());
}
#[Route(path: '/redirect/webshop', name: 'redirection_webshop')]
public function webshopMontures(): RedirectResponse
{
return $this->redirect('https://webshop.groupeall.com/login?token=' . $this->user->getTokenSSO());
}
#[Route(path: '/factory-presentation', name: 'consentement_factory')]
#[Route(path: '/factory-presentation-start', name: 'consentement_factory_start')]
public function presentationFactory(Request $request): RedirectResponse|Response
{
if ($request->getRequestUri() === "/factory-presentation-start") {
$packType = "gratuit";
} else {
$packType = "payant";
}
$consentementAdh = $this->em->getRepository(WebFactoryConsentement::class)->findOneBy(['adherent' => $this->getUser()->getKadherent()]);
if (!is_null($consentementAdh) || !$this->getUser()->getContratSiteWeb()['access']) {
return $this->redirectToRoute("home");
}
$newConsentementAdh = new WebFactoryConsentement();
$newConsentementAdh->setAdherent($this->getUser()->getKadherent());
$form = $this->createForm(WebFactoryConsentementType::class, $newConsentementAdh, ['type_pack' => $packType]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$newConsentementAdh->setTypePack($packType);
$this->em->persist($newConsentementAdh);
$this->em->flush();
$this->addFlash("success", "Vos informations ont bien été enregistrées");
return $this->redirectToRoute("home");
}
return $this->render("home/consentement_factory.html.twig", [
'form' => $form->createView(),
'fitting_pack' => $packType
]);
}
}