src/Controller/NotificationController.php line 72

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notification;
  4. use App\Entity\UserNotification;
  5. use DateTime;
  6. use Knp\Component\Pager\PaginatorInterface;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpFoundation\RedirectResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\Routing\Annotation\Route;
  13. use Symfony\Contracts\Translation\TranslatorInterface;
  14. class NotificationController extends AbstractController
  15. {
  16.     /**
  17.      * @Route("/notification", name="notification.index", methods={"GET"})
  18.      * @param Request $request
  19.      * @param PaginatorInterface $paginator
  20.      * @return Response
  21.      */
  22.     public function index(Request $requestPaginatorInterface $paginator): Response
  23.     {
  24.         /** @var UserNotification $userNotifications */
  25.         $query $this->getDoctrine()->getRepository(UserNotification::class)->getUserNotification($this->getUser()->getId(), true);
  26.         $userNotifications $paginator->paginate(
  27.             $query,
  28.             $request->query->getInt('page'1),
  29.             15,
  30.             ['defaultSortFieldName' => 'n.createdAt''defaultSortDirection' => 'desc']
  31.         );
  32.         return $this->render('notification/index.html.twig', [
  33.             'controller_name' => 'NotificationController',
  34.             'userNotifications' => $userNotifications
  35.         ]);
  36.     }
  37.     /**
  38.      * @Route("/notification/mark-as-read-all", name="notification.mark.as.read.all", methods={"GET"})
  39.      * @return RedirectResponse
  40.      */
  41.     public function markAllNotificationReaded(): RedirectResponse
  42.     {
  43.         /** @var UserNotification[] $notifications */
  44.         $notifications $this->getDoctrine()->getRepository(UserNotification::class)->getAllNotificationUnread(
  45.             $this->getUser()->getId()
  46.         );
  47.         foreach ($notifications as $notification) {
  48.             $notification->setIsRead(true);
  49.             $this->getDoctrine()->getManager()->persist($notification);
  50.         }
  51.         $this->getDoctrine()->getManager()->flush();
  52.         return $this->redirectToRoute('notification.index');
  53.     }
  54.     /**
  55.      * @Route("/notification/unread", name="notification.unread", methods={"GET"})
  56.      * @param TranslatorInterface $translator
  57.      * @return JsonResponse
  58.      */
  59.     public function getLastNotification(TranslatorInterface $translator): JsonResponse
  60.     {
  61.         /** @var UserNotification[] $userNotifications */
  62.         $userNotifications $this->getDoctrine()->getRepository(UserNotification::class)->getLastNotificationUnread($this->getUser()->getId());
  63.         $unreadNotificationCounter $this->getDoctrine()->getRepository(UserNotification::class)->count([
  64.             'user' => $this->getUser(),
  65.             'isRead' => false
  66.         ]);
  67.         $response = [
  68.             'count' => $unreadNotificationCounter,
  69.             'notifications' => []
  70.         ];
  71.         foreach ($userNotifications as $key => $notification) {
  72.             $customer '';
  73.             if (!is_null($notification->getNotification()->getJob())) {
  74.                 if (!is_null($notification->getNotification()->getJob()->getContact())) {
  75.                     $customer $notification->getNotification()->getJob()->getContact()->getFirstname().' '.
  76.                                 $notification->getNotification()->getJob()->getContact()->getLastname();
  77.                 } else {
  78.                     $customer $notification->getNotification()->getJob()->getFromAddress();
  79.                 }
  80.             }
  81.             $response['notifications'][$key] = [
  82.                 'id' => $notification->getNotification()->getId(),
  83.                 'title' => $notification->getNotification()->getTitle(),
  84.                 'content' => $translator->trans($notification->getNotification()->getContent()),
  85.                 'customer' => $customer,
  86.                 'level' => $notification->getNotification()->getLevel(),
  87.                 'createdAt' => $notification->getNotification()->getCreatedAt()
  88.             ];
  89.         }
  90.         return new JsonResponse($response);
  91.     }
  92.     /**
  93.      * @Route("/notification/show/{id}", name="notification.show", methods={"GET"})
  94.      * @param Notification $notification
  95.      * @return Response
  96.      */
  97.     public function show(Notification $notification): Response
  98.     {
  99.         $userNotification $this->getDoctrine()->getRepository(UserNotification::class)->findOneBy([
  100.             'notification' => $notification->getId(),
  101.             'user' => $this->getUser()
  102.         ]);
  103.         if (!is_null($userNotification)) {
  104.             $userNotification->setIsRead(true)
  105.                              ->setReadAt(new DateTime());
  106.             $this->getDoctrine()->getManager()->persist($userNotification);
  107.             $this->getDoctrine()->getManager()->flush();
  108.         }
  109.         return $this->render('notification/show.html.twig', [
  110.             'controller_name' => 'NotificationController',
  111.             'notification' => $notification
  112.         ]);
  113.     }
  114.     /**
  115.      * @Route("/notification/markastreated/{id}", name="notification.markastreated", methods={"GET"})
  116.      * @param Notification $notification
  117.      * @return RedirectResponse
  118.      */
  119.     public function markAsTreated(Notification $notification): RedirectResponse
  120.     {
  121.         $notification->setIsTreated(true)
  122.                      ->setTreatedBy($this->getUser());
  123.         $this->getDoctrine()->getManager()->persist($notification);
  124.         $this->getDoctrine()->getManager()->flush();
  125.         $this->addFlash(
  126.             'success',
  127.             'Enregistrement effectuĂ© avec succès !'
  128.         );
  129.         return $this->redirectToRoute('notification.show', ['id' => $notification->getId()]);
  130.     }
  131.     /**
  132.      * @Route("/notification/markasunread/{id}", name="notification.markasunread", methods={"GET"})
  133.      * @param Notification $notification
  134.      * @return RedirectResponse
  135.      */
  136.     public function markAsUnread(Notification $notification): RedirectResponse
  137.     {
  138.         $userNotification $this->getDoctrine()->getRepository(UserNotification::class)->findOneBy([
  139.             'user' => $this->getUser(),
  140.             'notification' => $notification
  141.         ]);
  142.         if (!is_null($userNotification)) {
  143.             $userNotification->setIsRead(false);
  144.             $this->getDoctrine()->getManager()->persist($userNotification);
  145.             $this->getDoctrine()->getManager()->flush();
  146.         }
  147.         return $this->redirectToRoute('notification.index');
  148.     }
  149. }