src/Controller/NotificationController.php line 32

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notification;
  4. use App\Repository\NotificationRepository;
  5. use App\Service\Pagination;
  6. use Doctrine\{ DBAL\ConnectionORM\EntityManagerInterface };
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\{ RequestResponse };
  10. use Symfony\Component\Routing\Annotation\Route;
  11. #[Route(path'/notifications'), IsGranted(data'IS_AUTHENTICATED_REMEMBERED')]
  12. class NotificationController extends AbstractController
  13. {
  14.     #[Route(path'/'name'notification_index')]
  15.     public function index(Request $requestPagination $paginationNotificationRepository $notifRepo)
  16.     {
  17.         $query $notifRepo->findByUser($this->getUser());
  18.         $notifications $pagination->paginate($query$request$this->getParameter('items_per_page'));
  19.         return $this->render('notification/index.html.twig', [
  20.             'notifications' => $notifications,
  21.             'cntNotification' => count($notifications),
  22.             'last_page' => $pagination->lastPage($notifications)
  23.         ]);
  24.     }
  25.     #[Route(path'/latest'name'notification_latest')]
  26.     public function latest(NotificationRepository $notifRepo): Response
  27.     {
  28.         $notifications $notifRepo->findBy(['user' => $this->getUser(), 'isRead' => false], ['createdAt' => 'DESC'], 5);
  29.         return $this->render('notification/latest.html.twig', [
  30.             'notifications' => $notifications
  31.         ]);
  32.     }
  33.     #[Route(path'/read/{id}'name'notification_read'methods: ['GET'])]
  34.     public function read(Notification $notificationEntityManagerInterface $entityManager): Response
  35.     {
  36.         $notification->setIsRead(true);
  37.         $entityManager->flush();
  38.         $this->addFlash(
  39.             'success',
  40.             'Notification marked as read successfully!'
  41.         );
  42.         return $this->redirectToRoute('notification_index');
  43.     }
  44.     #[Route(path'/read-all'name'notification_read_all'methods: ['GET'])]
  45.     public function readAll(Connection $conn): Response
  46.     {
  47.         $conn->update('notifications', ['is_read' => 1], [
  48.             'user_id' => $this->getUser()->getId(),
  49.             'is_read' => 0
  50.         ]);
  51.         $this->addFlash(
  52.             'success',
  53.             'All notifications marked as read successfully!'
  54.         );
  55.         return $this->redirectToRoute('notification_index');
  56.     }
  57. }