src/Controller/AlertsController.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Alertes;
  4. use App\Service\Securizer;
  5. use App\Repository\AlertesRepository;
  6. use App\Repository\ContactsRepository;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use App\Repository\TypesAlerteRepository;
  9. use DateTime;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Serializer\SerializerInterface;
  13. use Symfony\Component\Validator\Validator\ValidatorInterface;
  14. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  17. use Symfony\Component\Validator\Constraints\Date;
  18. class AlertsController extends AbstractController
  19. {
  20.     /**
  21.      * @IsGranted("ROLE_TECH")
  22.      */
  23.     public function index(AlertesRepository $alertsRepoTypesAlerteRepository $repoTypeAlertesAccessDecisionManagerInterface $accessDecisionManagerContactsRepository $repoContacts): Response
  24.     {
  25.         // liste tous les alertes de type notifications et celles de groupe par rapport a user_role
  26.         $notifications $repoTypeAlertes->findOneBy(["alerte" => 'Notification']);
  27.         $groupe $repoTypeAlertes->findOneBy(["alerte" => 'Groupe']);
  28.         $elements $alertsRepo->findBy(["idTypeAlerte" => $notifications->getId()]);
  29.         foreach (($this->getUser()->getRoles()) as $role) {
  30.             $add $alertsRepo->findBy(["idTypeAlerte" => $groupe->getId(), "roleGroup" => $role'traite' => 0]);
  31.             $elements =  array_merge($elements$add);
  32.         }
  33.         usort($elements, function ($a$b) {
  34.             return $a->getDateAlerteTimestamp() - $b->getDateAlerteTimestamp();
  35.         });
  36.         $res = [];
  37.         $securizer = new Securizer($accessDecisionManager);
  38.         $idUser $repoContacts->findOneBy(['mail' => $this->getUser()->getUserIdentifier()])->getId();
  39.         for ($i 0$i count($elements); $i++) {
  40.             $alerte $elements[$i];
  41.             if ($alerte->getTraite() === false && ($securizer->isGranted($this->getUser(), "ROLE_ADMIN") || $idUser)) {
  42.                 if (count($res) === || $alerte->getDateAlerteTimestamp() !== $res[count($res) - 1]["timestamp"]) $res[] = ['timestamp' => $alerte->getDateAlerteTimestamp(), 'alerts' => [$alerte]];
  43.                 else $res[count($res) - 1]["alerts"][] = $alerte;
  44.             }
  45.         }
  46.         return $this->json($res200, [], ['groups' => 'affichageAlert']);
  47.     }
  48.     /**
  49.      * @IsGranted("ROLE_TECH")
  50.      */
  51.     public function getClient($idAlertesRepository $alertsRepoAccessDecisionManagerInterface $accessDecisionManagerContactsRepository $repoContacts): Response
  52.     {
  53.         // liste tous les alertes de type general
  54.         $elements $alertsRepo->findBy(["idClient" => $id]);
  55.         usort($elements, function ($a$b) {
  56.             return $a->getDateAlerteTimestamp() - $b->getDateAlerteTimestamp();
  57.         });
  58.         $res = [];
  59.         $securizer = new Securizer($accessDecisionManager);
  60.         $idUser $repoContacts->findOneBy(['mail' => $this->getUser()->getUserIdentifier()])->getId();
  61.         for ($i 0$i count($elements); $i++) {
  62.             $alerte $elements[$i];
  63.             if ($alerte->getTraite() === false && ($securizer->isGranted($this->getUser(), "ROLE_ADMIN") || $alerte->getIdContact()->getId() === $idUser)) {
  64.                 if (count($res) === || $alerte->getDateAlerteTimestamp() !== $res[count($res) - 1]["timestamp"]) $res[] = ['timestamp' => $alerte->getDateAlerteTimestamp(), 'alerts' => [$alerte]];
  65.                 else $res[count($res) - 1]["alerts"][] = $alerte;
  66.             }
  67.         }
  68.         return $this->json($res200, [], ['groups' => 'affichageAlert']);
  69.     }
  70.     /**
  71.      * @IsGranted("ROLE_TECH")
  72.      */
  73.     public function getAlertesElements($idAlertesRepository $alertsRepo): Response
  74.     {
  75.         // liste tous les alertes de type general
  76.         $elements $alertsRepo->findBy(["idElementsPark" => $id]);
  77.         usort($elements, function ($a$b) {
  78.             return $a->getDateAlerteTimestamp() - $b->getDateAlerteTimestamp();
  79.         });
  80.         return $this->json($elements200, [], ['groups' => 'affichageAlertParc']);
  81.     }
  82.     /**
  83.      * @IsGranted("ROLE_TECH")
  84.      */
  85.     public function creer(Request $requestValidatorInterface $validatorSerializerInterface $serializerEntityManagerInterface $manager): Response
  86.     {
  87.         // creer un nouveau client, seul les roles commercial et admin peuvent accer a cette url
  88.         try {
  89.             $jsonRecu $request->getContent();
  90.             $alert $serializer->deserialize($jsonRecuAlertes::class, 'json');
  91.             $errors $validator->validate($alert);
  92.             if (count($errors) > 0) {
  93.                 return $this->json($errors400);
  94.             }
  95.             $manager->persist($alert);
  96.             $manager->flush();
  97.         } catch (\throwable $e) {
  98.             return $this->json([
  99.                 'status' => 400,
  100.                 'message' => $e->getMessage()
  101.             ], 400);
  102.         }
  103.         return $this->json($alert201, [], ['groups' => 'affichageElement']);
  104.     }
  105.     /**
  106.      * @IsGranted("ROLE_TECH")
  107.      */
  108.     public function delete(int $idAlertesRepository $repoElementEntityManagerInterface $manager): Response
  109.     {
  110.         //permets d'effacer une alerte, seul les user avec role tech ou supperieur  
  111.         try {
  112.             $element $repoElement->find($id);
  113.             $manager->remove($element);
  114.             $manager->flush();
  115.         } catch (\throwable $e) {
  116.             return $this->json([
  117.                 'status' => 400,
  118.                 'message' => $e->getMessage()
  119.             ], 400);
  120.         }
  121.         return $this->json(true200, [], ['groups' => 'affichageElement']);
  122.     }
  123.     /**
  124.      * @IsGranted("ROLE_TECH")
  125.      */
  126.     public function getTypes(TypesAlerteRepository $typesAlertRepo): Response
  127.     {
  128.         //retourne les types de alerte
  129.         $elements $typesAlertRepo->findAll();
  130.         return $this->json($elements200, [], ['groups' => 'affichageElement']);
  131.     }
  132.     /**
  133.      * @IsGranted("ROLE_USER")
  134.      */
  135.     public function getNotifications(int $idAlertesRepository $alertsRepo)
  136.     {
  137.         //retourne les notifications d'un utilisateur
  138.         $elements $alertsRepo->findNotifications($id$this->getUser());
  139.         return $this->json($elements200, [], ['groups' => 'affichageAlert']);
  140.     }
  141.     /**
  142.      * @IsGranted("ROLE_USER")
  143.      */
  144.     public function setAlertAsTreated(int $idAlertesRepository $alertsRepoEntityManagerInterface $manager)
  145.     {
  146.         //permets de marquer une notification comme traite
  147.         $alert $alertsRepo->findOneBy(["id" => $id]);
  148.         try {
  149.             $alert->setTraite(true);
  150.             $today = new DateTime();
  151.             $alert->setDateTraitement($today);
  152.             $manager->persist($alert);
  153.             $manager->flush();
  154.         } catch (\throwable $e) {
  155.             return $this->json([
  156.                 'status' => 400,
  157.                 'message' => $e->getMessage()
  158.             ], 400);
  159.         }
  160.         return $this->json($alert200, [], ['groups' => 'affichageAlert']);
  161.     }
  162. }