src/Controller/NotificationController.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Component\Tabs\Tab;
  4. use App\Component\Tabs\Tabs;
  5. use App\Entity\Notification;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. class NotificationController extends ABaseController
  11. {
  12.     #[Route('/notifications'name'route.notifications')]
  13.     public function index(Request $request): Response
  14.     {
  15.         $notificationsNotReadCount $this->getNotificationsNotReadCount();
  16.         $tabs = new Tabs('tabs');
  17.         $tabs->addTab('all', new Tab('Všechny'$this->generateUrl('route.notifications')));
  18.         $tabs->addTab('not_read', new Tab('Nepřečtené (<span class="notification-count">' $notificationsNotReadCount '</span>)',
  19.             $this->generateUrl('route.notifications', ['not_read' => true])));
  20.         if ($request->get('not_read')) {
  21.             $tabs->setActive('not_read');
  22.             $notifications $this->getEntityManager()->getRepository(Notification::class)->getNotReadByUser($this->getUser())
  23.                 ->getQuery()->getResult();
  24.         } else {
  25.             $tabs->setActive('all');
  26.             $notifications $this->getEntityManager()->getRepository(Notification::class)->getByUser($this->getUser())
  27.                 ->getQuery()->getResult();
  28.         }
  29.         return $this->render('notification/index.html.twig', [
  30.             'notifications' => $notifications,
  31.             'tabs' => $tabs
  32.         ]);
  33.     }
  34.     #[Route('/notifications/ajax/read'name'route.notifications.ajax.read')]
  35.     public function markAsReadAction(Request $request): Response
  36.     {
  37.         return $this->markAs($request);
  38.     }
  39.     #[Route('/notifications/ajax/not_read'name'route.notifications.ajax.not_read')]
  40.     public function markAsNotReadAction(Request $request): Response
  41.     {
  42.         return $this->markAs($requestfalse);
  43.     }
  44.     public function findNotification(int $id): Notification
  45.     {
  46.         $notification $this->getEntityManager()->getRepository(Notification::class)->find($id);
  47.         if (!$notification instanceof Notification) {
  48.             throw new \Exception('Notification not found');
  49.         }
  50.         return $notification;
  51.     }
  52.     protected function markAs(Request $requestbool $read true): Response
  53.     {
  54.         $id $request->get('id');
  55.         try {
  56.             $notification $this->findNotification($id);
  57.         } catch (\Throwable $exception) {
  58.             return new Response($exception->getMessage());
  59.         }
  60.         if ($read) {
  61.             $notification->markAsRead();
  62.         } else {
  63.             $notification->markAsNotRead();
  64.         }
  65.         $this->getEntityManager()->flush();
  66.         return new JsonResponse([
  67.             'count' => $this->getNotificationsNotReadCount()
  68.         ]);
  69.     }
  70.     protected function getNotificationsNotReadCount(): int
  71.     {
  72.         $notificationsNotReadCount $this->getEntityManager()->getRepository(Notification::class)->getNotReadByUserCount($this->getUser());
  73.         return $notificationsNotReadCount;
  74.     }
  75. }