<?php
namespace App\Controller;
use App\Entity\Accessoire;
use App\Entity\AccessoireProduit;
use App\Entity\AccessoireTranslation;
use App\Entity\Caracteristique;
use App\Entity\CaracteristiqueProduit;
use App\Entity\CaracteristiqueTranslation;
use App\Entity\Category;
use App\Entity\Coloris;
use App\Entity\ColorisCategory;
use App\Entity\Contact;
use App\Entity\ExpertiseParagraph;
use App\Entity\FaqCategory;
use App\Entity\FicheTechnique;
use App\Entity\Gamme;
use App\Entity\Member;
use App\Entity\Newsletter;
use App\Entity\Page;
use App\Entity\Paragraph;
use App\Entity\Partner;
use App\Entity\Post;
use App\Entity\PostTranslation;
use App\Entity\Product;
use App\Entity\ProductColoris;
use App\Entity\ProductColorisOption;
use App\Entity\ProductMedia;
use App\Entity\ProductTranslation;
use App\Entity\ServiceParagraph;
use App\Entity\Setting;
use App\Entity\SubCategory;
use App\Entity\SubCategoryTranslation;
use App\Entity\Testimonial;
use App\Form\ContactType;
use App\Form\NewsletterType;
use App\Form\ProductsFilterType;
use App\Service\AppService;
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\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 Twig\Environment;
use Twig\Loader\FilesystemLoader;
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);
if ($redirectUrl == "front_product") {
return $this->redirectToRoute("front_products", ['_locale' => $locale]);
} elseif ($redirectUrl == "front_post") {
return $this->redirectToRoute("front_posts", ['_locale' => $locale]);
}
return $this->redirectToRoute($redirectUrl, ['_locale' => $locale]);
}
/**
* @Route({
* "fr": "/societe/presentation",
* "en": "/company/who-are-we"
* }, name="front_who_are_we")
*/
public function whoAreWe(ManagerRegistry $managerRegistry, Request $request)
{
$repoSetting = $managerRegistry->getRepository(Setting::class);
$setting = $repoSetting->findOneBy([]);
return $this->render('front/whoAreWe.html.twig', array(
"setting" => $setting
));
}
/**
* @Route({
* "fr": "/societe/expertises",
* "en": "/company/expertises"
* }, name="front_expertises")
*/
public function expertises(ManagerRegistry $managerRegistry, Request $request)
{
$repoExpertiseParagraph = $managerRegistry->getRepository(ExpertiseParagraph::class);
$expertiseParagraphs = $repoExpertiseParagraph->findBy([], ["position" => "asc"]);
return $this->render('front/expertises.html.twig', array(
"expertiseParagraphs" => $expertiseParagraphs
));
}
/**
* @Route({
* "fr": "/societe/services",
* "en": "/company/services"
* }, name="front_services")
*/
public function services(ManagerRegistry $managerRegistry, Request $request)
{
$repoServiceParagraph = $managerRegistry->getRepository(ServiceParagraph::class);
$serviceParagraphs = $repoServiceParagraph->findBy([], ["position" => "asc"]);
return $this->render('front/services.html.twig', array(
"serviceParagraphs" => $serviceParagraphs
));
}
/**
* @Route({
* "fr": "/produits/{catSlug}/{subCatSlug}",
* "en": "/products/{catSlug}/{subCatSlug}"
* }, name="front_products")
*/
public function products(ManagerRegistry $managerRegistry, PaginatorInterface $paginator, Request $request, $subCatSlug = null, $catSlug = 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]);
// $data["orderBys"] = ["a.category" => "asc", "a.subCategory" => "asc", "a.gamme" => "asc"];
$data["orderBys"] = ["a.id" => "asc"];
$data["limit"] = 15;
$data["searchProducts"] = ["categories" => [], "subCategories" => []];
if (!$request->isXmlHttpRequest()) {
if ($subCategory) {
$data["searchProducts"]["subCategories"][] = $subCategory;
}
if ($category) {
$data["searchProducts"]["categories"][] = $category;
}
}
$productsFilterForm = $this->createForm(ProductsFilterType::class);
$productsFilterForm->handleRequest($request);
if ($productsFilterForm->isSubmitted() && $productsFilterForm->isValid()) {
$data["searchProducts"] = $productsFilterForm->getData();
}
$repoProduct = $managerRegistry->getRepository(Product::class);
$products = $repoProduct->search($data);
$products = (is_array($products) ? $products : ($products ? [$products] : []));
return $this->render('front/products.html.twig', array(
'productsFilterForm' => $productsFilterForm->createView(),
"products" => $products,
"categoryActive" => $category,
"subCategoryActive" => $subCategory,
));
}
/**
* @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()) {
$appService->suscribeToList($newsletter->getEmail());
$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("/fdskj986765GDHOjfdkfhjr87890", name="front_import_products_csv")
*/
public function importProductsCsv(ManagerRegistry $managerRegistry, Request $request)
{
dump("cancel");
exit;
$row = 1;
$repoCategory = $managerRegistry->getRepository(Category::class);
$repoSubCategory = $managerRegistry->getRepository(SubCategory::class);
$repoGamme = $managerRegistry->getRepository(Gamme::class);
$repoColoris = $managerRegistry->getRepository(Coloris::class);
$repoCaracteristique = $managerRegistry->getRepository(Caracteristique::class);
$repoCaracteristiqueProduit = $managerRegistry->getRepository(CaracteristiqueProduit::class);
$repoCaracteristiqueTranslation = $managerRegistry->getRepository(CaracteristiqueTranslation::class);
$repoAccessoire = $managerRegistry->getRepository(Accessoire::class);
$repoProduct = $managerRegistry->getRepository(Product::class);
$em = $managerRegistry->getManager();
for ($i = 0; $i < 2; $i++) {
$nbColonnesAttributs = ($i ? 14 : 10);
if (($handle = fopen("products" . ($i ? "1" : "") . ".csv", "r")) !== FALSE) {
$attributes = [];
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if ($row == 1) {
for ($j = 18; $j < (18 + $nbColonnesAttributs); $j++) {
$attribute = explode("(", $data[$j]);
$attribute = explode(")", $attribute[1]);
$attribute = $attribute[0];
$attributes[] = $attribute;
}
}
if ($row > 1) {
$product = new Product();
$em->persist($product);
$category = $repoCategory->search(["limit" => 1, "title" => trim($data[0])]);
$product->setCategory($category);
$subCategory = $repoSubCategory->search(["limit" => 1, "title" => trim($data[1])]);
$product->setSubCategory($subCategory);
$gamme = $repoGamme->search(["limit" => 1, "title" => trim($data[2])]);
$product->setGamme($gamme);
$product->translate('fr')->setTitle(trim($data[3]));
$product->setReference(trim($data[4]));
$product->translate('fr')->setIntro(trim($data[5]));
$product->translate('fr')->setDescription(trim($data[6]));
$colorisArr = array_merge(explode(",", $data[8]), explode(",", $data[12]));
$colorisOptionArr = array_merge(explode(",", $data[10]), explode(",", $data[14]));
foreach ([$colorisArr, $colorisOptionArr] as $key1 => $colSerOpt) {
$isOption = ($key1 == 1);
foreach ($colSerOpt as $key => $colorisTitle) {
$coloris = $repoColoris->search(["limit" => 1, "title" => trim($colorisTitle)]);
if ($coloris) {
$productColoris = ($isOption ? new ProductColorisOption() : new ProductColoris());
$productColoris->setProduct($product);
$productColoris->setColoris($coloris);
$em->persist($productColoris);
}
}
}
$product->translate('fr')->setInfo(trim($data[15]));
$caracteristiques = explode("\n", $data[16]);
$caracteristiquesValues = explode("\n", $data[17]);
foreach ([$caracteristiques, $attributes] as $key => $carAtt) {
$isFilter = ($key == 1);
foreach ($carAtt as $key => $caracteristiqueTitle) {
$caracteristiqueTitle = trim($caracteristiqueTitle);
$caracteristique = $repoCaracteristique->search(["limit" => 1, "title" => $caracteristiqueTitle]);
if (!$caracteristique) {
$caracteristique = new Caracteristique();
$caracteristique->translate('fr')->setTitle($caracteristiqueTitle);
$caracteristique->mergeNewTranslations();
$em->persist($caracteristique);
}
$caracteristique->setIsFilter($isFilter);
$carAttrValue = ($isFilter ? $data[(18 + $key)] : (isset($caracteristiquesValues[$key]) ? $caracteristiquesValues[$key] : null));
if ($carAttrValue) {
$caracteristiqueProduit = $repoCaracteristiqueProduit->findOneBy(["caracteristique" => $caracteristique, "product" => $product, "locale" => "fr"]) ?? new CaracteristiqueProduit();
$caracteristiqueProduit->setProduct($product);
$caracteristiqueProduit->setCaracteristique($caracteristique);
$caracteristiqueProduit->setLocale("fr");
$caracteristiqueProduit->setTitle(trim($carAttrValue));
$em->persist($caracteristiqueProduit);
}
$product->mergeNewTranslations();
$em->flush();
}
}
$mediasArr = explode("\n", $data[(18 + $nbColonnesAttributs)]);
$mediasArr[] = $data[(20 + $nbColonnesAttributs)];
foreach ($mediasArr as $key => $productMediaFileName) {
$productMedia = new ProductMedia();
$productMedia->setProduct($product);
$productMedia->translate('fr')->setTitle(trim($productMediaFileName));
$productMedia->translate('en')->setTitle(trim($productMediaFileName));
$productMedia->setProductMediaFileName(trim($productMediaFileName));
$productMedia->mergeNewTranslations();
$em->persist($productMedia);
}
$ficheTechnique = new FicheTechnique();
$ficheTechnique->setLocale("fr");
$ficheTechnique->setProduct($product);
$ficheTechnique->setFicheTechniqueFileName(trim($data[(19 + $nbColonnesAttributs)]));
$product->setProductCe(($data[(21 + $nbColonnesAttributs)] == "logo CE"));
$product->setFabricationFrancaise(($data[(22 + $nbColonnesAttributs)] == "oui"));
$em->persist($ficheTechnique);
$product->mergeNewTranslations();
}
$row++;
}
$em->flush();
fclose($handle);
}
if (($handle = fopen("products" . ($i ? "1" : "") . "_en.csv", "r")) !== FALSE) {
$row = 1;
$attributes = [];
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if ($row == 1) {
for ($j = 18; $j < (18 + $nbColonnesAttributs); $j++) {
$attribute = explode("(", $data[$j]);
$attribute = explode(")", $attribute[1]);
$attribute = $attribute[0];
$attributes[] = $attribute;
}
}
if ($row > 1) {
$product = $repoProduct->findOneBy(["reference" => trim($data[4])]);
if ($product) {
$productTranslation = new ProductTranslation();
$productTranslation->setTranslatable($product);
$productTranslation->setLocale("en");
$productTranslation->setTitle(trim($data[3]));
$productTranslation->setIntro(trim($data[5]));
$productTranslation->setDescription(trim($data[6]));
$productTranslation->setInfo(trim($data[15]));
$em->persist($productTranslation);
$caracteristiques = explode("\n", $data[16]);
$caracteristiquesValues = explode("\n", $data[17]);
$attributesClean = [];
$attributesValClean = [];
foreach ($attributes as $key => $attribute) {
if ($data[(18 + $key)]) {
$attributesClean[] = $attribute;
$attributesValClean[] = $data[(18 + $key)];
}
}
$carAttrVal = array_merge($caracteristiquesValues, $attributesValClean);
$carAttr = array_merge($caracteristiques, $attributesClean);
$caracteristiqueProduits = $repoCaracteristiqueProduit->findBy(["product" => $product], ["position" => 'asc']);
foreach ($caracteristiqueProduits as $key => $caracteristiqueProduit) {
$caracteristique = $caracteristiqueProduit->getCaracteristique();
$caracteristiqueTranslation = $repoCaracteristiqueTranslation->findOneBy(["translatable" => $caracteristique, "locale" => "en"]);
if (!$caracteristiqueTranslation) {
$caracteristiqueTranslation = new CaracteristiqueTranslation();
$caracteristiqueTranslation->setTranslatable($caracteristique);
$caracteristiqueTranslation->setLocale("en");
$caracteristiqueTranslation->setTitle(trim($carAttr[$key]));
$em->persist($caracteristiqueTranslation);
}
$caracteristiqueProduit = new CaracteristiqueProduit();
$caracteristiqueProduit->setProduct($product);
$caracteristiqueProduit->setCaracteristique($caracteristique);
$caracteristiqueProduit->setLocale("en");
$caracteristiqueProduit->setTitle(trim($carAttrVal[$key]));
$em->persist($caracteristiqueProduit);
$em->flush();
}
$ficheTechnique = new FicheTechnique();
$ficheTechnique->setLocale("en");
$ficheTechnique->setProduct($product);
$ficheTechnique->setFicheTechniqueFileName(trim($data[(19 + $nbColonnesAttributs)]));
$em->persist($ficheTechnique);
}
}
$row++;
}
$em->flush();
fclose($handle);
}
}
$row = 1;
if (($handle = fopen("accessoires.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if ($row > 1) {
$accessoire = new Accessoire();
$em->persist($accessoire);
$productRefs = explode("\n", $data[0]);
foreach ($productRefs as $key => $productRef) {
$product = $repoProduct->findOneBy(["reference" => trim($productRef)]);
if ($product) {
$accessoireProduit = new AccessoireProduit();
$accessoireProduit->setAccessoire($accessoire);
$accessoireProduit->setProduct($product);
$em->persist($accessoireProduit);
}
}
$accessoire->setReference(trim($data[1]));
$accessoire->setTitle(trim($data[2]));
$accessoire->setAccessoireFileName(trim($data[3]));
$accessoire->mergeNewTranslations();
}
$row++;
}
$em->flush();
fclose($handle);
}
$row = 1;
$accessoires = $repoAccessoire->findBy([], ["id" => "asc"]);
if (($handle = fopen("accessoires_en.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {
if ($row > 1) {
$accessoire = $accessoires[($row - 2)];
if ($accessoire) {
$accessoireTranslation = new AccessoireTranslation();
$accessoireTranslation->setTranslatable($accessoire);
$accessoireTranslation->setLocale("en");
$accessoireTranslation->setTitle(trim($data[2]));
$em->persist($accessoireTranslation);
}
}
$row++;
}
$em->flush();
fclose($handle);
}
exit;
}
/**
* @Route({
* "fr": "/support",
* "en": "/faq"
* }, name="front_faq")
*/
public function faq(ManagerRegistry $managerRegistry, Request $request)
{
$repoFaqCategory = $managerRegistry->getRepository(FaqCategory::class);
$faqCategories = $repoFaqCategory->findBy([], ["position" => "asc"]);
return $this->render('front/faq.html.twig', array(
"faqCategories" => $faqCategories
));
}
/**
* @Route("/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');
}
/**
* @Route({
* "fr": "/guide-coloris",
* "en": "/colors-guide"
* }, name="front_coloris_guide")
*/
public function colorisGuide(ManagerRegistry $managerRegistry, Request $request)
{
$repoColorisCategory = $managerRegistry->getRepository(ColorisCategory::class);
$colorisCategories = $repoColorisCategory->findBy([], ["position" => "asc"]);
return $this->render('front/colorisGuide.html.twig', array(
"colorisCategories" => $colorisCategories
));
}
/**
* @Route({
* "fr": "/produit/{slug}",
* "en": "/product/{slug}"
* }, name="front_product")
*/
public function product(ManagerRegistry $managerRegistry, Request $request, ProductTranslation $productTranslation)
{
$repoColorisCategory = $managerRegistry->getRepository(ColorisCategory::class);
$colorisCategories = $repoColorisCategory->findBy([], ["position" => "asc"]);
$product = $productTranslation->getTranslatable();
$repoProductColoris = $managerRegistry->getRepository(ProductColoris::class);
$productColoris = $repoProductColoris->findBy(["product" => $product], ["position" => "asc"]);
$repoProductColorisOption = $managerRegistry->getRepository(ProductColorisOption::class);
$productColorisOptions = $repoProductColorisOption->findBy(["product" => $product], ["position" => "asc"]);
return $this->render('front/product.html.twig', array(
"product" => $product,
"colorisCategories" => $colorisCategories,
"productColoris" => $productColoris,
"productColorisOptions" => $productColorisOptions,
));
}
/**
* @Route({
* "fr": "/mentions-legales",
* "en": "/legals"
* }, name="front_privacy_policy")
*/
public function privacyPolicy(ManagerRegistry $managerRegistry, Request $request)
{
return $this->render('front/privacyPolicy.html.twig', array());
}
/**
* @Route("/", name="front_landing")
*/
public function landing(Request $request, ManagerRegistry $managerRegistry): Response
{
$repoPartner = $managerRegistry->getRepository(Partner::class);
$partners = $repoPartner->findBy([], ["position" => "asc"]);
$repoPost = $managerRegistry->getRepository(Post::class);
$data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
$data["endDate"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$data["limit"] = 6;
$posts = $repoPost->search($data);
$repoTestimonial = $managerRegistry->getRepository(Testimonial::class);
$testimonials = $repoTestimonial->findBy([], ["position" => "asc"]);
$repoProduct = $managerRegistry->getRepository(Product::class);
$products = $repoProduct->findBy(["displayOnLanding" => true], ["position" => "asc"], 4);
$repoCategory = $managerRegistry->getRepository(Category::class);
$categories = $repoCategory->findBy([], ["position" => "asc"]);
return $this->render('front/landing.html.twig', [
"posts" => $posts,
"partners" => $partners,
"testimonials" => $testimonials,
"products" => $products,
"categories" => $categories,
]);
}
/**
* @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({
* "fr": "/contact/{slug}",
* "en": "/contact-us/{slug}"
* }, name="front_contact", defaults={"slug":null})
*/
public function contact(Request $request, ManagerRegistry $managerRegistry, ProductTranslation $productTranslation = null)
{
$product = $productTranslation ? $productTranslation->getTranslatable() : null;
$contact = new Contact();
$contactForm = $this->createForm(ContactType::class, $contact, [
"product" => $product
]);
$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);
$locale = $request->getLocale();
$email = (new TemplatedEmail())
->from($this->getParameter('mailer_user'))
->to(($locale == "en" ? "s.ivanoff@carinamedical.com" : "commercial@carinamedical.com"))
->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_contact');
}
}
return $this->render('front/contact.html.twig', array(
'contactForm' => $contactForm->createView(),
));
}
/**
* @Route({
* "fr": "/plan-du-site",
* "en": "/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({
* "fr": "/actualites",
* "en": "/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["endDate"] = new \DateTime();
$data["orderBy"] = ["a.date", "desc"];
$data["isPublished"] = true;
$posts = $paginator->paginate(
$repoPost->search($data), $request->query->getInt('page', 1)/* page number */, 6/* limit per page */
);
return $this->render('front/posts.html.twig', array(
"posts" => $posts
));
}
/**
* @Route("/navbar-search", name="front_navbar_search")
*/
public function navbarSearch(ManagerRegistry $managerRegistry, Request $request)
{
if ($request->isXmlHttpRequest()) {
$query = $request->request->get('query', null);
$repoProduct = $managerRegistry->getRepository(Product::class);
$products = $repoProduct->search(["search" => $query, "orderBy" => ["a.position", "asc"]]);
$response = new Response(json_encode(array(
'template' => $this->renderView('front/navbarSearch.html.twig', [
"products" => $products,
]),
)));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
// return $this->render('front/navbarSearch.html.twig', array());
}
/**
* @Route("/distributeurs", name="front_distributeurs")
*/
public function distributeurs(ManagerRegistry $managerRegistry, Request $request)
{
return $this->render('front/distributeurs.html.twig', array());
}
/**
* @Route({"en": "/post/{slug}", "fr": "/actualite/{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"]);
return $this->render('front/post.html.twig', array(
"post" => $post,
"paragraphs" => $paragraphs,
));
} 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');
}
}
}