src/Service/FrontService.php line 116

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