src/Controller/Utilisateur/ProfilController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Utilisateur;
  3. use App\Controller\Adresses\Commande;
  4. use App\Entity\Adresses\Adresse;
  5. use App\Entity\Clients\Client;
  6. use App\Entity\Utilisateur\Utilisateur;
  7. use App\Entity\GestionComerciale\StatutCommande;
  8. use App\Model\GestionCommerciale\TypeDocumentCommercial;
  9. use App\Form\GestionComerciale\CommandeType;
  10. use App\Form\Utilisateur\ChangerClientType;
  11. use App\Form\Utilisateur\ChangePasswordType;
  12. use App\Library\Datatable\Util\Datatable;
  13. use App\Library\Uploader\Services\FileUploader;
  14. use App\Security\Voter\EntityVoter;
  15. use App\Service\GestionComerciale\EmailService;
  16. use App\Service\Utilisateur\ColonneTableauService;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use JMS\Serializer\SerializerBuilder;
  19. use Knp\Component\Pager\PaginatorInterface;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\DependencyInjection\Container;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\HttpFoundation\JsonResponse;
  24. use Symfony\Component\HttpFoundation\Response;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\HttpFoundation\ResponseHeaderBag;
  27. use Symfony\Component\HttpFoundation\Cookie;
  28. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  29. use Symfony\Component\Validator\Validator\ValidatorInterface;
  30. use Symfony\Contracts\Translation\TranslatorInterface;
  31. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  32. class ProfilController extends AbstractController
  33. {
  34.     /**
  35.      * @Route("/compte/profil", name="dtc_utilisateur_profil")
  36.      */
  37.     public function profilAction(
  38.         Request $request,
  39.         EntityManagerInterface $em,
  40.         UserPasswordHasherInterface $passwordHasher,
  41.         Datatable $datatable,
  42.         TranslatorInterface $translator,
  43.         ColonneTableauService $serviceColonneTableau
  44.     )
  45.     {
  46.         $user $this->getUser();
  47.         $errors = [];
  48.         $formPassword $this->createForm(ChangePasswordType::class);
  49.         $formPassword->handleRequest($request);
  50.         if ($formPassword->isSubmitted() ) {
  51.             $currentPassword $formPassword->get('currentPassword')->getData();
  52.             $newPassword     $formPassword->get('newPassword')->getData();
  53.             if($formPassword->isValid()) {
  54.                 // Vérification de l'ancien mot de passe
  55.                 if(!$passwordHasher->isPasswordValid($user$currentPassword)) {
  56.                     $this->addFlash('error''Mot de passe actuel incorrect.');
  57.                     return $this->redirect($this->generateUrl('dtc_utilisateur_profil') . '#password-form');
  58.                 } else {
  59.                     // Hash du nouveau mot de passe
  60.                     $user->setPassword(
  61.                         $passwordHasher->hashPassword($user$newPassword)
  62.                     );
  63.                     $em->flush();
  64.                     $this->addFlash('success''Votre mot de passe a bien été modifié.');
  65.                     return $this->redirectToRoute('dtc_utilisateur_profil');
  66.                 }
  67.             }
  68.             else {
  69.                 $errors $formPassword->getErrors(truetrue);
  70.                // return $this->redirect($this->generateUrl('dtc_utilisateur_profil') . '#change-password-form');
  71.             }
  72.         }
  73.         $response = new Response();
  74.         $param $request->query->all();
  75.         $adresseFacturation NULL;
  76.         $equipiers = [];
  77.         if(is_object($user->getClient()))  {
  78.             $adresseFacturation $em->getRepository(Adresse::class)->findOneBy(["client" =>$user->getClient(), "facturationDefaut" => "1"] );
  79.             $equipiers $em->getRepository(Utilisateur::class)->findBy(["client" =>$user->getClient(),"visible"=>1] );
  80.         }
  81.         return $this->render('Utilisateur/Profil/profil.html.twig',
  82.             [
  83.                 'parametres'=> $param,
  84.                 'adresseFacturation'=> $adresseFacturation,
  85.                 'equipiers'=> $equipiers,
  86.                 'formPassword' => $formPassword->createView(),
  87.                 'errors' => $errors,
  88.             ]
  89.             ,$response);
  90.     }
  91.     /**
  92.      * @Route("/compte/definir-client/{id}", name="dtc_definir_client", requirements={"id"="\d+"})
  93.      */
  94.     public function definirClientAction(Client $client,EntityManagerInterface $em ): Response
  95.     {
  96.         $user $this->getUser();
  97.         if ($user->getType()->getId() !== 1) {
  98.             throw $this->createAccessDeniedException();
  99.         }
  100.         if (!$client) {
  101.             throw $this->createNotFoundException();
  102.         }
  103.         $user->setClient($client);
  104.         $em->persist($user);
  105.         $commandeRepo $em->getRepository(\App\Entity\GestionComerciale\Commande::class);
  106.         $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  107.         $panier $commandeRepo->findOneBy([
  108.             'statutCommande' => $statutPanier,
  109.             'utilisateur' => $user,
  110.             'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  111.         ]);
  112.         if ($panier) {
  113.             $panier->setClient($user->getClient());
  114.             $em->persist($panier);
  115.         }
  116.         $em->flush();
  117.         $em->refresh($user);
  118.         return $this->redirectToRoute('dtcfo_homepage');
  119.     }
  120.     /**
  121.      * @Route("/compte/change-client", name="dtc_change_client_modal")
  122.      */
  123.     public function ChangerClientModalAction(Request$requestEntityManagerInterface $em,TranslatorInterface $translator,ValidatorInterface $validator)
  124.     {
  125.         $titre_modal "Changer de client";
  126.         $errors=[];
  127.         $user $this->getUser();
  128.         if(!$user or !$user->getType() or $user->getType()->getId() != 1) {
  129.             $titre_modal "ERREUR";
  130.             $errors="ERREUR";
  131.             $rendu     $this->renderView('FO/Supprimer/supprimer_impossible.html.twig',['errors' => $errors]);
  132.             return new JsonResponse(['rendu' => $rendu'valide' => '0''url' => '''titre' => $titre_modal]);
  133.         }
  134.         $user->setClient(NULL);
  135.         $form   $this->createForm(ChangerClientType::class, $user);
  136.         $form->handleRequest($request);
  137.         if ($form->isSubmitted()) {
  138.             if ($form->isValid()) {
  139.                 $em->persist($user);
  140.                 $commandeRepo $em->getRepository(\App\Entity\GestionComerciale\Commande::class);
  141.                 $statutPanier $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER"ordre" => "1"] );
  142.                 // Récupérer ou créer le panier
  143.                 $panier $commandeRepo->findOneBy([
  144.                     'statutCommande' => $statutPanier,
  145.                     'utilisateur' => $user,
  146.                     'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
  147.                 ]);
  148.                 if ($panier) {
  149.                     $panier->setClient($user->getClient());
  150.                     $em->persist($panier);
  151.                 }
  152.                 $em->flush();
  153.                 $em->refresh($user);
  154.                 return new Response(
  155.                     json_encode(['valide' => '1''referenceClient' => $user->getClient()->getReference(), 'nomClient' => $user->getClient()->getNom(), 'type' => 'callbackChangeClient']),
  156.                     200,
  157.                     ['Content-Type' => 'application/json']
  158.                 );
  159.             }
  160.             else {
  161.                 $errors $validator->validate($user);
  162.             }
  163.         }
  164.         $rendu     $this->renderView('Utilisateur/Profil/changer_client_modal.html.twig',['form' => $form->createView(),'errors' => $errors]);
  165.         return new JsonResponse(['rendu' => $rendu'valide' => '0''url' => '''titre' => $titre_modal]);
  166.     }
  167.     /**
  168.      * @Route("/informations-manquantes", name="dtc_infos_manquantes")
  169.      */
  170.     public function faqAction(Request$request,TranslatorInterface $translator)
  171.     {
  172.         $user $this->getUser();
  173.         if($user->getClient()) return $this->redirectToRoute('dtcfo_homepage');
  174.         return $this->render('Utilisateur/Profil/selection_client.html.twig',[]);
  175.     }
  176. }