src/Controller/Front/HomePageController.php line 67

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