src/Controller/HomeController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Interfaces\CountriesInterface;
  4. use App\Interfaces\IHotels;
  5. use App\Interfaces\SearchInterface;
  6. use App\Vendor\Symfony\MAbstractController;
  7. use App\Vendor\Symfony\UserException;
  8. use Psr\Log\LoggerInterface;
  9. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  10. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Mailer\MailerInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Twig\Environment;
  17. use Symfony\Component\Mime\Email;
  18. #[Route('/',name'home_')]
  19. class HomeController extends MAbstractController
  20. {
  21.     #[Route('/{_locale}'name'app'locale'ru'requirements: ["_locale" =>"en|ru"])]
  22.     public function index(Request $requestIHotels $hotelsServiceCountriesInterface $countries): Response
  23.     {
  24.         $country $countries->getCurrentCountry();
  25.         $featuredHotels $hotelsService->featuredHotels($country);
  26.         $randomHotels $hotelsService->randomHotels($country);
  27.         return $this->render('home/index.html.twig',[
  28.             "featuredHotels" => $featuredHotels,
  29.             "randomHotels" => $randomHotels,
  30.             "country" => $country
  31.         ]);
  32.     }
  33.     #[Route('/search/{_locale}'name'search'methods: ["GET","POST"])]
  34.     public function search(Request $requestSearchInterface $search){
  35.         $params$this->getParams();
  36.         $response = ["success" => false];
  37.         try{
  38.             if(isset($params["search"])){
  39.                 if(is_numeric($params["search"]["location"])){
  40.                     return $this->redirectToRoute("hotel_list"$params);
  41.                 } else {
  42.                     $params["search"]["hotelId"] = $params["search"]["location"];
  43.                     $params["id"] = $params["search"]["location"];
  44.                     $params[]["search"] = $params["search"];
  45.                     unset($params["search"]);
  46.                     return $this->redirectToRoute("app_detail_hotel_detail"$params);
  47.                 }
  48.             }
  49.         }
  50.         catch (UserException $e){
  51.             $response["error"] = $e->getMessage();
  52.         }
  53.         return new JsonResponse($response);
  54.     }
  55.     #[Route('/currency-change-ajax/{_locale}'name'home_currency_change_ajax'methods: ["POST"])]
  56.     public function changeCurrencyAjax(Request $requestEnvironment $twig){
  57.         $currency $request->get('currency');
  58.         $qp $request->get('qp');
  59.         if(isset($qp["routeParams"]) && !empty($qp["routeParams"]["id"])){
  60.             $qp["id"] = $qp["routeParams"]["id"];
  61.             $request->setLocale($qp["routeParams"]["_locale"]);
  62.         }
  63.         $cp $request->get('cp');
  64.         if(isset($qp[0])){
  65.             $qp[0]["search"]["currency"] = $currency;
  66.         } else {
  67.             $qp["search"]["currency"] = $currency;
  68.         }
  69.         unset( $qp["routeParams"]);
  70.         $url $this->generateUrl($cp$qp);
  71.         return new JsonResponse(["success" => true"url" => $url]);
  72.     }
  73.     #[Route('/search-countries-ajax/{_locale}'name'search_countries_ajax'methods: ["GET"])]
  74.     public function searchCountriesAjax(Request $requestCountriesInterface $countriesService){
  75.         $response["success"] = false;
  76.         try {
  77.             $query $request->get('country');
  78.             $response["results"] = $countriesService->search($request->getLocale(), $query);
  79.             $response["success"] = true;
  80.         } catch (\Exception $e){
  81.             $response["error"] = $e->getMessage();
  82.         }
  83.         return new JsonResponse($response);
  84.     }
  85.     #[Route('/reset-pass/{_locale}'name'reset_pass'methods: ["GET","POST"])]
  86.     public function resetPassword(Request $requestMailerInterface $mailer,LoggerInterface $passLogger): Response
  87.     {
  88.         $form $this->createFormBuilder()
  89.             ->add('Email'EmailType::class, ['attr' => ['placeholder' => 'Эл. адрес*''class' => 'form-control mb-3'], 'label' => false])
  90.             ->add('save'SubmitType::class, ['label' => 'ОТПРАВИТЬ''attr' => ['class' => 'btn btn-primary']])
  91.             ->getForm();
  92.         $form->handleRequest($request);
  93.         if ($form->isSubmitted() && $form->isValid()) {
  94.             $text "testPass";
  95.             $email = (new Email())
  96.                 ->from('info@sunscape.ru')
  97.                 ->to('artalmel@mail.ru')
  98.                 ->subject('Заказать тестирование')
  99.                 ->text('Заказать тестирование')
  100.                 ->html('<p>'.$text.'</p>');
  101.             try {
  102.                 $mailer->send($email);
  103.                 $passLogger->info("SEND MAIL", ["info" => true]);
  104.             } catch (\Exception $e){
  105.                 $passLogger->error("test", ["Error" => $e->getMessage()]);
  106.                 echo $e->getMessage();
  107.             }
  108.             return $this->redirectToRoute('home_app');
  109.         }
  110.         return $this->render('security/reset_password.html.twig', [
  111.             'form' => $form->createView()
  112.         ]);
  113. //        return new JsonResponse(["test"=>"test"]);
  114.     }
  115.     /* #[Route('/list/{_locale}', name: 'app_list', locale: 'ru', requirements: ["_locale" =>"en|ru"])]
  116.      public function list(Request $request, IHotels $hotelsService){
  117.          $hotels = $hotelsService->list($request);
  118.          // parameters to template
  119.          return $this->render('home/list.html.twig', $hotels);
  120.      }
  121.      #[Route('/static', name: 'app_static')]
  122.      public function staticData(Request $request){
  123.          return new JsonResponse([]);
  124.      }
  125.      #[Route('/insert', name: 'app_insert')]
  126.      public function insertData(Request $request, IZst $zst, IHotels $hotels): Response
  127.      {
  128.          $logFilePath = BASE_DIR.'/var/log/my.log';
  129.          file_put_contents($logFilePath, "Start date: ".date("Y-m-d H:i:s"));
  130.          $zst->hotelIncrementalDataDump(IZst::ZST_INCREMENTAL,'ru');
  131.  //        $zst->hotelAllDataDump(Ilangs::Russian);
  132.          file_put_contents($logFilePath, "End date: ".date("Y-m-d H:i:s"));
  133.          return new Response("insert");
  134.      }
  135.      #[Route('/import', name: 'app_import')]
  136.      public function importMongo(IZst $zst){
  137.          echo date("Y-m-d H:i:s");
  138.          $jsonFile = BASE_DIR . '/files/partner_feed_ru.json';
  139.          $zst->readDumpData($jsonFile,IZst::ZST_ALL);
  140.          echo date("Y-m-d H:i:s");
  141.      }*/
  142.     private function test(){
  143.         echo "OK";
  144.     }
  145.     private function postData3ds($params3ds){
  146.         $data json_decode($params3dstrue);
  147.         $getData $data["data"]["data_3ds"];
  148.         $action_url $getData["action_url"];
  149.         $postData = [
  150.             "md" => $getData["data"]["MD"],
  151.             "PaReq" => $getData["data"]["PaReq"],
  152.             "TermUrl" => $getData["data"]["TermUrl"]
  153.         ];
  154.         $jsonData json_encode($postData);
  155.         $curl curl_init();
  156.         curl_setopt_array($curl, array(
  157.             CURLOPT_URL => $action_url,
  158.             CURLOPT_RETURNTRANSFER => true,
  159.             CURLOPT_ENCODING => '',
  160.             CURLOPT_MAXREDIRS => 10,
  161.             CURLOPT_TIMEOUT => 0,
  162.             CURLOPT_FOLLOWLOCATION => true,
  163.             CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  164.             CURLOPT_CUSTOMREQUEST => 'POST',
  165.             CURLOPT_POSTFIELDS =>$jsonData,
  166.             CURLOPT_HTTPHEADER => array(
  167.                 'Content-Type: application/json'
  168.             ),
  169.         ));
  170.         $response curl_exec($curl);
  171.         curl_close($curl);
  172.         return json_decode($responsetrue);
  173.     }
  174. }