src/Controller/DefaultController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Symfony\Component\Routing\Annotation\Route;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\Form\FormError;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. use Symfony\Component\HttpFoundation\AcceptHeader;
  14. use App\Repository\UserRepository;
  15. class DefaultController extends AbstractController
  16. {
  17.     /**
  18.      * @Route("/", name="index")
  19.      */
  20.      
  21.     public function index(Request $request)
  22.     {
  23.         
  24.         $preferredLanguage $request->getPreferredLanguage();
  25.         $request->setLocale($request->getSession()->get('_locale'$this->defaultLocale));
  26.         return $this->redirectToRoute('homepage');
  27.     }
  28.     
  29.     
  30.     /**
  31.      * @Route({
  32.          "en": "/homepage", 
  33.          "fr": "/accueil",
  34.          "es": "/inicio"
  35.          
  36.      }, name="homepage")
  37.      */
  38.     public function indexAction(Request $requestEntityManagerInterface $emUserRepository $userRepository)
  39.     {
  40.         
  41.         //$listLastPlayer = $em->getRepository('App\Entity\User')->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 4, 0);
  42.         $listLastPlayer $userRepository->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 40);
  43.         $listLastClub $userRepository->findBy(['roles' => "ROLE_CLUB"], ['dateCreated' => 'DESC'], 40);
  44.         $listLastPro $userRepository->findBy(['roles' => "ROLE_PRO"], ['dateCreated' => 'DESC'], 40);
  45.         
  46.         return $this->render('Front/index.html.twig', [
  47.             'listLastPlayer' => $listLastPlayer,
  48.             'listLastClub' => $listLastClub,
  49.             'listLastPro' => $listLastPro
  50.         ]);
  51.     }
  52.     
  53.     /**
  54.      * @Route({
  55.          "en": "/", 
  56.          "fr": "/",
  57.          "es": "/"
  58.          
  59.      }, name="homepage2")
  60.      */
  61.     public function index2Action(Request $requestEntityManagerInterface $em)
  62.     {
  63.         
  64.         return $this->redirectToRoute('homepage');
  65.         /*return $this->render('Front/index.html.twig', [
  66.             
  67.         ]);*/
  68.     }
  69.     
  70.     
  71.     /**
  72.      * @Route("/SearchAroundPublic", name="searchAroundPublic")
  73.      * @Route("/searcharoundpublic")
  74.      */
  75.     public function searchAroundPublicAction(Request $requestEntityManagerInterface $em)
  76.     {
  77.         
  78.         $listLastPlayer $em->getRepository('App:User')->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 40);
  79.         $listLastClub $em->getRepository('App:User')->findBy(['roles' => "ROLE_CLUB"], ['dateCreated' => 'DESC'], 40);
  80.         $listLastPro $em->getRepository('App:User')->findBy(['roles' => "ROLE_PRO"], ['dateCreated' => 'DESC'], 40);
  81.         
  82.         return $this->render('Front/searchAround.html.twig', [
  83.             'location' => "",
  84.             'distance' => 10000,
  85.             'listLastPlayer' => $listLastPlayer,
  86.             'listLastClub' => $listLastClub,
  87.             'listLastPro' => $listLastPro
  88.         ]); 
  89.     }
  90.     
  91.     /**
  92.      * @Route("/SearchAroundPublicResult/{location}/{distance}", name="searchAroundPublicResult")
  93.      */
  94.     public function searchAroundPublicResultAction(Request $requestEntityManagerInterface $em\App\Service\FindCoordinates $findcoord$location$distance)
  95.     {
  96.             
  97.         if ($location && $distance) {
  98.         
  99.             
  100.             //$findcoord = $this->get('ildev_find_coordinates');
  101.             $coord $findcoord->findWithout($location);
  102.             
  103.             if($distance 100000)
  104.                 $distance 100000;
  105.             
  106.             $listPlayerAround =  $em->getRepository('App:User')->findPlayerByLocationAndDistance(null$coord['lat'], $coord['lng'], $distance);
  107.             $listClubAround =  $em->getRepository('App:User')->findClubByLocationAndDistance(null$coord['lat'], $coord['lng'], $distance);
  108.             $listProAround =  $em->getRepository('App:User')->findProByLocationAndDistance(null$coord['lat'], $coord['lng'], $distance);
  109.             $listLeagueAround =  $em->getRepository('App:User')->findLeagueByLocationAndDistance(null$coord['lat'], $coord['lng'], $distance);
  110.             
  111.         }
  112.         
  113.         
  114.         
  115.         $listLastPlayer $em->getRepository('App:User')->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 40);
  116.         $listLastClub $em->getRepository('App:User')->findBy(['roles' => "ROLE_CLUB"], ['dateCreated' => 'DESC'], 40);
  117.         $listLastPro $em->getRepository('App:User')->findBy(['roles' => "ROLE_PRO"], ['dateCreated' => 'DESC'], 40);
  118.     
  119.         
  120.         return $this->render('Common/resultSearch.html.twig', array(
  121.             'location' => $location,
  122.             'distance' => $distance,
  123.             'listPlayerAround' => $listPlayerAround,
  124.             'listClubAround' => $listClubAround,
  125.             'listProAround' => $listProAround,
  126.             'listLeagueAround' => $listLeagueAround,
  127.             'listLastPlayer' => $listLastPlayer,
  128.             'listLastClub' => $listLastClub,
  129.             'listLastPro' => $listLastPro
  130.         ));
  131.     }
  132.     
  133.     /**
  134.      * @Route("/DetailUser/{user}/{location}/{distance}", name="publicDetailUser")
  135.      * @Route("/DetailUser/{user}/{location}/{distance}/{name}", name="publicDetailUserWithNameAndLocation")
  136.      */
  137.     public function publicDetailUserAction(Request $requestUser $user$name=""$location=""$distance=10000)
  138.     {
  139.         
  140.         return $this->render('Common/detailUser.html.twig', array(
  141.             'location' => $location,
  142.             'distance' => $distance,
  143.             'user' => $user
  144.         ));
  145.     }
  146.     
  147.     /**
  148.      * @Route("/DetailUser2/{user}/{name}", name="publicDetailUserWithName")
  149.      * @Route("/detailUser2/{user}/{name}")
  150.      * @Route("/detailuser2/{user}/{name}")
  151.      * @Route("/DetailUser2/{user}/", name="publicDetailUserWithoutName")
  152.      */
  153.     public function publicDetailUserWithNameAction(Request $requestUser $user$name="")
  154.     {
  155.         
  156.         return $this->render('Common/detailUser.html.twig', array(
  157.             'user' => $user
  158.         ));
  159.     }
  160.     
  161.     
  162.     
  163.     /**
  164.      * @Route("/SearchAround", name="SearchAround")
  165.      */
  166.     public function searchAroundAction(Request $requestEntityManagerInterface $emMyAccountFormSearchAround $formSearchAround)
  167.     {
  168.         
  169.         $form $formSearchAround->getForm();
  170.         
  171.         $form->handleRequest($request);
  172.         
  173.         if ($form->isSubmitted() && $form->isValid()) {
  174.         
  175.             $location $form['location']->getData();
  176.             $distance $form['distance']->getData();
  177.             
  178.             if($location == null)
  179.                 $location " ";
  180.                 
  181.             
  182.             if($this->getUser())
  183.                 return $this->redirect($this->generateUrl('myAccountSearchAround', array('location' => $location ,'distance' => $distance)));
  184.             else
  185.                 return $this->redirect($this->generateUrl('searchAroundPublicResult', array('location' => $location ,'distance' => $distance)));
  186.             
  187.             
  188.         }else{
  189.             if($this->getUser())
  190.                 return $this->redirect($this->generateUrl('myAccountDashboard'));
  191.             else
  192.                 return $this->redirect($this->generateUrl('index'));
  193.         }
  194.     
  195.     }
  196.     
  197.     /**
  198.      * @Route("/User/confirm/{stringConfirm}", name="userEmailConfirm")
  199.      */
  200.     public function userEmailConfirmAction(Request $requestEntityManagerInterface $em$stringConfirm)
  201.     {    
  202.         $userTam $em->getRepository('App:User')->findOneByStringConfirmEmail($stringConfirm);
  203.         
  204.         if($userTam){
  205.             $userTam->setActive(true);
  206.             $em->persist($userTam);
  207.             $em->flush();
  208.             
  209.             return $this->redirect($this->generateUrl('frontMessage', array('message' => 'emailConfirm')));
  210.         }else{
  211.             return $this->redirect($this->generateUrl('frontMessage', array('message' => 'emailConfirmError')));
  212.         }
  213.            
  214.     }
  215.     
  216.     
  217.     /**
  218.      * @Route("/message/{message}", name="frontMessage")
  219.      */
  220.     public function frontMessageAction(Request $request$message)
  221.     {
  222.         
  223.         return $this->render('Front/frontMessage.html.twig', [
  224.              "message" => $message
  225.         ]);
  226.     }
  227.     
  228.     
  229.     /**
  230.      * @Route({
  231.          "en": "/About", 
  232.          "fr": "/A-propos",
  233.          "es": "/A-proposito"
  234.      }, name="about")
  235.      */
  236.     public function frontAboutAction(Request $request)
  237.     {
  238.         
  239.         return $this->render('Front/about.html.twig', [
  240.         ]);
  241.     }
  242.     
  243.     /**
  244.      * @Route({
  245.          "en": "/legal-mention", 
  246.          "fr": "/mentions-legales",
  247.          "es": "/menciones-legales"
  248.      }, name="legalMention")
  249.      */
  250.     public function frontLegalMentionAction(Request $request)
  251.     {
  252.         
  253.         return $this->render('Front/legalMention.html.twig', [
  254.         ]);
  255.     }
  256.     
  257.     /**
  258.      * @Route({
  259.          "en": "/PrivacyPolicy", 
  260.          "fr": "/PolitiqueDeConfidentialite",
  261.          "es": "/PolíticaDeConfidencialidad"
  262.      }, name="privacyPolicy")
  263.      */
  264.     public function frontPrivacyPolicyAction(Request $request)
  265.     {
  266.         
  267.         return $this->render('Front/privacyPolicy.html.twig', [
  268.         ]);
  269.     }
  270.     
  271.     
  272.     /**
  273.      * @Route({
  274.          "en": "/RecoveryPassword", 
  275.          "fr": "/RecoveryPassword",
  276.          "es": "/RecoveryPassword"
  277.      }, name="recoveryPassword")
  278.      */
  279.     public function recoveryPasswordAction(Request $requestEntityManagerInterface $em\Swift_Mailer $mailer)
  280.     {
  281.         
  282.         $output = array();
  283.         $output['result'] = -1;
  284.         $arrayFormOption['action'] = $this->generateUrl('recoveryPassword');
  285.         
  286.         $recovery = new Recovery();
  287.         
  288.         // Build the form
  289.         $form $this->createForm(RecoveryType::class, $recovery$arrayFormOption);
  290.         
  291.         $form->handleRequest($request);
  292.         
  293.         if ($form->isSubmitted() && $form->isValid()) {
  294.             $userTarget $em->getRepository('App:User')->findOneByEmail($recovery->getEmail());
  295.             
  296.             if($userTarget){
  297.                 $stringConfirm sha1($userTarget->getId()."&".$userTarget->getEmail()."&".$userTarget->getId());
  298.                 $userTarget->setStringConfirmRecovery($stringConfirm);
  299.                 
  300.                 $em->persist($userTarget);
  301.                 $em->flush();
  302.                 
  303.                 $privateKey file_get_contents('/home/tam/dkim/private.key');
  304.                 $domainName 'tennis-aroundme.com';
  305.                 $selector '20200130';
  306.                 $signer = new \Swift_Signers_DKIMSigner($privateKey$domainName$selector);
  307.                 
  308.                 
  309.                 $urlRecovery $request->getScheme() . '://' $request->getHttpHost().$this->generateUrl('recoveryPasswordCreateNew', array('stringConfirm' => $stringConfirm));
  310.                 
  311.                 $message = (new \Swift_Message('Vos identifiants'))
  312.                         ->setFrom(array('no-reply@tennis-aroundme.com' => 'Tennis-Aroundme'));
  313.                 $message->setTo($form["email"]->getData());
  314.                 $message->setBody($this->renderView('Front/Mails/recoveryPassword.html.twig', array('urlRecovery' => $urlRecovery)), 'text/html'//'text/html'
  315.                         ->addPart($this->renderView('Front/Mails/recoveryPasswordPlain.html.twig', array('urlRecovery' => $urlRecovery)), 'text/plain'); //'text/plain'
  316.                         
  317.                 $message->attachSigner($signer);
  318.                 $mailer->send($message);
  319.                 
  320.             }
  321.             
  322.             $output['result'] = 0//FOR HIDE MODAL
  323.             //$output['alertFlash']['type']='success';
  324.             //$output['alertFlash']['message']='Mot de passe envoyé avec succès !';
  325.             
  326.             return $this->redirect($this->generateUrl('frontMessage', array('message' => 'recoveryPassword')));
  327.             
  328.         }else{
  329.             $html $this->renderView('Front/Modals/recovery.html.twig', array("form" => $form->createView()));
  330.             $output['result'] = 1//FOR DISPLAY MODAL
  331.             $output['formHtml'] = $html;
  332.         }
  333.         
  334.         
  335.         $response = new JsonResponse($output);
  336.         $response->headers->set('Content-Type''application/json; charset=utf-8');
  337.         
  338.         
  339.         return $response;
  340.         
  341.     }
  342.     
  343.     
  344.     /**
  345.      * @Route("/RecoveryPassword/createNewPassword/{stringConfirm}", name="recoveryPasswordCreateNew")
  346.      */
  347.     public function recoveryPasswordCreateNewAction(Request $requestEntityManagerInterface $emUserPasswordEncoderInterface $passwordEncoder\Swift_Mailer $mailer$stringConfirm)
  348.     {    
  349.         $userResetPasswd $em->getRepository('App:User')->findOneByStringConfirmRecovery($stringConfirm);
  350.         
  351.         
  352.         if($userResetPasswd){
  353.             $arrayFormOption['action'] = $this->generateUrl('recoveryPasswordCreateNew', array('stringConfirm' => $stringConfirm));
  354.             // Build the form
  355.             $form $this->createForm(UserChangePasswordType::class, null$arrayFormOption);
  356.             
  357.             $form->handleRequest($request);
  358.             
  359.             if ($form->isSubmitted() && $form->isValid()) {
  360.                 $password $form->get('password')->getData();
  361.                 /*$passwordEncrypt = $this->get('security.password_encoder')
  362.                     ->encodePassword($userResetPasswd, $password);*/
  363.                 $userResetPasswd->setPassword($passwordEncoder->encodePassword($userResetPasswd$password));
  364.                 $userResetPasswd->setStringConfirmRecovery('');
  365.                 
  366.                 
  367.                 $em->persist($userResetPasswd);
  368.                 $em->flush();
  369.                 
  370.                 return $this->redirect($this->generateUrl('frontMessage', array('message' => 'changePasswordSuccess')));
  371.             }
  372.             
  373.             return $this->render('Front/createNewPassword.html.twig', array(
  374.                 'form' => $form->createView(),
  375.             ));
  376.             
  377.             
  378.         }else{
  379.             return $this->redirect($this->generateUrl('frontMessage', array('message' => 'recoveryPasswordFailed')));
  380.         }
  381.     
  382.     }
  383.     
  384.     
  385.     
  386.     /**
  387.      * @Route("/Contact", name="contact")
  388.      * @Route("/contact")
  389.      * @Route("/contacto.html")
  390.      */
  391.     public function contactAction(Request $requestEntityManagerInterface $em\Swift_Mailer $mailer)
  392.     {
  393.         $contact = new Contact();
  394.         
  395.         // Build the form
  396.         $form $this->createForm(ContactType::class, $contact);
  397.         
  398.         $form->handleRequest($request);
  399.         
  400.         if ($form->isSubmitted() && $form->isValid() && $this->captchaverify($request->get('g-recaptcha-response'))) {
  401.             
  402.                 
  403.             $privateKey file_get_contents('/home/tam/dkim/private.key');
  404.             $domainName 'tennis-aroundme.com';
  405.             $selector '20200130';
  406.             $signer = new \Swift_Signers_DKIMSigner($privateKey$domainName$selector);
  407.             
  408.             $message = (new \Swift_Message("Demande de renseignement TAM"))
  409.                         ->setFrom("no-reply@tennis-aroundme.com")
  410.                         ->setTo('contact@tennis-aroundme.com')
  411.                         ->setBody($this->renderView('Front/Mails/contact.html.twig', array('contact' => $contact)), 'text/html'//'text/html'
  412.                         ->addPart($this->renderView('Front/Mails/contactPlain.html.twig', array('contact' => $contact)), 'text/plain'); //'text/plain'
  413.                         
  414.             $message->attachSigner($signer);
  415.             $mailer->send($message);
  416.             
  417.             $request->getSession()->getFlashBag()->add('success''Message envoyé avec succès !');
  418.             
  419.             return $this->redirect($this->generateUrl('homepage'));
  420.             
  421.         }
  422.         
  423.         # check if captcha response isn't get throw a message
  424.         if($form->isSubmitted() &&  $form->isValid() && !$this->captchaverify($request->get('g-recaptcha-response'))){
  425.                  
  426.             $this->addFlash(
  427.                 'error',
  428.                 'Captcha Require'
  429.                 );             
  430.         }
  431.         
  432.         return $this->render('Front/contact.html.twig', array(
  433.             "form" => $form->createView(),
  434.         ));
  435.     
  436.     }
  437.     
  438.     # get success response from recaptcha and return it to controller
  439.     function captchaverify($recaptcha){
  440.             $url "https://www.google.com/recaptcha/api/siteverify";
  441.             $ch curl_init();
  442.             curl_setopt($chCURLOPT_URL$url);
  443.             curl_setopt($chCURLOPT_HEADER0);
  444.             curl_setopt($chCURLOPT_RETURNTRANSFERTRUE); 
  445.             curl_setopt($chCURLOPT_POSTtrue);
  446.             curl_setopt($chCURLOPT_POSTFIELDS, array(
  447.                 "secret"=>"6LcfUNQUAAAAADnbKm_L4Srgprwu8-0TfbFE3Smg","response"=>$recaptcha));
  448.             $response curl_exec($ch);
  449.             curl_close($ch);
  450.             $data json_decode($response);     
  451.         
  452.         return $data->success;        
  453.     }
  454.     
  455.     
  456. }