<?php
namespace App\Controller\Utilisateur;
use App\Controller\Adresses\Commande;
use App\Entity\Adresses\Adresse;
use App\Entity\Clients\Client;
use App\Entity\Utilisateur\Utilisateur;
use App\Entity\GestionComerciale\StatutCommande;
use App\Model\GestionCommerciale\TypeDocumentCommercial;
use App\Form\GestionComerciale\CommandeType;
use App\Form\Utilisateur\ChangerClientType;
use App\Form\Utilisateur\ChangePasswordType;
use App\Library\Datatable\Util\Datatable;
use App\Library\Uploader\Services\FileUploader;
use App\Security\Voter\EntityVoter;
use App\Service\GestionComerciale\EmailService;
use App\Service\Utilisateur\ColonneTableauService;
use Doctrine\ORM\EntityManagerInterface;
use JMS\Serializer\SerializerBuilder;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class ProfilController extends AbstractController
{
/**
* @Route("/compte/profil", name="dtc_utilisateur_profil")
*/
public function profilAction(
Request $request,
EntityManagerInterface $em,
UserPasswordHasherInterface $passwordHasher,
Datatable $datatable,
TranslatorInterface $translator,
ColonneTableauService $serviceColonneTableau
)
{
$user = $this->getUser();
$errors = [];
$formPassword = $this->createForm(ChangePasswordType::class);
$formPassword->handleRequest($request);
if ($formPassword->isSubmitted() ) {
$currentPassword = $formPassword->get('currentPassword')->getData();
$newPassword = $formPassword->get('newPassword')->getData();
if($formPassword->isValid()) {
// Vérification de l'ancien mot de passe
if(!$passwordHasher->isPasswordValid($user, $currentPassword)) {
$this->addFlash('error', 'Mot de passe actuel incorrect.');
return $this->redirect($this->generateUrl('dtc_utilisateur_profil') . '#password-form');
} else {
// Hash du nouveau mot de passe
$user->setPassword(
$passwordHasher->hashPassword($user, $newPassword)
);
$em->flush();
$this->addFlash('success', 'Votre mot de passe a bien été modifié.');
return $this->redirectToRoute('dtc_utilisateur_profil');
}
}
else {
$errors = $formPassword->getErrors(true, true);
// return $this->redirect($this->generateUrl('dtc_utilisateur_profil') . '#change-password-form');
}
}
$response = new Response();
$param = $request->query->all();
$adresseFacturation = NULL;
$equipiers = [];
if(is_object($user->getClient())) {
$adresseFacturation = $em->getRepository(Adresse::class)->findOneBy(["client" =>$user->getClient(), "facturationDefaut" => "1"] );
$equipiers = $em->getRepository(Utilisateur::class)->findBy(["client" =>$user->getClient(),"visible"=>1] );
}
return $this->render('Utilisateur/Profil/profil.html.twig',
[
'parametres'=> $param,
'adresseFacturation'=> $adresseFacturation,
'equipiers'=> $equipiers,
'formPassword' => $formPassword->createView(),
'errors' => $errors,
]
,$response);
}
/**
* @Route("/compte/definir-client/{id}", name="dtc_definir_client", requirements={"id"="\d+"})
*/
public function definirClientAction(Client $client,EntityManagerInterface $em ): Response
{
$user = $this->getUser();
if ($user->getType()->getId() !== 1) {
throw $this->createAccessDeniedException();
}
if (!$client) {
throw $this->createNotFoundException();
}
$user->setClient($client);
$em->persist($user);
$commandeRepo = $em->getRepository(\App\Entity\GestionComerciale\Commande::class);
$statutPanier = $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER, "ordre" => "1"] );
$panier = $commandeRepo->findOneBy([
'statutCommande' => $statutPanier,
'utilisateur' => $user,
'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
]);
if ($panier) {
$panier->setClient($user->getClient());
$em->persist($panier);
}
$em->flush();
$em->refresh($user);
return $this->redirectToRoute('dtcfo_homepage');
}
/**
* @Route("/compte/change-client", name="dtc_change_client_modal")
*/
public function ChangerClientModalAction(Request$request, EntityManagerInterface $em,TranslatorInterface $translator,ValidatorInterface $validator)
{
$titre_modal = "Changer de client";
$errors=[];
$user = $this->getUser();
if(!$user or !$user->getType() or $user->getType()->getId() != 1) {
$titre_modal = "ERREUR";
$errors="ERREUR";
$rendu = $this->renderView('FO/Supprimer/supprimer_impossible.html.twig',['errors' => $errors]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
$user->setClient(NULL);
$form = $this->createForm(ChangerClientType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted()) {
if ($form->isValid()) {
$em->persist($user);
$commandeRepo = $em->getRepository(\App\Entity\GestionComerciale\Commande::class);
$statutPanier = $em->getRepository(StatutCommande::class)->findOneBy(["documentCommercial" => TypeDocumentCommercial::PANIER, "ordre" => "1"] );
// Récupérer ou créer le panier
$panier = $commandeRepo->findOneBy([
'statutCommande' => $statutPanier,
'utilisateur' => $user,
'typeDocumentCommercial' => TypeDocumentCommercial::PANIER
]);
if ($panier) {
$panier->setClient($user->getClient());
$em->persist($panier);
}
$em->flush();
$em->refresh($user);
return new Response(
json_encode(['valide' => '1', 'referenceClient' => $user->getClient()->getReference(), 'nomClient' => $user->getClient()->getNom(), 'type' => 'callbackChangeClient']),
200,
['Content-Type' => 'application/json']
);
}
else {
$errors = $validator->validate($user);
}
}
$rendu = $this->renderView('Utilisateur/Profil/changer_client_modal.html.twig',['form' => $form->createView(),'errors' => $errors]);
return new JsonResponse(['rendu' => $rendu, 'valide' => '0', 'url' => '', 'titre' => $titre_modal]);
}
/**
* @Route("/informations-manquantes", name="dtc_infos_manquantes")
*/
public function faqAction(Request$request,TranslatorInterface $translator)
{
$user = $this->getUser();
if($user->getClient()) return $this->redirectToRoute('dtcfo_homepage');
return $this->render('Utilisateur/Profil/selection_client.html.twig',[]);
}
}