<?php
namespace App\Controller;
use App\Entity\Budget;
use App\Entity\Client;
use App\Entity\Personne;
use App\Entity\User;
use App\Entity\Tache;
use App\Entity\Commande;
use App\Entity\Campagne;
use App\Entity\Connexion;
use App\Entity\OffreCommande;
use DateTime;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/*use Kreait\Firebase\Factory;
use Kreait\Firebase\Request\UpdateUser;*/
class AdminController extends AbstractController
{
/**
* @Route("/", name="index")
*/
public function index(): Response
{
return $this->clients();
}
/**
* @Route("/clients", name="clients")
*/
public function clients(): Response
{
return $this->render('admin/index.html.twig', [
'success' => $_GET['succes'] ?? 0
]);
}
/**
* @Route("/clients/filter/{orderby}/{order}/{actifsOnly}", name="getClientsOffset")
*/
public function getClientsOffset(string $orderby, string $order, string $actifsOnly)
{
$personneRepo = $this->getDoctrine()->getRepository(Personne::class);
$orderby = ($orderby == 'idClient' || $orderby == 'dtExpirationClient' || $orderby == 'creditClient' ? 'c.' : 'p.') . $orderby;
if ($actifsOnly == '1') {
$clientsBy20 = $personneRepo->getClientsActifsOnly($orderby, $order);
} else if ($actifsOnly == '2') {
$clientsBy20 = $personneRepo->getClientsInactifsOnly($orderby, $order);
} else {
$clientsBy20 = $personneRepo->getClients($orderby, $order);
}
$serializer = $this->container->get('serializer');
$response = new Response();
$response->setContent($serializer->serialize($clientsBy20, 'json'));
return $response;
}
/**
* @Route("/client/{id}", name="getClient")
*/
public function getClient(string $id): Response
{
$personneRepo = $this->getDoctrine()->getRepository(Personne::class);
$serializer = $this->container->get('serializer');
return $this->render('admin/edit.html.twig', [
'client' => $serializer->serialize($personneRepo->getClient($id), 'json')
]);
}
/**
* @Route("/client/edit/{id}", name="editClient")
*/
public function editClient(string $id, Request $request)
{
//try {
// Récupération du post et des entités
$post = json_decode($request->getContent(), true)['client'];
$em = $this->getDoctrine()->getManager();
$personneRepo = $this->getDoctrine()->getRepository(Personne::class);
$personne = $personneRepo->find($post['idPersonne']);
$clientRepo = $this->getDoctrine()->getRepository(Client::class);
$client = $clientRepo->find($id);
$client->setPaysClient($post['paysPersonne']);
$userRepo = $this->getDoctrine()->getRepository(User::class);
$user = $userRepo->findOneBy(array('idPersonneUser' => $post['idPersonne'], 'idClientUser' => $id));
//GESTION FIREBASE
/* $factory = (new Factory)
->withServiceAccount($_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . 'firebase_credentials.json')
->withProjectId('previooterrain')
->withDatabaseUri('https://previooterrain.firebaseio.com');*/
/*$auth = $factory->createAuth();
try {
$user = $auth->getUserByEmail($user->getLoginUser());
} catch (Exception $e) {
}*/
/* $uid = $user->uid;
$properties = [
'email' => $post['emailPersonne']
];*/
/*$updatedUser = $auth->updateUser($uid, $properties);
$firestore = $factory->createFirestore();
$database = $firestore->database();
$collectionReference = $database->collection('users');
$test = $collectionReference->where('email', "=", $user->getLoginUser());
$documents = $test->documents();
foreach ($documents as $document) {
$collectionReference->document($document->id())->update([
[
'path' => "email",
'value' => $post['emailPersonne']
]
]);
}*/
//GESTION FIREBASE FINI
// Mise à jour
if ($post['dtExpirationClient'] != "Invalid date") {
$client->setDtExpirationClient(new DateTime($post['dtExpirationClient']));
}
$client->setLimitClient($post['limitClient']);
$client->setCreditClient(str_replace(',', '.', $post['creditClient']));
$personne->setGenrePersonne($post['genrePersonne']);
$personne->setNomPersonne($post['nomPersonne']);
$personne->setPrenomPersonne($post['prenomPersonne']);
$personne->setEmailPersonne($post['emailPersonne']);
if ($post['dtNaissancePersonne'] != "Invalid date") {
$personne->setDtNaissancePersonne(new DateTime($post['dtNaissancePersonne']));
}
$personne->setTelPersonne($post['telPersonne']);
$personne->setPortablePersonne($post['portablePersonne']);
$personne->setAdressePersonne($post['adressePersonne']);
$personne->setAdresse2Personne($post['adresse2Personne']);
$personne->setCpPersonne($post['cpPersonne']);
$personne->setVillePersonne($post['villePersonne']);
$personne->setPaysPersonne($post['paysPersonne']);
$user->setLoginUser($post['emailPersonne']);
// Sauvegarde
//$em->persist($personne);
//$em->persist($client);
$em->flush();
return $this->json('success');
/*} catch (Exception $e) {
return $this->json(['error', $e->getMessage()]);
}*/
}
/**
* @Route("/client/lock/{id}", name="lockClient")
*/
public function lockClient(string $id): Response
{
try {
$em = $this->getDoctrine()->getManager();
$clientRepo = $this->getDoctrine()->getRepository(Client::class);
$client = $clientRepo->find($id);
$client->setIdStatutClient(8);
$em->persist($client);
$em->flush();
return $this->json(['success']);
} catch (Exception $e) {
return $this->json(['error', $e->getMessage()]);
}
}
/**
* @Route("/client/unlock/{id}", name="unlockClient")
*/
public function unlockClient(string $id): Response
{
try {
$em = $this->getDoctrine()->getManager();
$clientRepo = $this->getDoctrine()->getRepository(Client::class);
$client = $clientRepo->find($id);
$client->setIdStatutClient(2);
$em->persist($client);
$em->flush();
return $this->json(['success']);
} catch (Exception $e) {
return $this->json(['error', $e->getMessage()]);
}
}
/**
* @Route("/client/delete/{id}", name="deleteClient")
*/
public function deleteClient(string $id): Response
{
try {
// Entity manager et repositories
$em = $this->getDoctrine()->getManager();
$clientRepo = $this->getDoctrine()->getRepository(Client::class);
$userRepo = $this->getDoctrine()->getRepository(User::class);
$personneRepo = $this->getDoctrine()->getRepository(Personne::class);
$budgetRepo = $this->getDoctrine()->getRepository(Budget::class);
// Suppression des users
foreach ($userRepo->findBy(['idClientUser' => $id]) as $user) {
$em->remove($user);
}
// Suppression des personnes
foreach ($personneRepo->findBy(['idClientPersonne' => $id]) as $personne) {
$em->remove($personne);
}
// Suppression des budgets
foreach ($budgetRepo->findBy(['idClientBudget' => $id]) as $budget) {
$em->remove($budget);
}
// Suppression du client
$em->remove($clientRepo->find($id));
$em->flush();
return $this->json(['success']);
} catch (Exception $e) {
return $this->json(['error', $e->getMessage()]);
}
}
/**
* @Route("/client/info/{id}", name="getInfoClient")
*/
public function getInfoClient(string $id): Response
{
$personneRepo = $this->getDoctrine()->getRepository(Personne::class);
$connexionRepo = $this->getDoctrine()->getRepository(Connexion::class);
$serializer = $this->container->get('serializer');
$client = $personneRepo->getClient($id);
$users = $personneRepo->getUsers($id);
$connexions = $connexionRepo->getConnexionsByIdClient($id);
return $this->render('admin/clientInfo.html.twig', [
'client' => $serializer->serialize($client, 'json'),
'users' => $serializer->serialize($users, 'json'),
'connexions' => $serializer->serialize($connexions, 'json'),
]);
}
/**
* @Route("/client/stats/{id}", name="getStatsClient")
*/
public function getStatsClient(string $id): Response
{
$tacheRepo = $this->getDoctrine()->getRepository(Tache::class);
$clientRepo = $this->getDoctrine()->getRepository(Client::class);
$personneRepo = $this->getDoctrine()->getRepository(Personne::class);
$cmdRepo = $this->getDoctrine()->getRepository(Commande::class);
$offreCom = $this->getDoctrine()->getRepository(OffreCommande::class);
$campagneRepo = $this->getDoctrine()->getRepository(Campagne::class);
$serializer = $this->container->get('serializer');
//client
$client = $personneRepo->getClient($id);
// Missions
$missions = $tacheRepo->CountNumberActionsByClient($id);
// factures
$bills = $cmdRepo->getFacturesSuccesOnlyByClient($id);
// abonnements
$abonnements = $cmdRepo->getAbonnementsOnlyByClient($id);
// offres abonnements
$offresAbo = $offreCom->findAll();
// campagnes
$NbCampagnesEmail = $campagneRepo->getNbCampagnesEmailByClient($id);
$NbCampagnesSMS = $campagneRepo->getNbCampagnesSMSByClient($id);
$campagnes = [$NbCampagnesEmail, $NbCampagnesSMS];
return $this->render('admin/clientStats.html.twig', [
'data' => json_encode($missions),
'bills' => $serializer->serialize($bills, 'json'),
'client' => $serializer->serialize($client, 'json'),
'abonnements' => $serializer->serialize($abonnements, 'json'),
'offres' => $serializer->serialize($offresAbo, 'json'),
'campagnes' => json_encode($campagnes),
]);
}
}