src/Controller/AdminController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Budget;
  4. use App\Entity\Client;
  5. use App\Entity\Personne;
  6. use App\Entity\User;
  7. use App\Entity\Tache;
  8. use App\Entity\Commande;
  9. use App\Entity\Campagne;
  10. use App\Entity\Connexion;
  11. use App\Entity\OffreCommande;
  12. use DateTime;
  13. use Exception;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Routing\Annotation\Route;
  18. /*use Kreait\Firebase\Factory;
  19. use Kreait\Firebase\Request\UpdateUser;*/
  20. class AdminController extends AbstractController
  21. {
  22.     /**
  23.      * @Route("/", name="index")
  24.      */
  25.     public function index(): Response
  26.     {
  27.         return $this->clients();
  28.     }
  29.     /**
  30.      * @Route("/clients", name="clients")
  31.      */
  32.     public function clients(): Response
  33.     {
  34.         return $this->render('admin/index.html.twig', [
  35.             'success' => $_GET['succes'] ?? 0
  36.         ]);
  37.     }
  38.     /**
  39.      * @Route("/clients/filter/{orderby}/{order}/{actifsOnly}", name="getClientsOffset")
  40.      */
  41.     public function getClientsOffset(string $orderbystring $orderstring $actifsOnly)
  42.     {
  43.         $personneRepo $this->getDoctrine()->getRepository(Personne::class);
  44.         $orderby = ($orderby == 'idClient' || $orderby == 'dtExpirationClient' || $orderby == 'creditClient' 'c.' 'p.') . $orderby;
  45.         if ($actifsOnly == '1') {
  46.             $clientsBy20 $personneRepo->getClientsActifsOnly($orderby$order);
  47.         } else if ($actifsOnly == '2') {
  48.             $clientsBy20 $personneRepo->getClientsInactifsOnly($orderby$order);
  49.         } else {
  50.             $clientsBy20 $personneRepo->getClients($orderby$order);
  51.         }
  52.         $serializer $this->container->get('serializer');
  53.         $response = new Response();
  54.         $response->setContent($serializer->serialize($clientsBy20'json'));
  55.         return $response;
  56.     }
  57.     /**
  58.      * @Route("/client/{id}", name="getClient")
  59.      */
  60.     public function getClient(string $id): Response
  61.     {
  62.         $personneRepo $this->getDoctrine()->getRepository(Personne::class);
  63.         $serializer $this->container->get('serializer');
  64.         return $this->render('admin/edit.html.twig', [
  65.             'client' => $serializer->serialize($personneRepo->getClient($id), 'json')
  66.         ]);
  67.     }
  68.     /**
  69.      * @Route("/client/edit/{id}", name="editClient")
  70.      */
  71.     public function editClient(string $idRequest $request)
  72.     {
  73.         //try {
  74.         // Récupération du post et des entités
  75.         $post json_decode($request->getContent(), true)['client'];
  76.         $em $this->getDoctrine()->getManager();
  77.         $personneRepo $this->getDoctrine()->getRepository(Personne::class);
  78.         $personne $personneRepo->find($post['idPersonne']);
  79.         $clientRepo $this->getDoctrine()->getRepository(Client::class);
  80.         $client $clientRepo->find($id);
  81.         $client->setPaysClient($post['paysPersonne']);
  82.         $userRepo $this->getDoctrine()->getRepository(User::class);
  83.         $user $userRepo->findOneBy(array('idPersonneUser' => $post['idPersonne'], 'idClientUser' => $id));
  84.         //GESTION FIREBASE
  85.         /* $factory = (new Factory)
  86.             ->withServiceAccount($_SERVER['DOCUMENT_ROOT']  . DIRECTORY_SEPARATOR . 'firebase_credentials.json')
  87.             ->withProjectId('previooterrain')
  88.             ->withDatabaseUri('https://previooterrain.firebaseio.com');*/
  89.         /*$auth = $factory->createAuth();
  90.         try {
  91.             $user = $auth->getUserByEmail($user->getLoginUser());
  92.         } catch (Exception $e) {
  93.         }*/
  94.         /* $uid = $user->uid;
  95.         $properties = [
  96.             'email' =>  $post['emailPersonne']
  97.         ];*/
  98.         /*$updatedUser = $auth->updateUser($uid, $properties);
  99.         $firestore = $factory->createFirestore();
  100.         $database = $firestore->database();
  101.         $collectionReference = $database->collection('users');
  102.         $test = $collectionReference->where('email', "=", $user->getLoginUser());
  103.         $documents = $test->documents();
  104.         foreach ($documents as $document) {
  105.             $collectionReference->document($document->id())->update([
  106.                 [
  107.                     'path' => "email",
  108.                     'value' => $post['emailPersonne']
  109.                 ]
  110.             ]);
  111.         }*/
  112.         //GESTION FIREBASE FINI
  113.         // Mise à jour
  114.         if ($post['dtExpirationClient'] != "Invalid date") {
  115.             $client->setDtExpirationClient(new DateTime($post['dtExpirationClient']));
  116.         }
  117.         $client->setLimitClient($post['limitClient']);
  118.         $client->setCreditClient(str_replace(',''.'$post['creditClient']));
  119.         $personne->setGenrePersonne($post['genrePersonne']);
  120.         $personne->setNomPersonne($post['nomPersonne']);
  121.         $personne->setPrenomPersonne($post['prenomPersonne']);
  122.         $personne->setEmailPersonne($post['emailPersonne']);
  123.         if ($post['dtNaissancePersonne'] != "Invalid date") {
  124.             $personne->setDtNaissancePersonne(new DateTime($post['dtNaissancePersonne']));
  125.         }
  126.         $personne->setTelPersonne($post['telPersonne']);
  127.         $personne->setPortablePersonne($post['portablePersonne']);
  128.         $personne->setAdressePersonne($post['adressePersonne']);
  129.         $personne->setAdresse2Personne($post['adresse2Personne']);
  130.         $personne->setCpPersonne($post['cpPersonne']);
  131.         $personne->setVillePersonne($post['villePersonne']);
  132.         $personne->setPaysPersonne($post['paysPersonne']);
  133.         $user->setLoginUser($post['emailPersonne']);
  134.         // Sauvegarde
  135.         //$em->persist($personne);
  136.         //$em->persist($client);
  137.         $em->flush();
  138.         return $this->json('success');
  139.         /*} catch (Exception $e) {
  140.             return $this->json(['error', $e->getMessage()]);
  141.         }*/
  142.     }
  143.     /**
  144.      * @Route("/client/lock/{id}", name="lockClient")
  145.      */
  146.     public function lockClient(string $id): Response
  147.     {
  148.         try {
  149.             $em $this->getDoctrine()->getManager();
  150.             $clientRepo $this->getDoctrine()->getRepository(Client::class);
  151.             $client $clientRepo->find($id);
  152.             $client->setIdStatutClient(8);
  153.             $em->persist($client);
  154.             $em->flush();
  155.             return $this->json(['success']);
  156.         } catch (Exception $e) {
  157.             return $this->json(['error'$e->getMessage()]);
  158.         }
  159.     }
  160.     /**
  161.      * @Route("/client/unlock/{id}", name="unlockClient")
  162.      */
  163.     public function unlockClient(string $id): Response
  164.     {
  165.         try {
  166.             $em $this->getDoctrine()->getManager();
  167.             $clientRepo $this->getDoctrine()->getRepository(Client::class);
  168.             $client $clientRepo->find($id);
  169.             $client->setIdStatutClient(2);
  170.             $em->persist($client);
  171.             $em->flush();
  172.             return $this->json(['success']);
  173.         } catch (Exception $e) {
  174.             return $this->json(['error'$e->getMessage()]);
  175.         }
  176.     }
  177.     /**
  178.      * @Route("/client/delete/{id}", name="deleteClient")
  179.      */
  180.     public function deleteClient(string $id): Response
  181.     {
  182.         try {
  183.             // Entity manager et repositories
  184.             $em $this->getDoctrine()->getManager();
  185.             $clientRepo $this->getDoctrine()->getRepository(Client::class);
  186.             $userRepo $this->getDoctrine()->getRepository(User::class);
  187.             $personneRepo $this->getDoctrine()->getRepository(Personne::class);
  188.             $budgetRepo $this->getDoctrine()->getRepository(Budget::class);
  189.             // Suppression des users
  190.             foreach ($userRepo->findBy(['idClientUser' => $id]) as $user) {
  191.                 $em->remove($user);
  192.             }
  193.             // Suppression des personnes
  194.             foreach ($personneRepo->findBy(['idClientPersonne' => $id]) as $personne) {
  195.                 $em->remove($personne);
  196.             }
  197.             // Suppression des budgets
  198.             foreach ($budgetRepo->findBy(['idClientBudget' => $id]) as $budget) {
  199.                 $em->remove($budget);
  200.             }
  201.             // Suppression du client
  202.             $em->remove($clientRepo->find($id));
  203.             $em->flush();
  204.             return $this->json(['success']);
  205.         } catch (Exception $e) {
  206.             return $this->json(['error'$e->getMessage()]);
  207.         }
  208.     }
  209.     /**
  210.      * @Route("/client/info/{id}", name="getInfoClient")
  211.      */
  212.     public function getInfoClient(string $id): Response
  213.     {
  214.         $personneRepo $this->getDoctrine()->getRepository(Personne::class);
  215.         $connexionRepo $this->getDoctrine()->getRepository(Connexion::class);
  216.         $serializer $this->container->get('serializer');
  217.         $client $personneRepo->getClient($id);
  218.         $users $personneRepo->getUsers($id);
  219.         $connexions $connexionRepo->getConnexionsByIdClient($id);
  220.         return $this->render('admin/clientInfo.html.twig', [
  221.             'client' => $serializer->serialize($client'json'),
  222.             'users' => $serializer->serialize($users'json'),
  223.             'connexions' => $serializer->serialize($connexions'json'),
  224.         ]);
  225.     }
  226.     /**
  227.      * @Route("/client/stats/{id}", name="getStatsClient")
  228.      */
  229.     public function getStatsClient(string $id): Response
  230.     {
  231.         $tacheRepo $this->getDoctrine()->getRepository(Tache::class);
  232.         $clientRepo $this->getDoctrine()->getRepository(Client::class);
  233.         $personneRepo $this->getDoctrine()->getRepository(Personne::class);
  234.         $cmdRepo $this->getDoctrine()->getRepository(Commande::class);
  235.         $offreCom $this->getDoctrine()->getRepository(OffreCommande::class);
  236.         $campagneRepo $this->getDoctrine()->getRepository(Campagne::class);
  237.         $serializer $this->container->get('serializer');
  238.         //client
  239.         $client $personneRepo->getClient($id);
  240.         // Missions
  241.         $missions $tacheRepo->CountNumberActionsByClient($id);
  242.         // factures
  243.         $bills $cmdRepo->getFacturesSuccesOnlyByClient($id);
  244.         // abonnements
  245.         $abonnements $cmdRepo->getAbonnementsOnlyByClient($id);
  246.         // offres abonnements
  247.         $offresAbo $offreCom->findAll();
  248.         // campagnes 
  249.         $NbCampagnesEmail $campagneRepo->getNbCampagnesEmailByClient($id);
  250.         $NbCampagnesSMS $campagneRepo->getNbCampagnesSMSByClient($id);
  251.         $campagnes = [$NbCampagnesEmail$NbCampagnesSMS];
  252.         return $this->render('admin/clientStats.html.twig', [
  253.             'data' => json_encode($missions),
  254.             'bills' => $serializer->serialize($bills'json'),
  255.             'client' => $serializer->serialize($client'json'),
  256.             'abonnements' => $serializer->serialize($abonnements'json'),
  257.             'offres' => $serializer->serialize($offresAbo'json'),
  258.             'campagnes' => json_encode($campagnes),
  259.         ]);
  260.     }
  261. }