src/Controller/HomeController.php line 56

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Actualite;
  4. use App\Entity\Announce;
  5. use App\Entity\Background;
  6. use App\Entity\BookingRoom;
  7. use App\Entity\Building;
  8. use App\Entity\BuildingType;
  9. use App\Entity\Contact;
  10. use App\Entity\MediaObject;
  11. use App\Entity\Testimony;
  12. use App\Entity\Town;
  13. use App\Entity\Visitor;
  14. use App\Security\EmailVerifier;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use GuzzleHttp\Client;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\RequestStack;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use FOS\RestBundle\Controller\Annotations as Rest;
  23. use FOS\RestBundle\Controller\Annotations\View;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Symfony\Component\HttpFoundation\JsonResponse;
  26. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  27. use Symfony\Component\Serializer\Serializer;
  28. use Symfony\Component\Serializer\SerializerInterface;
  29. class HomeController extends AbstractController
  30. {
  31.     private $emailVerifier;
  32.     private $entityManager;
  33.     public function __construct(EmailVerifier $emailVerifierEntityManagerInterface $entityManager)
  34.     {
  35.         $this->emailVerifier $emailVerifier;
  36.         $this->entityManager $entityManager;
  37.     }
  38.     /**
  39.      * @Route({"es": "/","en": "/"}, name="app_home")
  40.      */
  41.     public function index(Request $request): Response
  42.     {
  43.         $data=array();
  44.         $testimonies$this->entityManager->getRepository(Testimony::class)->findAll();
  45.         foreach ($testimonies as $testimony){
  46.             if($testimony-> getApproved() ){
  47.                 $url=$this->siteURL();
  48.                 if(!is_null($testimony->getPicture())){
  49.                     if($_SERVER['HTTP_HOST']=="localhost"  ){
  50.                         $client = new Client(['base_uri' => $this->siteURL()]);
  51.                         $response $client->request('GET''media_objects/'.$testimony->getPicture()->getId());
  52.                     }else{
  53.                         $client = new Client(['base_uri' => $this->siteURL()]);
  54.                         $response $client->request('GET''media_objects/'.$testimony->getPicture()->getId());
  55.                     }
  56.                     $responseBody=json_decode($response->getBody()->getContents());
  57.                     $responseBody->contentUrl=$url."".$responseBody->contentUrl;
  58.                 }else{
  59.                     $responseBody=new MediaObject();
  60.                     $responseBody->contentUrl=$url."/img/login.png";
  61.                 }
  62.                 $responseMessage=array(
  63.                     "picture"=>$responseBody,
  64.                     "data"=>$testimony
  65.                 );
  66.                 array_push($data,$responseMessage);
  67.             }
  68.         }
  69.         $testimonies=$data;
  70.         $news$this->entityManager->getRepository(Actualite::class)->findAll();
  71.         $anounces$this->entityManager->getRepository(Announce::class)->findAll();
  72.        $buildingTypes $this->entityManager->getRepository(BuildingType::class)->findAll();
  73.         $visitors$this->entityManager->getRepository(Visitor::class)->findAll();
  74.         $backgrounds$this->entityManager->getRepository(Background::class)->findAll();
  75.         $dataBackground=array();
  76.            $trendingDestinations=array();
  77.         $towns$this->entityManager->getRepository(Town::class)->findBy(array(),array("vues"=>"DESC"));
  78.         $bookings$this->entityManager->getRepository(BookingRoom::class)->findAll();
  79.         foreach ($towns as $town) {
  80.                 $count=0;
  81.                 foreach ($bookings as $booking) {
  82.                 if ($booking->getRoom()->getBuilding()->getAddress()->getTown()->getId()==$town->getId()) {
  83.                     $count++;
  84.                 }
  85.             }
  86.                 if($town->getPicture()){
  87.                     array_push($trendingDestinations,
  88.                         array(
  89.                              "totalBookings"=>$count,
  90.                                 "town"=>$town->getTownName(),
  91.                                 "townId"=>"".$town->getId(),
  92.                                 "country"=>$town->getCountry(),
  93.                                 "picture"=>$town->getPicture()->getFilePath(),
  94.                         ));
  95.                 }else{
  96.                     array_push($trendingDestinations,
  97.                         array(
  98.                             "totalBookings"=>$count,
  99.                                 "town"=>$town->getTownName(),
  100.                                 "townId"=>"".$town->getId(),
  101.                             "country"=>$town->getCountry(),
  102.                                 "picture"=>null,
  103.                         ));
  104.                 }
  105.         }
  106.         foreach ($backgrounds as $background) {
  107.             if ($background->getActive()) {
  108.                 $url=$this->siteURL();
  109.                 if(!is_null($background->getPicture())) {
  110.                     $client = new Client(['base_uri' => $this->siteURL()]);
  111.                     $response $client->request('GET''media_objects/' $background->getPicture()->getId());
  112.                     $responseBody=json_decode($response->getBody()->getContents());
  113.                     $responseBody->contentUrl=$url."".$responseBody->contentUrl;
  114.                     $responseMessage=array(
  115.                         "picture"=>$responseBody,
  116.                         "data"=>$background
  117.                     );
  118.                     array_push($dataBackground,$responseMessage);
  119.                 }
  120.             }
  121.         }
  122.         $updateVisitor=new Visitor();
  123.         foreach ($visitors as $currentVisitor){
  124.             if($currentVisitor->getWebAgent()==$_SERVER['HTTP_USER_AGENT']){
  125.                 $updateVisitor=$currentVisitor;
  126.             }
  127.         }
  128.         $local=$request->getLocale();
  129.         $language="";
  130.         $onlinePath=$request->getBasePath();
  131.         if(strcmp($local,"fr")==0)  {
  132.             $language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language' style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/french_flag.png\" style=\"height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> Français</a>";
  133.             $body='
  134.                <li>  <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Anglais</a></li>
  135.                 <li> <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Espagnol</a></li>
  136.                 <li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Italien</a></li>
  137.                 <li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Chinois</a></li>
  138.                 ';
  139.         }else   if(strcmp($local,"es")==0)   {
  140.             $language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language' style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/flag_spanish.png\" style=\"height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> Español</a>";
  141.             $body='
  142.                 <li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Francés</a></li>
  143.                 <li>  <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Inglés</a></li>
  144.                 <li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Italiano</a></li>
  145.                 <li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Chinise</a></li>
  146.                 ';
  147.         }else if  (strcmp($local,"en")==0)  {
  148.             $language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language'style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/flag_england.png\" style=\"height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> English</a>";
  149.             $body='
  150.                 <li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> French</a></li>
  151.                 <li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Italiano</a></li>
  152.                 <li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Chinise</a></li>
  153.                 <li>  <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Spanish</a></li>';
  154.         }else if  (strcmp($local,"it")==0)  {
  155.             $language=" <a href=\"#\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language'style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/italiano_flag.png\" style=\"height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> Italiano</a>";
  156.             $body='
  157.                 <li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Francese</a></li>
  158.                 <li> <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> inglese</a></li>
  159.                 <li> <a href="'.$onlinePath.'/change_locale/zh_CN" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/china_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Cinese</a></li>
  160.                 <li>  <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> Spagnolo</a></li>';
  161.         }else if  (strcmp($local,"zh_CN")==0)  {
  162.             $language=" <a href=\"#!\" class=\"dropdown-button grey-text text-darken-1\" data-activates='choose_language'style=\"font-size: 18px\"><img src=\"".$onlinePath."/img/china_flag.png\" style=\"height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px\" alt=\"\"> 中國人</a>";
  163.             $body='
  164.                 <li> <a href="'.$onlinePath.'/change_locale/fr" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/french_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 法語</a></li>
  165.                 <li> <a href="'.$onlinePath.'/change_locale/it" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/italiano_flag.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 意大利語</a></li>
  166.                 <li> <a href="'.$onlinePath.'/change_locale/en" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_england.png" style="height: 24px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 中國人</a></li>
  167.                 <li>  <a href="'.$onlinePath.'/change_locale/es" class="grey-text text-darken-1" style="font-size: 18px"><img src="'.$onlinePath.'/img/flag_spanish.png" style="height: 27px; width: 24px;margin-right: 10px; margin-bottom: -6px" alt=""> 西班牙語</a></li>';
  168.         }
  169.         arsort($trendingDestinations);
  170.         $onlinePath=$request->getBasePath();
  171.         return $this->render('home/home_index.html.twig', [
  172.             'controller_name' => 'HomeController',
  173.             'companyName' => $this->getParameter("app_client"),
  174.             'languageChoose' => $body,
  175.             'onlinePath' => $onlinePath,
  176.             'language' => $language,
  177.             'host'=>$this->siteURL(),
  178.             'local' => $local,
  179.             'testimonies' => $testimonies,
  180.             'backgrounds' => $dataBackground,
  181.             'announces' => $anounces,
  182.             'towns' => $towns,
  183.             'buildingTypes' => $buildingTypes,
  184.             'news' =>$news,
  185.             'visitor' =>$updateVisitor,
  186.             'trendingDestinations' =>($trendingDestinations),
  187.             'appUser' =>$this->getParameter('appUser'),
  188.         ]);
  189.     }
  190.     /**
  191.      * @Route("/change_locale/{locale}", name="change_locale")
  192.      */
  193.     public function changeLocale($localeRequest $request)
  194.     {
  195.         // On stocke la langue dans la session
  196.         $request->getSession()->set('_locale'$locale);
  197.         // On revient sur la page précédente
  198.         $this->addFlash('success''Local website language updated.');
  199.         return $this->redirect($this->siteURL().$request->getBasePath());
  200.     }
  201.     function getLocationInfoByIp(){
  202.         $client  = @$_SERVER['HTTP_CLIENT_IP'];
  203.         $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  204.         $remote  = @$_SERVER['REMOTE_ADDR'];
  205.         $result  = array('country'=>'''city'=>'');
  206.         if(filter_var($clientFILTER_VALIDATE_IP)){
  207.             $ip $client;
  208.         }elseif(filter_var($forwardFILTER_VALIDATE_IP)){
  209.             $ip $forward;
  210.         }else{
  211.             $ip $remote;
  212.         }
  213.         $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
  214.         if($ip_data && $ip_data->geoplugin_countryName != null){
  215.             $result['country'] = $ip_data->geoplugin_countryCode;
  216.             $result['city'] = $ip_data->geoplugin_city;
  217.             $result['client'] = $client;
  218.         }
  219.         return $result;
  220.     }
  221.     function ip_info($ip NULL$purpose "location"$deep_detect TRUE) {
  222.         $output NULL;
  223.         if (filter_var($ipFILTER_VALIDATE_IP) === FALSE) {
  224.             $ip $_SERVER["REMOTE_ADDR"];
  225.             if ($deep_detect) {
  226.                 if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
  227.                     $ip $_SERVER['HTTP_X_FORWARDED_FOR'];
  228.                 if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
  229.                     $ip $_SERVER['HTTP_CLIENT_IP'];
  230.             }
  231.         }
  232.         $purpose    str_replace(array("name""\n""\t"" ""-""_"), NULLstrtolower(trim($purpose)));
  233.         $support    = array("country""countrycode""state""region""city""location""address");
  234.         $continents = array(
  235.             "AF" => "Africa",
  236.             "AN" => "Antarctica",
  237.             "AS" => "Asia",
  238.             "EU" => "Europe",
  239.             "OC" => "Australia (Oceania)",
  240.             "NA" => "North America",
  241.             "SA" => "South America"
  242.         );
  243.         if (filter_var($ipFILTER_VALIDATE_IP) && in_array($purpose$support)) {
  244.             $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" $ip));
  245.             if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
  246.                 switch ($purpose) {
  247.                     case "location":
  248.                         $output = array(
  249.                             "city"           => @$ipdat->geoplugin_city,
  250.                             "state"          => @$ipdat->geoplugin_regionName,
  251.                             "country"        => @$ipdat->geoplugin_countryName,
  252.                             "country_code"   => @$ipdat->geoplugin_countryCode,
  253.                             "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
  254.                             "continent_code" => @$ipdat->geoplugin_continentCode
  255.                         );
  256.                         break;
  257.                     case "address":
  258.                         $address = array($ipdat->geoplugin_countryName);
  259.                         if (@strlen($ipdat->geoplugin_regionName) >= 1)
  260.                             $address[] = $ipdat->geoplugin_regionName;
  261.                         if (@strlen($ipdat->geoplugin_city) >= 1)
  262.                             $address[] = $ipdat->geoplugin_city;
  263.                         $output implode(", "array_reverse($address));
  264.                         break;
  265.                     case "city":
  266.                         $output = @$ipdat->geoplugin_city;
  267.                         break;
  268.                     case "state":
  269.                         $output = @$ipdat->geoplugin_regionName;
  270.                         break;
  271.                     case "region":
  272.                         $output = @$ipdat->geoplugin_regionName;
  273.                         break;
  274.                     case "country":
  275.                         $output = @$ipdat->geoplugin_countryName;
  276.                         break;
  277.                     case "countrycode":
  278.                         $output = @$ipdat->geoplugin_countryCode;
  279.                         break;
  280.                 }
  281.             }
  282.         }
  283.         return $output;
  284.     }
  285.     function siteURL()
  286.     {
  287.         $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" "http://";
  288.         $domainName $_SERVER['HTTP_HOST'].'/';
  289.         if ($_SERVER['HTTP_HOST'] == "localhost") {
  290.             return $protocol.$domainName.$this->getParameter('localRepository')."/".$this->getParameter('apiLink');
  291.         }else{
  292.             return $protocol.$domainName.$this->getParameter('apiLink');
  293.         }
  294.     }
  295.     /**
  296.      * Locates user  .
  297.      * @Rest\Post("/client_location")
  298.      * @View
  299.      */
  300.     public function locatedClientAction(Request $request)
  301.     {
  302.         $data json_decode($request->getContent(), true);
  303.         if (empty($data['api_address']) ) {
  304.             return new JsonResponse($dataResponse::HTTP_NOT_ACCEPTABLE);
  305.         }
  306.         $visitors$this->entityManager->getRepository(Visitor::class)->findAll();
  307.         $trendingDestinations=array();
  308.         $towns$this->entityManager->getRepository(Town::class)->findAll();
  309.         $bookings$this->entityManager->getRepository(BookingRoom::class)->findAll();
  310.         $webVisitor=0;
  311.         $updateVisitor=new Visitor();
  312.         foreach ($visitors as $currentVisitor){
  313.             if($currentVisitor->getWebAgent()==$_SERVER['HTTP_USER_AGENT']){
  314.                 $webVisitor=$currentVisitor->getId();
  315.                 $updateVisitor=$currentVisitor;
  316.             }
  317.         }
  318.         if($webVisitor==0){
  319.             $visitor= new Visitor();
  320.             try{
  321.                 $externalIp =$data['api_address'];
  322.                 $location$this->ip_info($externalIp"Location");
  323.                 $visitor->setIpLocation("".$externalIp);
  324.                 $visitor->setCountryLocation("".$location["country"]);
  325.                 $visitor->setContinent("".$location["continent"]);
  326.                 $visitor->setVille("".$location["city"] );
  327.                 $visitor->setRegion("".$location["state"] );
  328.                 $visitor->setCountryCode("".$location["country_code"] );
  329.                 $em=$this->getDoctrine()->getManager();
  330.                 $em->persist($visitor);
  331.                 $em->flush();
  332.                 $this->addFlash('success''Welcome in '.$location["country"].' we are using cookies on this website for better user experience');
  333.                 $webVisitor=$visitor->getId();
  334.             }catch (\Exception $e){
  335.             }
  336.             $location["id"]=$visitor->getId();
  337.             foreach ($towns as $town) {
  338.                 if(strtolower($town->getCountry()->getCode())==strtolower($location["country_code"])){
  339.                 $count=0;
  340.                 foreach ($bookings as $booking) {
  341.                     if ($booking->getRoom()->getBuilding()->getAddress()->getTown()->getId()==$town->getId()) {
  342.                         $count++;
  343.                     }
  344.                 }
  345.                 if($town->getPicture()){
  346.                     array_push($trendingDestinations,
  347.                         array(
  348.                             "totalBookings"=>$count,
  349.                             "town"=>$town->getTownName(),
  350.                             "townId"=>"".$town->getId(),
  351.                             "country"=>$town->getCountry()->getCountryName(),
  352.                             "countryFlag"=>$town->getCountry()->getCountryFlag()->getFilePath(),
  353.                             "picture"=>$town->getPicture()->getFilePath(),
  354.                         ));
  355.                 }else{
  356.                     array_push($trendingDestinations,
  357.                         array(
  358.                             "totalBookings"=>$count,
  359.                             "town"=>$town->getTownName(),
  360.                             "townId"=>"".$town->getId(),
  361.                             "country"=>$town->getCountry()->getCountryName(),
  362.                             "countryFlag"=>$town->getCountry()->getCountryFlag()->getFilePath(),
  363.                             "picture"=>null,
  364.                         ));
  365.                 }
  366.             }
  367.             }
  368.             arsort($trendingDestinations,SORT_NUMERIC);
  369.             $location["trendingTown"]=$trendingDestinations;
  370.             $location["total"]=sizeof($trendingDestinations);
  371.             return new JsonResponse($locationResponse::HTTP_ACCEPTED);
  372.         }else{
  373.             $externalIp =$data['api_address'];
  374.             $location$this->ip_info($externalIp"location");
  375.             $updateVisitor->setIpLocation("".$externalIp);
  376.             $updateVisitor->setCountryLocation("".$location["country"]);
  377.             $updateVisitor->setContinent("".$location["continent"]);
  378.             $updateVisitor->setVille("".$location["city"] );
  379.             $updateVisitor->setRegion("".$location["state"] );
  380.             $updateVisitor->setCountryCode("".$location["country_code"] );
  381.             $em=$this->getDoctrine()->getManager();
  382.             $em->persist($updateVisitor);
  383.             $em->flush();
  384.             $location["id"]=$updateVisitor->getId();
  385.             $capital=new Town();
  386.             foreach ($towns as $town) {
  387.                 if(strtolower($town->getCountry()->getCode())==strtolower($location["country_code"])){
  388.                     if($town->getIsCapital()){
  389.                         $capital=$town;
  390.                     }
  391.                     $count=0;
  392.                     foreach ($bookings as $booking) {
  393.                         if ($booking->getRoom()->getBuilding()->getAddress()->getTown()->getId()==$town->getId()) {
  394.                             $count++;
  395.                         }
  396.                     }
  397.                     if($town->getPicture()){
  398.                         array_push($trendingDestinations,
  399.                             array(
  400.                                 "totalBookings"=>$count,
  401.                                 "town"=>$town->getTownName(),
  402.                                 "isCapital"=>$town->getIsCapital(),
  403.                                 "townId"=>"".$town->getId(),
  404.                                 "country"=>$town->getCountry()->getCountryName(),
  405.                                 "countryFlag"=>$town->getCountry()->getCountryFlag()->getFilePath(),
  406.                                 "picture"=>$town->getPicture()->getFilePath(),
  407.                             ));
  408.                     }else{
  409.                         array_push($trendingDestinations,
  410.                             array(
  411.                                 "totalBookings"=>$count,
  412.                                 "town"=>$town->getTownName(),
  413.                                 "townId"=>"".$town->getId(),
  414.                                 "isCapital"=>$town->getIsCapital(),
  415.                                 "country"=>$town->getCountry()->getCountryName(),
  416.                                 "countryFlag"=>$town->getCountry()->getCountryFlag()->getFilePath(),
  417.                                 "picture"=>null,
  418.                             ));
  419.                     }
  420.                 }
  421.             }
  422.             arsort($trendingDestinations,SORT_NUMERIC);
  423.             $location["trendingTown"]=$trendingDestinations;
  424.             $location["capital"]=$capital->getId();
  425.             $location["total"]=sizeof($trendingDestinations);
  426.             return new JsonResponse($locationResponse::HTTP_ACCEPTED);
  427.         }
  428.     }
  429.     /**
  430.      * Add  Client to new letter.
  431.      *
  432.      * @Rest\Post("/client_newletter")
  433.      * @View
  434.      */
  435.     public function addToNewLetterAction(Request $request)
  436.     {
  437.         $data json_decode($request->getContent(), true);
  438.         if (empty($data['email']) ) {
  439.             return new JsonResponse($dataResponse::HTTP_NOT_ACCEPTABLE);
  440.         }
  441.         $visitors$this->getDoctrine()
  442.             ->getRepository(Visitor::class)->findAll();
  443.         $response="SUBMIT";
  444.         $webVisitor=0;
  445.         foreach ($visitors as $currentVisitor){
  446.             if($currentVisitor->getWebAgent()==$_SERVER['HTTP_USER_AGENT']){
  447.                 $webVisitor=$currentVisitor;
  448.             }
  449.         }
  450.         if($webVisitor->getId()!=null){
  451.             try{
  452.                 if(!is_null($webVisitor->getVisitorEmail()||
  453.                     !empty($webVisitor->getVisitorEmail()))){
  454.                     $email =$data['email'];
  455.                     $this->addFlash('success''Congratulation!! You have been had in our newLetters.');
  456.                     $webVisitor->setVisitorEmail("".$email );
  457.                     $em=$this->getDoctrine()->getManager();
  458.                     $em->persist($webVisitor);
  459.                     $em->flush();
  460.                 }else{
  461.                     $response="FOUND";
  462.                 }
  463.             }catch (\Exception $e){
  464.             }
  465.         }
  466.         return new JsonResponse($responseResponse::HTTP_ACCEPTED);
  467.     }
  468.     /**
  469.      * conctact us.
  470.      * @Rest\Post("/client_contact_us")
  471.      * @View
  472.      */
  473.     public function sendMessageToAdminAction(Request $request)
  474.     {
  475.         $data json_decode($request->getContent(), true);
  476.         $email =$data['email'];
  477.         $contactName =$data['contactName'];
  478.         $message =$data['message'];
  479.         $phone =$data['phone'];
  480.         if (empty($data['email']) || empty($data['message']) || empty($data['contactName'])  ) {
  481.             return new JsonResponse($dataResponse::HTTP_NOT_ACCEPTABLE);
  482.         }
  483.         $visitors$this->getDoctrine()
  484.             ->getRepository(Visitor::class)->findAll();
  485.         $response="success";
  486.         $webVisitor=0;
  487.         foreach ($visitors as $currentVisitor){
  488.             if($currentVisitor->getWebAgent()==$_SERVER['HTTP_USER_AGENT']){
  489.                 $webVisitor=$currentVisitor;
  490.             }
  491.         }
  492.         if($webVisitor->getId()!=null){
  493.             try{
  494.                 if(is_null($webVisitor->getVisitorEmail()||
  495.                     empty($webVisitor->getVisitorEmail()))) {
  496.                     $webVisitor->setVisitorEmail("" $email);
  497.                 }
  498.                 if(empty($webVisitor->getVisitorName()) || is_null($webVisitor->getVisitorName())){
  499.                     $webVisitor->setVisitorName("".$contactName );
  500.                 }
  501.                 if(empty($webVisitor->getVisitorPhone()) || is_null($webVisitor->getVisitorPhone())){
  502.                     $webVisitor->setVisitorPhone("".$phone );
  503.                 }
  504.                 $em=$this->getDoctrine()->getManager();
  505.                 $em->persist($webVisitor);
  506.                 $em->flush();
  507.                 $contactUs=new Contact();
  508.                 $contactUs->setMessage($message);
  509.                 $contactUs->setVisitor($webVisitor);
  510.                 $em->persist($contactUs);
  511.                 $em->flush();
  512.             }catch (\Exception $e){
  513.             }
  514.         }
  515.         return new JsonResponse($responseResponse::HTTP_ACCEPTED);
  516.     }
  517.     /**
  518.      * Locates user  .
  519.      * @Rest\Post("/api/v1/find_buildings")
  520.      * @View
  521.      */
  522.     public function findBuildAction(Request $request,SerializerInterface $serializer):Response
  523.     {
  524.         $data json_decode($request->getContent(), true);
  525.         if (empty($data['town'])) {
  526.             return new JsonResponse($dataResponse::HTTP_NOT_ACCEPTABLE);
  527.         }
  528.         $updateTown=$this->entityManager->getRepository(Town::class)->find($data['town']);
  529.         if(!is_null($updateTown)){
  530.             $updateTown->setVues($updateTown->getVues()+1);
  531.             $this->entityManager->persist($updateTown);
  532.             $this->entityManager->flush();
  533.         }
  534.         $town $data['town'];
  535.         $buildType=$data['buildType'];
  536.         $startedDate=$data['startedDate'];
  537.         $endDate=$data['endDate'];
  538.         $buildRooms=$this->entityManager->getRepository(Building::class)->findAllDetailByTown($town,$buildType);
  539.         $arraysOfData=array();
  540.         // $loader is any of the valid loaders explained later in this article
  541.         foreach ($buildRooms as $buildRoom ){
  542.             if($buildRoom->isActive()){
  543.                 array_push($arraysOfData,  json_decode($serializer->serialize($buildRoom'json')) );
  544.             }
  545.         }
  546.         return new JsonResponse(
  547.             array(
  548.             "requestKeys"=> $data,
  549.             "data"=> $arraysOfData,
  550.             "length"=> sizeof($arraysOfData),
  551.         ), Response::HTTP_OK);
  552.     }
  553. }