src/Controller/Front/HomePageController.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Constants\Platform;
  4. use App\Constants\Setting as ConstSetting;
  5. use App\Entity\User;
  6. use App\Services\Common\Point\UserPointService;
  7. use App\Services\Common\PlatformService;
  8. use App\Services\Common\Point\UserPointServiceInterface;
  9. use App\Services\Common\SettingService;
  10. use App\Services\Common\User\WorkflowUser;
  11. use App\Services\Portal\PortalService;
  12. use App\Services\Security\EncryptionManager;
  13. use DateInterval;
  14. use DateTime;
  15. use Doctrine\ORM\EntityManagerInterface;
  16. use Exception;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\HttpFoundation\Response;
  20. use Symfony\Component\HttpKernel\KernelInterface;
  21. class HomePageController extends AbstractController
  22. {
  23. private UserPointServiceInterface $userPointService;
  24. private EntityManagerInterface $em;
  25. private PlatformService $platformService;
  26. private WorkflowUser $workflowUser;
  27. private PortalService $portalService;
  28. private SettingService $settingService;
  29. private KernelInterface $kernel;
  30. private EncryptionManager $encryptionManager;
  31. /**
  32. * @throws Exception
  33. */
  34. public function __construct(EntityManagerInterface $em, PlatformService $platformService, WorkflowUser $workflowUser, UserPointService $userPointService, PortalService $portalService, SettingService $settingService, KernelInterface $kernel, EncryptionManager $encryptionManager)
  35. {
  36. $this->userPointService = $userPointService;
  37. $this->em = $em;
  38. $this->platformService = $platformService;
  39. $this->workflowUser = $workflowUser;
  40. $this->portalService = $portalService;
  41. $this->settingService = $settingService;
  42. $this->kernel = $kernel;
  43. $this->encryptionManager = $encryptionManager;
  44. }
  45. /**
  46. * Page d'accueil pour les utilisateurs identifiés
  47. *
  48. * @param Request $request
  49. *
  50. * @return Response
  51. *
  52. * @throws Exception
  53. */
  54. public function show(Request $request): Response
  55. {
  56. if($this->platformService->getType() === 'dtv')
  57. {
  58. return $this->redirectToRoute('dtv_back_dashboard_show');
  59. }
  60. /** @var User $currentUser */
  61. $currentUser = $this->getUser();
  62. $t = $this->userPointService->getLevel($currentUser);
  63. $totalMovement = [
  64. "debit" => $this->userPointService->getSpendedPoints($currentUser),
  65. "credit" => $this->userPointService->getFidelityPoints($currentUser),
  66. 'expired' => $this->userPointService->getExpiredPoints($currentUser),
  67. 'exceptional' => $this->userPointService->getExceptionalPoints($currentUser),
  68. ];
  69. if($request->getMethod() === 'POST')
  70. {
  71. $choice = $request->request->get('donneesPersonnellesChoice');
  72. $currentUser->setDonneesPersonnelles($choice);
  73. if($choice === FALSE)
  74. {
  75. $this->workflowUser->unsubscribeUser($currentUser);
  76. return $this->redirectToRoute('universal_logout');
  77. }
  78. $this->em->flush();
  79. }
  80. $data = ['totalPoint' => $totalMovement, 't' => $t];
  81. if($this->platformService->getType() === Platform::PORTAIL)
  82. {
  83. $platforms = $this->settingService->getValueFromName(ConstSetting::PORTAL_PARENT);
  84. $listToClose = $listWithoutGroup = $listGroups = [];
  85. $hasGroups = true;
  86. $listWithAlert = [];
  87. $now = new DateTime();
  88. $now30days = new DateTime();
  89. $now30days->add(new DateInterval("P30D"));
  90. if(isset($platforms['children']))
  91. {
  92. foreach($platforms['children'] as $key => $child)
  93. {
  94. if(!$currentUser->isDeveloperOrSuperAdmin())
  95. {
  96. $continue = true;
  97. if(!empty($child['roles']))
  98. {
  99. foreach($currentUser->getRoles() as $role)
  100. {
  101. if(in_array($role, $child['roles']))
  102. {
  103. $continue = false;
  104. break;
  105. }
  106. }
  107. // controle strict sur le role
  108. // if($continue) continue;
  109. }
  110. if($continue && !empty($child['jobs']) && !in_array($currentUser->getJob(), $child['jobs'])) continue;
  111. }
  112. $platformInfo = $this->portalService->fetchChildrenPlatformFromApi($currentUser, $child['url']);
  113. if($platformInfo->getStatusCode() === Response::HTTP_INTERNAL_SERVER_ERROR)
  114. {
  115. $listWithAlert[] = [
  116. 'url' => $child['url'],
  117. 'code' => $platformInfo->getStatusCode(),
  118. 'content' => json_decode($platformInfo->getContent(), TRUE) ?? $platformInfo->getContent(),
  119. ];
  120. continue;
  121. }
  122. $response = json_decode($platformInfo->getContent(), TRUE);
  123. $response = $response['content'];
  124. $response['platform']['roles'] = $child['roles'];
  125. $response['platform']['jobs'] = $child['jobs'];
  126. $startDate = $response['platform']['startDate'] ?? null;
  127. $endDate = $response['platform']['endDate'] ?? null;
  128. if(!empty($endDate)) $endDate = new DateTime($endDate);
  129. if(!empty($startDate)) $startDate = new DateTime($startDate);
  130. // On ne prend pas en compte les plateformes fermées
  131. if(!$currentUser->isDeveloperOrSuperAdmin() && isset($response['platform']['enabled']) && $response['platform']['enabled'] === "0")
  132. {
  133. continue;
  134. }
  135. // On ne prend pas en compte dont la date est inferieur à la date d'ouverture de la plateforme
  136. if(!$currentUser->isDeveloperOrSuperAdmin() && $startDate && $now < $startDate) continue;
  137. // On ne prend pas en compte dont la date de fin est dépassée
  138. if(!$currentUser->isDeveloperOrSuperAdmin() && $endDate && $now > $endDate) continue;
  139. // On autorise l'accès par défaut au référent pour tous les users, sinon on autorise par défaut les admins, superadmins et développeurs
  140. if($child['referent'] || $currentUser->isAdmin() || $currentUser->isDeveloperOrSuperAdmin())
  141. {
  142. $response['platform']['canAccess'] = TRUE;
  143. }
  144. $response['platform']['referent'] = $child['referent'];
  145. $childUrl = base64_encode($child['url']);
  146. $response['platform']['login_url'] = $this->generateUrl('create_user_on_children_via_api', [
  147. 'child' => $childUrl,
  148. 'userNotFound' => $response['platform']['userNotFound'] ?? FALSE,
  149. ]);
  150. $groupe = $child['groupe'];
  151. if(empty($groupe))
  152. {
  153. $groupe = 'Autre Plateformes';
  154. $platforms['children'][$key]['groupe'] = $groupe;
  155. }
  156. $listGroups[$groupe][] = $response;
  157. $listWithoutGroup[] = $response;
  158. if($endDate < $now30days) $listToClose[] = $response;
  159. }
  160. }
  161. $list = $listGroups;
  162. $this->orderPlatform($listWithoutGroup);
  163. if(count($listGroups) === 1)
  164. {
  165. $list = $listWithoutGroup;
  166. $hasGroups = false;
  167. }
  168. if($hasGroups)
  169. {
  170. foreach($list as $groupe => $plts)
  171. {
  172. $this->orderPlatform($plts);
  173. $list[$groupe] = $plts;
  174. }
  175. if(!$currentUser->isDeveloperOrSuperAdmin())
  176. {
  177. uasort($list, function($groupeA, $groupeB) use ($currentUser)
  178. {
  179. $scoreA = $scoreB = 0;
  180. $this->scoreGroup($groupeA, $scoreA, $currentUser);
  181. $this->scoreGroup($groupeB, $scoreB, $currentUser);
  182. if($scoreA === $scoreB) return 0;
  183. return ($scoreA < $scoreB) ? 1 : -1;
  184. });
  185. }
  186. }
  187. $data['portailData']['list'] = $list;
  188. $data['portailData']['listWithoutGroup'] = $listWithoutGroup;
  189. $data['portailData']['listWithAlert'] = $listWithAlert;
  190. $data['portailData']['listToClose'] = $listToClose;
  191. $data['portailData']['hasGroups'] = $hasGroups;
  192. }
  193. return $this->render('front/homepage/show.html.twig', $data);
  194. }
  195. private function scoreGroup(array $group, int &$score, User $user)
  196. {
  197. foreach($group as $plateforme)
  198. {
  199. $plateforme = $plateforme['platform'];
  200. if(!empty($plateforme['roles']))
  201. {
  202. foreach($user->getRoles() as $role)
  203. {
  204. if(in_array($role, $plateforme['roles']))
  205. {
  206. $score++;
  207. }
  208. }
  209. }
  210. if(!empty($plateforme['jobs']) && in_array($user->getJob(), $plateforme['jobs'])) $score += 2;
  211. }
  212. }
  213. /**
  214. * Ordonne la liste des enfants du portail, indique si la plateforme est référente, retourne la liste des plateformes fermant dans les 30j ours
  215. *
  216. * @param $platforms
  217. *
  218. * @return array
  219. */
  220. private function orderPlatform(&$platforms): array
  221. {
  222. // Fonction de comparaison pour le tri
  223. usort($platforms, static function($a, $b)
  224. {
  225. // Si l'objet a la clé "referent" à TRUE, le placer en première position
  226. if(isset($a['platform']['referent']) && $a['platform']['referent'] === TRUE)
  227. {
  228. return -1;
  229. }
  230. if(isset($b['platform']['referent']) && $b['platform']['referent'] === TRUE)
  231. {
  232. return 1;
  233. }
  234. // Sinon, trier par startDate
  235. if(isset($a['platform']['startDate']) && isset($b['platform']['startDate']))
  236. {
  237. return strtotime($a['platform']['startDate']) - strtotime($b['platform']['startDate']);
  238. }
  239. return -1;
  240. });
  241. // Obtenez la date actuelle
  242. $currentDate = time();
  243. $list = [];
  244. // Parcoure le tableau trié
  245. foreach($platforms as $object)
  246. {
  247. if(isset($object['platform']['endDate']))
  248. {
  249. // Convertissez la date de fin en timestamp
  250. $endDateTimestamp = strtotime($object['platform']['endDate']);
  251. // Vérifiez si la date de fin est inférieure à 30 jours de la date actuelle
  252. if($endDateTimestamp < ($currentDate + (30 * 24 * 60 * 60)))
  253. {
  254. $list[] = $object;
  255. }
  256. }
  257. }
  258. if(isset($a['platform']['endDate']) && isset($b['platform']['endDate']))
  259. {
  260. usort($list, static function($a, $b)
  261. {
  262. return strtotime($a['platform']['endDate']) - strtotime($b['platform']['endDate']);
  263. });
  264. }
  265. return $list;
  266. }
  267. }