src/Controller/BubbleController.php line 153

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\BubbleEditType;
  4. use App\Model\Bubble;
  5. use App\Utils\DefaultRights;
  6. use App\Form\BubbleType;
  7. use App\Model\Industrial;
  8. use App\Service\SiteBackend;
  9. use App\Service\BubbleBackend;
  10. use App\Form\BubbleAddUserType;
  11. use App\Service\IndustrialBackend;
  12. use App\Utils\SessionUtils;
  13. use Symfony\Component\HttpFoundation\RedirectResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Component\Serializer\SerializerInterface;
  18. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  19. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  20. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  21. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. use Symfony\Component\HttpFoundation\JsonResponse;
  25. use App\Model\Structure;
  26. use App\Service\StructureBackend;
  27. /**
  28.  * BubbleController
  29.  */
  30. class BubbleController extends AbstractController
  31. {
  32.     use SessionUtils;
  33.     const ROLE_OBJECTS = ['user''site''industrial''plan''eco_model''token''history''quality_control''billing''job_order''printer''dashboard''structures''planning'];
  34.     const CRUD_LABELS = ['read''create''update''delete'];
  35.     /**
  36.      *
  37.      * @return Response
  38.      *
  39.      * @Route("/bubble", name="bubble")
  40.      */
  41.     public function bubble()
  42.     {
  43.         $session $this->sessionByRequestStack();
  44.         $user $this->getuser();
  45.         $profiles $user->getProfiles();
  46.         $b_key $session->get('b_key'0);
  47.         $profile $profiles[$b_key];
  48.         if ($profile['user']['read'] == 0) {
  49.             return $this->redirectToRoute('custom_error_403');
  50.         }
  51.         // User is SA
  52.         if ($user->getIsSA() == 1) {
  53.             $bubbles_infos $user->getBubblesInfo();
  54.         } else {
  55.             $bubbles_infos = [];
  56.         }
  57.         return $this->render('bubble/bubble.html.twig', [
  58.             'bubbles_infos' => $bubbles_infos
  59.         ]);
  60.     }
  61.     /**
  62.      * bubbleAdd
  63.      *
  64.      * @param TranslatorInterface $translator
  65.      * @param Request $request
  66.      * @param BubbleBackend $backend
  67.      * @return RedirectResponse|Response
  68.      *
  69.      * @Route("/bubble/add", name="bubble_add")
  70.      */
  71.     public function bubbleAdd(
  72.         TranslatorInterface $translator,
  73.         Request $request,
  74.         BubbleBackend $backend
  75.     ) {
  76.         $session $this->sessionByRequestStack();
  77.         $user $this->getuser();
  78.         $profiles  $user->getProfiles();
  79.         $b_key $session->get('b_key'0);
  80.         if ($profiles[$b_key]['user']['create'] == 0) {
  81.             return $this->redirectToRoute('custom_error_403');
  82.         }
  83.         $form $this->createForm(BubbleType::class, NULL, []);
  84.         $form->handleRequest($request);
  85.         if ($form->isSubmitted() && $form->isValid()) {
  86.             $data $form->getData();
  87.             $responseStatus $backend->postNewBubble($data$session);
  88.             if ($responseStatus  === Response::HTTP_OK) {
  89.                 $this->addFlash('success'sprintf($translator->trans('La bulle <strong>%s</strong> a été créé.'), $data['bubble_name']));
  90.                 return $this->redirectToRoute('bubble');
  91.             } elseif ($responseStatus === Response::HTTP_CONFLICT) {
  92.                 $this->addFlash('warning'sprintf$translator->trans('La bulle <strong>%s</strong> existe déjà. Veuillez essayer un autre nom de bulle.'), $data['bubble_name']));
  93.                 return $this->redirectToRoute('bubble_add');
  94.             } elseif ($responseStatus === Response::HTTP_UNAUTHORIZED) {
  95.                 return $this->redirectToRoute('custom_error_403');
  96.             } elseif ($responseStatus === Response::HTTP_INTERNAL_SERVER_ERROR) {
  97.                 $this->addFlash('errors'$translator->trans('Le serveur a rencontré une difficultée, veuillez ré-essayer ultérieurement'));
  98.                 return $this->redirectToRoute('bubble_add');
  99.             } elseif ($responseStatus === Response::HTTP_BAD_REQUEST) {
  100.                 $this->addFlash('warning'$translator->trans('Veuillez remplir tous les champs du formulaire'));
  101.                 return $this->redirectToRoute('bubble_add');
  102.             } else {
  103.                 $this->addFlash('errors'$translator->trans('Une erreur s\'est produite durant la création de la bulle. Veuillez ré-essayer ultérieurement.'));
  104.             }
  105.         }
  106.         return $this->render('bubble/bubble_add.html.twig', [
  107.             'bubbleForm' => $form->createView()
  108.         ]);
  109.     }
  110.     /**
  111.      * bubble update
  112.      *
  113.      * @param $bId
  114.      * @param TranslatorInterface $translator
  115.      * @param Request $request
  116.      * @param BubbleBackend $backend
  117.      * @return RedirectResponse|Response
  118.      *
  119.      * @Route("/bubble/update/{bId}", name="bubble_update")
  120.      */
  121.     public function bubbleUpdate (
  122.         $bId,
  123.         TranslatorInterface $translator,
  124.         Request $request,
  125.         BubbleBackend $backend
  126.     ) {
  127.         $session $this->sessionByRequestStack();
  128.         $user $this->getuser();
  129.         $profiles  $user->getProfiles();
  130.         $b_key $session->get('b_key'0);
  131.         if ($profiles[$b_key]['user']['create'] == 0) {
  132.             return $this->redirectToRoute('custom_error_403');
  133.         }
  134.         $bubbleResp $backend->getBubble($user$bId);
  135.         $bubbleRespStatus $bubbleResp->getStatusCode();
  136.         if ($bubbleRespStatus  === Response::HTTP_OK) {
  137.             $data $bubbleResp->toArray()['contents'][0];
  138.             $bubble = new Bubble();
  139.             if ($data['ad_valorem'] != null){
  140.                 $adValorem =  $data['ad_valorem'] * 100;
  141.             } else {
  142.                 $adValorem null;
  143.             }
  144.             $bubble->setBName($data['b_name'])
  145.                 ->setBAddress($data['b_address'])
  146.                 ->setBPostalCode($data['b_postal_code'])
  147.                 ->setBCityName($data['b_city_name'])
  148.                 ->setBCountry($data['b_country'])
  149.                 ->setIsQcUpdatable($data['is_qc_updatable'])
  150.                 ->setNumberCredit($data['number_credit'])
  151.                 ->setCreditValue($data['credit_value'])
  152.                 ->setAdValorem($adValorem)
  153.             ;
  154.         } elseif ($bubbleRespStatus === Response::HTTP_UNAUTHORIZED) {
  155.             return $this->redirectToRoute('custom_error_403');
  156.         } elseif ($bubbleRespStatus === Response::HTTP_INTERNAL_SERVER_ERROR) {
  157.             $this->addFlash('errors'$translator->trans('Le serveur a rencontré une difficultée, veuillez ré-essayer ultérieurement'));
  158.             return $this->redirectToRoute('bubble_update');
  159.         } elseif ($bubbleRespStatus === Response::HTTP_BAD_REQUEST) {
  160.             $this->addFlash('warning'$translator->trans('Veuillez remplir tous les champs du formulaire'));
  161.             return $this->redirectToRoute('bubble_update');
  162.         }
  163.         $form $this->createForm(BubbleEditType::class, $bubble);
  164.         $form->handleRequest($request);
  165.         if ($form->isSubmitted()) {
  166.             $data $form->getData();
  167.             $responseStatus $backend->updateBubble($data$bId);
  168.             if ($responseStatus == Response::HTTP_OK) {
  169.                 $this->addFlash('success'sprintf($translator->trans('La bulle<strong>%s</strong> a été modifiéé.'), $data->getBName()));
  170.                 return $this->redirectToRoute('bubble');
  171.             } elseif ($responseStatus == Response::HTTP_UNAUTHORIZED) {
  172.                 return $this->redirectToRoute('custom_error_403');
  173.             } elseif ($responseStatus == Response::HTTP_INTERNAL_SERVER_ERROR) {
  174.                 $this->addFlash('errors'$translator->trans('Le serveur a rencontré une difficultée, veuillez ré-essayer ultérieurement'));
  175.                 return $this->redirectToRoute('bubble_update');
  176.             } elseif ($responseStatus == Response::HTTP_BAD_REQUEST) {
  177.                 $this->addFlash('warning'$translator->trans('Veuillez remplir tous les champs du formulaire'));
  178.                 return $this->redirectToRoute('bubble_update');
  179.             } else {
  180.                 $this->addFlash('errors'$translator->trans('Une erreur s\'est produite durant la modification de la bulle. Veuillez ré-essayer ultérieurement.'));
  181.             }
  182.         }
  183.         return $this->render('bubble/bubble_update.html.twig', [
  184.             'bubbleForm' => $form->createView()
  185.         ]);
  186.     }
  187.     /**
  188.      * bubbleUserList
  189.      *
  190.      * @param BubbleBackend $backend
  191.      * @return Response
  192.      *
  193.      *  @Route("/bubble/users", name="bubble_user_list")
  194.      */
  195.     public function bubbleUserList(
  196.         BubbleBackend $backend
  197.     ) {
  198.         $session $this->sessionByRequestStack();
  199.         $user $this->getuser();
  200.         $profiles  $user->getProfiles();
  201.         $b_key $session->get('b_key'0);
  202.         $profile $profiles[$b_key];
  203.         $bubbleId $profile['b_id'];
  204.         if ($profiles[$b_key]['user']['read'] == 0) {
  205.             return $this->redirectToRoute('custom_error_403');
  206.         }
  207.         $response $backend->getBubbleProfiles($user$bubbleId);
  208.         if ($response->getStatusCode() == Response::HTTP_OK) {
  209.             $result json_decode($response->getContent(), true);
  210.         } elseif ($response->getStatusCode() == Response::HTTP_UNAUTHORIZED) {
  211.             return $this->redirectToRoute('custom_error_401');
  212.         } elseif ($response->getStatusCode() == Response::HTTP_FORBIDDEN) {
  213.             return $this->redirectToRoute('custom_error_403');
  214.         } elseif ($response->getStatusCode() == Response::HTTP_NOT_FOUND) {
  215.             return $this->redirectToRoute('custom_error_404');
  216.         } else {
  217.             return $this->redirectToRoute('custom_error_500');
  218.         }
  219.         $user_profiles $result['contents'];
  220.         $manageProfile $backend->manageProfile($user_profiles);
  221.         return $this->render('bubble/bubble_user_list.html.twig', [
  222.             'user_profiles' => $user_profiles,
  223.             'profilesBA' => $manageProfile['profilesBA'],
  224.             'profilesOP' => $manageProfile['profilesOP'],
  225.             'profilesVP' => $manageProfile['profilesVP'],
  226.             'profilesMan' => $manageProfile['profilesFAB'],
  227.             'profilesIND' => $manageProfile['profilesIND'],
  228.             'profilesFM' => $manageProfile['profilesFM'],
  229.             'profilesOM' => $manageProfile['profilesOM'],
  230.             'usersTotal' => count($user_profiles),
  231.             'usersBaNb' => count($manageProfile['profilesBA']),
  232.             'usersManNb' => count($manageProfile['profilesFAB']),
  233.             'usersOpeNb' => count($manageProfile['profilesOP']),
  234.             'usersVplanNb' => count($manageProfile['profilesVP']),
  235.             'usersIndusNb' => count($manageProfile['profilesIND']),
  236.             'usersFmNb' => count($manageProfile['profilesFM']),
  237.             'usersOmNb' => count($manageProfile['profilesOM']),
  238.             'user' => $user
  239.         ]);
  240.     }
  241.     /**
  242.      * Update profile roles
  243.      *
  244.      * @param TranslatorInterface $translator
  245.      * @param Request $request
  246.      * @param BubbleBackend $backend
  247.      * @param $perm_id
  248.      * @return Response
  249.      *
  250.      * @Route("/bubble/profiles/{perm_id}", name="bubble_user_update")
  251.      */
  252.     public function bubbleUserUpdate (
  253.         $perm_id,
  254.         TranslatorInterface $translator,
  255.         Request $request,
  256.         BubbleBackend $backend
  257.     ): Response {
  258.         $session $this->sessionByRequestStack();
  259.         $user $this->getuser();
  260.         $profiles $user->getProfiles();
  261.         $b_key $session->get('b_key'0);
  262.         $bubbleId $profiles[$b_key]['b_id'];
  263.         if ($profiles[$b_key]['user']['update'] == 0) {
  264.             return $this->redirectToRoute('custom_error_403');
  265.         }
  266.         $profileUserId $request->request->get('profile_user_id');
  267.         $nBRoleObjects self::ROLE_OBJECTS;
  268.         $nBCrudLabels self::CRUD_LABELS;
  269.         $permissions = [];
  270.         for ($j 0$j count($nBRoleObjects); $j++) {
  271.             for ($i 0$i count($nBCrudLabels); $i++) {
  272.                 $permissions[self::ROLE_OBJECTS[$j]] =
  273.                     [
  274.                         'read' => ($request->request->get('read' $j) == 1) ? 0,
  275.                         'create' => ($request->request->get('create' $j) == 1) ? 0,
  276.                         'update' => ($request->request->get('update' $j) == 1) ? 0,
  277.                         'delete' => ($request->request->get('delete' $j) == 1) ? 0
  278.                     ];
  279.             }
  280.         }
  281.         /** Block to manage default rigths > */
  282.         $inputs_name json_decode($request->request->get('inputs_name'), true);
  283.         if (!is_null($inputs_name)) {
  284.             foreach ($permissions as $permission) {
  285.                 foreach ($inputs_name as $key => $input_name) {
  286.                     $label_name substr($key0strpos($key'_'0));
  287.                     if ('ecoModel' == $label_name) {
  288.                         $label_name 'eco_model';
  289.                         $key 'eco_model';
  290.                     }
  291.                     if ('tokenApiAccess' == $label_name) {
  292.                         $label_name 'token';
  293.                         $key 'token';
  294.                     }
  295.                     if ('qualityControl' == $label_name) {
  296.                         $label_name 'quality_control';
  297.                         $key 'quality_control';
  298.                     }
  299.                     if ('jobOrders' == $label_name) {
  300.                         $label_name 'job_order';
  301.                         $key 'job_order';
  302.                     }
  303.                     if ('billingElements' == $label_name) {
  304.                         $label_name 'billing';
  305.                         $key 'billing';
  306.                     }
  307.                     if ('jobHistory' == $label_name) {
  308.                         $label_name 'history';
  309.                         $key 'history';
  310.                     }
  311.                     if ('printers' == $label_name) {
  312.                         $label_name 'printer';
  313.                         $key 'printer';
  314.                     }
  315.                     if ('read' == substr($input_name04) && stristr($key$label_name)) {
  316.                         unset($permissions[$label_name]['read']);
  317.                         $permissions[$label_name]['read'] = 1;
  318.                     }
  319.                     if ('create' == substr($input_name06) && stristr($key$label_name)) {
  320.                         unset($permissions[$label_name]['create']);
  321.                         $permissions[$label_name]['create'] = 1;
  322.                     }
  323.                     if ('update' == substr($input_name06) && stristr($key$label_name)) {
  324.                         unset($permissions[$label_name]['update']);
  325.                         $permissions[$label_name]['update'] = 1;
  326.                     }
  327.                     if ('delete' == substr($input_name06) && stristr($key$label_name)) {
  328.                         unset($permissions[$label_name]['delete']);
  329.                         $permissions[$label_name]['delete'] = 1;
  330.                     }
  331.                 }
  332.             }
  333.         }
  334.         /** Block to manage default rigths < */
  335.         // $permissions['bubble'] = json_decode($request->request->get('bubble'), TRUE);
  336.         // $permissions['bc_certification'] = json_decode($request->request->get('bc_certification'), TRUE);
  337.         $permissions['bubble'] = $profiles[$b_key]['bubble'];
  338.         $permissions['bc_certification'] = $profiles[$b_key]['bc_certification'];
  339.         $response $backend->getBubbleProfiles($user$bubbleId);
  340.         $result json_decode($response->getContent(), true);
  341.         $user_profiles $result['contents'];
  342.         $label '';
  343.         foreach ($user_profiles as $profile) {
  344.             if ($profile['perm_id'] == $perm_id) {
  345.                 $label $profile['label'];
  346.                 $u_profile_id $profile['u_id'];
  347.             }
  348.         }
  349.         $results $backend->updateProfiles($bubbleId$permissions$label$perm_id$u_profile_id);
  350.         $manageProfile $backend->manageProfile($user_profiles);
  351.         if ($manageProfile['iSnotVpIsNotFab']) {
  352.             $this->addFlash('warning'$translator->trans('Rôle non attribué : ') . $manageProfile['labelNorme']);
  353.         }   
  354.         return $this->render('bubble/bubble_user_list.html.twig', [
  355.             'user_profiles' => $user_profiles,
  356.             'profilesBA' => $manageProfile['profilesBA'],
  357.             'profilesOP' => $manageProfile['profilesOP'],
  358.             'profilesVP' => $manageProfile['profilesVP'],
  359.             'profilesMan' => $manageProfile['profilesFAB'],
  360.             'profilesIND' => $manageProfile['profilesIND'],
  361.             'profilesFM' => $manageProfile['profilesFM'],
  362.             'profilesOM' => $manageProfile['profilesOM'],
  363.             'usersTotal' => count($user_profiles),
  364.             'usersBaNb' => count($manageProfile['profilesBA']),
  365.             'usersManNb' => count($manageProfile['profilesOP']),
  366.             'usersOpeNb' => count($manageProfile['profilesVP']),
  367.             'usersVplanNb' => count($manageProfile['profilesFAB']),
  368.             'usersIndusNb' => count($manageProfile['profilesIND']),
  369.             'usersFmNb' => count($manageProfile['profilesFM']),
  370.             'usersOmNb' => count($manageProfile['profilesOM']),
  371.             'user' => $user
  372.         ]);
  373.     }
  374.     /**
  375.      * Re-activate Account
  376.      *
  377.      * @param  [type] $recipient_u_id
  378.      * @param  mixed $backend
  379.      * @param TranslatorInterface $translator
  380.      *
  381.      * @return Response
  382.      *
  383.      * @Route("/bubble/user/activate/{recipient_u_id}/{u_email}", name="re_send_activation")
  384.      */
  385.     public function reSendActivation(
  386.         $recipient_u_id$u_email,
  387.         BubbleBackend $backend,
  388.         TranslatorInterface $translator
  389.     ) {
  390.         $session $this->sessionByRequestStack();
  391.         $recipient_u_id = (int)$recipient_u_id;
  392.         $user $this->getuser();
  393.         $profiles  $user->getProfiles();
  394.         $b_key $session->get('b_key'0);
  395.         $bubbleId $profiles[$b_key]['b_id'];
  396.         if ($profiles[$b_key]['user']['update'] == 0) {
  397.             return $this->redirectToRoute('custom_error_403');
  398.         }
  399.         if ($recipient_u_id) {
  400.             $response $backend->reActivateAccount($bubbleId$recipient_u_id);
  401.             if ($response == Response::HTTP_OK) {
  402.                 $this->addFlash("success"$translator->trans("L'utilisateur " .  $u_email $translator->trans(" a reçu un e-mail d'activation" )));
  403.                 return $this->redirectToRoute('bubble_user_list');
  404.             } elseif ($response == Response::HTTP_UNAUTHORIZED) {
  405.                 $this->addFlash("errors"$translator->trans("Vous n'êtes pas autorisé pour envoyer un e-mail d'activation" ));
  406.                 return $this->redirectToRoute('bubble_user_list');
  407.             } else {
  408.                 $this->addFlash("errors"$translator->trans("Une erreur s'est produite lors de l'envoi d'un e-mail d'activation pour l'utilisateur " $u_email $translator->trans(". Veuillez vérifier les droits d'accès ou interroger l'administrateur." )));
  409.                 return $this->redirectToRoute('bubble_user_list');
  410.             }
  411.         } else{
  412.             $this->addFlash("errors"$translator->trans("L'utilisateur " $u_email $translator->trans(" n'existe pas. Veuillez interroger l'administrateur." )));
  413.             return $this->redirectToRoute('bubble_user_list');
  414.         }
  415.     }
  416.     /**
  417.      * @param Request $request
  418.      * @param BubbleBackend $backend
  419.      * @param StructureBackend $structureBackend
  420.      * @param SiteBackend $sbackend
  421.      * @param IndustrialBackend $indusBackend
  422.      * @param TranslatorInterface $translator
  423.      * @param SerializerInterface $serializer
  424.      * @return Response
  425.      * @throws ClientExceptionInterface
  426.      * @throws RedirectionExceptionInterface
  427.      * @throws ServerExceptionInterface
  428.      * @throws TransportExceptionInterface
  429.      *
  430.      * @Route("/bubble/user/add", name="bubble_user_add")
  431.      */
  432.     public function bubbleAddUser (
  433.         Request $request,
  434.         BubbleBackend $backend,
  435.         SiteBackend $sbackend,
  436.         IndustrialBackend $indusBackend,
  437.         StructureBackend $structureBackend,
  438.         TranslatorInterface $translator,
  439.         SerializerInterface $serializer
  440.     ) : Response {
  441.         $session $this->sessionByRequestStack();
  442.         $user $this->getuser();
  443.         $profiles $user->getProfiles();
  444.         $b_key $session->get('b_key'0);
  445.         $bubbleId $profiles[$b_key]['b_id'];
  446.         $profile $profiles[$b_key];
  447.         if ($profiles[$b_key]['user']['create'] == 0) {
  448.             return $this->redirectToRoute('custom_error_403');
  449.         }
  450.         $lng $request->getLocale();
  451.         $sites $sbackend->getSiteByBubble($bubbleId$user);
  452.         if ($profiles[$b_key]['industrial']['read'] == 1) {
  453.             $response $indusBackend->getIndustrials($bubbleId$user);
  454.             if ($response->getStatusCode() == Response::HTTP_OK) {
  455.                 $data json_decode($response->getContent());
  456.                 $industrials = [];
  457.                 foreach ($data->contents as $industrial) {
  458.                     $industrials[] = $serializer->deserialize(json_encode($industrial), Industrial::class, 'json');
  459.                 }
  460.             } elseif ($response->getStatusCode() == Response::HTTP_UNAUTHORIZED) {
  461.                 return $this->redirectToRoute('custom_error_401');
  462.             } elseif ($response->getStatusCode() == Response::HTTP_FORBIDDEN) {
  463.                 return $this->redirectToRoute('custom_error_403'); //bubble_user_add
  464.             } elseif ($response->getStatusCode() == Response::HTTP_NOT_FOUND) {
  465.                 return $this->redirectToRoute('custom_error_404');
  466.             } else {
  467.                 return $this->redirectToRoute('custom_error_500');
  468.             }
  469.             // Remove plan with name = fichier interne
  470.             foreach ($industrials as $k => $company) {
  471.                 if ($company->getIndName() == 'Fichier interne') {
  472.                     unset($industrials[$k]);
  473.                 }
  474.             }
  475.         } else {
  476.             $industrials = [];
  477.         }
  478.         $organizations $structureBackend->getOrganizations($bubbleId);
  479.         $organizationsList = [];
  480.         if ($organizations->getStatusCode() === Response::HTTP_OK) {
  481.             $organizationsArr json_decode($organizations->getContent());
  482.             foreach ($organizationsArr->contents as $organization) {
  483.                 $organizationsList[] = $serializer->deserialize(json_encode($organization), Structure::class, 'json');
  484.             }
  485.         }
  486.         $formations $structureBackend->getFormations($bubbleId);
  487.         $formationsList = [];
  488.         if ($formations->getStatusCode() === Response::HTTP_OK) {
  489.             $formationsArr json_decode($formations->getContent());
  490.             foreach ($formationsArr->contents as $formation) {
  491.                 $formationsList[] = $serializer->deserialize(json_encode($formation), Structure::class, 'json');
  492.             }
  493.         }
  494.         $userRights json_decode(( $backend->getAllProfileRights($bubbleId$user))->getContent(), true);
  495.         $profilesLabels = [];
  496.         foreach ($userRights['contents'] as $p) {
  497.             $pId $p['perm_id'];
  498.             if ($p['label'] == 'BA') {
  499.                 $l $lng == 'fr' 'Administrateur' 'Administrator';
  500.                 $profilesLabels[$l] = $pId;
  501.             } elseif ($p['label'] == 'OP') {
  502.                 $l $lng == 'fr' 'Opérateur' 'Operator';
  503.                 $profilesLabels[$l] = $pId;
  504.             } elseif ($p['label'] == 'IND') {
  505.                 $l $lng == 'fr' 'Industriel' 'Industrial';
  506.                 $profilesLabels[$l] = $pId;
  507.             } elseif ($p['label'] == 'MAN') {
  508.                 $l =  $lng == 'fr' 'Fabricant' 'Manufacturer';
  509.                 $profilesLabels[$l] = $pId;
  510.             } elseif ($p['label'] == 'VP') {
  511.                 $l $lng == 'fr' 'Valideur plan' 'Plan validator';
  512.                 $profilesLabels[$l] = $pId;
  513.             } elseif ($p['label'] == 'FM') {
  514.                 $l $lng == 'fr' 'Responsable de formation' 'Formation manager';
  515.                 $profilesLabels[$l] = $pId;
  516.             } elseif ($p['label'] == 'OM') {
  517.                 $l $lng == 'fr' 'Responsable délégué' 'Organization manager';
  518.                 $profilesLabels[$l] = $pId;
  519.             }
  520.         }
  521.         $options['profiles'] = $profilesLabels;
  522.         $options['sites'] = $sites;
  523.         $options['companies'] = $industrials;
  524.         $options['organizations'] = $organizationsList;
  525.         $options['formations'] = $formationsList;
  526.         $label 'nouvel utilisateur';
  527.         $form $this->createForm(BubbleAddUserType::class, NULL$options);
  528.         $form->handleRequest($request);
  529.         if ($form->isSubmitted() && $form->isValid()) {
  530.             $formData $form->getData();
  531.             $data = [
  532.                 'b_id' => (int)$bubbleId,
  533.                 'new_u_email' => $formData['email'],
  534.                 'u_pref_lang' => $formData['lang'],
  535.                 'label' => $userRights['contents'][$request->get("hiddenchoice")]['label']
  536.             ];
  537.             if (array_key_exists("site"$formData)) {
  538.                 if (!is_null($formData['site'])) {
  539.                     $data['site_id'] = $formData['site']->getSId();
  540.                 }
  541.             }
  542.             if (array_key_exists("compagnie"$formData)) {
  543.                 if (!is_null($formData['compagnie'])) {
  544.                     $data['ind_id'] = $formData['compagnie']->getIndId();
  545.                 }
  546.             }
  547.             if (array_key_exists("form_id"$formData)) {
  548.                 if (!is_null($formData['form_id'])) {
  549.                     $data['struct_id'] = $formData['form_id']->getStructId();
  550.                 }
  551.             }
  552.             if (array_key_exists("org_id"$formData)) {
  553.                 if (!is_null($formData['org_id'])) {
  554.                     $data['struct_id'] = $formData['org_id']->getStructId();
  555.                 }
  556.             }
  557.             $response $backend->addBubbleUser($data);
  558.             if ($response  == Response::HTTP_OK) {
  559.                 $this->addFlash('success'sprintf($translator ->trans('L\'utilisateur') . " <strong>%s</strong> " .  sprintf($translator ->trans('a été ajouté à votre compte')), $formData['email']));
  560.                 return $this->redirectToRoute('bubble_user_list');
  561.             } elseif ($response  == Response::HTTP_CONFLICT) {
  562.                 $this->addFlash('warning'sprintf($translator ->trans("L’utilisateur %s existe déjà sur cet espace MainChain"), $formData['email']));
  563.                 return $this->redirectToRoute('bubble_user_list');
  564.             } else {
  565.                 $this->addFlash('errors'sprintf($translator ->trans('Une erreur est survenue lors de l\'ajout de') . ' <strong>%s</strong>'$formData['email']));
  566.                 return $this->redirectToRoute('bubble_user_list');
  567.             }
  568.         }
  569.         if(empty($sites)){
  570.             $url $this->generateUrl('site_add');
  571.             if ($profile['site']['create'] == 1) {
  572.                 $this -> addFlash('info'$translator -> trans('Votre compte ne contient pas de ') . '<a href="' $url '" >' $translator -> trans('site de production'). '</a>, ' $translator -> trans('vous ne pourrez pas ajouter de profil "Opérateur".'));
  573.             } else {
  574.                 $this->addFlash('info'$translator->trans('Votre compte ne contient pas de site de production, vous ne pourrez pas ajouter de profil "Opérateur".'));
  575.             }
  576.         }
  577.         
  578.         return $this->render('bubble/bubble_user_add.html.twig', [
  579.             'form' => $form->createView(),
  580.             'label' => $label,
  581.             'sites' => $sites,
  582.             'industrials' => $industrials,
  583.             'organizations' => $organizationsList,
  584.             'formations' => $formationsList
  585.         ]);
  586.     }
  587.     /**
  588.      * bubbleDeleteProfile
  589.      *
  590.      * @param $u_id
  591.      * @param BubbleBackend $backend
  592.      * @param TranslatorInterface $translator
  593.      * @return RedirectResponse
  594.      * @throws TransportExceptionInterface
  595.      *
  596.      * @Route("/bubble/profile/delete/{u_id}", name="bubble_profile_delete")
  597.      */
  598.     public function bubbleDeleteProfile (
  599.         $u_id,
  600.         BubbleBackend $backend,
  601.         TranslatorInterface $translator
  602.     ): RedirectResponse {
  603.         $session $this->sessionByRequestStack();
  604.         $user $this->getuser();
  605.         $profiles  $user->getProfiles();
  606.         $b_key $session->get('b_key'0);
  607.         $profile $profiles[$b_key];
  608.         $bubbleId $profiles[$b_key]['b_id'];
  609.         if ($profile['user']['delete'] == 0) {
  610.             return $this->redirectToRoute('custom_error_403');
  611.         }
  612.         if ($u_id) {
  613.             $response $backend->deleteBubbleProfile($bubbleId$user$u_id);
  614.             if ($response->getStatusCode() == Response::HTTP_OK || $response->getStatusCode() == Response::HTTP_NO_CONTENT) {
  615.                 $this->addFlash("info"$translator->trans("L'utilisateur a bien été supprimé" ));
  616.                 return $this->redirectToRoute('bubble_user_list');
  617.             } elseif ($response->getStatusCode() == Response::HTTP_UNAUTHORIZED) {
  618.                 $this->addFlash("errors"$translator->trans("Vous n'êtes pas autorisé à supprimer cette utilisateur" ));
  619.                 return $this->redirectToRoute('bubble_user_list');
  620.             } else {
  621.                 $this->addFlash("errors"$translator->trans("Une erreur s'est produite lors de la suppression d'utilisateur. Veuillez vérifier les droits d'accès ou interroger l'administrateur." ));
  622.                 return $this->redirectToRoute('bubble_user_list');
  623.             }
  624.         } else {
  625.             $this->addFlash("errors"$translator->trans("L'utilisateur n'existe pas. Veuillez interroger l'administrateur." ));
  626.             return $this->redirectToRoute('bubble_user_list');
  627.         }
  628.     }
  629.     /**
  630.      * Display single user right
  631.      *
  632.      * @param $pem_id
  633.      * @param BubbleBackend $backend
  634.      * @param TranslatorInterface $translator
  635.      * @param Request $request
  636.      * @return JsonResponse|RedirectResponse|void
  637.      *
  638.      * @Route("/bubble/profiles/user/{u_id}", name="single_user_rights")
  639.      */
  640.     public function singleUserRights(
  641.         $pem_id,
  642.         BubbleBackend $backend,
  643.         TranslatorInterface $translator,
  644.         Request $request
  645.     ) {
  646.         $session $this->sessionByRequestStack();
  647.         $user $this->getuser();
  648.         $profiles  $user->getProfiles();
  649.         $b_key $session->get('b_key'0);
  650.         $profile $profiles[$b_key];
  651.         $bubbleId $profiles[$b_key]['b_id'];
  652.         if ($profile['user']['read'] == 0) {
  653.             return $this->redirectToRoute('custom_error_403');
  654.         }
  655.         $profileUserId $request->request->get('profile_user_id');
  656.         if ($pem_id) {
  657.             $response $backend->getOneProfileRights($bubbleId$profileUserId$pem_id);
  658.             $result json_decode($response->getContent(), true);
  659.             $dataFiltered $this->dataFiltered($result['contents']);
  660.             if ($response->getStatusCode() == Response::HTTP_OK) {
  661.                 if ($request->isXmlHttpRequest()) {
  662.                     foreach ($dataFiltered as $row) {
  663.                         return $this->json([
  664.                             'content' => $row
  665.                         ], '200');
  666.                     }
  667.                 }
  668.                 return $this->redirectToRoute('bubble_user_list');
  669.             } elseif ($response-> getStatusCode() == Response::HTTP_UNAUTHORIZED) {
  670.                 $this->addFlash("errors""single_user_rights unauthorized" );
  671.                 return $this->redirectToRoute('bubble_user_list');
  672.             } else {
  673.                 $this->addFlash("errors"$translator->trans("Une erreur single_user_rights. Veuillez vérifier les droits d'accès ou interroger l'administrateur." ));
  674.                 return $this->redirectToRoute('bubble_user_list');
  675.             }
  676.         }
  677.     }
  678.     /**
  679.      * Display user rigths matrice in popup for update
  680.      * singleUserProfileRights
  681.      *
  682.      * @param $perm_id
  683.      * @param BubbleBackend $bubbleBackend
  684.      * @param TranslatorInterface $translator
  685.      * @param Request $request
  686.      * @return JsonResponse|RedirectResponse|void
  687.      *
  688.      * @Route("/bubble/profiles/rights/{perm_id}", name="single_profiles_rights")
  689.      */
  690.     public function singleUserProfileRights(
  691.         $perm_id,
  692.         BubbleBackend $bubbleBackend,
  693.         TranslatorInterface $translator,
  694.         Request $request
  695.     ) {
  696.         $session $this->sessionByRequestStack();
  697.         $user $this->getuser();
  698.         $profiles  $user->getProfiles();
  699.         $b_key $session->get('b_key'0);
  700.         $profile $profiles[$b_key];
  701.         $bubbleId $profiles[$b_key]['b_id'];
  702.         if ($profile['user']['read'] == 0) {
  703.             return $this->redirectToRoute('custom_error_403');
  704.         }
  705.         $profileUserId $request->request->get('profile_user_id');
  706.         if ($perm_id) {
  707.             $response $bubbleBackend->getOneProfileRightsByPermId($bubbleId$profileUserId$perm_id);
  708.             $result json_decode($response->getContent(), true);
  709.             if ($response->getStatusCode() == Response::HTTP_OK) {
  710.                 if ($request->isXmlHttpRequest()) {
  711.                     return $this->json([
  712.                         'content' => [
  713.                             'result' => $result['contents'][0],
  714.                             'inputsName' => DefaultRights::inputsNameByProfile($result['contents'][0])
  715.                         ]
  716.                     ], '200');
  717.                 }
  718.                 return $this->redirectToRoute('bubble_user_list');
  719.             } elseif ($response-> getStatusCode() == Response::HTTP_UNAUTHORIZED) {
  720.                 $this->addFlash("errors""single_user_rights unauthorized" );
  721.                 return $this->redirectToRoute('bubble_user_list');
  722.             } else {
  723.                 $this->addFlash("errors"$translator->trans("Une erreur single_user_rights. Veuillez vérifier les droits d'accès ou interroger l'administrateur." ));
  724.                 return $this->redirectToRoute('bubble_user_list');
  725.             }
  726.         }
  727.     }
  728.     /**
  729.      * Display all user rights
  730.      *
  731.      * @param BubbleBackend $backend
  732.      * @param TranslatorInterface $translator
  733.      * @param Request $request
  734.      * @return Response
  735.      * @throws ClientExceptionInterface
  736.      * @throws RedirectionExceptionInterface
  737.      * @throws ServerExceptionInterface
  738.      * @throws TransportExceptionInterface
  739.      *
  740.      * @return Response
  741.      *
  742.      * @Route("/bubble/profiles-default", name="all_user_rights")
  743.      */
  744.     public function allUserRights(
  745.         BubbleBackend $backend,
  746.         TranslatorInterface $translator,
  747.         Request $request
  748.     ): Response {
  749.         $session $this->sessionByRequestStack();
  750.         $user $this->getuser();
  751.         $profiles  $user->getProfiles();
  752.         $b_key $session->get('b_key'0);
  753.         $profile $profiles[$b_key];
  754.         $bubbleId $profiles[$b_key]['b_id'];
  755.         if ($profile['user']['read'] == 0) {
  756.             return $this->redirectToRoute('custom_error_403');
  757.         }
  758.         $response $backend->getAllProfileRights($bubbleId$user);
  759.         $result json_decode($response->getContent(), true);
  760.         $dataFiltered $backend->dataFilteredAll($result['contents']);
  761.         if ($response->getStatusCode() == Response::HTTP_OK) {
  762.             if ($request->isXmlHttpRequest()) {
  763.                 $index $request->request->get('index');
  764.                 foreach ($dataFiltered as $k => $row) {
  765.                     if ($result['contents'][$k]['label'] == $index) {
  766.                         return $this->json([
  767.                             'content' => $dataFiltered[$k]
  768.                         ], '200');
  769.                     }
  770.                 }
  771.             }
  772.             return $this->redirectToRoute('bubble_user_add');
  773.         } elseif ($response->getStatusCode() == Response::HTTP_UNAUTHORIZED) {
  774.             $this->addFlash("errors""all_user_rights unauthorized" );
  775.             return $this->redirectToRoute('bubble_user_add');
  776.         } else {
  777.             $this->addFlash("errors"$translator->trans("Une erreur all_user_rights. Veuillez vérifier les droits d'accès ou interroger l'administrateur." ));
  778.             return $this->redirectToRoute('bubble_user_add');
  779.         }
  780.     }
  781.     /**
  782.      * Array for filtering profile
  783.      *
  784.      * @param $data
  785.      * @return array
  786.      */
  787.     private function dataFiltered($data): array
  788.     {
  789.         return [
  790.             'User'             => $data['profiles'][0]['user'],
  791.             'Site'             => $data['profiles'][0]['site'],
  792.             'Industrial'       => $data['profiles'][0]['industrial'],
  793.             'Plan'             => $data['profiles'][0]['plan'],
  794.             'Ecomodel'         => $data['profiles'][0]['eco_model'],
  795.             'Token API Access' => $data['profiles'][0]['token'],
  796.             'Job History'      => $data['profiles'][0]['history'],
  797.             'Quality Control'  => $data['profiles'][0]['quality_control'],
  798.             'Billing Elements' => $data['profiles'][0]['billing'],
  799.             'Job Orders'       => $data['profiles'][0]['job_order'],
  800.             'Printers'         => $data['profiles'][0]['printer'],
  801.             'Dashboard'        => $data['profiles'][0]['dashboard'],
  802.             'Structures'       => $data['profiles'][0]['structures'],
  803.             'Planning'         => $data['profiles'][0]['planning'],
  804.         ];
  805.     }
  806. }