<?php
namespace App\Controller\Main;
use App\Services\Admin\InstagramPostsManager;
use App\Entity\Main\MarketingTestimony;
use App\Form\Main\AffiliationContactType;
use App\Form\Main\ContactType;
use App\Services\EmailManager;
use App\Services\ReCaptchaManager;
use App\Services\SiteManager;
use App\Services\TranslationManager;
use Doctrine\ORM\EntityManagerInterface;
use data\brulafine\staticData\IngredientsProvider;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @Route(
* condition="not (context.getHost() matches '%coaching_domain.host.regexp%')"
* )
*/
class StaticController extends AbstractController
{
use BrulafineControllerTrait;
/**
* @param Request $request
* @param EntityManagerInterface $entityManager
* @return Response
*/
#[Route(path: [
'fr' => '/',
'en' => '/en',
'es' => '/es',
'it' => '/it',
], name: "brulafine_home")]
public function indexAction(Request $request, EntityManagerInterface $entityManager, SiteManager $siteManager, InstagramPostsManager $instagramPostsManager)
{
// We have two different systems we need to handle there.
if ($siteManager->getYamlConfigParameter('siteUseStaticTestimonies')) {
$testimonyProvider = "data\\{$siteManager->getYamlConfigParameter('websiteName')}\\staticData\\TestimoniesProvider";
$rawTestimonies = $testimonyProvider::$testimonies;
$testimonies = array_map(function (array $item): array {
$item['age'] = date_create()->diff(date_create($item['birthdate']))->y;
return $item;
}, array_slice($rawTestimonies, 0, 3));
} else {
$testimonies = $entityManager->getRepository(MarketingTestimony::class)->getTestimonies($request->getLocale());
}
$session = $request->getSession();
$email = $request->query->get('f_mail');
if (null === $email) {
$email = $session->get('f_mail');
}
if (isset($email) && filter_var($email, FILTER_VALIDATE_EMAIL)) {
$session->set('f_mail', $email);
}
$ingredientsProvider = '\data\\'. $siteManager->getYamlConfigParameter('websiteName') .'\staticData\IngredientsProvider';
$activeIgPosts = $instagramPostsManager->getActiveIgPosts(true);
return $this->staticAction($this->getTemplatesDir() . '/Static/index.html.twig', [
'testimonies' => $testimonies,
'ingredients' => $ingredientsProvider::$ingredients,
'activeIgPosts' => $activeIgPosts
]);
}
/**
* @param Request $request
*/
#[Route(path: [
'fr' => '/ingredients/',
'en' => '/ingredients/',
'es' => '/ingredientes/',
'it' => '/ingredienti/',
], name: "brulafine_ingredients")]
public function ingredientsAction(SiteManager $siteManager)
{
$ingredientsProvider = '\data\\'. $siteManager->getYamlConfigParameter('websiteName') .'\staticData\IngredientsProvider';
return $this->staticAction(
$this->getTemplatesDir() . '/Static/ingredients.html.twig',
[
'ingredients' => $ingredientsProvider::$ingredients
]
);
}
/**
* @Route("/contact/", name="brulafine_contact")
* @param Request $request
* @param ReCaptchaManager $reCaptchaManager
* @return RedirectResponse|Response
*/
public function contactAction(Request $request, ReCaptchaManager $reCaptchaManager, EmailManager $emailManager, TranslatorInterface $translator)
{
$setup = $this->getSetup();
$form = $this->createForm(ContactType::class);
$form->handleRequest($request);
$googleResponse = $reCaptchaManager->getResponse();
if ($form->isSubmitted() && $form->isValid() && $googleResponse->success) {
$emailManager->sendContactMail($form, $request);
$this->addFlash('info', 'flashMessages.site.messageWillBeSend');
return $this->redirectToRoute('brulafine_contact');
}
if ($form->isSubmitted() && !$googleResponse->success && property_exists($googleResponse, "error-codes")) {
$recaptchaErrorMessage = $translator->trans('flashMessages.site.recaptchaErrorMsg', array(), TranslationManager::TRANSLATION_DOMAIN_FLASH);
}
$mtcaptchaSiteKey = $this->parameterBag->get('mtcaptcha_site_key');
return $this->staticAction($this->getTemplatesDir() . '/Static/contact.html.twig', [
'form' => $form->createView(),
'cart' => $setup->getCart(),
'recaptchaErrorMessage' => $recaptchaErrorMessage ?? '',
'mtcaptchaSiteKey' => $mtcaptchaSiteKey
]);
}
/**
* @Route("/affiliation/", name="brulafine_affiliation")
* @param Request $request
* @param ReCaptchaManager $reCaptchaManager
* @return RedirectResponse|Response
*/
public function affiliationAction(Request $request, ReCaptchaManager $reCaptchaManager, EmailManager $emailManager)
{
$setup = $this->getSetup();
$form = $this->createForm(AffiliationContactType::class);
$form->handleRequest($request);
$googleResponse = $reCaptchaManager->getResponse();
if ($form->isSubmitted() && $form->isValid() && $googleResponse->success) {
$emailManager->sendAffiliationContactMail($form);
$this->addFlash('info', 'flashMessages.site.messageWillBeSend');
return $this->redirectToRoute('brulafine_affiliation');
}
if ($form->isSubmitted() && !$googleResponse->success && property_exists($googleResponse, "error-codes")) {
$recaptchaErrorMessage = 'flashMessages.site.recaptchaErrorMsg';
}
return $this->staticAction($this->getTemplatesDir() . '/Static/affiliation.contact.html.twig', [
'form' => $form->createView(),
'cart' => $setup->getCart(),
'recaptchaErrorMessage' => $recaptchaErrorMessage ?? ''
]);
}
/**
* @Route(
* "/temoignages/",
* name="brulafine_temoignages_no_page")
* @Route(
* "/temoignages/{page}/",
* name="brulafine_temoignages",
* requirements={"page"="\d*"}, defaults={"page"="2"})
* @Route (
* "/avis/",
* name="brulafine_avis_no_page")
* @Route(
* "/avis/{page}/",
* name="brulafine_avis",
* requirements={"page"="\d*"},
* defaults={"page"="2"})
* @param Request $request
* @param EntityManagerInterface $entityManager
* @param SiteManager $siteManager
* @param mixed $page
* @return Response
*/
public function temoignagesAction(Request $request, EntityManagerInterface $entityManager, SiteManager $siteManager, $page = 1)
{
$route = $request->attributes->get('_route');
// Chris don't want anymore the page 1 to exists... we destroy her (RIP)
if (1 == $page && in_array($route, ['brulafine_temoignages', 'brulafine_avis'])) {
throw new NotFoundHttpException("Page not found.");
}
$pageSize = $siteManager->getYamlConfigParameter('testimoniesPerPage');
if ($siteManager->getYamlConfigParameter('siteUseStaticTestimonies')) {
$pagesCount = 5;
if ($page > $pagesCount) {
$page = $pagesCount;
} elseif ($page < 1) {
$page = 1;
}
$testimonyProvider = "data\\{$siteManager->getYamlConfigParameter('websiteName')}\\staticData\\TestimoniesProvider";
$rawTestimonies = $testimonyProvider::$testimonies;
$testimonies = array_map(function (array $item): array {
$item['age'] = date_create()->diff(date_create($item['birthdate']))->y;
return $item;
}, array_slice($rawTestimonies, ($page - 1) * $pageSize, $pageSize));
} else {
$result = $entityManager->getRepository(MarketingTestimony::class)->getTestimoniesPaginator($page, $pageSize, $request->getLocale());
$pagesCount = ceil($result['totalItems'] / $pageSize);
$testimonies = $result['paginator'];
}
$start = (($page - 1) * $pageSize);
if (0 === $start) {
$start = 1;
}
$end = ($page * $pageSize);
return $this->staticAction($this->getTemplatesDir() . '/Static/temoignages.html.twig', [
'testimonies' => $testimonies,
'page' => $page,
'nb_pages' => $pagesCount,
'startItem' => $start,
'endItem' => $end
]);
}
/**
* @Route("/temoignage/{client}/", name="brulafine_temoignage_interactive", defaults={"client"="cyrielle"})
* @param null|string $client
*/
public function temoignagesInteractiveAction(SiteManager $siteManager, string $client = null)
{
return $this->redirectToRoute('brulafine_temoignage_videos', [], 301);
// $hasVideoTestimony = $this->getParameter("hasVideoTestimony");
//
// if (!$hasVideoTestimony) {
// return $this->redirect($this->generateUrl("brulafine_home"));
// }
//
// $testimonyProvider = "data\\{$siteManager->getYamlConfigParameter('websiteName')}\\staticData\\TestimoniesProvider";
// $rawTestimonies = $testimonyProvider::$videoTestimonies;
//
// if (empty($rawTestimonies[$client])) {
// return $this->redirect($this->generateUrl("brulafine_home"));
// }
//
// return $this->staticAction(
// $this->getTemplatesDir() . '/Static/temoignages.photos.html.twig',
// $rawTestimonies[$client]
// );
}
/**
* @Route("/avis/temoignages/", name="brulafine_temoignage_videos")
*/
public function temoignagesVideosAction(Request $request, TranslatorInterface $translator)
{
if (!$translator->getCatalogue($translator->getLocale())->has($request->get('_route'), TranslationManager::TRANSLATION_DOMAIN_ROUTES)) {
return $this->redirect($this->generateUrl("brulafine_home"));
}
// TODO - make it compatible with all the sites and do the translations
$hasVideoTestimony = $this->getParameter("hasVideoTestimony");
if (!$hasVideoTestimony) {
return $this->redirect($this->generateUrl("brulafine_home"));
}
return $this->staticAction(
$this->getTemplatesDir() . '/Static/temoignages.photos.html.twig',
);
}
/**
* @Route("/coaching/", name="brulafine_coaching")
* @param Request $request
*/
public function coachingAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/coaching.html.twig');
}
/**
* @Route("/aide/", name="brulafine_aide")
* @param Request $request
*/
public function aideAction(SiteManager $siteManager)
{
$ingredientsProvider = '\data\\'. $siteManager->getYamlConfigParameter('websiteName') .'\staticData\IngredientsProvider';
return $this->staticAction(
$this->getTemplatesDir() . '/Static/aide.html.twig',
[
'ingredients' => $ingredientsProvider::$ingredients
]
);
}
/**
* @Route("/plan/", name="brulafine_plan")
* @return Response
*/
public function planAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/plan.html.twig');
}
/**
* @Route("/cgu/", name="brulafine_cgu")
* @return Response
*/
public function cguAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/cgu.html.twig');
}
/**
* @Route("/cgv/", name="brulafine_cgv")
* @return Response
*/
public function cgvAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/cgv.html.twig');
}
/**
* @Route("/legal/", name="brulafine_legal")
* @return Response
*/
public function legalAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/mentions-legales.html.twig');
}
/**
* @Route("/confidentialite/", name="brulafine_confidential")
* @return Response
*/
public function confidentialAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/confidentialite-vie-privee.html.twig');
}
/**
* @Route("/cookies/", name="brulafine_cookies")
* @return Response
*/
public function cookiesAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/cookies.html.twig');
}
/**
* @Route("/bruleur-graisse-comment/", name="brulafine_landing_fat_burner_how")
* @return Response
*/
public function landingFatBurnerHowAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/LandingPages/bruleur-graisse-comment.html.twig');
}
/**
* @Route("/bruleur-graisse-naturel/", name="brulafine_landing_fat_burner_normal")
* @return Response
*/
public function landingFatBurnerNormalAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/LandingPages/bruleur-graisse-naturel.html.twig');
}
/**
* @Route("/coupe-faim/", name="brulafine_landing_appetite_suppressant")
* @return Response
*/
public function landingAppetiteSuppressantAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/LandingPages/coupe-faim.html.twig');
}
/**
* @Route("/gelule-minceur/", name="brulafine_landing_slimming_gel")
* @return Response
*/
public function landingSlimmingGelAction()
{
return $this->staticAction($this->getTemplatesDir() . '/Static/LandingPages/gelule-minceur.html.twig');
}
/**
* @Route("/sitemap/sitemap.xml", name="sitemap", defaults={"_format"="xml"})
*/
public function sitemapAction(Request $request)
{
$urls = array();
$hostname = $request->getSchemeAndHttpHost();
// add static urls
$urls[] = array('loc' => $this->generateUrl('brulafine_home'));
$urls[] = array('loc' => $this->generateUrl('brulafine_ingredients'));
$urls[] = array('loc' => $this->generateUrl('brulafine_coaching'));
$urls[] = array('loc' => $this->generateUrl('brulafine_aide'));
$urls[] = array('loc' => $this->generateUrl('brulafine_contact'));
$urls[] = array('loc' => $this->generateUrl('brulafine_plan'));
$urls[] = array('loc' => $this->generateUrl('brulafine_show_packs'));
$urls[] = array('loc' => $this->generateUrl('brulafine_affiliation'));
$urls[] = array('loc' => $this->generateUrl('brulafine_avis_no_page'));
//all avis pages.
$pages = 11;
if ('it' == $request->getLocale()) {
$pages = 6;
}
for ($i = 1; $i <= $pages; $i++) {
$urls[] = array('loc' => $this->generateUrl('brulafine_avis', ['page' => $i]));
}
$urls[] = array('loc' => $this->generateUrl('brulafine_temoignage_videos'));
// return response in XML format
$response = new Response(
$this->renderView($this->getTemplatesDir() . '/Static/sitemap.html.twig', array( 'urls' => $urls,
'hostname' => $hostname)),
200
);
$response->headers->set('Content-Type', 'text/xml');
return $response;
}
protected function get($id)
{
// TODO: Implement get() method.
}
}