src/Controller/Front/HomePageController.php line 70

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