src/Controller/Api/NotificationsController.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Api;
  3. use App\Entity\Core\Notifications;
  4. use App\Services\Dossiers;
  5. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  17. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  18. use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
  19. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  20. use Symfony\Component\Form\Extension\Core\Type\HiddenType;
  21. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  22. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  23. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  24. use Symfony\Component\Form\Extension\Core\Type\TextType;
  25. use Symfony\Component\HttpFoundation\Session\Session;
  26. class NotificationsController extends AbstractController
  27. {
  28.     private $em;
  29.     private $paginator;
  30.     private $dossier;
  31.     public function __construct(EntityManagerInterface $em,
  32.                                 Dossiers $dossier,
  33.                                 PaginatorInterface $paginator
  34.     ) {
  35.         $this->em $em;
  36.         $this->paginator $paginator;
  37.         $this->dossier $dossier;
  38.     }
  39.     /**
  40.      * Notifications
  41.      * @param Request $request
  42.      * @return JsonResponse
  43.      */
  44.     public function recents(Request $request)
  45.     {
  46.         $user $this->getUser();
  47.         $limit $request->query->get('limit'20);
  48.         $queries $this->em->getRepository(Notifications::class)->recents($user,$limit);
  49.         $array = [];
  50.         foreach ($queries as $query) {
  51.             $createdAt "";
  52.             if($query->getCreatedAt() !== null) {
  53.                 $createdAt $query->getCreatedAt()->format('c');
  54.             }
  55.             $array[] = [
  56.                 "id" => (string)$query->getId(),
  57.                 "type" => (string)$query->getType(),
  58.                 "title" => (string)$query->getTitle(),
  59.                 "description" => (string)$query->getDescription(),
  60.                 "createdAt" => $createdAt,
  61.                 "viewed" => (bool)$query->isViewed(),
  62.             ];
  63.             $query->setViewed(true);
  64.             $this->em->persist($query);
  65.             $this->em->flush();
  66.         }
  67.         return new JsonResponse($array);
  68.     }
  69.     public function dashboardRecents(Request $request)
  70.     {
  71.         $user $this->getUser();
  72.         $queries $this->em->getRepository(Notifications::class)->dashboardRecents($user);
  73.         $array = [];
  74.         foreach ($queries as $query) {
  75.             $createdAt "";
  76.             if($query->getCreatedAt() !== null) {
  77.                 $createdAt $query->getCreatedAt()->format('c');
  78.             }
  79.             $array[] = [
  80.                 "id" => (string)$query->getId(),
  81.                 "type" => (string)$query->getType(),
  82.                 "title" => (string)$query->getTitle(),
  83.                 "description" => (string)$query->getDescription(),
  84.                 "createdAt" => $createdAt,
  85.                 "viewed" => (bool)$query->isViewed(),
  86.             ];
  87.             $query->setViewed(true);
  88.             $this->em->persist($query);
  89.             $this->em->flush();
  90.         }
  91.         return new JsonResponse($array);
  92.     }
  93.     /**
  94.      * Supprimer
  95.      * @param Notifications $notification
  96.      * @return JsonResponse
  97.      */
  98.     public function delete(Notifications $notification)
  99.     {
  100.         $user $this->getUser();
  101.         if($notification->getUser() !== $user) {
  102.             return new JsonResponse(false);
  103.         }
  104.         $this->em->remove($notification);
  105.         $this->em->flush();
  106.         return new JsonResponse(true);
  107.     }
  108.     /**
  109.      * Supprimer toutes les notifications
  110.      * @param Notifications $notification
  111.      * @return JsonResponse
  112.      */
  113.     public function clearAll(Request $request)
  114.     {
  115.         $user $this->getUser();
  116.         $queries $this->em->getRepository(Notifications::class)->findBy(["user" => $user]);
  117.         foreach ($queries as $query) {
  118.             $this->em->remove($query);
  119.             $this->em->flush();
  120.         }
  121.         return new JsonResponse(true);
  122.     }
  123.     public function readAll(Request $request)
  124.     {
  125.         $user $this->getUser();
  126.         $queries $this->em->getRepository(Notifications::class)->findBy(["user" => $user]);
  127.         foreach ($queries as $query) {
  128.             $query->setViewed(true);
  129.             $this->em->persist($query);
  130.             $this->em->flush();
  131.         }
  132.         return new JsonResponse(true);
  133.     }
  134.     /**
  135.      * Mise à jour de la lecture.
  136.      * @param Notifications $notification
  137.      * @return JsonResponse
  138.      */
  139.     public function viewed(Notifications $notification)
  140.     {
  141.         $user $this->getUser();
  142.         if($notification->getUser() !== $user) {
  143.             return new JsonResponse(false);
  144.         }
  145.         $notification->setViewed(true);
  146.         $this->em->persist($notification);
  147.         $this->em->flush();
  148.         return new JsonResponse(true);
  149.     }
  150. }