src/Controller/MainController.php line 23
<?php
namespace App\Controller;
use App\Entity\Organisation;
use App\Entity\Signature;
use App\Repository\OrganisationRepository;
use App\Repository\SignatureRepository;
use App\Services\XLSImporter;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\ORMException;
use Doctrine\ORM\OptimisticLockException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends AbstractController
{
#[Route('/', name: 'app_main')]
public function index(): Response
{
return $this->render('main/index.html.twig', [
'controller_name' => 'MainController',
]);
}
#[Route('/import', name: 'app_import')]
public function import(XLSImporter $XLSImporter, OrganisationRepository $organisationRepo): Response
{
$parents=[];
$res= $XLSImporter->import("C:\Users\kille\Desktop\spiclient.xlsx", false, "row");
foreach ($res as $key=>$r){
$contacts =[
1=>[
"val"=>$r[10],
"libelle"=>"Téléphone",
],
2=>[
"val"=>$r[9],
"libelle"=>"Email",
],
3=>[
"val"=>$r[18],
"libelle"=>"Email manager",
]
];
$etablisement =[
"cp"=> $r[8],
"nic"=> null,
"etat"=> "Actif",
"pays"=> "France",
"type"=> $r[24]==0?"Siége":"Ets secondaire",
"ville"=> $r[7],
"region"=> "Île-de-France",
"libelle"=> "APSIE Courbevoie",
"adresseLigne1"=> $r[5],
"adresseLigne2"=> $r[6],
"adresseLigne3"=> null
];
$org=$organisationRepo->findOneBy(["libelle"=>$r[4],"siren"=>$r[42]??"" ]);
if($org!=null)
continue;
$org = new Organisation();
$org->setLibelle($r[4])
->setSiren($r[42]??"")
->setContactsOrganisation($contacts)
->setEtablissement($etablisement)
;
if($r[24]!=0 ){
if(isset($parents[$r[24]]))
{
$orgParent =$organisationRepo->find($parents[$r[24]]);
$org->setParent($orgParent);
}
$organisationRepo->save($org, true);
$parents[$r[24]]=$org->getId();
}
else{
$organisationRepo->save($org, true);
}
}
die();
return $this->render('main/index.html.twig', [
'controller_name' => 'MainController',
]);
}
#[Route('/themes', name: 'app_themes')]
public function test(Request $request, RequestStack $requestStack): Response
{
$session = $requestStack->getSession();
$curTheme=$request->request->get("curTheme");
if($curTheme!=null)
$session->set("curTheme", $curTheme);
$directory ="/css/themes/";
$files=[];
if(file_exists(getcwd().$directory))
$files = array_diff(scandir( getcwd().$directory), array('..', '.'));
$themes=[];
foreach ($files as $f){
$themes[str_replace("-bootstrap.min.css", "" ,$f)]=$f;
}
return $this->render('main/test.html.twig', [
'controller_name' => 'MainController',
"themes"=>$themes,
]);
}
#[Route('/dashboard', name: 'app_dashboard')]
public function showDashboard(Request $request, RequestStack $requestStack): Response{
return $this->render('main/dashboard.html.twig');
}
#[Route('/signature', name: 'app_signature')]
public function newSignature(Request $request)
{
// $form = $this->createForm(AutographType::class);
// $form->handleRequest($request);
//
// if ($form->isSubmitted() && $form->isValid()) {
// $data = $form->getData();
// $signature = $data['signature'];
//
// // Save the signature as an image
// // ...
//
// return new Response('Signature saved!');
// }
return $this->render('main/signature.html.twig');
}
/**
* @throws OptimisticLockException
* @throws ORMException
*/
#[Route('/signature/save', name: 'save_signature')]
public function saveSignature(Request $request, EntityManager $entityManager)
{
$signature= new Signature();
$signer = $request->request->get('signature');
$donnees = json_decode(file_get_contents('php://input'), false);
$usager= $this->getUser();
$userDirectory = '/public/uploads/signatures/'.$usager;
if (!is_dir($userDirectory)) {
mkdir($userDirectory);
}
list($type, $data) = explode(';', $donnees->image);
list(, $image) = explode(',', $data);
$image_decodee = base64_decode($image);
$fichier = 'signature_' . md5(uniqid()) . '.png';
$filePath = $this->getParameter('kernel.project_dir') . $userDirectory . $fichier;
if (file_put_contents($filePath, $image_decodee)) {
$this->addFlash('success', 'Signature saved successfully');
} else {
$this->addFlash('alert', 'Failed to save your signature');
}
//insert to db
$signature->setNomImage($fichier);
$signature->setUsager($this->getUser());
//to get the IP address
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
$IP = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
$IP = $_SERVER['REMOTE_ADDR'];
}
$signature->setAdresseIP($IP);
$entityManager->persist($signature);
$entityManager->flush();
return new JsonResponse(['message' => 'Signature saved', 'filePath' => $filePath], 200, ['Content-Type' => 'image/png']);
}
}