src/Service/FrontService.php line 98

Open in your IDE?
  1. <?php
  2. namespace App\Service;
  3. use App\Config\FaqRouteEnum;
  4. use App\Entity\Customer;
  5. use App\Entity\FileData;
  6. use App\Entity\FrontPage;
  7. use App\Entity\HotelXmlPrice;
  8. use App\Entity\MenuItemFront;
  9. use App\Repository\AgencyRepository;
  10. use App\Repository\CityRepository;
  11. use App\Repository\CurrencyExchangerateRepository;
  12. use App\Repository\CustomerRepository;
  13. use App\Repository\ExtraTypeRepository;
  14. use App\Repository\FileDataRepository;
  15. use App\Repository\FrontPageRepository;
  16. use App\Repository\FrontSectionRepository;
  17. use App\Repository\FrontThemeRepository;
  18. use App\Repository\FrontZoneRepository;
  19. use App\Repository\HotelXmlPriceRepository;
  20. use App\Repository\HotelXmlRepository;
  21. use App\Repository\MenuItemFrontRepository;
  22. use App\Repository\ProductFeeRepository;
  23. use App\Repository\SocialNetworkRepository;
  24. use App\Repository\HubLocationRepository;
  25. use Symfony\Component\HttpFoundation\RequestStack;
  26. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  27. class FrontService
  28. {
  29.     public function __construct(
  30.         private readonly MenuItemFrontRepository        $menuItemFrontRepository,
  31.         private readonly FileDataRepository             $fileDataRepository,
  32.         private readonly Helpers                        $helpers,
  33.         private readonly RequestStack                   $requestStack,
  34.         private readonly FrontZoneRepository            $frontZoneRepository,
  35.         private readonly HotelXmlPriceRepository        $hotelXmlPriceRepository,
  36.         private readonly CityRepository                 $cityRepository,
  37.         private readonly UrlGeneratorInterface          $router,
  38.         private readonly CurrencyExchangerateRepository $currencyExchangerateRepository,
  39.         private readonly ParameterService               $parameterService,
  40.         private readonly FrontPageRepository            $frontPageRepository,
  41.         private readonly FrontSectionRepository  $frontSectionRepository,
  42.         private readonly HotelXmlRepository      $hotelXmlRepository,
  43.         private readonly SocialNetworkRepository $socialNetworkRepository,
  44.         private readonly AgencyRepository        $agencyRepository,
  45.         private readonly CurrencyService         $currencyService,
  46.         private readonly FrontThemeRepository    $frontThemeRepository,
  47.         private readonly ProductFeeRepository    $productFeeRepository,
  48.         private readonly HubLocationRepository   $xmlApiDefaultRepository,
  49.         private readonly CustomerRepository      $customerRepository,
  50.         private readonly ExtraTypeRepository     $extraTypeRepository,
  51.         private readonly FaqService              $faqService,
  52.     )
  53.     {
  54.     }
  55.     function getFrontMenu(): array
  56.     {
  57.         $session $this->requestStack->getSession();
  58.         $menu_front_items $this->menuItemFrontRepository->findBy(['active' => true], ['displayOrder' => 'ASC']);
  59.         $menu_front_items_array $this->helpers->convert_ObjectArray_to_2DArray($menu_front_items);
  60.         foreach ($menu_front_items as $menu_front_item) {
  61.             switch ($menu_front_item->getId()) {
  62.                 case str_starts_with($menu_front_item->getId(), MenuItemFront::HOTEL_CONTRY_CODE):
  63.                     //if the country is null doesn't need to continue
  64.                     if (is_null($menu_front_item->getCountry())) break;
  65.                     $cities $this->cityRepository->findByCriteria([
  66.                         'active' => true,
  67.                         'country' => $menu_front_item->getCountry()
  68.                     ]);
  69.                     foreach ($cities as $key_city => $city) {
  70.                         $path $this->router->generate('app_front_hotel_city', ['name' => $city->getName()]);
  71.                         $menu_city = [
  72.                             'id' => 'HOTEL_' strtoupper($city->getName()),
  73.                             'title' => ucwords(strtolower($city->getName())),
  74.                             'route' => $path,
  75.                             'displayOrder' => $key_city 1,
  76.                             'externalUrl' => null,
  77.                             "parent" => $menu_front_item->getId()
  78.                         ];
  79.                         $menu_front_items_array[] = $menu_city;
  80.                     }
  81.                     break;
  82.             }
  83.         }
  84.         //dd($menu_front_items_array);
  85.         $menu_as_tree $this->helpers->buildTree($menu_front_items_array);
  86.         if (array_key_exists('FRONT'$menu_as_tree) && array_key_exists('children'$menu_as_tree['FRONT'])) {
  87.             $session->set('menu_front'$menu_as_tree['FRONT']['children']);
  88.         }
  89.         return $menu_as_tree;
  90.     }
  91.     public function getItemHotel(HotelXmlPrice $hotelXmlPrice): array
  92.     {
  93.         $currency_session $this->currencyService->getSessionCurrency();
  94.         $hotel $hotelXmlPrice->getHotelXml();
  95.         $promos $hotel->getHotel() !== null $hotel->getHotel()->getPromos() : [];
  96.         $hotel_direct $hotel->getHotel();
  97.         $badges $hotel->getHotel() !== null $hotel->getHotel()->getBadges() : [];
  98.         return [
  99.             'type' => 'hotel',
  100.             'title' => ($hotel_direct != null) ? $hotel_direct->getName() : $hotel->getName(),
  101.             'link' => $this->router->generate('hotel_detail', [
  102.                 'city_label' => $hotel->getCity(),
  103.                 'country_label' => $hotel->getCountry(),
  104.                 'hotel_label' => $hotel->getName(),
  105.                 'id' => $hotel->getCode()
  106.             ]),
  107.             'image' => ($hotel_direct != null) ? $hotel_direct->getPrimaryImageUrl() : $hotel->getImageUrl(),
  108.             'city' => $hotel->getCity(),
  109.             'country' => $hotel->getCountry(),
  110.             'stars' => ($hotel_direct != null) ? $hotel_direct->getStarRating() : $hotel->getStarRating(),
  111.             'promos' => $promos,
  112.             'badges' => $badges,
  113.             'price' => $this->currencyService->convert($hotelXmlPrice->getPrice(), $hotelXmlPrice->getCurrency(), $currency_session),
  114.             'currency' => $currency_session,
  115.             'board' => $hotelXmlPrice->getBoard(),
  116.         ];
  117.     }
  118.     private function getItemCity(array $city_data): array
  119.     {
  120.         return [
  121.             'title' => $city_data['name'],
  122.             'type' => 'city',
  123.             'link' => $this->router->generate('app_front_hotel_city', [
  124.                 'name' => $city_data['name']
  125.             ]),
  126.             'image' => $this->getCityImage($city_data['name']),
  127.             'city' => $city_data['name'],
  128.             'badges' => [],
  129.             'country' => $city_data['country'],
  130.             'hotelsCount' => $city_data['hotelsCount'],
  131.         ];
  132.     }
  133.     private function getItemTheme(array $theme_data): array
  134.     {
  135.         return [
  136.             'title' => $theme_data['name'],
  137.             'type' => 'theme',
  138.             'link' => $this->router->generate('app_front_hotel_theme', [
  139.                 'name' => $theme_data['name']
  140.             ]),
  141.             'image' => $this->getThemeImage($theme_data['name']),
  142.             'city' => "",
  143.             'country' => "",
  144.             'badges' => [],
  145.             'hotelsCount' => $theme_data['hotelsCount'],
  146.         ];
  147.     }
  148.     public  function getItemExtra(array $theme_data): array
  149.     {
  150.         return [
  151.             'title' => $theme_data['name'],
  152.             'type' => 'extra',
  153.             'link' => $this->router->generate('extra_detail', ['extra_label' => urlencode($theme_data['name']), 'id' => $theme_data['id']]),
  154.             'image' => $theme_data['image'],
  155.             'city' => "",
  156.             'badges' => [],
  157.             'country' => "",
  158.             'currency' =>$theme_data['currency'],
  159.             'price' => $theme_data['price'],
  160.             'hotelsCount' => "",
  161.         ];
  162.     }
  163.     private function getItemExtraType(array $extra_type_data): array
  164.     {
  165.         return [
  166.             'title' => $extra_type_data['name'],
  167.             'type' => 'extra',
  168.             'link' => $this->router->generate('app_front_extra_by_type', ['id' => $extra_type_data['id'], 'name' => urlencode($extra_type_data['name'])]),
  169.             'image' => $extra_type_data['image'],
  170.             'city' => "",
  171.             'badges' => [],
  172.             'country' => "",
  173.             'price' => "",
  174.             'hotelsCount' => "",
  175.         ];
  176.     }
  177.     public function getFrontZones(): array
  178.     {
  179.         $zones $this->frontZoneRepository->findBy(['active' => true]);
  180.         //dd($zones);
  181.         $zones_data = [];
  182.         foreach ($zones as $zone) {
  183.             if (empty($zone->getQuery())) continue;
  184.             $zoneID $zone->getId();
  185.             $zones_data[$zoneID] = [
  186.                 'view' => $zone->getHtmlFileName(), # view 1, 2, 3, 4 ...
  187.                 'title' => $zone->getTitle(),
  188.                 'description' => $zone->getDescription(),
  189.                 'active' => $zone->isActive(),
  190.                 'backgroundColor' => $zone->getbackgroundColor(),
  191.                 'backgroundImage' => $zone->getBackgroundImage() ? $zone->getBackgroundImage()->getUrl() : null,
  192.                 'itemsCount' => $zone->getNbElementsToDisplay(),
  193.                 'query' => $zone->getQuery(),
  194.                 'items' => [],
  195.             ];
  196.             switch ($zone->getQuery()) {
  197.                 case 'hotel' :
  198.                 {
  199.                     $criterias = [];
  200.                     foreach ($zone->getFrontZoneParamValues() as $paramValue) {
  201.                         if ($paramValue->getValue()) {
  202.                             $criterias[$paramValue->getParameter()][] = $paramValue->getValue();
  203.                         }
  204.                     }
  205.                     $hotelXmlPrices $this->hotelXmlPriceRepository->getHotelXmlPriceByCriterias($criterias$zone->getNbElementsToDisplay());
  206. //                    dd($hotelXmlPrices);
  207.                     foreach ($hotelXmlPrices as $hotelXmlPrice) {
  208.                         $zones_data[$zoneID]['items'][] = $this->getItemHotel($hotelXmlPrice);
  209.                     }
  210.                     break;
  211.                 }
  212.                 case 'cities' :
  213.                 {
  214.                     $cityNames = [];
  215.                     $frontZoneParamValues $zone->getFrontZoneParamValues();
  216.                     foreach ($frontZoneParamValues as $frontZoneParamValue) {
  217.                         $cityNames[] = $frontZoneParamValue->getValue();
  218.                     }
  219.                     $cities $this->hotelXmlRepository->getCities($cityNames);
  220.                     foreach ($cities as $city_data) {
  221.                         $zones_data[$zoneID]['items'][] = $this->getItemCity($city_data);
  222.                     }
  223.                     break;
  224.                 }
  225.                 case 'themes':
  226.                 {
  227.                     $themeNames = [];
  228.                     $frontZoneParamValues $zone->getFrontZoneParamValues();
  229.                     foreach ($frontZoneParamValues as $frontZoneParamValue) {
  230.                         $themeNames[] = $frontZoneParamValue->getValue();
  231.                     }
  232.                     $themes $this->frontThemeRepository->getSelectedFrontTheme($themeNames);
  233.                     foreach ($themes as $theme) {
  234.                         $hotelsCount $this->hotelXmlRepository->getThemeCount($theme);
  235.                         $theme_data = [
  236.                             "name" => $theme->getName(),
  237.                             "hotelsCount" => $hotelsCount
  238.                         ];
  239.                         $zones_data[$zoneID]['items'][] = $this->getItemTheme($theme_data);
  240.                     }
  241.                     break;
  242.                 }
  243.                 case 'extras':
  244.                 {
  245.                     $types = [];
  246.                     $frontZoneParamValues $zone->getFrontZoneParamValues();
  247.                     foreach ($frontZoneParamValues as $frontZoneParamValue) {
  248.                         $types[] = $frontZoneParamValue->getValue();
  249.                     }
  250.                     $extraTypes $this->extraTypeRepository->getSelectedFrontExtraType($types);
  251.                     foreach ($extraTypes as $extraType_data) {
  252.                         $extra_type_data = [
  253.                             "id" => $extraType_data->getId(),
  254.                             "name" => $extraType_data->getName(),
  255.                             "image" => $extraType_data->getImage()?->getUrl(),
  256.                         ];
  257.                         $zones_data[$zoneID]['items'][] = $this->getItemExtraType($extra_type_data);
  258.                     }
  259.                     break;
  260.                 }
  261.             }
  262.         }
  263.         return $zones_data;
  264.     }
  265.     public function getCityImage($cityName): string
  266.     {
  267.         $city $this->cityRepository->findOneBy(['name' => $cityName]);
  268.         if (is_null($city)) return FileData::IMAGE_NOT_AVAILABLE_PATH;
  269.         $image $city->getImage();
  270.         return $image $image->getUrl() : FileData::IMAGE_NOT_AVAILABLE_PATH;
  271.     }
  272.     public function getThemeImage($themeName): string
  273.     {
  274.         $theme $this->frontThemeRepository->findOneBy(['name' => $themeName]);
  275.         if (is_null($theme)) return FileData::IMAGE_NOT_AVAILABLE_PATH;
  276.         $image $theme->getImage();
  277.         return $image $image->getUrl() : FileData::IMAGE_NOT_AVAILABLE_PATH;
  278.     }
  279.     public function getExtraImage($themeName): string
  280.     {
  281.         $extra $this->extraTypeRepository->findOneBy(['name' => $themeName]);
  282.         if (is_null($extra)) return FileData::IMAGE_NOT_AVAILABLE_PATH;
  283.         $extra $extra->getImage();
  284.         return $extra $extra->getUrl() : FileData::IMAGE_NOT_AVAILABLE_PATH;
  285.     }
  286.     public function getSliderImages($type): array
  287.     {
  288.         return $this->fileDataRepository->findBy(['type' => $type'active' => true]);
  289.     }
  290.     public function getSeoInfo(string $slug): array
  291.     {
  292.         $seoStaticPage $this->frontPageRepository->findOneBy(['slug' => $slug]);
  293.         return [
  294.             'homeSeoTitle' => $seoStaticPage->getSeoTitle(),
  295.             'homeSeoDescription' => $seoStaticPage->getSeoDescription(),
  296.             'socialMedias' => $this->getSocialNetworks()
  297.         ];
  298.     }
  299.     public function getSocialNetworks(): array
  300.     {
  301.         return $this->socialNetworkRepository->findBy(['active' => 'true']);
  302.     }
  303.     public function getCurrencies($fromExchangeRates false): array
  304.     {
  305.         $currencies = [];
  306.         if ($fromExchangeRates) {
  307.             $currencies_exchanges $this->currencyExchangerateRepository->findAll();
  308.             foreach ($currencies_exchanges as $currencies_exchange) {
  309.                 $currencies[] = $currencies_exchange->getCurrencyTo();
  310.             }
  311.         } else {
  312.             $customers_default $this->customerRepository->findBy(['isDefault' => true'active' => true]);
  313.             foreach ($customers_default as $customer) {
  314.                 $currencies[] = $customer->getCurrency();
  315.             }
  316.         }
  317.         return array_unique($currencies);
  318.     }
  319.     public function getAgencies(): array
  320.     {
  321.         return $this->agencyRepository->findby(['isDefault' => false]);
  322.     }
  323.     public function getHomePage(): FrontPage
  324.     {
  325.         return $this->frontPageRepository->findOneBy(['slug' => FrontPage::HOME_PAGE]);
  326.     }
  327.     public function getProductFee(string $productCustomer $customer): float|int
  328.     {
  329.         $productFee $this->productFeeRepository->findOneBy(['product' => $product]);
  330.         if ($productFee) {
  331.             $exchangeRate $this->currencyService->calculateExchangeRate(
  332.                 $this->parameterService->getReferenceCurrency(), // referenceCurrency
  333.                 $customer->getCurrency()
  334.             );
  335.             if ($customer->getClass() == 'CustomerMoral') {
  336.                 return $productFee->getB2bAmount() * $exchangeRate;
  337.             } else {
  338.                 return $productFee->getAmount() * $exchangeRate;
  339.             }
  340.         }
  341.         return 0;
  342.     }
  343.     public function getSuggestions($cookieName$xmlApiType): array
  344.     {
  345.         $suggestions = [];
  346.         $resultSuggestions $this->xmlApiDefaultRepository->findBy(['productType' => $xmlApiType]);
  347.         foreach ($resultSuggestions as $resultSuggestion) {
  348.             $suggestions[] = json_decode($resultSuggestion->getDataJson());
  349.         }
  350.         return $suggestions;
  351.     }
  352.     public function getSections(): array
  353.     {
  354.         $sections $this->frontSectionRepository->findAll();
  355.         return $sections;
  356.     }
  357.     public function getSessionCustomer(): ?Customer
  358.     {
  359.         $session $this->requestStack->getSession();
  360.         $customer_in_session $session->get('backend_customer');
  361.         $defaultCustomerId $customer_in_session?->getId();
  362.         $defaultCustomer null;
  363.         if ($defaultCustomerId) {
  364.             $defaultCustomer $this->customerRepository->find($defaultCustomerId);
  365.         }
  366.         return $defaultCustomer;
  367.     }
  368.     public function getTrackingTools() : array
  369.     {
  370.         $twig_parameters ['META_PIXEL_ID'] = $this->parameterService->getTrackerMetaId();
  371.         $twig_parameters ['GOOGLE_ANALYTICS_ID'] = $this->parameterService->getTrackerGoogleAnalyticsID();
  372.         $twig_parameters ['GOOGLE_ADS_ID'] = $this->parameterService->getTrackerGoogleADsID();
  373.         $twig_parameters ['GOOGLE_TAG_MANAGER_ID'] = $this->parameterService->getTrackerGoogleTagManagerID();
  374.         $twig_parameters ['FACEBOOK_DOMAIN_VERIFICATION_CODE'] = $this->parameterService->getTrackerFacebookDomainVerification();
  375.         $twig_parameters ['GOOGLE_SEARCH_VERIFICATION'] = $this->parameterService->getTrackerGoogleSearchVerification();
  376.         
  377.         return  $twig_parameters;
  378.     }
  379.     /**
  380.      * @return \App\Entity\Faq[]
  381.      */
  382.     public function getFaqs(FaqRouteEnum $route, ?string $routeParameter null): array
  383.     {
  384.         return $this->faqService->getFaqs($route$routeParameter);
  385.     }
  386. }