src/Controller/MainController.php line 23

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Organisation;
  4. use App\Entity\Signature;
  5. use App\Repository\OrganisationRepository;
  6. use App\Repository\SignatureRepository;
  7. use App\Services\XLSImporter;
  8. use Doctrine\ORM\EntityManager;
  9. use Doctrine\ORM\Exception\ORMException;
  10. use Doctrine\ORM\OptimisticLockException;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. class MainController extends AbstractController
  18. {
  19.     #[Route('/'name'app_main')]
  20.     public function index(): Response
  21.     {
  22.         return $this->render('main/index.html.twig', [
  23.             'controller_name' => 'MainController',
  24.         ]);
  25.     }
  26.     #[Route('/import'name'app_import')]
  27.     public function import(XLSImporter $XLSImporterOrganisationRepository $organisationRepo): Response
  28.     {
  29.         $parents=[];
  30.         $res$XLSImporter->import("C:\Users\kille\Desktop\spiclient.xlsx"false"row");
  31.         foreach ($res as $key=>$r){
  32.             $contacts =[
  33.                 1=>[
  34.                     "val"=>$r[10],
  35.                     "libelle"=>"Téléphone",
  36.                 ],
  37.                 2=>[
  38.                     "val"=>$r[9],
  39.                     "libelle"=>"Email",
  40.                 ],
  41.                 3=>[
  42.                     "val"=>$r[18],
  43.                     "libelle"=>"Email manager",
  44.                 ]
  45.             ];
  46.             
  47.             $etablisement =[
  48.                 "cp"=> $r[8],
  49.                 "nic"=> null,
  50.                 "etat"=> "Actif",
  51.                 "pays"=> "France",
  52.                 "type"=> $r[24]==0?"Siége":"Ets secondaire",
  53.                 "ville"=> $r[7],
  54.                 "region"=> "Île-de-France",
  55.                 "libelle"=> "APSIE Courbevoie",
  56.                 "adresseLigne1"=> $r[5],
  57.                 "adresseLigne2"=> $r[6],
  58.                 "adresseLigne3"=> null
  59.             ];
  60.             $org=$organisationRepo->findOneBy(["libelle"=>$r[4],"siren"=>$r[42]??"" ]);
  61.             if($org!=null)
  62.                 continue;
  63.             $org =  new Organisation();
  64.             $org->setLibelle($r[4])
  65.                 ->setSiren($r[42]??"")
  66.                 ->setContactsOrganisation($contacts)
  67.                 ->setEtablissement($etablisement)
  68.             ;
  69.             if($r[24]!=){
  70.                 if(isset($parents[$r[24]]))
  71.                 {
  72.                     $orgParent =$organisationRepo->find($parents[$r[24]]);
  73.                     $org->setParent($orgParent);
  74.                 }
  75.                 $organisationRepo->save($orgtrue);
  76.                 $parents[$r[24]]=$org->getId();
  77.             }
  78.             else{
  79.                 $organisationRepo->save($orgtrue);
  80.             }
  81.         }
  82.         die();
  83.         return $this->render('main/index.html.twig', [
  84.             'controller_name' => 'MainController',
  85.         ]);
  86.     }
  87.     #[Route('/themes'name'app_themes')]
  88.     public function test(Request $requestRequestStack $requestStack): Response
  89.     {
  90.         $session $requestStack->getSession();
  91.         $curTheme=$request->request->get("curTheme");
  92.         if($curTheme!=null)
  93.             $session->set("curTheme"$curTheme);
  94.         $directory ="/css/themes/";
  95.         $files=[];
  96.         if(file_exists(getcwd().$directory))
  97.             $files array_diff(scandirgetcwd().$directory), array('..''.'));
  98.         $themes=[];
  99.         foreach ($files as $f){
  100.             $themes[str_replace("-bootstrap.min.css""" ,$f)]=$f;
  101.         }
  102.         return $this->render('main/test.html.twig', [
  103.             'controller_name' => 'MainController',
  104.             "themes"=>$themes,
  105.         ]);
  106.     }
  107.     #[Route('/dashboard'name'app_dashboard')]
  108.     public function showDashboard(Request $requestRequestStack $requestStack): Response{
  109.         return $this->render('main/dashboard.html.twig');
  110.     }
  111.     #[Route('/signature'name'app_signature')]
  112.     public function newSignature(Request $request)
  113.     {
  114. //        $form = $this->createForm(AutographType::class);
  115. //        $form->handleRequest($request);
  116. //
  117. //        if ($form->isSubmitted() && $form->isValid()) {
  118. //            $data = $form->getData();
  119. //            $signature = $data['signature'];
  120. //
  121. //            // Save the signature as an image
  122. //            // ...
  123. //
  124. //            return new Response('Signature saved!');
  125. //        }
  126.         return $this->render('main/signature.html.twig');
  127.     }
  128.     /**
  129.      * @throws OptimisticLockException
  130.      * @throws ORMException
  131.      */
  132.     #[Route('/signature/save'name'save_signature')]
  133.     public function saveSignature(Request $requestEntityManager $entityManager)
  134.     {
  135.         $signature= new Signature();
  136.         $signer $request->request->get('signature');
  137.         $donnees json_decode(file_get_contents('php://input'), false);
  138.         $usager$this->getUser();
  139.         $userDirectory '/public/uploads/signatures/'.$usager;
  140.         if (!is_dir($userDirectory)) {
  141.             mkdir($userDirectory);
  142.         }
  143.         list($type$data) = explode(';'$donnees->image);
  144.         list(, $image) = explode(','$data);
  145.         $image_decodee base64_decode($image);
  146.         $fichier 'signature_' md5(uniqid()) . '.png';
  147.         $filePath $this->getParameter('kernel.project_dir') . $userDirectory $fichier;
  148.         if (file_put_contents($filePath$image_decodee)) {
  149.             $this->addFlash('success''Signature saved successfully');
  150.         } else {
  151.             $this->addFlash('alert''Failed to save your signature');
  152.         }
  153.         //insert to db
  154.         $signature->setNomImage($fichier);
  155.         $signature->setUsager($this->getUser());
  156.         //to get the IP address
  157.         if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  158.             $IP $_SERVER['HTTP_CLIENT_IP'];
  159.         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  160.             $IP $_SERVER['HTTP_X_FORWARDED_FOR'];
  161.         } else {
  162.             $IP $_SERVER['REMOTE_ADDR'];
  163.         }
  164.         $signature->setAdresseIP($IP);
  165.         $entityManager->persist($signature);
  166.         $entityManager->flush();
  167.         return new JsonResponse(['message' => 'Signature saved''filePath' => $filePath], 200, ['Content-Type' => 'image/png']);
  168.     }
  169. }