<?php
namespace App\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Form\FormError;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\AcceptHeader;
use App\Repository\UserRepository;
class DefaultController extends AbstractController
{
/**
* @Route("/", name="index")
*/
public function index(Request $request)
{
$preferredLanguage = $request->getPreferredLanguage();
$request->setLocale($request->getSession()->get('_locale', $this->defaultLocale));
return $this->redirectToRoute('homepage');
}
/**
* @Route({
"en": "/homepage",
"fr": "/accueil",
"es": "/inicio"
}, name="homepage")
*/
public function indexAction(Request $request, EntityManagerInterface $em, UserRepository $userRepository)
{
//$listLastPlayer = $em->getRepository('App\Entity\User')->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 4, 0);
$listLastPlayer = $userRepository->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 4, 0);
$listLastClub = $userRepository->findBy(['roles' => "ROLE_CLUB"], ['dateCreated' => 'DESC'], 4, 0);
$listLastPro = $userRepository->findBy(['roles' => "ROLE_PRO"], ['dateCreated' => 'DESC'], 4, 0);
return $this->render('Front/index.html.twig', [
'listLastPlayer' => $listLastPlayer,
'listLastClub' => $listLastClub,
'listLastPro' => $listLastPro
]);
}
/**
* @Route({
"en": "/",
"fr": "/",
"es": "/"
}, name="homepage2")
*/
public function index2Action(Request $request, EntityManagerInterface $em)
{
return $this->redirectToRoute('homepage');
/*return $this->render('Front/index.html.twig', [
]);*/
}
/**
* @Route("/SearchAroundPublic", name="searchAroundPublic")
* @Route("/searcharoundpublic")
*/
public function searchAroundPublicAction(Request $request, EntityManagerInterface $em)
{
$listLastPlayer = $em->getRepository('App:User')->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 4, 0);
$listLastClub = $em->getRepository('App:User')->findBy(['roles' => "ROLE_CLUB"], ['dateCreated' => 'DESC'], 4, 0);
$listLastPro = $em->getRepository('App:User')->findBy(['roles' => "ROLE_PRO"], ['dateCreated' => 'DESC'], 4, 0);
return $this->render('Front/searchAround.html.twig', [
'location' => "",
'distance' => 10000,
'listLastPlayer' => $listLastPlayer,
'listLastClub' => $listLastClub,
'listLastPro' => $listLastPro
]);
}
/**
* @Route("/SearchAroundPublicResult/{location}/{distance}", name="searchAroundPublicResult")
*/
public function searchAroundPublicResultAction(Request $request, EntityManagerInterface $em, \App\Service\FindCoordinates $findcoord, $location, $distance)
{
if ($location && $distance) {
//$findcoord = $this->get('ildev_find_coordinates');
$coord = $findcoord->findWithout($location);
if($distance > 100000)
$distance = 100000;
$listPlayerAround = $em->getRepository('App:User')->findPlayerByLocationAndDistance(null, $coord['lat'], $coord['lng'], $distance);
$listClubAround = $em->getRepository('App:User')->findClubByLocationAndDistance(null, $coord['lat'], $coord['lng'], $distance);
$listProAround = $em->getRepository('App:User')->findProByLocationAndDistance(null, $coord['lat'], $coord['lng'], $distance);
$listLeagueAround = $em->getRepository('App:User')->findLeagueByLocationAndDistance(null, $coord['lat'], $coord['lng'], $distance);
}
$listLastPlayer = $em->getRepository('App:User')->findBy(['roles' => "ROLE_PLAYER"], ['dateCreated' => 'DESC'], 4, 0);
$listLastClub = $em->getRepository('App:User')->findBy(['roles' => "ROLE_CLUB"], ['dateCreated' => 'DESC'], 4, 0);
$listLastPro = $em->getRepository('App:User')->findBy(['roles' => "ROLE_PRO"], ['dateCreated' => 'DESC'], 4, 0);
return $this->render('Common/resultSearch.html.twig', array(
'location' => $location,
'distance' => $distance,
'listPlayerAround' => $listPlayerAround,
'listClubAround' => $listClubAround,
'listProAround' => $listProAround,
'listLeagueAround' => $listLeagueAround,
'listLastPlayer' => $listLastPlayer,
'listLastClub' => $listLastClub,
'listLastPro' => $listLastPro
));
}
/**
* @Route("/DetailUser/{user}/{location}/{distance}", name="publicDetailUser")
* @Route("/DetailUser/{user}/{location}/{distance}/{name}", name="publicDetailUserWithNameAndLocation")
*/
public function publicDetailUserAction(Request $request, User $user, $name="", $location="", $distance=10000)
{
return $this->render('Common/detailUser.html.twig', array(
'location' => $location,
'distance' => $distance,
'user' => $user
));
}
/**
* @Route("/DetailUser2/{user}/{name}", name="publicDetailUserWithName")
* @Route("/detailUser2/{user}/{name}")
* @Route("/detailuser2/{user}/{name}")
* @Route("/DetailUser2/{user}/", name="publicDetailUserWithoutName")
*/
public function publicDetailUserWithNameAction(Request $request, User $user, $name="")
{
return $this->render('Common/detailUser.html.twig', array(
'user' => $user
));
}
/**
* @Route("/SearchAround", name="SearchAround")
*/
public function searchAroundAction(Request $request, EntityManagerInterface $em, MyAccountFormSearchAround $formSearchAround)
{
$form = $formSearchAround->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$location = $form['location']->getData();
$distance = $form['distance']->getData();
if($location == null)
$location = " ";
if($this->getUser())
return $this->redirect($this->generateUrl('myAccountSearchAround', array('location' => $location ,'distance' => $distance)));
else
return $this->redirect($this->generateUrl('searchAroundPublicResult', array('location' => $location ,'distance' => $distance)));
}else{
if($this->getUser())
return $this->redirect($this->generateUrl('myAccountDashboard'));
else
return $this->redirect($this->generateUrl('index'));
}
}
/**
* @Route("/User/confirm/{stringConfirm}", name="userEmailConfirm")
*/
public function userEmailConfirmAction(Request $request, EntityManagerInterface $em, $stringConfirm)
{
$userTam = $em->getRepository('App:User')->findOneByStringConfirmEmail($stringConfirm);
if($userTam){
$userTam->setActive(true);
$em->persist($userTam);
$em->flush();
return $this->redirect($this->generateUrl('frontMessage', array('message' => 'emailConfirm')));
}else{
return $this->redirect($this->generateUrl('frontMessage', array('message' => 'emailConfirmError')));
}
}
/**
* @Route("/message/{message}", name="frontMessage")
*/
public function frontMessageAction(Request $request, $message)
{
return $this->render('Front/frontMessage.html.twig', [
"message" => $message
]);
}
/**
* @Route({
"en": "/About",
"fr": "/A-propos",
"es": "/A-proposito"
}, name="about")
*/
public function frontAboutAction(Request $request)
{
return $this->render('Front/about.html.twig', [
]);
}
/**
* @Route({
"en": "/legal-mention",
"fr": "/mentions-legales",
"es": "/menciones-legales"
}, name="legalMention")
*/
public function frontLegalMentionAction(Request $request)
{
return $this->render('Front/legalMention.html.twig', [
]);
}
/**
* @Route({
"en": "/PrivacyPolicy",
"fr": "/PolitiqueDeConfidentialite",
"es": "/PolíticaDeConfidencialidad"
}, name="privacyPolicy")
*/
public function frontPrivacyPolicyAction(Request $request)
{
return $this->render('Front/privacyPolicy.html.twig', [
]);
}
/**
* @Route({
"en": "/RecoveryPassword",
"fr": "/RecoveryPassword",
"es": "/RecoveryPassword"
}, name="recoveryPassword")
*/
public function recoveryPasswordAction(Request $request, EntityManagerInterface $em, \Swift_Mailer $mailer)
{
$output = array();
$output['result'] = -1;
$arrayFormOption['action'] = $this->generateUrl('recoveryPassword');
$recovery = new Recovery();
// Build the form
$form = $this->createForm(RecoveryType::class, $recovery, $arrayFormOption);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$userTarget = $em->getRepository('App:User')->findOneByEmail($recovery->getEmail());
if($userTarget){
$stringConfirm = sha1($userTarget->getId()."&".$userTarget->getEmail()."&".$userTarget->getId());
$userTarget->setStringConfirmRecovery($stringConfirm);
$em->persist($userTarget);
$em->flush();
$privateKey = file_get_contents('/home/tam/dkim/private.key');
$domainName = 'tennis-aroundme.com';
$selector = '20200130';
$signer = new \Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
$urlRecovery = $request->getScheme() . '://' . $request->getHttpHost().$this->generateUrl('recoveryPasswordCreateNew', array('stringConfirm' => $stringConfirm));
$message = (new \Swift_Message('Vos identifiants'))
->setFrom(array('no-reply@tennis-aroundme.com' => 'Tennis-Aroundme'));
$message->setTo($form["email"]->getData());
$message->setBody($this->renderView('Front/Mails/recoveryPassword.html.twig', array('urlRecovery' => $urlRecovery)), 'text/html') //'text/html'
->addPart($this->renderView('Front/Mails/recoveryPasswordPlain.html.twig', array('urlRecovery' => $urlRecovery)), 'text/plain'); //'text/plain'
$message->attachSigner($signer);
$mailer->send($message);
}
$output['result'] = 0; //FOR HIDE MODAL
//$output['alertFlash']['type']='success';
//$output['alertFlash']['message']='Mot de passe envoyé avec succès !';
return $this->redirect($this->generateUrl('frontMessage', array('message' => 'recoveryPassword')));
}else{
$html = $this->renderView('Front/Modals/recovery.html.twig', array("form" => $form->createView()));
$output['result'] = 1; //FOR DISPLAY MODAL
$output['formHtml'] = $html;
}
$response = new JsonResponse($output);
$response->headers->set('Content-Type', 'application/json; charset=utf-8');
return $response;
}
/**
* @Route("/RecoveryPassword/createNewPassword/{stringConfirm}", name="recoveryPasswordCreateNew")
*/
public function recoveryPasswordCreateNewAction(Request $request, EntityManagerInterface $em, UserPasswordEncoderInterface $passwordEncoder, \Swift_Mailer $mailer, $stringConfirm)
{
$userResetPasswd = $em->getRepository('App:User')->findOneByStringConfirmRecovery($stringConfirm);
if($userResetPasswd){
$arrayFormOption['action'] = $this->generateUrl('recoveryPasswordCreateNew', array('stringConfirm' => $stringConfirm));
// Build the form
$form = $this->createForm(UserChangePasswordType::class, null, $arrayFormOption);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $form->get('password')->getData();
/*$passwordEncrypt = $this->get('security.password_encoder')
->encodePassword($userResetPasswd, $password);*/
$userResetPasswd->setPassword($passwordEncoder->encodePassword($userResetPasswd, $password));
$userResetPasswd->setStringConfirmRecovery('');
$em->persist($userResetPasswd);
$em->flush();
return $this->redirect($this->generateUrl('frontMessage', array('message' => 'changePasswordSuccess')));
}
return $this->render('Front/createNewPassword.html.twig', array(
'form' => $form->createView(),
));
}else{
return $this->redirect($this->generateUrl('frontMessage', array('message' => 'recoveryPasswordFailed')));
}
}
/**
* @Route("/Contact", name="contact")
* @Route("/contact")
* @Route("/contacto.html")
*/
public function contactAction(Request $request, EntityManagerInterface $em, \Swift_Mailer $mailer)
{
$contact = new Contact();
// Build the form
$form = $this->createForm(ContactType::class, $contact);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid() && $this->captchaverify($request->get('g-recaptcha-response'))) {
$privateKey = file_get_contents('/home/tam/dkim/private.key');
$domainName = 'tennis-aroundme.com';
$selector = '20200130';
$signer = new \Swift_Signers_DKIMSigner($privateKey, $domainName, $selector);
$message = (new \Swift_Message("Demande de renseignement TAM"))
->setFrom("no-reply@tennis-aroundme.com")
->setTo('contact@tennis-aroundme.com')
->setBody($this->renderView('Front/Mails/contact.html.twig', array('contact' => $contact)), 'text/html') //'text/html'
->addPart($this->renderView('Front/Mails/contactPlain.html.twig', array('contact' => $contact)), 'text/plain'); //'text/plain'
$message->attachSigner($signer);
$mailer->send($message);
$request->getSession()->getFlashBag()->add('success', 'Message envoyé avec succès !');
return $this->redirect($this->generateUrl('homepage'));
}
# check if captcha response isn't get throw a message
if($form->isSubmitted() && $form->isValid() && !$this->captchaverify($request->get('g-recaptcha-response'))){
$this->addFlash(
'error',
'Captcha Require'
);
}
return $this->render('Front/contact.html.twig', array(
"form" => $form->createView(),
));
}
# get success response from recaptcha and return it to controller
function captchaverify($recaptcha){
$url = "https://www.google.com/recaptcha/api/siteverify";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"secret"=>"6LcfUNQUAAAAADnbKm_L4Srgprwu8-0TfbFE3Smg","response"=>$recaptcha));
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response);
return $data->success;
}
}