<?php
namespace App\Controller;
use App\Entity\Contact;
use App\Entity\Page;
use App\Entity\Paragraph;
use App\Entity\Partner;
use App\Entity\Post;
use App\Form\ContactType;
use Doctrine\Persistence\ManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\Mailer;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport;
use Symfony\Component\Mime\Email;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
class FrontController extends AbstractController
{
/**
* @Route("/", name="front_landing")
*/
public function landing(ManagerRegistry $managerRegistry): Response
{
$asbtemplate = $this->getParameter('asbtemplate');
if ($asbtemplate["landing"]["partners"]) {
$repoPartner = $managerRegistry->getRepository(Partner::class);
$partners = $repoPartner->findAll();
}
if ($asbtemplate["landing"]["lastPosts"]) {
$repoPost = $managerRegistry->getRepository(Post::class);
$data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$data["limit"] = $asbtemplate["landing"]["nbLandingPosts"];
$posts = $repoPost->search($data);
}
return $this->render('front/landing.html.twig', [
"posts" => (isset($posts) ? $posts : []),
"partners" => (isset($partners) ? $partners : [])
]);
}
/**
* @Route("/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
*/
public function sitemap(Request $request, Packages $packages)
{
$repoPage = $this->getDoctrine()->getRepository(Page::class);
$page = $repoPage->search(["notNull" => ["a.sitemapFileName"], "limit" => 1]);
if ($page and $page->getSitemapFileName()) {
return $this->redirect($packages->getUrl('upload/sitemap/' . $page->getSitemapFileName()));
} else {
return $this->redirectToRoute('front_landing');
}
}
/**
* @Route("/contact", name="front_contact")
*/
public function contact(Request $request, ManagerRegistry $managerRegistry)
{
$contact = new Contact();
$contactForm = $this->createForm(ContactType::class, $contact);
$contactForm->handleRequest($request);
if ($contactForm->isSubmitted() && $contactForm->isValid()) {
$recaptchaResponse = $request->request->get('g-recaptcha-response', null);
$isRecaptchaValid = false;
if ($recaptchaResponse) {
$paramsArr = array(
"response" => $recaptchaResponse,
"secret" => $this->getParameter('recaptchaSecret')
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($paramsArr));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$isRecaptchaValid = json_decode(curl_exec($ch))->success;
}
if (!$isRecaptchaValid) {
$this->addFlash("danger", "Veuillez recommencer en validant le captcha.");
} else {
$em = $managerRegistry->getManager();
$em->persist($contact);
$em->flush();
$this->addFlash("success", "Message envoyé.");
$transport = Transport::fromDsn($this->getParameter('mailer_dsn'));
$mailer = new Mailer($transport);
$asbtemplate = $this->getParameter('asbtemplate');
$email = (new TemplatedEmail())
->from($this->getParameter('mailer_user'))
->to($this->getParameter('mailer_user'))
->subject("Nouveau message")
// path of the Twig template to render
->htmlTemplate('mail/contact.html.twig')
// pass variables (name => value) to the template
->context(["contact" => $contact, 'asbtemplate' => $asbtemplate]);
if ($asbtemplate["contact"]["customFiles"]) {
foreach ($contact->getCustomFiles() as $key => $customFile) {
$email->attachFromPath(("upload/customFile/" . $customFile->getCustomFileFileName()));
}
}
$loader = new FilesystemLoader($this->getParameter('kernel.project_dir') . '/templates/');
$twigEnv = new Environment($loader);
$twigBodyRenderer = new BodyRenderer($twigEnv);
$twigBodyRenderer->render($email);
$mailer->send($email);
return $this->redirectToRoute('front_contact');
}
}
return $this->render('front/contact.html.twig', array(
'contactForm' => $contactForm->createView(),
));
}
/**
* @Route("/actualites", name="front_posts")
*/
public function posts(Request $request, PaginatorInterface $paginator, ManagerRegistry $managerRegistry)
{
$asbtemplate = $this->getParameter('asbtemplate');
$repoPost = $managerRegistry->getRepository(Post::class);
$data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$posts = $paginator->paginate(
$repoPost->search($data), $request->query->getInt('page', 1)/* page number */, $asbtemplate["post"]["postLimitPerPage"]/* limit per page */
);
return $this->render('front/posts.html.twig', array(
"posts" => $posts
));
}
/**
* @Route("/actualite/{slug}", name="front_post")
*/
public function post(Request $request, ManagerRegistry $managerRegistry, Post $post)
{
if ($post->getIsPublished() or $this->isGranted("ROLE_ADMIN")) {
$repoParagraph = $managerRegistry->getRepository(Paragraph::class);
$paragraphs = $repoParagraph->findBy(["post" => $post], ["position" => "asc"]);
return $this->render('front/post.html.twig', array(
"post" => $post,
"paragraphs" => $paragraphs,
));
} else {
$this->addFlash("danger", "Vous ne pouvez pas accéder à cet article.");
return $this->redirectToRoute('front_posts');
}
}
}