<?php
namespace App\Controller;
use App\Entity\User;
use App\Entity\Client;
use App\Form\ClientType;
use App\Form\ClientEditType;
use App\Form\ClientAdminType;
use App\Service\UtilsService;
use App\Service\BandeauService;
use App\Form\ClientEditUserType;
use App\Repository\ClientRepository;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
/**
* @Route("/client")
*/
class ClientController extends AbstractController
{
private $utilsService;
private $roleAdmin;
private $bandeauService;
public function __construct(UtilsService $utilsService, BandeauService $bandeauService)
{
$this->utilsService = $utilsService;
$this->roleAdmin = $this->utilsService->isAdmin();
$this->bandeauService = $bandeauService;
}
/**
* @Route("/", name="app_client_home", methods={"GET"})
*/
public function home(Request $request): Response
{
if(!$this->roleAdmin){
return $this->redirectIfNotAdmin();
}
return $this->render('client/home.html.twig', [
'user' => $this->getUser(),
'admin' => $this->roleAdmin,
'bandeau' => $this->bandeauService->displayPopup()
]);
}
/**
* @Route("/liste", name="app_client_index", methods={"GET"})
*/
public function index(ClientRepository $clientRepository, PaginatorInterface $paginator, Request $request): Response
{
if(!$this->roleAdmin){
return $this->redirectIfNotAdmin();
}
return $this->render('client/index.html.twig', [
'clients' => $clientRepository->findAll(),
'user' => $this->getUser(),
'admin' => $this->roleAdmin,
'bandeau' => $this->bandeauService->displayPopup()
]);
}
/**
* @Route("/new", name="app_client_new", methods={"GET", "POST"})
*/
public function new(Request $request, ClientRepository $clientRepository, UserPasswordHasherInterface $passwordHasher): Response
{
if(!$this->roleAdmin){
return $this->redirectIfNotAdmin();
}
$client = new Client();
$form = $this->createForm(ClientType::class, $client);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $passwordHasher->hashPassword($client->getUser(), $client->getUser()->getPlainPassword());
$client->getUser()->setPassword($password);
// 4) save the User!
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($client->getUser());
$entityManager->flush();
$clientRepository->add($client, true);
$indexData = [
'id' => $client->getId(),
'raison_sociale' => $client->getRaisonSociale()
];
$this->utilsService->updateIndex('client', $indexData);
return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('client/new.html.twig', [
'client' => $client,
'form' => $form,
'admin' => $this->roleAdmin,
'user' => $this->getUser(),
'bandeau' => $this->bandeauService->displayPopup()
]);
}
/**
* @Route("/newBulk", name="app_client_new_bulk", methods={"GET", "POST"})
*/
public function newBulk(Request $request, ClientRepository $clientRepository, UserPasswordHasherInterface $passwordHasher): Response
{
if(!$this->roleAdmin){
return $this->redirectIfNotAdmin();
}
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$form = $this->createFormBuilder()
->add('templateExcel', FileType::class, [
"label" => "Choisir un fichier",
"required" => true,
'data_class' => null,
"attr" => ["hidden" => true]
])
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted()) {
$spreadsheet = $reader->load($_FILES['form']['tmp_name']['templateExcel']);
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
for ($i = 2; $i <= count($sheetData); $i++) {
$client = new Client();
$user = new User();
/**
* [A] - Email de connexion
* [B] - Mot de passe
* [C] - Raison sociale
* [D] - Siret
* [E] - EORI
* [F] - Id client MBE hub
* [G] - Nom contact
* [H] - Prénom contact
* [I] - Email contact
* [J] - Téléphone contact
* [K] - Code postal
* [L] - Ville
* [M] - Adresse
* [N] - Assurance par défaut Oui / non
* [O] - Emballage par défaut Oui / non
*/
//set UserData
$password = $passwordHasher->hashPassword($user, $sheetData[$i]['B']);
$user->setPassword($password);
$user->setEmail($sheetData[$i]['A']);
//set clientData
$client->setUser($user);
$client->setRaisonSociale($sheetData[$i]['C']);
$client->setEori($sheetData[$i]['E']);
$client->setIdClientMBEHub($sheetData[$i]['F']);
$client->setNomContact($sheetData[$i]['G']);
$client->setPrenomContact($sheetData[$i]['H']);
$client->setEmailContact($sheetData[$i]['I']);
$client->setTelephoneContact($sheetData[$i]['J']);
$client->setAdresse($sheetData[$i]['K']);
$client->setVille($sheetData[$i]['L']);
$client->setCodePostal($sheetData[$i]['M']);
$client->setAssuranceDefaut($sheetData[$i]['N']);
$client->setEmballageDefaut($sheetData[$i]['O']);
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($client->getUser());
$entityManager->flush();
$clientRepository->add($client, true);
}
$this->addFlash("info", count($sheetData) - 1 . ' Caviste ajouter');
return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('client/newBulk.html.twig', [
'admin' => $this->roleAdmin,
'user' => $this->getUser(),
'form' => $form,
'bandeau' => $this->bandeauService->displayPopup()
]);
}
/**
* @Route("/{id}", name="app_client_show", methods={"GET"})
*/
public function show(Client $client): Response
{
$user = $this->getUser();
if($user->getClient()->getId() != $client->getId()){
return $this->redirectIfNotAdmin();
}
return $this->render('client/show.html.twig', [
'client' => $client,
'user' => $user,
'admin' => $this->roleAdmin,
'bandeau' => $this->bandeauService->displayPopup()
]);
}
/**
* @Route("/{id}/edit", name="app_client_edit", methods={"GET", "POST"})
*/
public function edit(Request $request, Client $client, ClientRepository $clientRepository): Response
{
$user = $this->getUser();
if(!$this->roleAdmin){
if($user->getClient()->getId() != $client->getId()){
return $this->redirectIfNotAdmin();
}
}
$form = $this->roleAdmin ? $this->createForm(ClientAdminType::class, $client) : $this->createForm(ClientEditType::class, $client);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if($_POST["client_admin"]['assuranceDefaut'] == NULL || $_POST["client_admin"]['assuranceDefaut'] == "false" || $_POST["client_admin"]['assuranceDefaut'] == false || $_POST["client_admin"]['assuranceDefaut'] == ""){
$client->setAssuranceDefaut(0);
}
if($_POST["client_admin"]['assuranceDefaut'] == "true"){
$client->setAssuranceDefaut(1);
}
if($_POST["client_admin"]['emballageDefaut'] == NULL || $_POST["client_admin"]['emballageDefaut'] == "false" || $_POST["client_admin"]['emballageDefaut'] == false || $_POST["client_admin"]['emballageDefaut'] == ""){
$client->setEmballageDefaut(0);
}
if($_POST["client_admin"]['emballageDefaut'] == "true"){
$client->setEmballageDefaut(1);
}
$clientRepository->add($client, true);
return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('client/edit.html.twig', [
'client' => $client,
'form' => $form,
'admin' => $this->roleAdmin,
'user' => $this->getUser(),
'edit' => 'client',
'bandeau' => $this->bandeauService->displayPopup()
]);
}
/**
* @Route("/{id}/editUser", name="app_client_edit_user", methods={"GET", "POST"})
*/
public function editUser(Request $request, Client $client, ClientRepository $clientRepository, UserPasswordHasherInterface $passwordHasher): Response
{
$user = $this->getUser();
if($user->getClient()->getId() != $client->getId()){
return $this->redirectIfNotAdmin();
}
$form = $this->createForm(ClientEditUserType::class, $client);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
if($client->getUser() != null){
$password = $passwordHasher->hashPassword($client->getUser(), $client->getUser()->getPlainPassword());
$client->getUser()->setPassword($password);
// 4) save the User!
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($client->getUser());
$entityManager->flush();
}
$clientRepository->add($client, true);
return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('client/edit.html.twig', [
'client' => $client,
'form' => $form,
'admin' => $this->roleAdmin,
'user' => $this->getUser(),
'edit' => 'user',
'bandeau' => $this->bandeauService->displayPopup()
]);
}
public function redirectIfNotAdmin(): Response
{
return $this->redirectToRoute('app_commande_index', [], Response::HTTP_SEE_OTHER);
}
}