<?php
namespace App\Controller;
use App\Entity\Catalog;
use App\Entity\Category;
use App\Entity\CategoryTranslation;
use App\Entity\Contact;
use App\Entity\JobOffer;
use App\Entity\JobOfferParagraph;
use App\Entity\JobOfferTranslation;
use App\Entity\LandingContact;
use App\Entity\LandingPage;
use App\Entity\LandingPageTranslation;
use App\Entity\Member;
use App\Entity\Newsletter;
use App\Entity\Page;
use App\Entity\Paragraph;
use App\Entity\Partner;
use App\Entity\PhaseEvolutionTranslation;
use App\Entity\Post;
use App\Entity\PostMedia;
use App\Entity\PostTranslation;
use App\Entity\Presentation;
use App\Entity\PresentationParagraph;
use App\Entity\PresentationValue;
use App\Entity\Product;
use App\Entity\ProductPublication;
use App\Entity\ProductTranslation;
use App\Entity\Publication;
use App\Entity\PublicationCategory;
use App\Entity\PublicationCategoryTranslation;
use App\Entity\PublicationTranslation;
use App\Entity\Setting;
use App\Entity\SubCategory;
use App\Entity\SubCategoryTranslation;
use App\Entity\Support;
use App\Entity\Testimonial;
use App\Form\ContactType;
use App\Form\LandingContactType;
use App\Form\NewsletterType;
use App\Form\PostsFilterType;
use App\Form\ProductsFilterType;
use App\Form\PublicationsFilterType;
use App\Form\SupportType;
use App\Service\AppService;
use App\Twig\AppExtension;
use Doctrine\Persistence\ManagerRegistry;
use Knp\Component\Pager\PaginatorInterface;
use Mailjet\Resources;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bridge\Twig\Mime\BodyRenderer;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkApplication;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\KernelInterface;
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 Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
class FrontController extends AbstractController
{
/**
* @Route("/changer-la-langue/{locale}/{redirectUrl}", name="front_change_language", requirements={"redirectUrl"=".+"})
*/
public function changeLanguage(Request $request, SessionInterface $session, $locale, $redirectUrl = null)
{
$session->set('_locale', $locale);
return ($redirectUrl ? $this->redirect($redirectUrl) : $this->redirectToRoute('front_landing'));
}
/**
* @Route("/admin/HJSD876326Hjfdhkljlk98hdfjfnb63569", name="admin_import_publications")
*/
public function adminImportPublications(ManagerRegistry $managerRegistry, Request $request)
{
$row = 1;
$em = $managerRegistry->getManager();
$repoCategory = $managerRegistry->getRepository(Category::class);
$repoPhaseEvolutionTranslation = $managerRegistry->getRepository(PhaseEvolutionTranslation::class);
$repoPublicationCategoryTranslation = $managerRegistry->getRepository(PublicationCategoryTranslation::class);
if (($handle = fopen("publications_en.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if ($row > 1 and $data[5]) {
$publication = new Publication();
$em->persist($publication);
$categoryTitle = trim($data[0]);
$category = $repoCategory->search(["title" => $categoryTitle, 'limit' => 1]);
$publication->setCategory($category);
$publicationCategoryTitle = trim($data[1]);
$publicationCategoryTranslation = $repoPublicationCategoryTranslation->findOneBy(["title" => $publicationCategoryTitle]);
if (!$publicationCategoryTranslation) {
$publicationCategory = new PublicationCategory();
$em->persist($publicationCategory);
$publicationCategoryTranslation = new PublicationCategoryTranslation();
$em->persist($publicationCategoryTranslation);
$publicationCategoryTranslation->setTitle($publicationCategoryTitle);
$publicationCategoryTranslation->setLocale("en");
$publicationCategoryTranslation->setTranslatable($publicationCategory);
} else {
$publicationCategory = $publicationCategoryTranslation->getTranslatable();
}
$publication->setPublicationCategory($publicationCategory);
foreach (explode(",", $data[2]) as $key => $phaseEvolutionTitle) {
$phaseEvolutionTitle = trim($phaseEvolutionTitle);
$phaseEvolutionTranslation = $repoPhaseEvolutionTranslation->findOneBy(["title" => $phaseEvolutionTitle]);
if ($phaseEvolutionTranslation) {
$phaseEvolution = $phaseEvolutionTranslation->getTranslatable();
$publication->addPhaseEvolution($phaseEvolution);
}
}
$year = trim($data[4]);
$year = str_replace("(", "", $year);
$year = str_replace(")", "", $year);
$publication->setDate($year);
$publicationTranslation = new PublicationTranslation();
$publicationTranslation->setTranslatable($publication);
$publicationTranslation->setLocale("en");
$publicationTranslation->setAuthor(trim($data[3]));
$publicationTranslation->setTitle(trim($data[5]));
$publication->setLink(trim($data[6]));
$publicationTranslation->setRevue(trim($data[7]));
$repoProductTranslation = $managerRegistry->getRepository(ProductTranslation::class);
foreach (explode(",", $data[8]) as $key => $productTitle) {
$productTitle = trim($productTitle);
$productTranslation = $repoProductTranslation->findOneBy(["title" => $productTitle]);
if ($productTranslation) {
$product = $productTranslation->getTranslatable();
$productPublication = new ProductPublication();
$productPublication->setProduct($product);
$productPublication->setPublication($publication);
$em->persist($productPublication);
}
}
$em->persist($publicationTranslation);
$em->flush();
}
$row++;
}
fclose($handle);
}
exit;
}
/**
* @Route("/about-us", name="front_presentation")
*/
public function presentation(ManagerRegistry $managerRegistry, Request $request, PaginatorInterface $paginator)
{
$repoPresentation = $managerRegistry->getRepository(Presentation::class);
$pres = $repoPresentation->findAll();
if (count($pres) > 0) {
$pres = $pres[0];
}
$repoPresentationParagraph = $managerRegistry->getRepository(PresentationParagraph::class);
$presentationParagraphs = $repoPresentationParagraph->findBy([], ["position" => "asc"]);
$repoMember = $managerRegistry->getRepository(Member::class);
$members = $paginator->paginate(
$repoMember->findBy([], ["position" => "asc"]), $request->query->getInt('page', 1)/* page number */, 10/* limit per page */
);
$repoPartner = $managerRegistry->getRepository(Partner::class);
$distributors = $repoPartner->findBy(["type" => "Distributor"], ["position" => "asc"]);
$partners = $repoPartner->findBy(["type" => "Partner"], ["position" => "asc"]);
$repoSetting = $managerRegistry->getRepository(Setting::class);
$setting = $repoSetting->findOneBy([]);
$repoCategory = $managerRegistry->getRepository(Category::class);
$categories = $repoCategory->findBy([], ["position" => "asc"]);
$repoPresentationValue = $managerRegistry->getRepository(PresentationValue::class);
$presentationValues = $repoPresentationValue->findBy([], ["position" => "asc"]);
return $this->render('front/presentation.html.twig', array(
"presentation" => $pres,
"presentationParagraphs" => $presentationParagraphs,
"members" => $members,
"partners" => $partners,
"distributors" => $distributors,
'setting' => $setting,
"categories" => $categories,
"presentationValues" => $presentationValues,
));
}
/**
* @Route("/products/{catSlug}/{subCatSlug}", name="front_products")
*/
public function products(ManagerRegistry $managerRegistry, TranslatorInterface $translator, PaginatorInterface $paginator, Request $request, $catSlug = null, $subCatSlug = null)
{
$repoSubCategory = $managerRegistry->getRepository(SubCategory::class);
$subCategory = $repoSubCategory->search(["slug" => $subCatSlug, "limit" => 1]);
$repoCategory = $managerRegistry->getRepository(Category::class);
$category = $subCategory ? $subCategory->getCategory() : $repoCategory->search(["slug" => $catSlug, "limit" => 1]);
$category = $category ?? $repoCategory->findOneBy([], ["position" => "asc"]);
if (!$category) {
return $this->redirectToRoute('front_landing');
}
$data["orderBy"] = ["a.position", "asc"];
$data["hide"] = false;
$limit = 9;
$data["searchProducts"] = ["categories" => [], "subCategories" => []];
if (!$request->isXmlHttpRequest()) {
if ($subCategory) {
$data["searchProducts"]["subCategories"][] = $subCategory;
}
if ($category) {
$data["searchProducts"]["categories"][] = $category;
}
}
$productsFilterForm = $this->createForm(ProductsFilterType::class, null, [
"category" => $category
]);
$productsFilterForm->handleRequest($request);
if ($productsFilterForm->isSubmitted() && $productsFilterForm->isValid()) {
$data["searchProducts"] = $productsFilterForm->getData();
}
$repoProduct = $managerRegistry->getRepository(Product::class);
$products = $repoProduct->search(array_merge($data, ["limit" => $limit]));
$products = (is_array($products) ? $products : ($products ? [$products] : []));
$totalProducts = count($repoProduct->search($data));
$contact = new Contact();
$contactForm = $this->createForm(ContactType::class, $contact, [
"noSubject" => true
]);
$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);
$email = (new TemplatedEmail())
->from($this->getParameter('mailer_user'))
->to($translator->trans("customer_email"))
->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]);
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_products');
}
}
return $this->render('front/products.html.twig', array(
'contactForm' => $contactForm->createView(),
'productsFilterForm' => $productsFilterForm->createView(),
"products" => $products,
"categoryActive" => $category,
"subCategoryActive" => $subCategory,
"limit" => $limit,
"totalProducts" => $totalProducts
));
}
/**
* @Route("/product/{catSlug}/{subCatSlug}/{slug}", name="front_product")
*/
public function product(ManagerRegistry $managerRegistry, Request $request, $catSlug, $subCatSlug, ProductTranslation $productTranslation)
{
$product = $productTranslation->getTranslatable();
$repoSubCategory = $managerRegistry->getRepository(SubCategory::class);
$subCategory = $repoSubCategory->search(["slug" => $subCatSlug, "limit" => 1]);
return $this->render('front/product.html.twig', array(
"product" => $product,
"subCategory" => $subCategory,
));
}
/**
* @Route("/application/{slug}", name="front_sub_category")
*/
public function subCategory(ManagerRegistry $managerRegistry, Request $request, SubCategoryTranslation $subCategoryTranslation)
{
$subCategory = $subCategoryTranslation->getTranslatable();
return $this->render('front/subCategory.html.twig', array(
"subCategory" => $subCategory
));
}
/**
* @Route("/legal-notice", name="front_privacy_policy")
*/
public function privacyPolicy(ManagerRegistry $managerRegistry, Request $request)
{
return $this->render('front/privacyPolicy.html.twig', array());
}
/**
* @Route("/publications/{slug}", name="front_publications")
*/
public function publications(ManagerRegistry $managerRegistry, Request $request, CategoryTranslation $categoryTranslation)
{
$category = $categoryTranslation->getTranslatable();
$repoPublication = $managerRegistry->getRepository(Publication::class);
$publicationsFilterForm = $this->createForm(PublicationsFilterType::class);
$publicationsFilterForm->handleRequest($request);
$data = ["category" => $category, "orderBys" => ["pc.position" => "asc", "a.position" => "desc"]];
if ($publicationsFilterForm->isSubmitted() && $publicationsFilterForm->isValid()) {
$data["search"] = $publicationsFilterForm["search"]->getData();
}
$publications = $repoPublication->search($data);
$publicationsClean = [];
foreach ($publications as $key => $publication) {
$publicationCategory = $publication->getPublicationCategory();
if (isset($publicationsClean[$publicationCategory->getId()])) {
$publicationsClean[$publicationCategory->getId()]["publications"][] = $publication;
} else {
$publicationsClean[$publicationCategory->getId()] = [
"publicationCategory" => $publicationCategory,
"publications" => [$publication]
];
}
}
return $this->render('front/publications.html.twig', array(
'publicationsFilterForm' => $publicationsFilterForm->createView(),
"category" => $category,
"publicationCleans" => $publicationsClean,
));
}
/**
* @Route("/landing/{slug}", name="front_landing_page")
*/
public function landingPage(ManagerRegistry $managerRegistry, Request $request, LandingPage $landingPage)
{
$repoCategory = $managerRegistry->getRepository(Category::class);
$categories = $repoCategory->findBy([], ["position" => "asc"]);
return $this->render('front/landingPage.html.twig', array(
"categories" => $categories,
"landingPage" => $landingPage,
));
}
/**
* @Route("/", name="front_landing")
*/
public function landing(Request $request, ManagerRegistry $managerRegistry): Response
{
$repoPartner = $managerRegistry->getRepository(Partner::class);
$dataPartners = ["type" => "Customer"];
if ($request->getLocale() == "zh") {
$dataPartners["isChinese"] = true;
}
$partners = $repoPartner->findBy($dataPartners, ["position" => "asc"]);
$repoPost = $managerRegistry->getRepository(Post::class);
$data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$data["limit"] = 3;
$posts = $repoPost->search($data);
$repoTestimonial = $managerRegistry->getRepository(Testimonial::class);
$testimonials = $repoTestimonial->findBy([], ["position" => "asc"]);
$repoCategory = $managerRegistry->getRepository(Category::class);
$categories = $repoCategory->findBy([], ["position" => "asc"]);
$repoPublication = $managerRegistry->getRepository(Publication::class);
$publications = $repoPublication->findBy(["displayOnLanding" => true], ["position" => "asc"]);
$landingContact = new LandingContact();
$landingContactForm = $this->createForm(LandingContactType::class, $landingContact);
$landingContactForm->handleRequest($request);
if ($landingContactForm->isSubmitted() && $landingContactForm->isValid()) {
$em = $managerRegistry->getManager();
$em->persist($landingContact);
$em->flush();
$this->addFlash("success", "landing.catalog.success");
return $this->redirectToRoute('front_landing');
}
$repoCatalog = $managerRegistry->getRepository(Catalog::class);
$catalogs = $repoCatalog->findAll();
return $this->render('front/landing.html.twig', [
'landingContactForm' => $landingContactForm->createView(),
"posts" => $posts,
"partners" => $partners,
"testimonials" => $testimonials,
"categories" => $categories,
"publications" => $publications,
"catalogs" => $catalogs,
]);
}
/**
* @Route("/offres-d-emploi", name="front_job_offers")
*/
public function jobOffers(ManagerRegistry $managerRegistry, Request $request)
{
$repoJobOffer = $managerRegistry->getRepository(JobOffer::class);
$jobOffers = $repoJobOffer->findBy([], ["position" => "asc"]);
return $this->render('front/jobOffers.html.twig', array(
"jobOffers" => $jobOffers
));
}
/**
* @Route("/offre-d-emploi/{slug}", name="front_job_offer")
*/
public function jobOffer(ManagerRegistry $managerRegistry, Request $request, JobOfferTranslation $jobOfferTranslation)
{
$jobOffer = $jobOfferTranslation->getTranslatable();
$repoJobOfferParagraph = $managerRegistry->getRepository(JobOfferParagraph::class);
$jobOfferParagraphs = $repoJobOfferParagraph->findBy(["jobOffer" => $jobOffer], ["position" => "asc"]);
return $this->render('front/jobOffer.html.twig', array(
"jobOffer" => $jobOffer,
"jobOfferParagraphs" => $jobOfferParagraphs,
));
}
/**
* @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("/support", name="front_support")
*/
public function support(ManagerRegistry $managerRegistry, TranslatorInterface $translator, Request $request)
{
$support = new Support();
$supportForm = $this->createForm(SupportType::class, $support);
$supportForm->handleRequest($request);
if ($supportForm->isSubmitted() && $supportForm->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($support);
$em->flush();
$transport = Transport::fromDsn($this->getParameter('mailer_dsn'));
$mailer = new Mailer($transport);
$email = (new TemplatedEmail())
->from($this->getParameter('mailer_user'))
->to("all_support@viewpoint.fr")
->subject("Nouveau message - Support")
// path of the Twig template to render
->htmlTemplate('mail/support.html.twig')
// pass variables (name => value) to the template
->context(["support" => $support]);
// 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);
$this->addFlash("success", "flash.support.success");
}
return $this->redirectToRoute('front_support');
}
return $this->render('front/support.html.twig', array(
'supportForm' => $supportForm->createView(),
));
}
/**
* @Route("/contact/{slug}/{isDemo}", name="front_contact", defaults={"slug":null})
*/
public function contact(Request $request, ManagerRegistry $managerRegistry, TranslatorInterface $translator, ProductTranslation $productTranslation = null, $isDemo = false)
{
$product = $productTranslation ? $productTranslation->getTranslatable() : null;
$contact = new Contact();
if ($product) {
$contact->setMessage($isDemo ? $translator->trans("contact.message_product_demo", ["%product%" => $product->getTitle()]) : $translator->trans("contact.message_product", ["%product%" => $product->getTitle()]));
}
$contactForm = $this->createForm(ContactType::class, $contact);
$contactForm->handleRequest($request);
if ($contactForm->isSubmitted() && $contactForm->isValid()) {
// If we have a middlename (which is a hidden field), we silently fail
// This should help detect bots I hope
if ($contactForm->get('middlename')->getData()) {
$this->addFlash("success", "Message envoyé.");
return $this->redirectToRoute('front_contact');
}
$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);
$subject = ($contact->getSubject() . " - " . $contact->getFirstname() . " " . $contact->getLastname() . " - " . $contact->getCountry() . " - " . ($product ? $product->getTitle() : null));
$email = (new TemplatedEmail())
->from($this->getParameter('mailer_user'))
->to($translator->trans("customer_email"))
->subject($subject)
// path of the Twig template to render
->htmlTemplate('mail/contact.html.twig')
// pass variables (name => value) to the template
->context(["contact" => $contact]);
if ($contact->getEmail()) {
$email
->replyTo($contact->getEmail());
}
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');
}
}
$repoSetting = $managerRegistry->getRepository(Setting::class);
$setting = $repoSetting->findOneBy([]);
return $this->render('front/contact.html.twig', array(
'contactForm' => $contactForm->createView(),
'setting' => $setting,
));
}
/**
* @Route("/sitemap", name="front_sitemap_links")
*/
public function sitemapLinks(ManagerRegistry $managerRegistry, Request $request)
{
return $this->render('front/sitemapLinks.html.twig', array());
}
/**
* @Route("/notre-equipe", name="front_members")
*/
public function members(ManagerRegistry $managerRegistry, Request $request)
{
$repoMember = $managerRegistry->getRepository(Member::class);
$members = $repoMember->findBy([], ["position" => "asc"]);
return $this->render('front/members.html.twig', array(
"members" => $members
));
}
/**
* @Route("/news", name="front_posts")
*/
public function posts(Request $request, PaginatorInterface $paginator, ManagerRegistry $managerRegistry)
{
$repoPost = $managerRegistry->getRepository(Post::class);
$data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$postsLimit = 12;
$data["limit"] = $postsLimit;
$postsFilterForm = $this->createForm(PostsFilterType::class);
$postsFilterForm->handleRequest($request);
if ($postsFilterForm->isSubmitted() && $postsFilterForm->isValid()) {
$data = array_merge($data, $postsFilterForm->getData());
}
$posts = $repoPost->search($data);
return $this->render('front/posts.html.twig', array(
'postsFilterForm' => $postsFilterForm->createView(),
"posts" => $posts,
"postsLimit" => $postsLimit
));
}
/**
* @Route("/navbar-search", name="front_navbar_search")
*/
public function navbarSearch(ManagerRegistry $managerRegistry, Request $request)
{
$query = $request->query->get('nav-search');
$repoPost = $managerRegistry->getRepository(Post::class);
$data["search"] = $query;
$data["isPublished"] = true;
$data["orderBy"] = ["a.date", "asc"];
$posts = $repoPost->search($data);
$repoProduct = $managerRegistry->getRepository(Product::class);
$products = $repoProduct->search(["search" => $query, "hide" => false, "orderBy" => ["a.position", "asc"]]);
return $this->render('front/navbarSearch.html.twig', array(
"posts" => $posts,
"products" => $products,
"navSearchQuery" => $query
));
}
/**
* @Route("/news/{slug}", name="front_post")
*/
public function post(Request $request, ManagerRegistry $managerRegistry, string $slug)
{
$repoPostTranslation = $managerRegistry->getRepository(PostTranslation::class);
$postTranslation = $repoPostTranslation->findOneBy(["slug" => $slug]);
if ($postTranslation and $post = $postTranslation->getTranslatable()) {
if ($post->getIsPublished() or $this->isGranted("ROLE_ADMIN")) {
$repoParagraph = $managerRegistry->getRepository(Paragraph::class);
$paragraphs = $repoParagraph->findBy(["post" => $post], ["position" => "asc"]);
$repoPostMedia = $managerRegistry->getRepository(PostMedia::class);
$postMedias = $repoPostMedia->findBy(["post" => $post], ["position" => "asc"]);
$repoPost = $managerRegistry->getRepository(Post::class);
$data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$data["limit"] = 3;
$posts = $repoPost->search($data);
return $this->render('front/post.html.twig', array(
"posts" => $posts,
"post" => $post,
"paragraphs" => $paragraphs,
"postMedias" => $postMedias,
));
} else {
$this->addFlash("danger", "flash.not_allowed_post");
return $this->redirectToRoute('front_posts');
}
} else {
$this->addFlash("danger", "flash.post_not_found");
return $this->redirectToRoute('front_posts');
}
}
/**
* @Route("/inscription-newsletter", name="front_subscribe_newsletter")
*/
public function subscribeNewsletter(ManagerRegistry $managerRegistry, Request $request, AppService $appService)
{
$newsletter = new Newsletter();
$newsletterForm = $this->createForm(NewsletterType::class, $newsletter);
$newsletterForm->handleRequest($request);
$em = $managerRegistry->getManager();
if ($newsletterForm->isSubmitted() && $newsletterForm->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 {
$mj = new \Mailjet\Client($this->getParameter('mj_apikey_public'), $this->getParameter('mj_apikey_private'), true, ['version' => 'v3']);
$body = [
// 'IsExcludedFromCampaigns' => "true",
'Name' => $newsletter->getName(),
'Email' => $newsletter->getEmail()
];
$response = $mj->post(Resources::$Contact, ['body' => $body]);
$em->persist($newsletter);
$em->flush();
$this->addFlash("success", "flash.newsletter.success");
return $this->redirectToRoute('front_landing');
}
}
return $this->render('form/newsletterForm.html.twig', array(
'newsletterForm' => $newsletterForm->createView(),
));
}
/**
* @Route("/admin/fsdhj78Hjkdfsb0920dfjdfhq87djhaoppsnv720", name="front_clear_cache")
*/
public function clearCache(Request $request, KernelInterface $kernel)
{
$application = new FrameworkApplication($kernel);
$application->setAutoExit(false);
$input = new ArrayInput([
'command' => 'cache:clear',
]);
// You can use NullOutput() if you don't need the output
$output = new NullOutput();
$application->run($input, $output);
$this->addFlash("success", "Le cache a bien été nettoyé, les traductions sont à jour !");
return $this->redirectToRoute('front_landing');
}
}