<?php
namespace App\Controller;
use App\Interfaces\CountriesInterface;
use App\Interfaces\IHotels;
use App\Interfaces\SearchInterface;
use App\Vendor\Symfony\MAbstractController;
use App\Vendor\Symfony\UserException;
use Psr\Log\LoggerInterface;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Routing\Annotation\Route;
use Twig\Environment;
use Symfony\Component\Mime\Email;
#[Route('/',name: 'home_')]
class HomeController extends MAbstractController
{
#[Route('/{_locale}', name: 'app', locale: 'ru', requirements: ["_locale" =>"en|ru"])]
public function index(Request $request, IHotels $hotelsService, CountriesInterface $countries): Response
{
$country = $countries->getCurrentCountry();
$featuredHotels = $hotelsService->featuredHotels($country);
$randomHotels = $hotelsService->randomHotels($country);
return $this->render('home/index.html.twig',[
"featuredHotels" => $featuredHotels,
"randomHotels" => $randomHotels,
"country" => $country
]);
}
#[Route('/search/{_locale}', name: 'search', methods: ["GET","POST"])]
public function search(Request $request, SearchInterface $search){
$params= $this->getParams();
$response = ["success" => false];
try{
if(isset($params["search"])){
if(is_numeric($params["search"]["location"])){
return $this->redirectToRoute("hotel_list", $params);
} else {
$params["search"]["hotelId"] = $params["search"]["location"];
$params["id"] = $params["search"]["location"];
$params[]["search"] = $params["search"];
unset($params["search"]);
return $this->redirectToRoute("app_detail_hotel_detail", $params);
}
}
}
catch (UserException $e){
$response["error"] = $e->getMessage();
}
return new JsonResponse($response);
}
#[Route('/currency-change-ajax/{_locale}', name: 'home_currency_change_ajax', methods: ["POST"])]
public function changeCurrencyAjax(Request $request, Environment $twig){
$currency = $request->get('currency');
$qp = $request->get('qp');
if(isset($qp["routeParams"]) && !empty($qp["routeParams"]["id"])){
$qp["id"] = $qp["routeParams"]["id"];
$request->setLocale($qp["routeParams"]["_locale"]);
}
$cp = $request->get('cp');
if(isset($qp[0])){
$qp[0]["search"]["currency"] = $currency;
} else {
$qp["search"]["currency"] = $currency;
}
unset( $qp["routeParams"]);
$url = $this->generateUrl($cp, $qp);
return new JsonResponse(["success" => true, "url" => $url]);
}
#[Route('/search-countries-ajax/{_locale}', name: 'search_countries_ajax', methods: ["GET"])]
public function searchCountriesAjax(Request $request, CountriesInterface $countriesService){
$response["success"] = false;
try {
$query = $request->get('country');
$response["results"] = $countriesService->search($request->getLocale(), $query);
$response["success"] = true;
} catch (\Exception $e){
$response["error"] = $e->getMessage();
}
return new JsonResponse($response);
}
#[Route('/reset-pass/{_locale}', name: 'reset_pass', methods: ["GET","POST"])]
public function resetPassword(Request $request, MailerInterface $mailer,LoggerInterface $passLogger): Response
{
$form = $this->createFormBuilder()
->add('Email', EmailType::class, ['attr' => ['placeholder' => 'Эл. адрес*', 'class' => 'form-control mb-3'], 'label' => false])
->add('save', SubmitType::class, ['label' => 'ОТПРАВИТЬ', 'attr' => ['class' => 'btn btn-primary']])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$text = "testPass";
$email = (new Email())
->from('info@sunscape.ru')
->to('artalmel@mail.ru')
->subject('Заказать тестирование')
->text('Заказать тестирование')
->html('<p>'.$text.'</p>');
try {
$mailer->send($email);
$passLogger->info("SEND MAIL", ["info" => true]);
} catch (\Exception $e){
$passLogger->error("test", ["Error" => $e->getMessage()]);
echo $e->getMessage();
}
return $this->redirectToRoute('home_app');
}
return $this->render('security/reset_password.html.twig', [
'form' => $form->createView()
]);
// return new JsonResponse(["test"=>"test"]);
}
/* #[Route('/list/{_locale}', name: 'app_list', locale: 'ru', requirements: ["_locale" =>"en|ru"])]
public function list(Request $request, IHotels $hotelsService){
$hotels = $hotelsService->list($request);
// parameters to template
return $this->render('home/list.html.twig', $hotels);
}
#[Route('/static', name: 'app_static')]
public function staticData(Request $request){
return new JsonResponse([]);
}
#[Route('/insert', name: 'app_insert')]
public function insertData(Request $request, IZst $zst, IHotels $hotels): Response
{
$logFilePath = BASE_DIR.'/var/log/my.log';
file_put_contents($logFilePath, "Start date: ".date("Y-m-d H:i:s"));
$zst->hotelIncrementalDataDump(IZst::ZST_INCREMENTAL,'ru');
// $zst->hotelAllDataDump(Ilangs::Russian);
file_put_contents($logFilePath, "End date: ".date("Y-m-d H:i:s"));
return new Response("insert");
}
#[Route('/import', name: 'app_import')]
public function importMongo(IZst $zst){
echo date("Y-m-d H:i:s");
$jsonFile = BASE_DIR . '/files/partner_feed_ru.json';
$zst->readDumpData($jsonFile,IZst::ZST_ALL);
echo date("Y-m-d H:i:s");
}*/
private function test(){
echo "OK";
}
private function postData3ds($params3ds){
$data = json_decode($params3ds, true);
$getData = $data["data"]["data_3ds"];
$action_url = $getData["action_url"];
$postData = [
"md" => $getData["data"]["MD"],
"PaReq" => $getData["data"]["PaReq"],
"TermUrl" => $getData["data"]["TermUrl"]
];
$jsonData = json_encode($postData);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $action_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>$jsonData,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
return json_decode($response, true);
}
}