src/Service/Internal/HomePageService.php line 174

Open in your IDE?
  1. <?php
  2. namespace App\Service\Internal;
  3. use App\Event\ModeEvent;
  4. use App\Manager\AccountManager;
  5. use App\Security\KasUserAuthenticator;
  6. use App\Service\Internal\User\PreferenceService;
  7. use App\Service\Internal\Schedule\FavoriteService;
  8. use App\Manager\BookingSecureBikeParkManager;
  9. use App\Manager\InteractiveMapManager;
  10. use App\Manager\DisruptionManager;
  11. use App\Normalizer\DisruptionNormalizer;
  12. use App\Normalizer\LineNormalizer;
  13. use App\Normalizer\UserPreferencesNormalizer;
  14. use App\Normalizer\Schedule\FavoriteNormalizer as ScheduleFavoriteNormalizer;
  15. use App\Normalizer\Line\FavoriteNormalizer as LineFavoriteNormalizer;
  16. use App\Utils\DeviceInfo;
  17. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\HttpFoundation\RequestStack;
  20. use Symfony\Component\Security\Core\Security;
  21. class HomePageService
  22. {
  23. public const TRANSITPASS_KEY = 'TRANSITPASS';
  24. protected string $networkId;
  25. protected array $network;
  26. protected string $ticketingModule;
  27. protected string $geoserverUrl;
  28. protected PreferenceService $preferenceService;
  29. protected UserPreferencesNormalizer $userPreferencesNormalizer;
  30. protected NetworkService $networkService;
  31. protected UserService $userService;
  32. protected DisruptionService $disruptionService;
  33. protected DisruptionNormalizer $disruptionNormalizer;
  34. protected FavoriteService $favoriteService;
  35. protected CmsService $cmsService;
  36. protected ScheduleFavoriteNormalizer $scheduleFavoriteNormalizer;
  37. protected LineFavoriteNormalizer $lineFavoriteNormalizer;
  38. protected MetaService$metaService;
  39. protected LocalizationService $localizationService;
  40. protected RequestStack $requestStack;
  41. protected Security $security;
  42. protected LineNormalizer $lineNormalizer;
  43. protected BookingSecureBikeParkManager $bookingSecureBikeParkManager;
  44. protected EventDispatcherInterface $eventDispatcher;
  45. protected InteractiveMapManager $interactiveMapManager;
  46. protected ParameterBagInterface $params;
  47. protected DisruptionManager $disruptionManager;
  48. protected Line\FavoriteService $lineFavoriteService;
  49. protected AccountManager $accountManager;
  50. public function __construct(
  51. string $networkId,
  52. string $ticketingModule,
  53. string $geoserverUrl,
  54. PreferenceService $preferenceService,
  55. UserPreferencesNormalizer $userPreferencesNormalizer,
  56. NetworkService $networkService,
  57. UserService $userService,
  58. DisruptionService $disruptionService,
  59. DisruptionNormalizer $disruptionNormalizer,
  60. Schedule\FavoriteService $favoriteService,
  61. Line\FavoriteService $lineFavoriteService,
  62. ScheduleFavoriteNormalizer $scheduleFavoriteNormalizer,
  63. LineFavoriteNormalizer $lineFavoriteNormalizer,
  64. CmsService $cmsService,
  65. MetaService $metaService,
  66. LocalizationService $localizationService,
  67. RequestStack $requestStack,
  68. Security $security,
  69. LineNormalizer $lineNormalizer,
  70. BookingSecureBikeParkManager $bookingSecureBikeParkManager,
  71. EventDispatcherInterface $eventDispatcher,
  72. InteractiveMapManager $interactiveMapManager,
  73. AccountManager $accountManager,
  74. ParameterBagInterface $parameterBag,
  75. DisruptionManager $disruptionManager
  76. ) {
  77. $this->networkId = $networkId;
  78. $this->ticketingModule = $ticketingModule;
  79. $this->geoserverUrl = $geoserverUrl;
  80. $this->preferenceService = $preferenceService;
  81. $this->userPreferencesNormalizer = $userPreferencesNormalizer;
  82. $this->disruptionNormalizer = $disruptionNormalizer;
  83. $this->networkService = $networkService;
  84. $this->userService = $userService;
  85. $this->disruptionService = $disruptionService;
  86. $this->favoriteService = $favoriteService;
  87. $this->lineFavoriteService = $lineFavoriteService;
  88. $this->scheduleFavoriteNormalizer = $scheduleFavoriteNormalizer;
  89. $this->lineFavoriteNormalizer = $lineFavoriteNormalizer;
  90. $this->cmsService = $cmsService;
  91. $this->metaService = $metaService;
  92. $this->localizationService = $localizationService;
  93. $this->requestStack = $requestStack;
  94. $this->security = $security;
  95. $this->lineNormalizer = $lineNormalizer;
  96. $this->bookingSecureBikeParkManager = $bookingSecureBikeParkManager;
  97. $this->eventDispatcher = $eventDispatcher;
  98. $this->interactiveMapManager = $interactiveMapManager;
  99. $this->accountManager = $accountManager;
  100. $this->params = $parameterBag;
  101. $this->disruptionManager = $disruptionManager;
  102. $this->network = $this->networkService->getNetwork();
  103. }
  104. /**
  105. * @return array
  106. * @throws \Exception
  107. */
  108. public function getRenderParameters(array $get = [], array $parameters = [])
  109. {
  110. //TODO: Temporaire, à supprimer quand la conf par réseau et environnement sera externalisée
  111. $parameters['baseUrl'] = 'https://' . $_SERVER['SERVER_NAME'];
  112. $parameters = DeviceInfo::addDeviceParameters($parameters);
  113. $session = $this->requestStack->getSession();
  114. $body = $get;
  115. $nbDayFuture = $this->networkService->getNbDayFuture();
  116. $maxDate = new \DateTime('now');
  117. $maxDate = $maxDate->add(new \DateInterval('P'.$nbDayFuture.'D'));
  118. $parameters['maxDate'] = $maxDate->format('Y-d-m');
  119. $parameters['networkId'] = isset($this->networkId) ? $this->networkId : '';
  120. $parameters['default_latlon'] = $this->networkService->getLatLon();
  121. $parameters['network_lat'] = $this->networkService->getLat();
  122. $parameters['network_lon'] = $this->networkService->getLon();
  123. $parameters['external'] = $this->networkService->isExternalNetwork();
  124. $parameters['operators'] = json_encode($this->networkService->getOperatorInfos());
  125. $parameters['geoserverUrl'] = isset($this->geoserverUrl) ? $this->geoserverUrl : '';
  126. $parameters['journeys'] = isset($parameters['journeys']) ? $parameters['journeys'] : [];
  127. $parameters['directions'] = isset($parameters['directions']) ? $parameters['directions'] : [];
  128. $parameters['stopPoints'] = isset($parameters['stopPoints']) ? $parameters['stopPoints'] : [];
  129. $parameters['languages'] = $this->localizationService->getLanguages();
  130. $parameters['bookingIsEnabled'] = $this->bookingSecureBikeParkManager->isEnabled($this->network);
  131. $parameters['fromSEO'] = preg_match('/(google|yahoo|bing|qwant)/i', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '') === 1;
  132. $parameters['fromWidget'] = $session->get('context', null) === 'WIDGET';
  133. $parameters['referer'] = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
  134. if($parameters['fromWidget']){
  135. $request = $this->requestStack->getCurrentRequest();
  136. $url = $request->headers->get('referer') ?? $request->getSchemeAndHttpHost();
  137. $parameters['isAuthorizedDomainForBookingSbp'] = $this->bookingSecureBikeParkManager->isAuthorizedDomain($url);
  138. }
  139. $layoutScheduleLine = $this->networkService->getLayoutScheduleLine();
  140. $session->set('layoutScheduleLine', $layoutScheduleLine);
  141. if (empty($parameters['activeTab'])) {
  142. $parameters['activeTab'] = 'journey';
  143. }
  144. if (!isset($parameters['displayDisruptionWithAjax']) || false === $parameters['displayDisruptionWithAjax'] || $parameters['activeTab'] == 'traffic') {
  145. $this->getDisruption($parameters);
  146. }
  147. if ($parameters['activeTab'] == 'interactiveMap') {
  148. $poi = $this->requestStack->getCurrentRequest()->get('poi');
  149. $parameters['poi'] = explode(',', $poi);
  150. $interactiveMapData = $this->interactiveMapManager->interactiveMapData();
  151. $parameters = array_merge($parameters, $interactiveMapData);
  152. }
  153. $userPreferences = [];
  154. if ($this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  155. $userPreferences = $this->preferenceService->getUserPreferences();
  156. $parameters = $this->accountManager->getRenderParameters($parameters);
  157. }
  158. $context['itineraryOptions'] = $this->networkService->getItineraryOptions();
  159. if ($this->params->get('global.login_mode') === KasUserAuthenticator::LOGIN_MODE
  160. && $this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  161. $settings = $this->accountManager->getItineraryPreferences();
  162. $parameters['preferences']['tripPlanner'] = $settings->body;
  163. $parameters['preferences'] = $this->userPreferencesNormalizer->normalize(json_decode(json_encode($parameters['preferences'] ?? []), true), $context);
  164. } else {
  165. $parameters['preferences'] = $this->userPreferencesNormalizer->normalize($userPreferences, $context, true);
  166. }
  167. $parameters['isTransitPassEnabled'] = in_array(self::TRANSITPASS_KEY, $context['itineraryOptions'], true);
  168. $includedModes = $body['modes'] ?? null;
  169. $excludedModesPreferences = !empty($parameters['preferences']['tripPlanner']['excludedModes']) ? $parameters['preferences']['tripPlanner']['excludedModes'] : [];
  170. $modeEvent = new ModeEvent($includedModes, $excludedModesPreferences);
  171. $this->eventDispatcher->dispatch($modeEvent, ModeEvent::NAME);
  172. $parameters['modes'] = $modeEvent->getDefaultModes();
  173. $parameters['modesLayout'] = $modeEvent->getLayoutModes();
  174. $scheduleFavorites = [];
  175. $parameters['lineFavorites'] = [];
  176. if ($this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  177. $scheduleFavorites = $this->favoriteService->getScheduleFavorites();
  178. $scheduleFavorites = $this->scheduleFavoriteNormalizer->normalize($scheduleFavorites);
  179. $userResponse = $this->userService->myAccount();
  180. if (isset($userResponse->body) && isset($userResponse->status) && $userResponse->status === 200) {
  181. $parameters['userData'] = $userResponse->body;
  182. }
  183. $parameters['lineFavorites'] = $this->lineFavoriteService->getFavoriteLines([]);
  184. $parameters['lineFavorites'] = $this
  185. ->lineFavoriteNormalizer
  186. ->normalize($parameters['lineFavorites'], $parameters['linesDisruptions'] ?? []);
  187. }
  188. $parameters['scheduleFavorites'] = isset($scheduleFavorites) ? $scheduleFavorites : [];
  189. $parameters['activeWidgets'] = [
  190. 'journey' => 1,
  191. 'schedule' => 1,
  192. 'traffic' => 1,
  193. 'linesAndPoi' => 1
  194. ];
  195. if (!isset($parameters['departurePlace'])) {
  196. $parameters['departurePlace']['id'] = '';
  197. $parameters['departurePlace']['value'] = '';
  198. $parameters['departurePlace']['latlon'] = '';
  199. $parameters['departurePlace']['latitude'] = '';
  200. $parameters['departurePlace']['longitude'] = '';
  201. }
  202. if (!isset($parameters['arrivalPlace'])) {
  203. $parameters['arrivalPlace']['id'] = '';
  204. $parameters['arrivalPlace']['value'] = '';
  205. $parameters['arrivalPlace']['latlon'] = '';
  206. $parameters['arrivalPlace']['latitude'] = '';
  207. $parameters['arrivalPlace']['longitude'] = '';
  208. }
  209. $menu = $this->cmsService->getMenu();
  210. $parameters['menuPages'] = $menu['menuPages'];
  211. $parameters['footerPages'] = $menu['footerPages'];
  212. $parameters['items'] = $menu['items'];
  213. $banners = $this->cmsService->getBanners();
  214. foreach($banners as $banner) {
  215. $banner->id = str_replace(':', '-', $banner->id);
  216. }
  217. $launchNews = $this->cmsService->getLaunchNews();
  218. $parameters['banners'] = $banners;
  219. $parameters['launchNews'] = $launchNews;
  220. $ids = [];
  221. foreach ($launchNews as $popupPost) {
  222. $ids[] = $popupPost->id;
  223. }
  224. $parameters['launchNewsId'] = isset($launchNews[0]->id) ? $launchNews[0]->id : '';
  225. $parameters['ticketingModule'] = $this->ticketingModule;
  226. $posts = $this->cmsService->getPosts();
  227. $parameters['posts'] = $posts;
  228. // Preferences
  229. $speedProfiles = [0 => 'SLOW', 1 => 'NORMAL', 2 => 'FAST', 3 => 'VTT', 4 => 'VAE'];
  230. $parameters['preferences']['tripPlanner']['excludedModes'] = $modeEvent->getExcludedModes();
  231. $parameters['preferences']['tripPlanner']['showAccessibility'] = isset($body['showAccessibility']) ? filter_var($body['showAccessibility'], FILTER_VALIDATE_BOOLEAN) : $parameters['preferences']['tripPlanner']['showAccessibility'];
  232. $parameters['preferences']['tripPlanner']['wheelchairAccessible'] = isset($body['accessible']) ? filter_var($body['accessible'], FILTER_VALIDATE_BOOLEAN) : $parameters['preferences']['tripPlanner']['wheelchairAccessible'];
  233. $parameters['preferences']['tripPlanner']['walkProfile'] = isset($body['walk']['speed']) ? $speedProfiles[$body['walk']['speed']] : $parameters['preferences']['tripPlanner']['walkProfile'];
  234. $parameters['preferences']['tripPlanner']['bikeProfile'] = isset($body['bike']['speed']) ? $speedProfiles[$body['bike']['speed']] : $parameters['preferences']['tripPlanner']['bikeProfile'];
  235. $this->generateMetas($parameters);
  236. return $parameters;
  237. }
  238. /**
  239. * @param array $parameters
  240. * @throws \Exception
  241. */
  242. public function getDisruption(array &$parameters = [])
  243. {
  244. $currentSubNetworksDisruptions = 0;
  245. $futureSubNetworksDisruptions = 0;
  246. $disruptionLayout = $this->networkService->getDisruptionLayout();
  247. $subNetworks = $this->networkService->getSubNetworks();
  248. $isSubNetworks = $parameters['issubnetworks'] ?? '';
  249. $currentDisruptions = $this->disruptionService->getDisruptions($isSubNetworks, DisruptionNormalizer::STATUS_ONGOING);
  250. $futureDisruptions = $this->disruptionService->getDisruptions($isSubNetworks, DisruptionNormalizer::STATUS_COMING);
  251. $parameters['nbSubNetworks'] = count($subNetworks) ?? 0;
  252. $parameters['currentDisruptions'] = $currentDisruptions['disruptions'] ?? [];
  253. $parameters['futureDisruptions'] = $futureDisruptions['disruptions'] ?? [];
  254. $withDisruptionOnly = null;
  255. if (in_array("FAV_WITH_DISRUPTIONS_BOARDS_LINES", $disruptionLayout)) {
  256. $withDisruptionOnly = 0;
  257. } elseif (in_array("FAV_WITH_DISRUPTIONS_BOARDS_LINES_WITHDISRUPTIONSONLY", $disruptionLayout)) {
  258. $withDisruptionOnly = 1;
  259. }
  260. if ($withDisruptionOnly !== null) {
  261. $parameters['currentDisruptions']['subNetworks'] = [];
  262. $parameters['futureDisruptions']['subNetworks'] = [];
  263. } else {
  264. $parameters['currentDisruptions']['subNetworks'] = $this->disruptionManager->getSubNetworksDisruptions($currentDisruptions, 'currents',$isSubNetworks);
  265. $parameters['futureDisruptions']['subNetworks'] = $this->disruptionManager->getSubNetworksDisruptions($futureDisruptions, 'futures', $isSubNetworks);
  266. $currentSubNetworksDisruptions = $this->getDisruptionsCountForSubNetworkOrLine($parameters['currentDisruptions']['subNetworks'], 'currents');
  267. $futureSubNetworksDisruptions = $this->getDisruptionsCountForSubNetworkOrLine($parameters['futureDisruptions']['subNetworks'], 'futures');
  268. }
  269. $parameters['currentDisruptions']['lines'] = $this->disruptionManager->getLinesDisruptions($currentDisruptions, 'currents');
  270. $parameters['futureDisruptions']['lines'] = $this->disruptionManager->getLinesDisruptions($futureDisruptions, 'futures');
  271. $parameters['currentDisruptions']['stopPoints'] = $this->disruptionManager->getStopPointsDisruptions($currentDisruptions, 'currents');
  272. $parameters['futureDisruptions']['stopPoints'] = $this->disruptionManager->getStopPointsDisruptions($futureDisruptions, 'futures');
  273. $parameters['currentDisruptions']['general'] = $this->disruptionManager->getGeneralDisruptions($currentDisruptions, 'currents');
  274. $parameters['futureDisruptions']['general'] = $this->disruptionManager->getGeneralDisruptions($futureDisruptions, 'futures');
  275. if ($this->security->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  276. $parameters['lineFavorites'] = $this->lineFavoriteService->getFavoriteLines([]);
  277. $parameters['lineFavorites'] = $this
  278. ->lineFavoriteNormalizer
  279. ->normalize($parameters['lineFavorites'], $parameters['linesDisruptions'] ?? []);
  280. }
  281. $parameters['withDisruptionOnly'] = $withDisruptionOnly;
  282. $parameters['currentSubNetworksDisruptions'] = $currentSubNetworksDisruptions;
  283. $parameters['futureSubNetworksDisruptions'] = $futureSubNetworksDisruptions;
  284. }
  285. /**
  286. * @param array $subNetworks
  287. * @return int Number of disruptions on a subnetwork
  288. */
  289. protected function getDisruptionsCountForSubNetworkOrLine(array $subNetworks, string $status) {
  290. return array_reduce($subNetworks, function ($disruptionsCount, $subNetwork) use ($status) {
  291. if ($this->hasDisruptionsForSubnetworkOrLine($subNetwork, $status)) {
  292. return $disruptionsCount + 1;
  293. }
  294. return $disruptionsCount;
  295. }, 0);
  296. }
  297. /**
  298. * @param array $subNetworkData
  299. * @return bool true if the subnetwork or one of its lines has disruptions
  300. */
  301. protected function hasDisruptionsForSubnetworkOrLine(array $subNetworkData, string $status): bool
  302. {
  303. $hasSubnetworkDisruption = !empty($subNetworkData['disruptions'][$status]);
  304. $hasLineDisruption = !empty($subNetworkData['disruptions']['lines'][$status]);
  305. return $hasSubnetworkDisruption || $hasLineDisruption;
  306. }
  307. protected function generateMetas(array &$params)
  308. {
  309. $params['page_title'] = $this->metaService->getPageTitle($params);
  310. $params['enable_no_index'] = $this->metaService->enableNoIndex();
  311. $params['page_keywords'] = $this->metaService->getPageKeywords($params);
  312. $params['page_description'] = $this->metaService->getPageDescription($params);
  313. }
  314. }