src/Controller/WebsiteController.php line 145

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\General;
  4. use App\Entity\Service;
  5. use App\Repository\ActualiteRepository;
  6. use App\Repository\ArticleRepository;
  7. use App\Repository\BlogRepository;
  8. use App\Repository\CategorieRealisationRepository;
  9. use App\Repository\ClientRepository;
  10. use App\Repository\CookiesRepository;
  11. use App\Repository\EmplacementRepository;
  12. use App\Repository\GeneralRepository;
  13. use App\Repository\ImagesAccueilRepository;
  14. use App\Repository\MentionsLegalesRepository;
  15. use App\Repository\NosGarantiesRepository;
  16. use App\Repository\PolitiqueDeConfidentialiteRepository;
  17. use App\Repository\RealisationRepository;
  18. use App\Repository\ServiceRepository;
  19. use App\Repository\TarifRepository;
  20. use App\Repository\TemoignageRepository;
  21. use Exception;
  22. use Symfony\Bundle\FrameworkBundle\Console\Application;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. use Symfony\Component\Console\Input\ArrayInput;
  25. use Symfony\Component\Console\Output\NullOutput;
  26. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  27. use Symfony\Component\HttpFoundation\File\UploadedFile;
  28. use Symfony\Component\HttpFoundation\JsonResponse;
  29. use Symfony\Component\HttpFoundation\RedirectResponse;
  30. use Symfony\Component\HttpFoundation\Request;
  31. use Symfony\Component\HttpFoundation\Response;
  32. use Symfony\Component\HttpKernel\KernelInterface;
  33. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  34. use Symfony\Component\Mailer\Mailer;
  35. use Symfony\Component\Mailer\MailerInterface;
  36. use Symfony\Component\Mime\Email;
  37. use Symfony\Component\Routing\Annotation\Route;
  38. use Symfony\Component\String\Slugger\AsciiSlugger;
  39. use Symfony\Component\Yaml\Yaml;
  40. class WebsiteController extends AbstractController
  41. {
  42.     public const RELATIVE_PATH_TO_PROJECT_FOLDER '../';
  43.     /**
  44.      * @Route("/", name="home")
  45.      */
  46.     public function home(NosGarantiesRepository $nosGarantiesRepositoryGeneralRepository $generalRepository,RealisationRepository $realisationRepositoryArticleRepository $articleRepositoryActualiteRepository $actualiteRepository,ClientRepository $clientRepository,TemoignageRepository $temoignageRepository,ImagesAccueilRepository $imageAccueilRepository,ServiceRepository $serviceRepository): Response
  47.     {
  48.         $generalItem $generalRepository->find(1);
  49.         $realisations $realisationRepository->findAll();
  50.         $allActualites $actualiteRepository->findAll();
  51.         $allArticles $articleRepository->findAll();
  52.         $allClient $clientRepository->findAll();
  53.         $allTemoignages $temoignageRepository->findAll();
  54.         $allEngagements $nosGarantiesRepository->findAll();
  55.         $imagesAccueil $imageAccueilRepository->find(1);
  56.         $services $serviceRepository->findBy([],['id' => 'DESC'],3);
  57.         return $this->render('pages/home.html.twig', [
  58.             "general" => $generalItem,
  59.             'realisations' => array_reverse($realisations),
  60.             'actualites' => array_reverse($allActualites),
  61.             'clients' => array_reverse($allClient),
  62.             'temoignages' => array_reverse($allTemoignages),
  63.             'articles' => array_reverse($allArticles),
  64.             'engagements' => $allEngagements,
  65.             "imageAccueil" => $imagesAccueil,
  66.             'services' => $services
  67.         ]);
  68.     }
  69.     /**
  70.      * @Route("/realisations", name="realisations")
  71.      */
  72.     public function realisation(RealisationRepository $realisationRepository,ClientRepository $clientRepository,CategorieRealisationRepository $categorieRealisationRepository): Response
  73.     {
  74.         $realisations $realisationRepository->findAll();
  75.         $allClient $clientRepository->findAll();
  76.         $categoriesRealisations $categorieRealisationRepository->findAll();
  77.         return $this->render('pages/realisations.html.twig', [
  78.             'realisations' => $realisations,
  79.             'clients' => $allClient,
  80.             'categories' => $categoriesRealisations
  81.         ]);
  82.     }
  83.     /**
  84.      * @Route("/blogs", name="blogs")
  85.      */
  86.     public function blogs(BlogRepository $blogRepositoryGeneralRepository $generalRepository): Response
  87.     {
  88.         $allBlogs $blogRepository->findAll();
  89.         $general $generalRepository->find(1);
  90.         $allBlogs array_reverse($allBlogs);
  91.         return $this->render('pages/blogs.html.twig', [
  92.             'blogs' => $allBlogs,
  93.             'general' => $general
  94.         ]);
  95.     }
  96.     /**
  97.      * @Route("/actualites", name="actualites")
  98.      */
  99.     public function articles(ArticleRepository $articleRepositoryGeneralRepository $generalRepository): Response
  100.     {
  101.         $allArticles $articleRepository->findAll();
  102.         $general $generalRepository->find(1);
  103.         $allArticles array_reverse($allArticles);
  104.         return $this->render('pages/articles.html.twig', [
  105.             'articles' => $allArticles,
  106.             'general' => $general
  107.         ]);
  108.     }
  109.     /**
  110.      * @Route("/social", name="social")
  111.      */
  112.     public function actualites(ActualiteRepository $actualiteRepository): Response
  113.     {
  114.         $allActualites $actualiteRepository->findAll();
  115.         return $this->render('pages/social.html.twig', [
  116.             'actualites' => array_reverse($allActualites),
  117.         ]);
  118.     }
  119.     /**
  120.      * @Route("/cookies", name="cookies")
  121.      */
  122.     public function cookies(CookiesRepository $cookieRepo): Response
  123.     {
  124.         $cookie $cookieRepo->find(1);
  125.         return $this->render('pages/cookies.html.twig', [
  126.             'cookies' => $cookie,
  127.         ]);
  128.     }
  129.     /**
  130.      * @Route("/mentions-legales", name="mentionsLegales")
  131.      */
  132.     public function mentionsLegales(MentionsLegalesRepository $mentionsLegalesRepository): Response
  133.     {
  134.         $mentionsLegales $mentionsLegalesRepository->find(1);
  135.         return $this->render('pages/mentionsLegales.html.twig', [
  136.             'mentionsLegales' => $mentionsLegales,
  137.         ]);
  138.     }
  139.     /**
  140.      * @Route("/politique-de-confidentialite", name="politiqueDeConfidentialite")
  141.      */
  142.     public function politiqueDeConfidentialite(PolitiqueDeConfidentialiteRepository $PolitiqueDeConfidentialiteRepository): Response
  143.     {
  144.         $pdc $PolitiqueDeConfidentialiteRepository->find(1);
  145.         return $this->render('pages/pdc.html.twig', [
  146.             'pdc' => $pdc,
  147.         ]);
  148.     }
  149.     /**
  150.      * @Route("/blog/{id}", name="blog", methods={"GET"})
  151.      */
  152.     public function blog(BlogRepository $blogRepositoryGeneralRepository $generalRepository,int $id): Response
  153.     {
  154.         $blog $blogRepository->find($id);
  155.         $general $generalRepository->find(1);
  156.         $nextBlog $blogRepository->find($blog->getId() +1);
  157.         $previousBlog $blogRepository->find($blog->getId() -1);
  158.         $otherBlogs $blogRepository->findBy([],[],3);
  159.         return $this->render('pages/blog.html.twig', [
  160.             'blog' => $blog,
  161.             'general' => $general,
  162.             'nextBlog' => $nextBlog,
  163.             'previousBlog' => $previousBlog,
  164.             'otherBlogs' => $otherBlogs
  165.         ]);
  166.     }
  167.     /**
  168.      * @Route("/actualite/{id}", name="actualite", methods={"GET"})
  169.      */
  170.     public function actualite(ArticleRepository $articleRepositoryGeneralRepository $generalRepository,int $id): Response
  171.     {
  172.         $article $articleRepository->find($id);
  173.         $general $generalRepository->find(1);
  174.         $nextArticle $articleRepository->find($article->getId() +1);
  175.         $previousArticle $articleRepository->find($article->getId() -1);
  176.         $otherArticles $articleRepository->findBy([],[],3);
  177.         return $this->render('pages/article.html.twig', [
  178.             'article' => $article,
  179.             'general' => $general,
  180.             'nextArticle' => $nextArticle,
  181.             'previousArticle' => $previousArticle,
  182.             'otherArticles' => $otherArticles
  183.         ]);
  184.     }
  185.     /**
  186.      * @Route("/contact", name="contact")
  187.      */
  188.     public function contact(EmplacementRepository $emplacementRepository,Request $request,MailerInterface $mailer,GeneralRepository $repository): Response
  189.     {
  190.         if($request->isMethod('POST')){
  191.             if($request->files->get('widget-contact-form-file')){
  192.                 /** @var UploadedFile $file */
  193.                 $file $request->files->get('widget-contact-form-file');
  194.                 $originalFilename pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME);
  195.                 $slugger = new AsciiSlugger();
  196.                 // this is needed to safely include the file name as part of the URL
  197.                 $safeFilename $slugger->slug($originalFilename);
  198.                 $newFilename $safeFilename.'-'.uniqid().'.'.$file->guessExtension();
  199.                 try {
  200.                     $file->move(
  201.                         'images/upload',
  202.                         $newFilename
  203.                     );
  204.                 } catch (FileException $e) {
  205.                     return new JsonResponse(['response' => 'danger','message' => 'Oups, une erreur est survenu pendant l\'envoi de votre fichier, tentez de nous contacter directement par mail.']);
  206.                 }
  207.             };
  208.             $name $request->get('widget-contact-form-name');
  209.             $mail $request->get('widget-contact-form-email');
  210.             $phone $request->get('widget-contact-form-phone');
  211.             $message $request->get('widget-contact-form-message');
  212.             $entrepriseName $repository->find(1)->getNomEntreprise();
  213.             $email = (new Email())
  214.                 ->from('do-not-reply@spidertec.fr')
  215.                 ->subject("[$entrepriseName] Nouveau message Client")
  216.                 ->html($this->renderView('email/contact.html.twig',[
  217.                     'name' => $name,
  218.                     'email' => $mail,
  219.                     'message' => $message,
  220.                     'phone' => $phone,
  221.                     'entrepriseName' => $entrepriseName
  222.                 ]))
  223.                 ->to($repository->find(1)->getAdresseEmail());
  224.             if(isset($file)){
  225.                 $email->attachFromPath("images/upload/$newFilename");
  226.             }
  227.             $email->ensureValidity();
  228.             $mailer->send($email);
  229.             return new JsonResponse(['response' => 'success',"message" => 'Message envoyé']);
  230.         }
  231.         else{
  232.             $text $request->get('text');
  233.             $allEmplacements $emplacementRepository->findAll();
  234.             return $this->render('pages/contact.html.twig', [
  235.                 'emplacements' => array_reverse($allEmplacements),
  236.                 'text' => $text
  237.             ]);
  238.         }
  239.     }
  240.     /**
  241.      * @Route("/tarifs" , name="tarifs")
  242.      */
  243.     public function tarifs(TarifRepository $tarifRepository){
  244.         $allTarifs $tarifRepository->findBy([],['id' => 'DESC']);
  245.         return $this->render('pages/tarifs.html.twig',[
  246.             'tarifs' => $allTarifs
  247.         ]);
  248.     }
  249.     /**
  250.      * @Route("/services", name="services")
  251.      */
  252.     public function services(ServiceRepository $serviceRepository): Response
  253.     {
  254.         $allServices $serviceRepository->findBy([],['id' => 'DESC']);
  255.         return $this->render('pages/services.html.twig', [
  256.             'services' => $allServices,
  257.         ]);
  258.     }
  259.     /**
  260.      * @Route("/service/{id}", name="service", methods={"GET"})
  261.      */
  262.     public function service(ServiceRepository $serviceRepositoryGeneralRepository $generalRepository,int $id): Response
  263.     {
  264.         $service $serviceRepository->find($id);
  265.         $general $generalRepository->find(1);
  266.         $nextService $serviceRepository->find($service->getId() +1);
  267.         $previousService $serviceRepository->find($service->getId() -1);
  268.         $otherServices $serviceRepository->findBy([],[],3);
  269.         return $this->render('pages/service.html.twig', [
  270.             'service' => $service,
  271.             'general' => $general,
  272.             'nextService' => $nextService,
  273.             'previousService' => $previousService,
  274.             'otherServices' => $otherServices
  275.         ]);
  276.     }
  277.     /**
  278.      * @Route("preview/service/{id}", name="preview-service")
  279.      */
  280.     public function previewService(Service $service,ServiceRepository $serviceRepositoryGeneralRepository $generalRepository): Response
  281.     {
  282.         $general $generalRepository->find(1);
  283.         $nextService $serviceRepository->find($service->getId() +1);
  284.         $previousService $serviceRepository->find($service->getId() -1);
  285.         $otherServices $serviceRepository->findBy([],[],3);
  286.         return $this->render('composantsPages/homeComponents/preview/service.html.twig',[
  287.             'service' => $service,
  288.             'general' => $general,
  289.             'nextService' => $nextService,
  290.             'previousService' => $previousService,
  291.             'otherServices' => $otherServices]);
  292.     }
  293.     /**
  294.      * @Route("qui-sommes-nous", name="page-qui-sommes-nous")
  295.      */
  296.     public function pageQuiSommesNous(){
  297.         return $this->render('pages/quiSommesNous.html.twig');
  298.     }
  299.     /**
  300.      * @Route("/admin/text-edit",name="text-edit")
  301.      * @Route("/admin/text-edit/{slug}",name="text-edit-single")
  302.      * @return Response
  303.      */
  304.     public function editText(string $slug nullRequest $request)
  305.     {
  306.         $data Yaml::parseFile('../translations/messages.fr.yaml');
  307.         if ($request->isMethod('POST')) {
  308.             $trueData = [];
  309.             $datas $request->request->all();
  310.             foreach ($datas as $singleLineKey => $value) {
  311.                 $accessors explode('&'$singleLineKey);
  312.                 $this->setValueByArrayKeys($accessors$trueData$value);
  313.             }
  314.             $yaml Yaml::dump($trueData);
  315.             file_put_contents('../translations/messages.fr.yaml'$yaml);
  316.             $data Yaml::parseFile('../translations/messages.fr.yaml');
  317.             if (!is_null($slug)) {
  318.                 return $this->redirect($request->headers->get('referer'));
  319.             }
  320.         }
  321.         return $this->render('pages/text-edit_ajax.html.twig', [
  322.             'data' => $data,
  323.             'search' => $slug
  324.         ]);
  325.     }
  326.     private function setValueByArrayKeys($array_keys, &$multi$value)
  327.     {
  328.         $m = &$multi;
  329.         foreach ($array_keys as $k) {
  330.             if (!isset($m[$k])) {
  331.                 $m[$k] = null;
  332.             }
  333.             $m = &$m[$k];
  334.         }
  335.         $m $value;
  336.     }
  337.     /**
  338.      * @Route("/cc",name="cc")
  339.      *
  340.      */
  341.     public function cc(Request $request,KernelInterface $kernel)
  342.     {
  343.         General::refreshColor();
  344.         if($this->getUser()){
  345.             if(in_array('ROLE_ADMIN',$this->getUser()->getRoles())){
  346.                 $application = new Application($kernel);
  347.                 $application->setAutoExit(false);
  348.                 $input = new ArrayInput(['command' => 'cache:clear']);
  349.                 $output = new NullOutput();
  350.                 $application->run($input$output);
  351.                 return $this->redirect($request->headers->get('referer'));
  352.             }
  353.         }
  354.         throw $this->createAccessDeniedException();
  355.     }
  356.     /**
  357.      * @Route("/status.json",name="website_status")
  358.      */
  359.     public function getStatus(Request $request)
  360.     {
  361.         return new JsonResponse([
  362.             'server_name' => $_SERVER['SERVER_NAME'] ?? 'empty',
  363.             'server_ip' => $_SERVER['SERVER_ADDR'] ?? 'empty',
  364.             'dns_records' => dns_get_record($_SERVER['SERVER_NAME'])
  365.         ],200,[
  366.             'Access-Control-Allow-Origin' => '*'
  367.         ]);
  368.     }
  369.     /**
  370.      * @Route("/mail-test", name="mail_test")
  371.      * @param Request $request
  372.      * @param GeneralRepository $repository
  373.      * @param MailerInterface $mailer
  374.      * @return Response
  375.      */
  376.     public function testEmail(Request $request,GeneralRepository $repository,MailerInterface $mailer) : Response
  377.     {
  378.         $email = (new Email())
  379.             ->from($repository->find(1)->getAdresseEmail())
  380.             ->subject("[".$_SERVER['SERVER_NAME']."] Message test de mail")
  381.             ->html("Mail de test")
  382.             ->to('test-spidertec@yopmail.com','cadarsir@gmail.com');
  383.         try{
  384.             $mailer->send($email);
  385.             $this->addFlash('success','Envoi d\'email réussi');
  386.         } catch (TransportExceptionInterface $e) {
  387.             $this->addFlash('danger','Envoi d\'email échoué');
  388.         }
  389.         return $this->redirect($request->headers->get('referer'));
  390.     }
  391.     /**
  392.      * @Route("/admin/urlset", name="url_api_add")
  393.      */
  394.     public function addSet(Request $request)
  395.     {
  396.         $this->checkClient($request->getClientIp());
  397.         $sites $this->getUrlSetContentFile();
  398.         $filePath self::RELATIVE_PATH_TO_PROJECT_FOLDER.$this->getParameter('sites_file');
  399.         $preprod $request->request->get('preprod');
  400.         $preprod_mdp $request->request->get('preprod_mdp');
  401.         $prod $request->request->get('prod');
  402.         $prod_mdp $request->request->get('prod_mdp');
  403.         $name ucfirst(str_replace('-',' ',$request->request->get('name','')));
  404.         if($request->getMethod() == 'POST'){
  405.             $toCreate = [];
  406.             if(!empty($name))
  407.             {
  408.                 $foundSite $this->findSite($sites,$name);
  409.                 if(!is_null($foundSite)){
  410.                     return new Response("Site déjà existant.",401);
  411.                 }
  412.                 $toCreate['name'] = $name;
  413.                 if(!is_null($preprod)){
  414.                     $toCreate['preprod'] = $preprod;
  415.                     if(!is_null($preprod_mdp)){
  416.                         $toCreate['preprod_mdp'] = $preprod_mdp;
  417.                     }
  418.                 }
  419.                 if(!is_null($prod) && !is_null($prod_mdp)){
  420.                     $toCreate['prod'] = $prod;
  421.                     $toCreate['prod_mdp'] = $prod_mdp;
  422.                 }
  423.                 $sites[] = $toCreate;
  424.                 file_put_contents($filePath,json_encode($sites));
  425.                 return new JsonResponse($this->getUrlSetContentFile());
  426.             }
  427.             else{
  428.                 return new Response("Paramètres incorrects.",400);
  429.             }
  430.         }
  431.         else{
  432.             return new Response("//todo.",401);
  433.         }
  434.     }
  435.     /**
  436.      * @Route("/admin/urlset/get",name="url_api_get")
  437.      * @param Request $request
  438.      * @return JsonResponse
  439.      */
  440.     public function getUrlsSetContent(Request $request)
  441.     {
  442.         try{
  443.             $this->denyAccessUnlessGranted('ROLE_ADMIN');
  444.             $content $this->getUrlSetContentFile();
  445.             return new JsonResponse($content);
  446.         }catch(Exception $e){
  447.             return new JsonResponse([]);
  448.         }
  449.     }
  450.     private function getUrlSetContentFile()
  451.     {
  452.         $sitesJsonEmplacement $this->getParameter('sites_file');
  453.         $filePath self::RELATIVE_PATH_TO_PROJECT_FOLDER.$sitesJsonEmplacement;
  454.         return json_decode(file_get_contents($filePath),true);
  455.     }
  456.     private function checkClient($ip){
  457.         $authorizedIps $this->getParameter('api_urlset_authorized_ips');
  458.         if(!in_array($ip,$authorizedIps)){
  459.             throw new Exception("Client non autorisé");
  460.         }
  461.     }
  462.     private function findSite($sites,$name) : ?array
  463.     {
  464.         return array_values(array_filter($sites,function(array $site) use ($name){return $site['name'] == $name;}))[0] ?? null;
  465.     }
  466. }