src/Controller/ClientController.php line 223

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Entity\Client;
  5. use App\Form\ClientType;
  6. use App\Form\ClientEditType;
  7. use App\Form\ClientAdminType;
  8. use App\Service\UtilsService;
  9. use App\Service\BandeauService;
  10. use App\Form\ClientEditUserType;
  11. use App\Repository\ClientRepository;
  12. use Knp\Component\Pager\PaginatorInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Component\Form\Extension\Core\Type\FileType;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. /**
  20.  * @Route("/client")
  21.  */
  22. class ClientController extends AbstractController
  23. {
  24.     private $utilsService;
  25.     private $roleAdmin;
  26.     private $bandeauService;
  27.     public function __construct(UtilsService $utilsServiceBandeauService $bandeauService)
  28.     {
  29.         $this->utilsService $utilsService;
  30.         $this->roleAdmin $this->utilsService->isAdmin();
  31.         $this->bandeauService $bandeauService;
  32.     }
  33.     /**
  34.      * @Route("/", name="app_client_home", methods={"GET"})
  35.      */
  36.     public function home(Request $request): Response
  37.     {
  38.         if(!$this->roleAdmin){
  39.             return $this->redirectIfNotAdmin();
  40.         }
  41.         return $this->render('client/home.html.twig', [
  42.             'user' => $this->getUser(),
  43.             'admin' => $this->roleAdmin,
  44.             'bandeau' => $this->bandeauService->displayPopup()   
  45.         ]);
  46.     }
  47.     /**
  48.      * @Route("/liste", name="app_client_index", methods={"GET"})
  49.      */
  50.     public function index(ClientRepository $clientRepositoryPaginatorInterface $paginatorRequest $request): Response
  51.     {
  52.         if(!$this->roleAdmin){
  53.             return $this->redirectIfNotAdmin();
  54.         }
  55.         return $this->render('client/index.html.twig', [
  56.             'clients' => $clientRepository->findAll(),
  57.             'user' => $this->getUser(),
  58.             'admin' => $this->roleAdmin,
  59.             'bandeau' => $this->bandeauService->displayPopup()   
  60.         ]);
  61.     }
  62.     /**
  63.      * @Route("/new", name="app_client_new", methods={"GET", "POST"})
  64.      */
  65.     public function new(Request $requestClientRepository $clientRepositoryUserPasswordHasherInterface $passwordHasher): Response
  66.     {
  67.         if(!$this->roleAdmin){
  68.             return $this->redirectIfNotAdmin();
  69.         }
  70.         
  71.         $client = new Client();
  72.         $form $this->createForm(ClientType::class, $client);
  73.         $form->handleRequest($request);
  74.         if ($form->isSubmitted() && $form->isValid()) {    
  75.             $password $passwordHasher->hashPassword($client->getUser(), $client->getUser()->getPlainPassword());
  76.             $client->getUser()->setPassword($password);
  77.             // 4) save the User!
  78.             $entityManager $this->getDoctrine()->getManager();
  79.             $entityManager->persist($client->getUser());
  80.             $entityManager->flush();
  81.                         
  82.             $clientRepository->add($clienttrue);
  83.             $indexData = [
  84.                 'id' => $client->getId(),
  85.                 'raison_sociale' => $client->getRaisonSociale()
  86.             ];
  87.     
  88.             $this->utilsService->updateIndex('client'$indexData);
  89.             return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
  90.         }
  91.         return $this->renderForm('client/new.html.twig', [
  92.             'client' => $client,
  93.             'form' => $form,
  94.             'admin' => $this->roleAdmin,
  95.             'user' => $this->getUser(),
  96.             'bandeau' => $this->bandeauService->displayPopup()   
  97.         ]);
  98.     }
  99.         /**
  100.      * @Route("/newBulk", name="app_client_new_bulk", methods={"GET", "POST"})
  101.      */
  102.     public function newBulk(Request $requestClientRepository $clientRepositoryUserPasswordHasherInterface $passwordHasher): Response
  103.     {
  104.         if(!$this->roleAdmin){
  105.             return $this->redirectIfNotAdmin();
  106.         }
  107.         $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();     
  108.         $form $this->createFormBuilder()
  109.                     ->add('templateExcel'FileType::class, [
  110.                         "label" => "Choisir un fichier",
  111.                         "required" => true,
  112.                         'data_class' => null,
  113.                         "attr" => ["hidden" => true]
  114.                     ])  
  115.                     ->getForm();
  116.         $form->handleRequest($request);
  117.         if ($form->isSubmitted()) {    
  118.             $spreadsheet $reader->load($_FILES['form']['tmp_name']['templateExcel']);
  119.             $sheetData $spreadsheet->getActiveSheet()->toArray(nulltruetruetrue);
  120.     
  121.             for ($i 2$i <= count($sheetData); $i++) {
  122.                 $client = new Client();
  123.                 $user = new User();
  124.     
  125.                 /**
  126.                  * [A] - Email de connexion
  127.                  * [B] - Mot de passe
  128.                  * [C] - Raison sociale
  129.                  * [D] - Siret
  130.                  * [E] - EORI
  131.                  * [F] - Id client MBE hub
  132.                  * [G] - Nom contact
  133.                  * [H] - Prénom contact
  134.                  * [I] - Email contact
  135.                  * [J] - Téléphone contact
  136.                  * [K] - Code postal
  137.                  * [L] - Ville
  138.                  * [M] - Adresse
  139.                  * [N] - Assurance par défaut Oui / non
  140.                  * [O] - Emballage par défaut Oui / non
  141.                  */
  142.                 
  143.                 //set UserData
  144.                 $password $passwordHasher->hashPassword($user$sheetData[$i]['B']);
  145.                 $user->setPassword($password);
  146.                 $user->setEmail($sheetData[$i]['A']);
  147.     
  148.                 //set clientData
  149.                 $client->setUser($user);
  150.                 $client->setRaisonSociale($sheetData[$i]['C']);
  151.                 $client->setEori($sheetData[$i]['E']);
  152.                 $client->setIdClientMBEHub($sheetData[$i]['F']);
  153.                 $client->setNomContact($sheetData[$i]['G']);
  154.                 $client->setPrenomContact($sheetData[$i]['H']);
  155.                 $client->setEmailContact($sheetData[$i]['I']);
  156.                 $client->setTelephoneContact($sheetData[$i]['J']);
  157.                 $client->setAdresse($sheetData[$i]['K']);
  158.                 $client->setVille($sheetData[$i]['L']);
  159.                 $client->setCodePostal($sheetData[$i]['M']);
  160.                 $client->setAssuranceDefaut($sheetData[$i]['N']);
  161.                 $client->setEmballageDefaut($sheetData[$i]['O']);
  162.     
  163.                 $entityManager $this->getDoctrine()->getManager();
  164.                 $entityManager->persist($client->getUser());
  165.                 $entityManager->flush();
  166.                             
  167.                 $clientRepository->add($clienttrue);
  168.             }
  169.             $this->addFlash("info"count($sheetData) - ' Caviste ajouter');
  170.             return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
  171.         }
  172.         return $this->renderForm('client/newBulk.html.twig', [
  173.             'admin' => $this->roleAdmin,
  174.             'user' => $this->getUser(),
  175.             'form' => $form,
  176.             'bandeau' => $this->bandeauService->displayPopup()   
  177.         ]);
  178.     }
  179.     /**
  180.      * @Route("/{id}", name="app_client_show", methods={"GET"})
  181.      */
  182.     public function show(Client $client): Response
  183.     {   
  184.         $user $this->getUser();
  185.         if($user->getClient()->getId() != $client->getId()){
  186.             return $this->redirectIfNotAdmin();
  187.         }
  188.         return $this->render('client/show.html.twig', [
  189.             'client' => $client,
  190.             'user' => $user,
  191.             'admin' => $this->roleAdmin,
  192.             'bandeau' => $this->bandeauService->displayPopup()   
  193.         ]);
  194.     }
  195.     /**
  196.      * @Route("/{id}/edit", name="app_client_edit", methods={"GET", "POST"})
  197.      */
  198.     public function edit(Request $requestClient $clientClientRepository $clientRepository): Response
  199.     {
  200.         $user $this->getUser();
  201.         if(!$this->roleAdmin){
  202.             if($user->getClient()->getId() != $client->getId()){
  203.                 return $this->redirectIfNotAdmin();
  204.             }
  205.         }
  206.         $form $this->roleAdmin $this->createForm(ClientAdminType::class, $client) : $this->createForm(ClientEditType::class, $client);
  207.         $form->handleRequest($request);
  208.         if ($form->isSubmitted() && $form->isValid()) {
  209.             if($_POST["client_admin"]['assuranceDefaut'] == NULL || $_POST["client_admin"]['assuranceDefaut'] == "false" || $_POST["client_admin"]['assuranceDefaut'] == false || $_POST["client_admin"]['assuranceDefaut'] == ""){
  210.                $client->setAssuranceDefaut(0);
  211.             }
  212.             if($_POST["client_admin"]['assuranceDefaut'] == "true"){
  213.                 $client->setAssuranceDefaut(1);
  214.             }
  215.             if($_POST["client_admin"]['emballageDefaut'] == NULL || $_POST["client_admin"]['emballageDefaut'] == "false" || $_POST["client_admin"]['emballageDefaut'] == false || $_POST["client_admin"]['emballageDefaut'] == ""){
  216.                 $client->setEmballageDefaut(0);
  217.              }
  218.             if($_POST["client_admin"]['emballageDefaut'] == "true"){
  219.                 $client->setEmballageDefaut(1);
  220.             }
  221.             
  222.             $clientRepository->add($clienttrue);
  223.             return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
  224.         }
  225.         return $this->renderForm('client/edit.html.twig', [
  226.             'client' => $client,
  227.             'form' => $form,
  228.             'admin' => $this->roleAdmin,
  229.             'user' => $this->getUser(),
  230.             'edit' => 'client',
  231.             'bandeau' => $this->bandeauService->displayPopup()     
  232.         ]);
  233.     }
  234.     /**
  235.      * @Route("/{id}/editUser", name="app_client_edit_user", methods={"GET", "POST"})
  236.      */
  237.     public function editUser(Request $requestClient $clientClientRepository $clientRepositoryUserPasswordHasherInterface $passwordHasher): Response
  238.     {
  239.         $user $this->getUser();
  240.         if($user->getClient()->getId() != $client->getId()){
  241.             return $this->redirectIfNotAdmin();
  242.         }
  243.         $form $this->createForm(ClientEditUserType::class, $client);
  244.         $form->handleRequest($request);
  245.         if ($form->isSubmitted() && $form->isValid()) {
  246.             if($client->getUser() != null){
  247.                 $password $passwordHasher->hashPassword($client->getUser(), $client->getUser()->getPlainPassword());
  248.                 $client->getUser()->setPassword($password);
  249.     
  250.                 // 4) save the User!
  251.                 $entityManager $this->getDoctrine()->getManager();
  252.                 $entityManager->persist($client->getUser());
  253.                 $entityManager->flush();
  254.             }
  255.             $clientRepository->add($clienttrue);
  256.             return $this->redirectToRoute('app_client_index', [], Response::HTTP_SEE_OTHER);
  257.         }
  258.         return $this->renderForm('client/edit.html.twig', [
  259.             'client' => $client,
  260.             'form' => $form,
  261.             'admin' => $this->roleAdmin,
  262.             'user' => $this->getUser(),   
  263.             'edit' => 'user',
  264.             'bandeau' => $this->bandeauService->displayPopup()            
  265.         ]);
  266.     }
  267.     public function redirectIfNotAdmin(): Response
  268.     {
  269.         return $this->redirectToRoute('app_commande_index', [], Response::HTTP_SEE_OTHER);
  270.     }
  271. }