<?php
namespace App\Controller;
use App\Entity\Alertes;
use App\Service\Securizer;
use App\Repository\AlertesRepository;
use App\Repository\ContactsRepository;
use Doctrine\ORM\EntityManagerInterface;
use App\Repository\TypesAlerteRepository;
use DateTime;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
use Symfony\Component\Validator\Constraints\Date;
class AlertsController extends AbstractController
{
/**
* @IsGranted("ROLE_TECH")
*/
public function index(AlertesRepository $alertsRepo, TypesAlerteRepository $repoTypeAlertes, AccessDecisionManagerInterface $accessDecisionManager, ContactsRepository $repoContacts): Response
{
// liste tous les alertes de type notifications et celles de groupe par rapport a user_role
$notifications = $repoTypeAlertes->findOneBy(["alerte" => 'Notification']);
$groupe = $repoTypeAlertes->findOneBy(["alerte" => 'Groupe']);
$elements = $alertsRepo->findBy(["idTypeAlerte" => $notifications->getId()]);
foreach (($this->getUser()->getRoles()) as $role) {
$add = $alertsRepo->findBy(["idTypeAlerte" => $groupe->getId(), "roleGroup" => $role, 'traite' => 0]);
$elements = array_merge($elements, $add);
}
usort($elements, function ($a, $b) {
return $a->getDateAlerteTimestamp() - $b->getDateAlerteTimestamp();
});
$res = [];
$securizer = new Securizer($accessDecisionManager);
$idUser = $repoContacts->findOneBy(['mail' => $this->getUser()->getUserIdentifier()])->getId();
for ($i = 0; $i < count($elements); $i++) {
$alerte = $elements[$i];
if ($alerte->getTraite() === false && ($securizer->isGranted($this->getUser(), "ROLE_ADMIN") || $idUser)) {
if (count($res) === 0 || $alerte->getDateAlerteTimestamp() !== $res[count($res) - 1]["timestamp"]) $res[] = ['timestamp' => $alerte->getDateAlerteTimestamp(), 'alerts' => [$alerte]];
else $res[count($res) - 1]["alerts"][] = $alerte;
}
}
return $this->json($res, 200, [], ['groups' => 'affichageAlert']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function getClient($id, AlertesRepository $alertsRepo, AccessDecisionManagerInterface $accessDecisionManager, ContactsRepository $repoContacts): Response
{
// liste tous les alertes de type general
$elements = $alertsRepo->findBy(["idClient" => $id]);
usort($elements, function ($a, $b) {
return $a->getDateAlerteTimestamp() - $b->getDateAlerteTimestamp();
});
$res = [];
$securizer = new Securizer($accessDecisionManager);
$idUser = $repoContacts->findOneBy(['mail' => $this->getUser()->getUserIdentifier()])->getId();
for ($i = 0; $i < count($elements); $i++) {
$alerte = $elements[$i];
if ($alerte->getTraite() === false && ($securizer->isGranted($this->getUser(), "ROLE_ADMIN") || $alerte->getIdContact()->getId() === $idUser)) {
if (count($res) === 0 || $alerte->getDateAlerteTimestamp() !== $res[count($res) - 1]["timestamp"]) $res[] = ['timestamp' => $alerte->getDateAlerteTimestamp(), 'alerts' => [$alerte]];
else $res[count($res) - 1]["alerts"][] = $alerte;
}
}
return $this->json($res, 200, [], ['groups' => 'affichageAlert']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function getAlertesElements($id, AlertesRepository $alertsRepo): Response
{
// liste tous les alertes de type general
$elements = $alertsRepo->findBy(["idElementsPark" => $id]);
usort($elements, function ($a, $b) {
return $a->getDateAlerteTimestamp() - $b->getDateAlerteTimestamp();
});
return $this->json($elements, 200, [], ['groups' => 'affichageAlertParc']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function creer(Request $request, ValidatorInterface $validator, SerializerInterface $serializer, EntityManagerInterface $manager): Response
{
// creer un nouveau client, seul les roles commercial et admin peuvent accer a cette url
try {
$jsonRecu = $request->getContent();
$alert = $serializer->deserialize($jsonRecu, Alertes::class, 'json');
$errors = $validator->validate($alert);
if (count($errors) > 0) {
return $this->json($errors, 400);
}
$manager->persist($alert);
$manager->flush();
} catch (\throwable $e) {
return $this->json([
'status' => 400,
'message' => $e->getMessage()
], 400);
}
return $this->json($alert, 201, [], ['groups' => 'affichageElement']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function delete(int $id, AlertesRepository $repoElement, EntityManagerInterface $manager): Response
{
//permets d'effacer une alerte, seul les user avec role tech ou supperieur
try {
$element = $repoElement->find($id);
$manager->remove($element);
$manager->flush();
} catch (\throwable $e) {
return $this->json([
'status' => 400,
'message' => $e->getMessage()
], 400);
}
return $this->json(true, 200, [], ['groups' => 'affichageElement']);
}
/**
* @IsGranted("ROLE_TECH")
*/
public function getTypes(TypesAlerteRepository $typesAlertRepo): Response
{
//retourne les types de alerte
$elements = $typesAlertRepo->findAll();
return $this->json($elements, 200, [], ['groups' => 'affichageElement']);
}
/**
* @IsGranted("ROLE_USER")
*/
public function getNotifications(int $id, AlertesRepository $alertsRepo)
{
//retourne les notifications d'un utilisateur
$elements = $alertsRepo->findNotifications($id, $this->getUser());
return $this->json($elements, 200, [], ['groups' => 'affichageAlert']);
}
/**
* @IsGranted("ROLE_USER")
*/
public function setAlertAsTreated(int $id, AlertesRepository $alertsRepo, EntityManagerInterface $manager)
{
//permets de marquer une notification comme traite
$alert = $alertsRepo->findOneBy(["id" => $id]);
try {
$alert->setTraite(true);
$today = new DateTime();
$alert->setDateTraitement($today);
$manager->persist($alert);
$manager->flush();
} catch (\throwable $e) {
return $this->json([
'status' => 400,
'message' => $e->getMessage()
], 400);
}
return $this->json($alert, 200, [], ['groups' => 'affichageAlert']);
}
}