src/Service/FrontService.php line 123

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