src/Controller/EntityController.php line 55

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Api\SalesforceRest\Controllers\ApiAccountController;
  4. use App\Api\SalesforceRest\Controllers\ApiContactController;
  5. use App\Api\SalesforceRest\Controllers\ApiCrmController;
  6. use App\Dto\DtoBrand;
  7. use App\Dto\DtoNavigationContext;
  8. use App\Dto\Enum\EnumHttpCode;
  9. use App\Dto\Request\DtoGetNotViewedPushNotificationsRequest;
  10. use App\Dto\Response\DtoResponse;
  11. use App\Entity\AccountType;
  12. use App\Entity\Article;
  13. use App\Entity\Contact;
  14. use App\Entity\ContactProductPrice;
  15. use App\Entity\ContentOnDemand;
  16. use App\Entity\InstructorRequest;
  17. use App\Entity\Referent;
  18. use App\Entity\SfBrand;
  19. use App\Entity\SfProduct;
  20. use App\Helper\ObjectConversionHelper;
  21. use App\Helper\StringHelper;
  22. use App\Security\ConnectAsAuthenticator;
  23. use App\Service\LanguageManager;
  24. use App\Service\NotificationManager;
  25. use App\Service\NotificationService;
  26. use Doctrine\ORM\EntityManagerInterface;
  27. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  28. use Symfony\Component\HttpFoundation\JsonResponse;
  29. use Symfony\Component\HttpFoundation\Request;
  30. use Symfony\Component\HttpFoundation\RequestStack;
  31. use Symfony\Component\Routing\RouterInterface;
  32. use Symfony\Component\Security\Core\Security;
  33. use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
  34. class EntityController extends AbstractController
  35. {
  36.     protected EntityManagerInterface $entityManager;
  37.     protected NotificationManager $notificationManager;
  38.     protected string $language;
  39.     protected LanguageManager $languageManager;
  40.     protected array $viewData;
  41.     protected RequestStack $request;
  42.     /**
  43.      * EntityController constructor.
  44.      * @param EntityManagerInterface $entityManager
  45.      * @param NotificationManager $notificationManager
  46.      * @param RequestStack $request
  47.      * @param LanguageManager $languageManager
  48.      */
  49.     public function __construct(EntityManagerInterface $entityManager,
  50.                                 NotificationManager $notificationManager,
  51.                                 RequestStack $requestStack,
  52.                                 LanguageManager $languageManager,
  53.                                 NotificationService $notificationService)
  54. //<<<<<<< HEAD
  55. //                                RequestStack $request,
  56. //                                LanguageManager $languageManager,
  57. //                                SessionInterface $session,
  58. //                                NotificationService $notificationService)
  59. //=======
  60. //                                RequestStack $requestStack,
  61. //                                LanguageManager $languageManager)
  62. //>>>>>>> 92-achat-unique
  63.     {
  64.         $this->entityManager = $entityManager;
  65.         $this->notificationManager = $notificationManager;
  66.         $this->language = $this->getLanguage($requestStack);
  67.         $this->languageManager = $languageManager;
  68.         $this->currentSessionStack = $requestStack->getSession();
  69.         $this->viewData = [];
  70.         $accounts = [];
  71.         $contactCsrfToken = null;
  72.         $lightContact = null;
  73.         // Set all accounts in session then whe can get em everywhere
  74.         $currentAccount = null;
  75.         if(!is_null($contact = $this->currentSessionStack->get('contact'))){
  76.             $accounts = $contact->getAccounts();
  77.             foreach($accounts as $account) {
  78.                 if($account->getSalesforceId() == $requestStack->getCurrentRequest()->get('salesforceAccountId')) {
  79.                     $currentAccount = $account;
  80.                     $this->viewData["accountId"] = $account->getSalesforceId();
  81.                 }
  82.             }
  83.             if(isset($currentAccount) && $currentAccount->getType()->getName() == AccountType::ACCOUNT_TYPES['PersonAccount']) {
  84.                 $clubsLinkedToCurrentInstructor = ApiAccountController::getAccountClubs($currentAccount->getSalesforceId());
  85.                 if(empty($clubsLinkedToCurrentInstructor)) {
  86.                     $instructorIsLinkedToAClub = false;
  87.                 } else {
  88.                     $instructorIsLinkedToAClub = true;
  89.                 }
  90.                 $this->viewData['instructorIsLinkedToAClub'] = $instructorIsLinkedToAClub;
  91.             }
  92.             $lightContact = $contact->cloneLightContact();
  93.         }
  94.         if(!is_null($this->currentSessionStack->get('contactCsrfToken'))){
  95.             $contactCsrfToken = $this->currentSessionStack->get('contactCsrfToken');
  96.         }
  97.         $instructorRequests = $this->entityManager->getRepository(InstructorRequest::class)->findBy([
  98.             'clubSFId' => $requestStack->getCurrentRequest()->get('salesforceAccountId')
  99.         ]);
  100.         if($instructorRequests) {
  101.             $countNotification = 0;
  102.             foreach($instructorRequests as $instructorRequest) {
  103.                 if($instructorRequest->getClubNotification() == true) {
  104.                     $countNotification++;
  105.                 }
  106.             }
  107.             $this->viewData['clubNotification'] = $countNotification;
  108.         }
  109.         if($this->language == 'it') {
  110.             $isItalian = 1;
  111.         } else {
  112.             $isItalian = 0;
  113.         }
  114.         $isHbx = false;
  115.         if($this->currentSessionStack->get('contact') != null) {
  116. //            get account access rights (for blog)
  117.             $accessLesMills = $this->currentSessionStack->get('accessLesMills');
  118.             $accessAqua = $this->currentSessionStack->get('accessAqua');
  119.             $accessHBXTT = $this->currentSessionStack->get('accessHBXTT');
  120.             $accessHBX = $this->currentSessionStack->get('accessHBX');
  121. //            set account access rights for HBX
  122.             $accessLesMills == true ? $isLesmills = true : $isLesmills = false;
  123.             $accessAqua == true ? $isAqua = true : $isAqua = false;
  124.             if($accessHBX == true || $accessHBXTT == true) {
  125.                 $isHbx = true;
  126.             } else {
  127.                 $isHbx = false;
  128.             }
  129. //          get articles not viewed by current contact for each article's access rights
  130.             $articlesNotViewed = $this->entityManager->getRepository(Article::class)->findArticlesNotViewed($this->entityManager, $this->currentSessionStack->get('contact')->getId(), $isItalian);
  131.             $articlesNotViewedLesmills = $this->entityManager->getRepository(Article::class)->findArticlesNotViewedLesmills($this->entityManager, $this->currentSessionStack->get('contact')->getId(), $isItalian, $isLesmills);
  132.             $articlesNotViewedHBX = $this->entityManager->getRepository(Article::class)->findArticlesNotViewedHBX($this->entityManager, $this->currentSessionStack->get('contact')->getId(), $isItalian, $isHbx);
  133.             $articlesNotViewedAqua = $this->entityManager->getRepository(Article::class)->findArticlesNotViewedAqua($this->entityManager, $this->currentSessionStack->get('contact')->getId(), $isItalian, $isAqua);
  134. //            array of array with all the article and the 3 access rights articles
  135.             $allArticles = [];
  136. //            we'll fill this array with all articles that current account can read (can contain duplicated articles)
  137.             $articlesArray = [];
  138. //            set every articles into an array
  139.             array_push($allArticles, $articlesNotViewed);
  140.             array_push($allArticles, $articlesNotViewedLesmills);
  141.             array_push($allArticles, $articlesNotViewedHBX);
  142.             array_push($allArticles, $articlesNotViewedAqua);
  143.             if(!empty($allArticles)) {
  144.                 foreach ($allArticles as $articles) {
  145.                     foreach($articles as $article) {
  146. //                        for all articles check if account as right on them
  147.                         if(($isLesmills == false && $article->getIsLesmills() == true) ||
  148.                             ($isAqua == false && $article->getIsAqua() == true) ||
  149.                             ($isHbx == false && $article->getIsHbx() == true)
  150.                         ) {
  151.                         } else {
  152. //                            if currentAccount can read an article, put it in an array
  153.                             $articlesArray[] = $article;
  154.                         }
  155.                     }
  156.                 }
  157.             }
  158.             // Récupération des alertes
  159.             $dtoGetNotViewedPushNotifications = new DtoGetNotViewedPushNotificationsRequest();
  160.             $dtoGetNotViewedPushNotifications->contactId = $contact->getId();
  161.             if ($currentAccount != null)
  162.             {
  163.                 $dtoGetNotViewedPushNotifications->salesforceAccountId = $currentAccount->getSalesforceId();
  164.             }
  165.             $dtoGetNotViewedPushNotifications->isAqua = $isAqua;
  166.             $dtoGetNotViewedPushNotifications->isLesmills = $isLesmills;
  167.             $dtoGetNotViewedPushNotifications->isHbx = $isHbx;
  168.             $dtoGetNotViewedPushNotifications->languageCode = isset($this->language) ? strtoupper($this->language) : "FR";
  169.             $dtoPusNotificationResponse = $notificationService->getNotViewedPushNotifications($dtoGetNotViewedPushNotifications);
  170.             $this->viewData['notViewedPushNotifications'] = $dtoPusNotificationResponse->dtoPushNotifications;
  171. //            array of all articles that current account can read (without duplicate articles)
  172.             $allArticlesNotViewed = array_unique($articlesArray);
  173.             $this->viewData['articlesNotViewed'] = count($allArticlesNotViewed);
  174.             $this->viewData['notViewedPushNotifications'] = $dtoPusNotificationResponse->dtoPushNotifications;
  175.             $this->viewData['isHbx'] = $isHbx;
  176.             $this->viewData['isAqua'] = $isAqua;
  177.             $this->viewData['isLesmills'] = $isLesmills;
  178.             $this->viewData['isGenfit'] = $this->currentSessionStack->get('accessGenfit');
  179.             // Campus e-learning — un lien navbar par programme dont le contact est membre.
  180.             // Map slug => label, cachée en session pour éviter un appel API par page.
  181.             // Use the PersonAccount's Contact ID to handle cases where the same email
  182.             // exists on both a PersonAccount (instructor) and a club referent Contact.
  183.             $campusPrograms = $this->currentSessionStack->get('campusPrograms');
  184.             if (is_null($campusPrograms) && !is_null($contact->getSalesforceID())) {
  185.                 $campusProgramService = new \App\Service\CampusProgramService();
  186.                 $personContactId = ApiContactController::getPersonAccountContactId(strtolower($contact->getEmail()));
  187.                 $contactIdForCampaign = $personContactId ?? $contact->getSalesforceID();
  188.                 $campusPrograms = [];
  189.                 foreach ($campusProgramService->getProgramsWithLegacyFallback() as $slug => $campusProgram) {
  190.                     $campaignMember = $campusProgramService->findMemberForProgram(
  191.                         $campusProgram,
  192.                         $contactIdForCampaign,
  193.                         strtolower($contact->getEmail())
  194.                     );
  195.                     if (!is_null($campaignMember)) {
  196.                         $campusPrograms[$slug] = $campusProgramService->resolveLabel($campusProgram);
  197.                     }
  198.                 }
  199.                 $this->currentSessionStack->set('campusPrograms', $campusPrograms);
  200.             }
  201.             $this->viewData['campusPrograms'] = $campusPrograms ?? [];
  202.             // Rétrocompatibilité : dérivé de la map tant que des templates l'utilisent encore.
  203.             $this->viewData['isCampusUpskillHeavy'] = isset($campusPrograms['upskill_heavy']);
  204.             if(!is_null($requestStack->getCurrentRequest()->get('salesforceAccountId'))) {
  205.                 $isHbxHub = false;
  206.                 $isAquaHub = false;
  207.                 $concepts = ApiContactController::getConceptsByAccount($requestStack->getCurrentRequest()->get('salesforceAccountId'));
  208.                 foreach($concepts as $concept) {
  209.                     if($concept->program_id == $_ENV['HBX_COACH']
  210.                         ||$concept->program_id == $_ENV['HBX_COACH_PRO']
  211.                         ||$concept->program_id == $_ENV['HBX_COACH_CLUB']
  212.                         ||$concept->program_id == $_ENV['HBX_COACH_CLUB_PRO']
  213.                         ||$concept->program_id == $_ENV['HBX_CLUB']
  214.                         || $concept->program_id == $_ENV['HBX_CLUB_PREMIUM']
  215.                         || $concept->program_id == $_ENV['HBX_CLUB_PREMIUM_PLUS']
  216.                         || $concept->program_id == in_array($concept->program_id, explode("|", $_ENV['HBX_HUB_PROGRAMS_FOR_MENU'] ))
  217.                     )
  218.                     {
  219.                         $isHbxHub = true;
  220.                     }
  221.                     if($concept->program_id == $_ENV['AQUA_COACH']
  222.                         ||$concept->program_id == $_ENV['AQUA_COACH_PRO']
  223.                         ||$concept->program_id == $_ENV['AQUA_CLUB']
  224.                         || $concept->program_id == $_ENV['AQUA_CLUB_PREMIUM']
  225.                         || $concept->program_id == $_ENV['AQUA_PISCINE_PUBLIQUE']
  226.                         || $concept->program_id == $_ENV['AQUA_PISCINE_PUBLIQUE_PREMIUM']
  227.                         || $concept->program_id == in_array($concept->program_id, explode("|", $_ENV['AQUA_HUB_PROGRAMS_FOR_MENU'] ))
  228.                     )
  229.                     {
  230.                         $isAquaHub = true;
  231.                     }
  232.                 }
  233.                 if(!empty(ApiAccountController::getClubsWhereInstructorIsHbx($requestStack->getCurrentRequest()->get('salesforceAccountId')))) {
  234.                     if(ApiAccountController::getClubsWhereInstructorIsHbx($requestStack->getCurrentRequest()->get('salesforceAccountId'))[0]->Instructeur_HBX_HUB__c == true) {
  235.                         $isHbxHub = true;
  236.                     }
  237.                 }
  238.                 if(!empty(ApiAccountController::getClubsWhereInstructorIsAqua($requestStack->getCurrentRequest()->get('salesforceAccountId')))) {
  239.                     if(ApiAccountController::getClubsWhereInstructorIsAqua($requestStack->getCurrentRequest()->get('salesforceAccountId'))[0]->Instructeur_AQUA_HUB__c == true) {
  240.                         $isAquaHub = true;
  241.                     }
  242.                 }
  243.                 $contactId = $this->currentSessionStack->get('contact')->getId();
  244.                 if($this->currentSessionStack->get('contact')->isHubAccess()) {
  245.                     $contact = $this->entityManager->getRepository(Contact::class)->findBy([
  246.                         'id' => $contactId
  247.                     ]);
  248.                     $contactProductPrices = $this->entityManager->getRepository(ContactProductPrice::class)->findBy([
  249.                         'contact' => $contact
  250.                     ]);
  251.                     if($contactProductPrices) {
  252.                         foreach($contactProductPrices as $contactProductPrice) {
  253.                             if(!is_null($contactProductPrice->getContentOnDemand())) {
  254.                                 if($contactProductPrice->getContentOnDemand()->getSfProduct()->getBrand() &&
  255.                                     $contactProductPrice->getContentOnDemand()->getSfProduct()->getBrand()->getCode() == $_ENV['CODE_BRAND_HUB_HBX']) {
  256.                                     $isHbxHub = true;
  257.                                 } else if($contactProductPrice->getContentOnDemand()->getSfProduct()->getBrand() &&
  258.                                     $contactProductPrice->getContentOnDemand()->getSfProduct()->getBrand()->getCode() == $_ENV['CODE_BRAND_HUB_AQUA']) {
  259.                                     $isAquaHub = true;
  260.                                 }
  261.                             }
  262.                         }
  263.                     }
  264.                 }
  265.                 $this->viewData['isHbxHub'] = $isHbxHub;
  266.                 $this->viewData['isAquaHub'] = $isAquaHub;
  267.             }
  268.         }
  269. //        $contentOnDemand = $this->entityManager->getRepository(ContentOnDemand::class)->findOneBy([
  270. //            'shopProductId' => '17443'
  271. //        ]);
  272. //        dump($contentOnDemand);
  273. //        $sfProduct = $this->entityManager->getRepository(SfProduct::class)->findOneBy([
  274. //            'code' => 'FORCHBX22',
  275. //        ]);
  276. //        dump($sfProduct);
  277. //        dd($contentOnDemand->getContentOnDemandRequirements()->getValues());
  278. //        foreach($contentOnDemand->getContentOnDemandRequirements()->getValues() as $contentOnDemandRequirement) {
  279. //            if($contentOnDemandRequirement->getSfProduct() == $sfProduct) {
  280. //                dump('oui');
  281. //            }
  282. //        }
  283. //        dd('stop');
  284.         $this->viewData['isHbx'] = $isHbx;
  285.         $this->viewData['language'] = $this->language;
  286.         $this->viewData['accounts'] = $accounts;
  287.         $this->viewData['contactCsrfToken'] = $contactCsrfToken;
  288.         $this->viewData['contact'] = $lightContact;
  289.         $this->viewData['env'] = $_ENV['APP_ENV'];
  290.         $this->viewData['previousAdminUsername'] = $_SESSION["_sf2_attributes"]["previousAdminUsername"] ?? null;
  291.     }
  292.     public function getNavigationContext(Request $request, RouterInterface $router): DtoNavigationContext
  293.     {
  294. //        dump($request);
  295. //        dd($router);
  296.         $expectedUrl= $request->getPathInfo();
  297.         $dtoNavigationContext = new DtoNavigationContext();
  298.         if ($request->getSession()->get('_security.default.target_path') != null)
  299.         {
  300.             $expectedUrl = str_replace(["https://", "http://"], "", str_replace($request->getHttpHost(), "", $request->getSession()->get('_security.default.target_path')));
  301.         }
  302.         $explodeExpectedUrl = explode('/', $expectedUrl);
  303.         $explodeExpectedUrlEnd = end($explodeExpectedUrl);
  304.         $matched = [];
  305.         try {
  306.             $matched = $router->match($expectedUrl);
  307.         } catch (\Exception $e){}
  308.         if (empty($matched) == false)
  309.         {
  310.             $productCodeRaw = array_key_exists("productCode", $matched) ? $matched['productCode'] : $request->get("productCode");
  311.             //S'il y a des paramètres après le productCode, on prend le string avant le '?'
  312.             $productCode = strtok($productCodeRaw, '?');
  313.             $dbBrand = null;
  314.             if (StringHelper::isNullOrEmptyWithSpace($productCode) == false) {
  315.                 $dbProduct = $this->entityManager->getRepository(SfProduct::class)->findOneBy(['code' => $productCode]);
  316.                 if(!is_null($dbProduct)) {
  317.                     $dbBrand = $dbProduct->getBrand();
  318.                 }
  319.             }
  320.             else if ($request->get("brandCode") != null)
  321.             {
  322.                 $dbBrand = $this->entityManager->getRepository(SfBrand::class)->findOneBy(['code' => $request->get("brandCode")]);
  323.             }
  324.             else if (isset($matched['brandCode']) && $matched['brandCode'] != null) {
  325.                 $dbBrand = $this->entityManager->getRepository(SfBrand::class)->findOneBy(['code' => $matched['brandCode']]);
  326.             }
  327.             else if (!is_null($explodeExpectedUrlEnd)) {
  328.                 $dbBrand = $this->entityManager->getRepository(SfBrand::class)->findOneBy(['code' => $explodeExpectedUrlEnd]);
  329.             }
  330.             if ($dbBrand != null) {
  331.                 $dtoBrand = ObjectConversionHelper::convert($dbBrand, DtoBrand::class);
  332.                 $dtoNavigationContext->dtoBrand = $dtoBrand;
  333.             }
  334.         }
  335.         return $this->viewData['context'] = $dtoNavigationContext;
  336.     }
  337.     public function refreshViewLightContactFromSession() : void
  338.     {
  339.         if(!is_null($contact = $this->session->get('contact'))){
  340.             $lightContact = $contact->cloneLightContact();
  341.             $this->viewData['contact'] = $lightContact;
  342.         }
  343.     }
  344.     /**
  345.      * @return array
  346.      */
  347.     public function getMessages() : array
  348.     {
  349.         return $this->notificationManager::getMessages();
  350.     }
  351.     /**
  352.      * Parse all errors and messages to create flash messages
  353.      */
  354.     protected function createFlashs() : void{
  355.         $messages = $this->getMessages();
  356.         foreach ($messages['error'] as $error){
  357.             $this->addFlash('danger', $this->languageManager->translate($error, $this->language));
  358.         }
  359.         foreach ($messages['info'] as $message){
  360.             $this->addFlash('success', $this->languageManager->translate($message, $this->language));
  361.         }
  362.         $this->notificationManager::eraseMessages();
  363.     }
  364.     /**
  365.      * Méthode permettant de convertir un DtoResponse en messages flash
  366.      * @param DtoResponse $dtoResponse
  367.      * @return void
  368.      */
  369.     protected function createFlashsFromDtoResponse(DtoResponse $dtoResponse)
  370.     {
  371.         if ($dtoResponse->isSuccess())
  372.         {
  373.             if (empty($dtoResponse->successMessages) == false)
  374.             {
  375.                 foreach ($dtoResponse->successMessages as $message){
  376.                     $this->addFlash('success', $this->languageManager->translate($message, $this->language));
  377.                 }
  378.             }
  379.         }
  380.         else if ($dtoResponse->isBadRequest())
  381.         {
  382.             // Création du message pour les champs obligatoires
  383.             if (empty($dtoResponse->requiredFields) == false)
  384.             {
  385.                 // Gestion de la phrase d'accroche
  386.                 $message = $this->languageManager->translate('REQUIRED_FIELDS_DEFAULT_MESSAGE', $this->language) . "<ul>";
  387.                 // Gestion du détail des champs en erreur
  388.                 foreach($dtoResponse->requiredFields as $requiredField)
  389.                 {
  390.                     $message .= "<li>" . $this->languageManager->translate($requiredField, $this->language) . "</li>";
  391.                 }
  392.                 $this->addFlash('danger', $message . "</ul>");
  393.             } // Création du message pour les champs invalides
  394.             else if (empty($dtoResponse->invalidRules) == false)
  395.             {
  396.                 // Gestion de la phrase d'accroche
  397.                 $message = $this->languageManager->translate('INVALID_RULES_DEFAULT_MESSAGE', $this->language) . "<ul>";
  398.                 // Gestion du détail des champs en erreur
  399.                 foreach($dtoResponse->invalidRules as $invalidRule)
  400.                 {
  401.                     $message .= "<li>" . $this->languageManager->translate($invalidRule, $this->language) . "</li>";
  402.                 }
  403.                 $this->addFlash('danger', $message. "</ul>");
  404.             }
  405.         }
  406.         else // Gestion des autres erreurs
  407.         {
  408.             if (empty($dtoResponse->errorMessages) == false)
  409.             {
  410.                 foreach($dtoResponse->errorMessages as $errorMessage)
  411.                 {
  412.                     $this->addFlash('danger', $this->languageManager->translate($errorMessage, $this->language));
  413.                 }
  414.             }
  415.             else // Erreur générique
  416.             {
  417.                 $this->addFlash('danger', $this->languageManager->translate("Un problème est survenu", $this->language));
  418.             }
  419.         }
  420.     }
  421.     public function createJsonResponseFromDtoResponse(DtoResponse $dtoResponse, string $language) : JsonResponse
  422.     {
  423.         $response = null;
  424.         if($dtoResponse->isSuccess()) {
  425.             $response = new JsonResponse($dtoResponse, $dtoResponse->getHttpCode());
  426.         } else {
  427.             $messagesErrorKey = 'error';
  428.             $messages = [$messagesErrorKey => []];
  429.             // Création du message pour les champs obligatoires
  430.             if (empty($dtoResponse->requiredFields) == false) {
  431.                 // Gestion de la phrase d'accroche
  432.                 $message = $this->languageManager->translate('REQUIRED_FIELDS_DEFAULT_MESSAGE', $language) . "<ul>";
  433.                 // Gestion du détail des champs en erreur
  434.                 foreach ($dtoResponse->requiredFields as $requiredField) {
  435.                     $message .= "<li>" . $this->languageManager->translate($requiredField, $language) . "</li>";
  436.                 }
  437.                 $messages[$messagesErrorKey][] = $message . '</ul>';
  438.             } // Création du message pour les champs invalides
  439.             else if (empty($dtoResponse->invalidRules) == false) {
  440.                 // Gestion de la phrase d'accroche
  441.                 $message = $this->languageManager->translate('INVALID_RULES_DEFAULT_MESSAGE', $language) . "<ul>";
  442.                 // Gestion du détail des champs en erreur
  443.                 foreach ($dtoResponse->invalidRules as $invalidRule) {
  444.                     $message .= "<li>" . $this->languageManager->translate($invalidRule, $language) . "</li>";
  445.                 }
  446.                 $messages[$messagesErrorKey][] = $message . '</ul>';
  447.             }
  448.             else // Gestion des autres erreurs
  449.             {
  450.                 if (empty($dtoResponse->errorMessages) == false) {
  451.                     foreach ($dtoResponse->errorMessages as $errorMessage) {
  452.                         $messages[$messagesErrorKey][] = $this->languageManager->translate($errorMessage, $language);
  453.                     }
  454.                 }
  455.                 else if (empty($dtoResponse->errorMessage) == false)
  456.                 {
  457.                     $messages[$messagesErrorKey][] = $this->languageManager->translate($dtoResponse->errorMessage, $language);
  458.                 }
  459.                 else // Erreur générique
  460.                 {
  461.                     $messages[$messagesErrorKey][] = $this->languageManager->translate('Un problème est survenu.', $language);
  462.                 }
  463.             }
  464.             $dtoResponse->errorSummaryMessages = $messages;
  465.             $response = new JsonResponse($dtoResponse);
  466.             $response->setStatusCode($dtoResponse->getHttpCode());
  467.         }
  468.         return $response;
  469.     }
  470.     protected function getLanguage(RequestStack $requestStack) : string{
  471.         return $requestStack->getCurrentRequest()->getLocale();
  472.     }
  473.     /**
  474.      * @param RequestStack $request
  475.      * @return Referent|null
  476.      */
  477.     protected function getReferentFromSession(RequestStack $requestStack) : ?Referent
  478.     {
  479.         $referent = $requestStack->getSession()->get('contact', null);
  480.         if(!($referent instanceof Referent)){
  481.             $referent = null;
  482.         }
  483.         return $referent;
  484.     }
  485.     /**
  486.      * add array data in the data array
  487.      * @param string $key
  488.      * @param array $dataArray
  489.      * @return EntityController
  490.      */
  491.     public function addViewDataArray(string $key, array $dataArray) : self
  492.     {
  493.         $this->viewData[$key] = $dataArray;
  494.         return $this;
  495.     }
  496.     /**
  497.      * add int data in the data array
  498.      * @param string $key
  499.      * @param int $dataInt
  500.      * @return EntityController
  501.      */
  502.     public function addViewDataInt(string $key, int $dataInt) : self
  503.     {
  504.         $this->viewData[$key] = $dataInt;
  505.         return $this;
  506.     }
  507.     /**
  508.      * add string data in the data array
  509.      * @param string $key
  510.      * @param string $dataString
  511.      * @return EntityController
  512.      */
  513.     public function addViewDataString(string $key, string $dataString) : self
  514.     {
  515.         $this->viewData[$key] = $dataString;
  516.         return $this;
  517.     }
  518.     /**
  519.      * add object data in the data array
  520.      * @param string $key
  521.      * @param object|null $dataObject
  522.      * @return EntityController
  523.      */
  524.     public function addViewDataObject(string $key, ?object $dataObject) : self
  525.     {
  526.         $this->viewData[$key] = $dataObject;
  527.         return $this;
  528.     }
  529.     /**
  530.      * add object data in the data array
  531.      * @param string $key
  532.      * @param bool $dataBool
  533.      * @return EntityController
  534.      */
  535.     public function addViewDataBool(string $key, bool $dataBool) : self
  536.     {
  537.         $this->viewData[$key] = $dataBool;
  538.         return $this;
  539.     }
  540. }