src/Service/FrontService.php line 114

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