src/Controller/FrontController.php line 105

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Application;
  4. use App\Entity\ApplicationParagraph;
  5. use App\Entity\ApplicationProduct;
  6. use App\Entity\ApplicationTranslation;
  7. use App\Entity\Category;
  8. use App\Entity\Contact;
  9. use App\Entity\JobOffer;
  10. use App\Entity\JobOfferParagraph;
  11. use App\Entity\JobOfferTranslation;
  12. use App\Entity\Member;
  13. use App\Entity\Newsletter;
  14. use App\Entity\Page;
  15. use App\Entity\Paragraph;
  16. use App\Entity\Partner;
  17. use App\Entity\Post;
  18. use App\Entity\PostMedia;
  19. use App\Entity\PostTranslation;
  20. use App\Entity\PresentationParagraph;
  21. use App\Entity\Product;
  22. use App\Entity\ProductMedia;
  23. use App\Entity\ProductTranslation;
  24. use App\Entity\RelatedProduct;
  25. use App\Entity\SubCategory;
  26. use App\Entity\Testimonial;
  27. use App\Form\ContactType;
  28. use App\Form\NewsletterType;
  29. use App\Form\ProductsFilterType;
  30. use App\Service\AppService;
  31. use Doctrine\Persistence\ManagerRegistry;
  32. use Knp\Component\Pager\PaginatorInterface;
  33. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  34. use Symfony\Bridge\Twig\Mime\BodyRenderer;
  35. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  36. use Symfony\Bundle\FrameworkBundle\Console\Application as FrameworkApplication;
  37. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  38. use Symfony\Component\Asset\Packages;
  39. use Symfony\Component\Console\Input\ArrayInput;
  40. use Symfony\Component\Console\Output\NullOutput;
  41. use Symfony\Component\HttpFoundation\Request;
  42. use Symfony\Component\HttpFoundation\Response;
  43. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  44. use Symfony\Component\HttpKernel\KernelInterface;
  45. use Symfony\Component\Mailer\Mailer;
  46. use Symfony\Component\Mailer\MailerInterface;
  47. use Symfony\Component\Mailer\Transport;
  48. use Symfony\Component\Mime\Email;
  49. use Symfony\Component\Routing\Annotation\Route;
  50. use Symfony\Contracts\Translation\TranslatorInterface;
  51. use Twig\Environment;
  52. use Twig\Loader\FilesystemLoader;
  53. use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
  54. use Symfony\Component\Filesystem\Filesystem;
  55. use Symfony\Component\Filesystem\Path;
  56. /**
  57.  * @Route("/{_locale}", requirements={"_locale": "%managed_locales_routing%"})
  58.  */
  59. class FrontController extends AbstractController
  60. {
  61.     public static $LIMIT_PRODUCTS 9;
  62.     /**
  63.      * @Route({
  64.      *     "fr": "/changer-la-langue/{locale}/{routeName}/{routeParams}",
  65.      *     "en": "/change-language/{locale}/{routeName}/{routeParams}"
  66.      * }, name="front_change_language", requirements={"routeParams"=".+"})
  67.      */
  68.     public function changeLanguage(Request $requestSessionInterface $session$locale$routeName$routeParams null)
  69.     {
  70.         $routeParams array_merge(["_locale" => $locale], $_GET);
  71.         return $this->redirectToRoute($routeName$routeParams);
  72.     }
  73.     /**
  74.      * @Route({
  75.      *     "fr": "/application/{slug}",
  76.      *     "en": "/application/{slug}"
  77.      * }, name="front_application")
  78.      */
  79.     public function application(ManagerRegistry $managerRegistryRequest $requestApplicationTranslation $applicationTranslation)
  80.     {
  81.         $application $applicationTranslation->getTranslatable();
  82.         $repoApplicationParagraph $managerRegistry->getRepository(ApplicationParagraph::class);
  83.         $applicationParagraphs $repoApplicationParagraph->findBy([], ["position" => "asc"]);
  84.         $repoApplicationProduct $managerRegistry->getRepository(ApplicationProduct::class);
  85.         $applicationProducts $repoApplicationProduct->findBy(["application" => $application], ["position" => "asc"]);
  86.         return $this->render('front/application.html.twig', array(
  87.             "application" => $application,
  88.             "applicationProducts" => $applicationProducts,
  89.             "applicationParagraphs" => $applicationParagraphs
  90.         ));
  91.     }
  92.     /**
  93.      * @Route({
  94.      *     "fr": "/presentation",
  95.      *     "en": "/presentation"
  96.      * }, name="front_presentation")
  97.      */
  98.     public function presentation(ManagerRegistry $managerRegistryRequest $request)
  99.     {
  100.         $repoPresentationParagraph $managerRegistry->getRepository(PresentationParagraph::class);
  101.         $presentationParagraphs $repoPresentationParagraph->findBy(["isOther" => false], ["position" => "asc"]);
  102.         $otherPresentationParagraphs $repoPresentationParagraph->findBy(["isOther" => true], ["position" => "asc"]);
  103.         return $this->render('front/presentation.html.twig', array(
  104.             "presentationParagraphs" => $presentationParagraphs,
  105.             "otherPresentationParagraphs" => $otherPresentationParagraphs,
  106.         ));
  107.     }
  108.     /**
  109.      * @Route({
  110.      *     "fr": "/produits/{catSlug}/{subCatSlug}",
  111.      *     "en": "/products/{catSlug}/{subCatSlug}"
  112.      * }, name="front_products")
  113.      */
  114.     public function products(ManagerRegistry $managerRegistryPaginatorInterface $paginatorRequest $request$catSlug$subCatSlug null)
  115.     {
  116.         $repoSubCategory $managerRegistry->getRepository(SubCategory::class);
  117.         $subCategory $repoSubCategory->search(["slug" => $subCatSlug"limit" => 1]);
  118.         $repoCategory $managerRegistry->getRepository(Category::class);
  119.         $category $subCategory $subCategory->getCategory() : $repoCategory->search(["slug" => $catSlug"limit" => 1]);
  120.         $data["orderBy"] = ["a.position""asc"];
  121.         $data["limit"] = self::$LIMIT_PRODUCTS;
  122.         $data["hide"] = false;
  123.         $data["searchProducts"] = ["categories" => [], "subCategories" => []];
  124.         if (!$request->isXmlHttpRequest()) {
  125.             if ($subCategory) {
  126.                 $data["searchProducts"]["subCategories"][] = $subCategory;
  127.             }
  128.             if ($category) {
  129.                 $data["searchProducts"]["categories"][] = $category;
  130.             }
  131.         }
  132.         $productsFilterForm $this->createForm(ProductsFilterType::class);
  133.         $productsFilterForm->handleRequest($request);
  134.         if ($productsFilterForm->isSubmitted() && $productsFilterForm->isValid()) {
  135.             $data["searchProducts"] = $productsFilterForm->getData();
  136.         }
  137.         $repoProduct $managerRegistry->getRepository(Product::class);
  138.         $products $repoProduct->search($data);
  139.         $data['count'] = true;
  140.         unset($data['offset']);
  141.         unset($data['limit']);
  142.         unset($data['orderBy']);
  143.         $nbProducts $repoProduct->search($data);
  144.         $nbProducts count($nbProducts);
  145.         $products = (is_array($products) ? $products : ($products ? [$products] : []));
  146.         $otherCategories $repoCategory->search(["different" => ["a.id" => $category->getId()], "isChaine" => false]);
  147.         $otherSubCategories $subCategory $repoSubCategory->search(["different" => ["a.id" => $subCategory->getId()], "category" => $category]) : [];
  148.         return $this->render('front/products.html.twig', array(
  149.             'productsFilterForm' => $productsFilterForm->createView(),
  150.             "products" => $products,
  151.             'limit' => self::$LIMIT_PRODUCTS,
  152.             'total' => $nbProducts,
  153.             "categoryActive" => $category,
  154.             "subCategoryActive" => $subCategory,
  155.             "otherCategories" => $otherCategories,
  156.             "otherSubCategories" => $otherSubCategories
  157.         ));
  158.     }
  159.     /**
  160.      * @Route({
  161.      *     "fr": "/produit/{catSlug}/{subCatSlug}/{slug}",
  162.      *     "en": "/product/{catSlug}/{subCatSlug}/{slug}",
  163.      * }, name="front_product")
  164.      */
  165.     public function product(ManagerRegistry $managerRegistryRequest $request$catSlug$subCatSlugProductTranslation $productTranslation)
  166.     {
  167.         $product $productTranslation->getTranslatable();
  168.         $repoRelatedProduct $managerRegistry->getRepository(RelatedProduct::class);
  169.         $relatedProducts $repoRelatedProduct->findBy(["product" => $product], ["position" => "asc"]);
  170.         $repoProductMedia $managerRegistry->getRepository(ProductMedia::class);
  171.         $productMedias $repoProductMedia->findBy(["product" => $product], ["position" => "asc"]);
  172.         return $this->render('front/product.html.twig', array(
  173.             "relatedProducts" => $relatedProducts,
  174.             "productMedias" => $productMedias,
  175.             "product" => $product
  176.         ));
  177.     }
  178.     /**
  179.      * @Route({
  180.      *     "fr": "/mentions-legales",
  181.      *     "en": "/legal-terms",
  182.      * }, name="front_privacy_policy")
  183.      */
  184.     public function privacyPolicy(ManagerRegistry $managerRegistryRequest $request)
  185.     {
  186.         return $this->render('front/privacyPolicy.html.twig', array());
  187.     }
  188.     /**
  189.      * @Route({
  190.      *     "fr": "/",
  191.      *     "en": "/",
  192.      * }, name="front_landing")
  193.      */
  194.     public function landing(Request $requestManagerRegistry $managerRegistry): Response
  195.     {
  196.         $repoPartner $managerRegistry->getRepository(Partner::class);
  197.         $partners $repoPartner->findBy([], ["position" => "asc"]);
  198.         $repoPost $managerRegistry->getRepository(Post::class);
  199.         $data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
  200.         $data["orderBy"] = ["a.date""desc"];
  201.         $data["isPublished"] = true;
  202.         $data["limit"] = 3;
  203.         $posts $repoPost->search($data);
  204.         $repoTestimonial $managerRegistry->getRepository(Testimonial::class);
  205.         $testimonials $repoTestimonial->findBy([], ["position" => "asc"]);
  206.         $repoApplication $managerRegistry->getRepository(Application::class);
  207.         $applications $repoApplication->findBy(["displayOnLanding" => true], ["position" => "asc"]);
  208.         return $this->render('front/landing.html.twig', [
  209.             "applications" => $applications,
  210.             "posts" => $posts,
  211.             "partners" => $partners,
  212.             "testimonials" => $testimonials,
  213.         ]);
  214.     }
  215.     /**
  216.      * @Route({
  217.      *     "fr": "/offres-d-emploi",
  218.      *     "en": "/job-offers",
  219.      * }, name="front_job_offers")
  220.      */
  221.     public function jobOffers(ManagerRegistry $managerRegistryRequest $request)
  222.     {
  223.         $repoJobOffer $managerRegistry->getRepository(JobOffer::class);
  224.         $jobOffers $repoJobOffer->findBy([], ["position" => "asc"]);
  225.         return $this->render('front/jobOffers.html.twig', array(
  226.             "jobOffers" => $jobOffers
  227.         ));
  228.     }
  229.     /**
  230.      * @Route({
  231.      *     "fr": "/offre-d-emploi/{slug}",
  232.      *     "en": "/job-offer/{slug}",
  233.      * }, name="front_job_offer")
  234.      */
  235.     public function jobOffer(ManagerRegistry $managerRegistryRequest $requestJobOfferTranslation $jobOfferTranslation)
  236.     {
  237.         $jobOffer $jobOfferTranslation->getTranslatable();
  238.         $repoJobOfferParagraph $managerRegistry->getRepository(JobOfferParagraph::class);
  239.         $jobOfferParagraphs $repoJobOfferParagraph->findBy(["jobOffer" => $jobOffer], ["position" => "asc"]);
  240.         return $this->render('front/jobOffer.html.twig', array(
  241.             "jobOffer" => $jobOffer,
  242.             "jobOfferParagraphs" => $jobOfferParagraphs,
  243.         ));
  244.     }
  245.     public function sitemap(Request $requestPackages $packages)
  246.     {
  247.         $repoPage $this->getDoctrine()->getRepository(Page::class);
  248.         $page $repoPage->search(["notNull" => ["a.sitemapFileName"], "limit" => 1]);
  249.         if ($page and $page->getSitemapFileName()) {
  250.             return $this->redirect($packages->getUrl('upload/sitemap/' $page->getSitemapFileName()));
  251.         } else {
  252.             return $this->redirectToRoute('front_landing');
  253.         }
  254.     }
  255.     /**
  256.      * @Route({
  257.      *     "fr": "/contactez-nous/{slug}",
  258.      *     "en": "/contact-us/{slug}",
  259.      * }, name="front_contact", defaults={"slug":null})
  260.      */
  261.     public function contact(Request $requestManagerRegistry $managerRegistryTranslatorInterface $translator$slug null)
  262.     {
  263.         $repoProduct $managerRegistry->getRepository(Product::class);
  264.         $product $slug $repoProduct->search(["slug" => $slug"limit" => 1]) : null;
  265.         $contact = new Contact();
  266.         $contactForm $this->createForm(ContactType::class, $contact, [
  267.             "product" => $product
  268.         ]);
  269.         $contactForm->handleRequest($request);
  270.         if ($contactForm->isSubmitted() && $contactForm->isValid()) {
  271.             $recaptchaResponse $request->request->get('g-recaptcha-response'null);
  272.             $isRecaptchaValid false;
  273.             if ($recaptchaResponse) {
  274.                 $paramsArr = array(
  275.                     "response" => $recaptchaResponse,
  276.                     "secret" => $this->getParameter('recaptchaSecret')
  277.                 );
  278.                 $ch curl_init();
  279.                 curl_setopt($chCURLOPT_URL"https://www.google.com/recaptcha/api/siteverify");
  280.                 curl_setopt($chCURLOPT_POST1);
  281.                 curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($paramsArr));
  282.                 curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  283.                 $isRecaptchaValid json_decode(curl_exec($ch))->success;
  284.             }
  285.             if (!$isRecaptchaValid) {
  286.                 $this->addFlash("danger""Veuillez recommencer en validant le captcha.");
  287.             } else {
  288.                 $em $managerRegistry->getManager();
  289.                 $em->persist($contact);
  290.                 $em->flush();
  291.                 $this->addFlash("success""Message envoyé.");
  292.                 $transport Transport::fromDsn($this->getParameter('mailer_dsn'));
  293.                 $mailer = new Mailer($transport);
  294.                 $subject = ("Nouveau message : " $contact->getSubject() . " " $contact->getCity());
  295.                 $email = (new TemplatedEmail())
  296.                     ->from($this->getParameter('mailer_user'))
  297.                     ->to("facomia.sarl@facomia.com")
  298.                     ->subject($subject)
  299.                     // path of the Twig template to render
  300.                     ->htmlTemplate('mail/contact.html.twig')
  301.                     // pass variables (name => value) to the template
  302.                     ->context(["contact" => $contact]);
  303.                 if ($contact->getEmail()) {
  304.                     $email
  305.                         ->replyTo($contact->getEmail());
  306.                 }
  307.                 foreach ($contact->getCustomFiles() as $key => $customFile) {
  308.                     $email->attachFromPath(("upload/customFile/" $customFile->getCustomFileFileName()));
  309.                 }
  310.                 $loader = new FilesystemLoader($this->getParameter('kernel.project_dir') . '/templates/');
  311.                 $twigEnv = new Environment($loader);
  312.                 $twigBodyRenderer = new BodyRenderer($twigEnv);
  313.                 $twigBodyRenderer->render($email);
  314.                 $mailer->send($email);
  315.                 return $this->redirectToRoute('front_contact');
  316.             }
  317.         }
  318.         return $this->render('front/contact.html.twig', array(
  319.             'contactForm' => $contactForm->createView(),
  320.         ));
  321.     }
  322.     /**
  323.      * @Route({
  324.      *     "fr": "/plan-du-site",
  325.      *     "en": "/sitemap",
  326.      * }, name="front_sitemap_links")
  327.      */
  328.     public function sitemapLinks(ManagerRegistry $managerRegistryRequest $request)
  329.     {
  330.         return $this->render('front/sitemapLinks.html.twig', array());
  331.     }
  332.     /**
  333.      * @Route({
  334.      *     "fr": "/notre-equipe",
  335.      *     "en": "/our-team",
  336.      * }, name="front_members")
  337.      */
  338.     public function members(ManagerRegistry $managerRegistryRequest $request)
  339.     {
  340.         $repoMember $managerRegistry->getRepository(Member::class);
  341.         $members $repoMember->findBy([], ["position" => "asc"]);
  342.         return $this->render('front/members.html.twig', array(
  343.             "members" => $members
  344.         ));
  345.     }
  346.     /**
  347.      * @Route({
  348.      *     "fr": "/actualites",
  349.      *     "en": "/news",
  350.      * }, name="front_posts")
  351.      */
  352.     public function posts(Request $requestPaginatorInterface $paginatorManagerRegistry $managerRegistry)
  353.     {
  354.         $repoPost $managerRegistry->getRepository(Post::class);
  355.         $data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
  356.         $data["orderBy"] = ["a.date""desc"];
  357.         $data["isPublished"] = true;
  358.         $posts $paginator->paginate(
  359.             $repoPost->search($data), $request->query->getInt('page'1)/* page number */12/* limit per page */
  360.         );
  361.         return $this->render('front/posts.html.twig', array(
  362.             "posts" => $posts
  363.         ));
  364.     }
  365.     /**
  366.      * @Route({
  367.      *     "fr": "/navbar-search",
  368.      *     "en": "/navbar-search",
  369.      * }, name="front_navbar_search")
  370.      */
  371.     public function navbarSearch(ManagerRegistry $managerRegistryRequest $request)
  372.     {
  373.         $query $request->query->get('nav-search');
  374.         $repoPost $managerRegistry->getRepository(Post::class);
  375.         $data["search"] = $query;
  376.         $data["isPublished"] = true;
  377.         $data["orderBy"] = ["a.date""asc"];
  378.         $posts $repoPost->search($data);
  379.         return $this->render('front/navbarSearch.html.twig', array(
  380.             "posts" => $posts,
  381.             "navSearchQuery" => $query
  382.         ));
  383.     }
  384.     /**
  385.      * @Route({
  386.      *     "fr": "/actualite/{slug}",
  387.      *     "en": "/news/{slug}",
  388.      * }, name="front_post")
  389.      */
  390.     public function post(Request $requestManagerRegistry $managerRegistrystring $slug)
  391.     {
  392.         $repoPostTranslation $managerRegistry->getRepository(PostTranslation::class);
  393.         $postTranslation $repoPostTranslation->findOneBy(["slug" => $slug]);
  394.         if ($postTranslation and $post $postTranslation->getTranslatable()) {
  395.             if ($post->getIsPublished() or $this->isGranted("ROLE_ADMIN")) {
  396.                 $repoParagraph $managerRegistry->getRepository(Paragraph::class);
  397.                 $paragraphs $repoParagraph->findBy(["post" => $post], ["position" => "asc"]);
  398.                 $repoPostMedia $managerRegistry->getRepository(PostMedia::class);
  399.                 $postMedias $repoPostMedia->findBy(["post" => $post], ["position" => "asc"]);
  400.                 $repoPost $managerRegistry->getRepository(Post::class);
  401.                 $data["between"]["notStrict"]["a.date"]["max"] = new \DateTime();
  402.                 $data["orderBy"] = ["a.date""desc"];
  403.                 $data["isPublished"] = true;
  404.                 $data["limit"] = 2;
  405.                 $data["different"] = ["a.id" => $post->getId()];
  406.                 $posts $repoPost->search($data);
  407.                 return $this->render('front/post.html.twig', array(
  408.                     "posts" => $posts,
  409.                     "post" => $post,
  410.                     "paragraphs" => $paragraphs,
  411.                     "postMedias" => $postMedias,
  412.                 ));
  413.             } else {
  414.                 $this->addFlash("danger""flash.not_allowed_post");
  415.                 return $this->redirectToRoute('front_posts');
  416.             }
  417.         } else {
  418.             $this->addFlash("danger""flash.post_not_found");
  419.             return $this->redirectToRoute('front_posts');
  420.         }
  421.     }
  422.     /**
  423.      * @Route({
  424.      *     "fr": "/inscription-newsletter",
  425.      *     "en": "/newsletter-register",
  426.      * }, name="front_subscribe_newsletter")
  427.      */
  428.     public function subscribeNewsletter(ManagerRegistry $managerRegistryRequest $requestAppService $appService)
  429.     {
  430.         $newsletter = new Newsletter();
  431.         $newsletterForm $this->createForm(NewsletterType::class, $newsletter);
  432.         $newsletterForm->handleRequest($request);
  433.         $em $managerRegistry->getManager();
  434.         if ($newsletterForm->isSubmitted() && $newsletterForm->isValid()) {
  435.             $appService->suscribeToList($newsletter->getEmail());
  436.             $em->persist($newsletter);
  437.             $em->flush();
  438.             $this->addFlash("success""flash.newsletter.success");
  439.             return $this->redirectToRoute('front_landing');
  440.         }
  441.         return $this->render('form/newsletterForm.html.twig', array(
  442.             'newsletterForm' => $newsletterForm->createView(),
  443.         ));
  444.     }
  445.     /**
  446.      * @Route({
  447.      *     "fr": "/admin/fsdhj78Hjkdfsb0920dfjdfhq87djhaoppsnv720",
  448.      *     "en": "/admin/fsdhj78Hjkdfsb0920dfjdfhq87djhaoppsnv720",
  449.      * }, name="front_clear_cache")
  450.      */
  451.     public function clearCache(Request $requestKernelInterface $kernel)
  452.     {
  453.         $application = new FrameworkApplication($kernel);
  454.         $application->setAutoExit(false);
  455.         $input = new ArrayInput([
  456.             'command' => 'cache:clear',
  457.         ]);
  458.         // You can use NullOutput() if you don't need the output
  459.         $output = new NullOutput();
  460.         $application->run($input$output);
  461.         $this->addFlash("success""Le cache a bien été nettoyé, les traductions sont à jour !");
  462.         return $this->redirectToRoute('front_landing');
  463.     }
  464. }