src/Service/FrontService.php line 92

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