<?php
namespace App\Controller\front\Pages;
use ReCaptcha\ReCaptcha;
use App\Entity\Pages\Page;
use App\Entity\Contact\Contact;
use App\Form\Contact\ContactType;
use App\Service\Mailer\MailerService;
use App\Controller\Admin\CoreController;
use App\Entity\Pages\PageCategory;
use App\Repository\Pages\PageRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Service\Manager\Blocs\BlocManager;
use App\Service\Manager\Media\MediaManager;
use App\Repository\Pages\PageLangRepository;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use App\Service\Manager\Pages\PaletteColorsManager;
use Symfony\Contracts\Translation\TranslatorInterface;
#[Route('/page')]
class PageController extends CoreController
{
#[Route('/{slug}', name: 'front_page_show')]
public function show(
PageLangRepository $pageLangRepository,
PageRepository $pageRepository,
MediaManager $mediaManager,
BlocManager $blocManager,
PaletteColorsManager $paletteColorsManager,
Request $request,MailerService $mailer,
ManagerRegistry $doctrine,
TranslatorInterface $translator,
$slug
): Response
{
$language = $this->getCurrentLanguage();
$pageLang = $pageLangRepository->findOneBy([
'language'=> $language,
'slug' => $slug
]);
$page = $pageLang->getPage();
$contact = new Contact();
$form = $this->createForm(ContactType::class, $contact, [
'action' => '#sectionContact',
//'origin' => str_replace("Proxies\\__CG__\\", '',get_class($page)),
'origin' => $doctrine->getManager()->getClassMetadata(PageCategory::class),
'originId' => ($page->getId() )
]);
$form->handleRequest($request);
if ($request->isMethod('POST')) {
if($form->isValid()) {
$em = $doctrine->getManager();
$name = $email = "";
$name = $form->get('name')->getData();
$entreprise = $form->get('entreprise')->getData();
$phone = $form->get('phone')->getData();
$email = $form->get('email')->getData();
$from['from'][$name] = $email;
$isAccepted = $form->get('isAccepted')->getData();
$to = $this->getParameter('email.to');
$from = $_ENV['EMAIL_FROM'];
$subject= 'Netisse'. ' - '.$pageLang->getName();
$mailer->sendEmail(
$to,
'<html><body><h1>' . $subject . '</h1>' .'<h4>NOM:</h4>'.$form->get('name')->getData().'<br>'.'<h4>EMAIL:</h4>'.$form->get('email')->getData() .
'<br><h4>ENTREPRISE:</h4>'.$form->get('entreprise')->getData() . '<br><h4>TELEPHONE:</h4>'.$form->get('phone')->getData().'<br><h4>MESSAGE:</h4>'.$form->get('message')->getData() . '</body></html>',
$subject,
$from
);
$em->persist($contact);
$em->flush();
$this->addFlash('success', $translator->trans('front.form.msg_send'));
// Create a redirect response to the current page with an anchor tag
$redirectUrl = $this->generateUrl('front_page_show', ['slug' => $slug]) . '#sectionContact';
return $this->redirect($redirectUrl);
}
else {
// Vérifier le champ Honeypot
$emailHoneypot = $form->get('email_honeypot')->getData();
if (!empty($emailHoneypot)) {
$this->addFlash('danger', $translator->trans('Vous êtes considéré comme Bot/Spammer !.'));
}
}
}
$medias = $mediaManager->getMedias($page, null, $language);
$blocs = $blocManager->getBlocs($page);
$paletteColors = $paletteColorsManager->getValues('App\\Entity\\Pages\\Page',$page->getId());
$colorPage=null;
if($paletteColors){
$colorPage=$paletteColors[0];
}
$template = 'front/pages/index.html.twig';
$paramsPage = $page->getPageSousCategory() ? ['id' => $page->getId(), 'category' => $page->getPageSousCategory()] : ['id' => $page->getId(), 'media_page' => true ];
if($page->getPageSousCategory()) {
$paramsPage['media_page'] = 1;
}
return $this->render($template, [
'blocs' => $blocs,
'page' => $pageRepository->getAllQb($language,$paramsPage)->getOneOrNullResult(),
'medias' => $medias,
'paletteColors' =>$colorPage,
'form' => $form->createView(),
]);
}
}