src/Controller/NotificationController.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notification;
  4. use App\Service\NotificationService;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. class NotificationController extends AbstractController
  12. {
  13.     #[Route('/api/notifications/count'name'api_notifications_count'methods: ['GET'])]
  14.     public function count(NotificationService $notificationService): JsonResponse
  15.     {
  16.         if (!$this->getUser()) {
  17.             return $this->json(['count' => 0]);
  18.         }
  19.         $count $notificationService->getUnreadCount($this->getUser());
  20.         return $this->json(['count' => $count]);
  21.     }
  22.     #[Route('/notifications'name'notifications_list')]
  23.     public function list(NotificationService $notificationServiceEntityManagerInterface $entityManagerRequest $request): Response
  24.     {
  25.         if (!$this->getUser()) {
  26.             return $this->redirectToRoute('ui_app_login');
  27.         }
  28.         $showArchived $request->query->getBoolean('archived'false);
  29.         $type $request->query->get('type''');
  30.         $notifications $notificationService->getUserNotifications($this->getUser(), 12false$showArchivedfalse$type !== '' $type null0);
  31.         $unreadCount $notificationService->getUnreadCount($this->getUser());
  32.         
  33.         // Récupérer tous les types de notifications pour le sélecteur
  34.         $allTypes $entityManager->getRepository(Notification::class)
  35.             ->createQueryBuilder('n')
  36.             ->select('DISTINCT n.type')
  37.             ->where('n.user = :user')
  38.             ->andWhere('n.isArchived = :archived')
  39.             ->andWhere('n.isDeleted = :deleted')
  40.             ->setParameter('user'$this->getUser())
  41.             ->setParameter('archived'$showArchived)
  42.             ->setParameter('deleted'false)
  43.             ->getQuery()
  44.             ->getResult();
  45.         
  46.         $types array_filter(array_column($allTypes'type'));
  47.         
  48.         // Vérifier s'il y a des notifications sans type pour afficher l'option "non spécifié"
  49.         $hasNonSpecified $entityManager->getRepository(Notification::class)
  50.             ->createQueryBuilder('n')
  51.             ->select('COUNT(n.id)')
  52.             ->where('n.user = :user')
  53.             ->andWhere('(n.type IS NULL OR n.type = :emptyType)')
  54.             ->andWhere('n.isArchived = :archived')
  55.             ->andWhere('n.isDeleted = :deleted')
  56.             ->setParameter('user'$this->getUser())
  57.             ->setParameter('emptyType''')
  58.             ->setParameter('archived'$showArchived)
  59.             ->setParameter('deleted'false)
  60.             ->getQuery()
  61.             ->getSingleScalarResult() > 0;
  62.         return $this->render('account/notifications.html.twig', [
  63.             'notifications' => $notifications,
  64.             'unreadCount' => $unreadCount,
  65.             'showArchived' => $showArchived,
  66.             'selectedType' => $type,
  67.             'availableTypes' => $types,
  68.             'hasNonSpecified' => $hasNonSpecified,
  69.         ]);
  70.     }
  71.     #[Route('/api/notifications'name'api_notifications_list'methods: ['GET'])]
  72.     public function getNotifications(NotificationService $notificationServiceRequest $request): JsonResponse
  73.     {
  74.         if (!$this->getUser()) {
  75.             return $this->json(['error' => 'Non autorisé'], 401);
  76.         }
  77.         $limit = (int) $request->query->get('limit'12);
  78.         $offset = (int) $request->query->get('offset'0);
  79.         $unreadOnly $request->query->getBoolean('unread_only'false);
  80.         $showArchived $request->query->getBoolean('archived'false);
  81.         $type $request->query->get('type''');
  82.         $notifications $notificationService->getUserNotifications(
  83.             $this->getUser(), 
  84.             $limit
  85.             $unreadOnly
  86.             $showArchived
  87.             false
  88.             $type !== '' $type null,
  89.             $offset
  90.         );
  91.         $data = [];
  92.         foreach ($notifications as $notification) {
  93.             $data[] = [
  94.                 'id' => $notification->getId(),
  95.                 'title' => $notification->getTitle(),
  96.                 'message' => $notification->getMessage(),
  97.                 'type' => $notification->getType(),
  98.                 'isRead' => $notification->isIsRead(),
  99.                 'priority' => $notification->getPriority(),
  100.                 'createdAt' => $notification->getCreatedAt()->format('Y-m-d H:i:s'),
  101.                 'readAt' => $notification->getReadAt()?->format('Y-m-d H:i:s'),
  102.                 'isArchived' => $notification->isIsArchived(),
  103.                 'archivedAt' => $notification->getArchivedAt()?->format('Y-m-d H:i:s'),
  104.             ];
  105.         }
  106.         return $this->json([
  107.             'notifications' => $data,
  108.             'hasMore' => count($notifications) === $limit,
  109.             'count' => count($data)
  110.         ]);
  111.     }
  112.     #[Route('/api/notifications/{id}/read'name'api_notification_read'methods: ['POST'])]
  113.     public function markAsRead(int $idNotificationService $notificationServiceEntityManagerInterface $em): JsonResponse
  114.     {
  115.         if (!$this->getUser()) {
  116.             return $this->json(['error' => 'Non autorisé'], 401);
  117.         }
  118.         $notification $em->getRepository(Notification::class)->find($id);
  119.         
  120.         if (!$notification || $notification->getUser() !== $this->getUser()) {
  121.             return $this->json(['error' => 'Non autorisé'], 403);
  122.         }
  123.         $notificationService->markAsRead($notification);
  124.         return $this->json(['success' => true]);
  125.     }
  126.     #[Route('/api/notifications/read-all'name'api_notifications_read_all'methods: ['POST'])]
  127.     public function markAllAsRead(NotificationService $notificationService): JsonResponse
  128.     {
  129.         if (!$this->getUser()) {
  130.             return $this->json(['error' => 'Non autorisé'], 401);
  131.         }
  132.         $notificationService->markAllAsRead($this->getUser());
  133.         return $this->json(['success' => true]);
  134.     }
  135.     #[Route('/api/notifications/{id}/archive'name'api_notification_archive'methods: ['POST'])]
  136.     public function archiveNotification(int $idNotificationService $notificationServiceEntityManagerInterface $em): JsonResponse
  137.     {
  138.         if (!$this->getUser()) {
  139.             return $this->json(['error' => 'Non autorisé'], 401);
  140.         }
  141.         $notification $em->getRepository(Notification::class)->find($id);
  142.         
  143.         if (!$notification || $notification->getUser() !== $this->getUser()) {
  144.             return $this->json(['error' => 'Non autorisé'], 403);
  145.         }
  146.         $notificationService->archiveNotification($notification);
  147.         return $this->json(['success' => true]);
  148.     }
  149.     #[Route('/api/notifications/{id}/delete'name'api_notification_delete'methods: ['POST'])]
  150.     public function deleteNotification(int $idNotificationService $notificationServiceEntityManagerInterface $em): JsonResponse
  151.     {
  152.         if (!$this->getUser()) {
  153.             return $this->json(['error' => 'Non autorisé'], 401);
  154.         }
  155.         $notification $em->getRepository(Notification::class)->find($id);
  156.         
  157.         if (!$notification || $notification->getUser() !== $this->getUser()) {
  158.             return $this->json(['error' => 'Non autorisé'], 403);
  159.         }
  160.         $notificationService->deleteNotification($notification);
  161.         return $this->json(['success' => true]);
  162.     }
  163. }