<?php
namespace App\Controller;
use DateTime;
use App\Entity\Regles;
use App\Service\Securizer;
use App\Library\GraphOutlook;
use App\Repository\ReglesRepository;
use App\Repository\ClientsRepository;
use App\Repository\ContactsRepository;
use App\Repository\ReglesVerificationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
class ReglesController extends AbstractController
{
/**
* @IsGranted("ROLE_TECH")
*/
public function index(int $id, ReglesRepository $repoRegles): Response
{
$rules = $repoRegles->findBy(['idClient' => $id]);
return $this->json($rules, 200, [], ['groups' => 'affichageRegles']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function getByDate($date, ReglesRepository $repoRegles, ReglesVerificationRepository $repoVerification): Response
{
$datetime = new DateTime(date('Y-m-d H:i:s', $date));
$rules = $repoRegles->findAllActive();
// $res = [];
for ($i = 0; $i < count($rules); $i++) {
$verifications = $repoVerification->findByDateAndRuleID($datetime, $rules[$i]->getId());
$rules[$i]->setReglesVerification($verifications);
}
return $this->json($rules, 200, [], ['groups' => 'affichageVerificationRegles']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function creer(Request $request, ContactsRepository $repoContacts, SerializerInterface $serializer, EntityManagerInterface $manager): Response
{
$jsonRecu = $request->getContent();
$today = new DateTime();
$user = $repoContacts->findOneBy(['mail' => $this->getUser()->getUserIdentifier()]);
try {
$rule = $serializer->deserialize($jsonRecu, Regles::class, 'json');
$rule->setCreatedBy($user);
$rule->setDateCreated($today);
$manager->persist($rule);
$manager->flush();
} catch (\Throwable $e) {
return $this->json([
'status' => 400,
'message' => $e->getMessage()
], 400);
}
return $this->json($rule, 200, [], ['groups' => 'affichageRegles']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function modification(int $id, Request $request, ReglesRepository $repoRegles, ContactsRepository $repoContacts, SerializerInterface $serializer, EntityManagerInterface $manager, AccessDecisionManagerInterface $accessDecisionManager): Response
{
//permets de modifier une image d'un client, seul les roles TECH et supperieur peuvent acceder a cette url
$securizer = new Securizer($accessDecisionManager);
if ($securizer->isGranted($this->getUser(), "ROLE_TECH")) {
$jsonRecu = $request->getContent();
$rule = $repoRegles->find($id);
if (!$rule) {
return $this->json([
'status' => 404,
'message' => "Cette règle n'existe pas"
], 404);
}
try {
$rule = $serializer->deserialize($jsonRecu, Regles::class, 'json', ['object_to_populate' => $rule]);
$manager->persist($rule);
$manager->flush();
} catch (\Throwable $e) {
return $this->json([
'status' => 400,
'message' => $e->getMessage()
], 400);
}
return $this->json($rule, 200, [], ['groups' => 'affichageRegles']);
} else {
return $this->json([
'status' => 400,
'message' => "Vous ne pouvez pas consulter cet élément"
], 400);
}
}
// /**
// * @IsGranted("ROLE_TECH")
// */
// public function delete(Int $id, ReglesRepository $repoRegles, ReglesVerificationRepository $repoVerification, EntityManagerInterface $manager, AccessDecisionManagerInterface $accessDecisionManager): Response
// {
// //permets de effacer une image d'un client, seul les roles TECH et supperieur peuvent acceder a cette url
// $securizer = new Securizer($accessDecisionManager);
// if ($securizer->isGranted($this->getUser(), "ROLE_TECH")) {
// $regle = $repoRegles->find($id);
// $repoRegles->remove($regle, true);
// } else {
// return $this->json([
// 'status' => 400,
// 'message' => "Vous ne pouvez pas consulter cet élément"
// ], 400);
// }
// return $this->json(true, 200, [], ['groups' => 'affichageRegles']);
// }
}