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