<?php
namespace App\Controller;
use App\Entity\Notification;
use App\Entity\UserNotification;
use DateTime;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class NotificationController extends AbstractController
{
/**
* @Route("/notification", name="notification.index", methods={"GET"})
* @param Request $request
* @param PaginatorInterface $paginator
* @return Response
*/
public function index(Request $request, PaginatorInterface $paginator): Response
{
/** @var UserNotification $userNotifications */
$query = $this->getDoctrine()->getRepository(UserNotification::class)->getUserNotification($this->getUser()->getId(), true);
$userNotifications = $paginator->paginate(
$query,
$request->query->getInt('page', 1),
15,
['defaultSortFieldName' => 'n.createdAt', 'defaultSortDirection' => 'desc']
);
return $this->render('notification/index.html.twig', [
'controller_name' => 'NotificationController',
'userNotifications' => $userNotifications
]);
}
/**
* @Route("/notification/mark-as-read-all", name="notification.mark.as.read.all", methods={"GET"})
* @return RedirectResponse
*/
public function markAllNotificationReaded(): RedirectResponse
{
/** @var UserNotification[] $notifications */
$notifications = $this->getDoctrine()->getRepository(UserNotification::class)->getAllNotificationUnread(
$this->getUser()->getId()
);
foreach ($notifications as $notification) {
$notification->setIsRead(true);
$this->getDoctrine()->getManager()->persist($notification);
}
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('notification.index');
}
/**
* @Route("/notification/unread", name="notification.unread", methods={"GET"})
* @param TranslatorInterface $translator
* @return JsonResponse
*/
public function getLastNotification(TranslatorInterface $translator): JsonResponse
{
/** @var UserNotification[] $userNotifications */
$userNotifications = $this->getDoctrine()->getRepository(UserNotification::class)->getLastNotificationUnread($this->getUser()->getId());
$unreadNotificationCounter = $this->getDoctrine()->getRepository(UserNotification::class)->count([
'user' => $this->getUser(),
'isRead' => false
]);
$response = [
'count' => $unreadNotificationCounter,
'notifications' => []
];
foreach ($userNotifications as $key => $notification) {
$customer = '';
if (!is_null($notification->getNotification()->getJob())) {
if (!is_null($notification->getNotification()->getJob()->getContact())) {
$customer = $notification->getNotification()->getJob()->getContact()->getFirstname().' '.
$notification->getNotification()->getJob()->getContact()->getLastname();
} else {
$customer = $notification->getNotification()->getJob()->getFromAddress();
}
}
$response['notifications'][$key] = [
'id' => $notification->getNotification()->getId(),
'title' => $notification->getNotification()->getTitle(),
'content' => $translator->trans($notification->getNotification()->getContent()),
'customer' => $customer,
'level' => $notification->getNotification()->getLevel(),
'createdAt' => $notification->getNotification()->getCreatedAt()
];
}
return new JsonResponse($response);
}
/**
* @Route("/notification/show/{id}", name="notification.show", methods={"GET"})
* @param Notification $notification
* @return Response
*/
public function show(Notification $notification): Response
{
$userNotification = $this->getDoctrine()->getRepository(UserNotification::class)->findOneBy([
'notification' => $notification->getId(),
'user' => $this->getUser()
]);
if (!is_null($userNotification)) {
$userNotification->setIsRead(true)
->setReadAt(new DateTime());
$this->getDoctrine()->getManager()->persist($userNotification);
$this->getDoctrine()->getManager()->flush();
}
return $this->render('notification/show.html.twig', [
'controller_name' => 'NotificationController',
'notification' => $notification
]);
}
/**
* @Route("/notification/markastreated/{id}", name="notification.markastreated", methods={"GET"})
* @param Notification $notification
* @return RedirectResponse
*/
public function markAsTreated(Notification $notification): RedirectResponse
{
$notification->setIsTreated(true)
->setTreatedBy($this->getUser());
$this->getDoctrine()->getManager()->persist($notification);
$this->getDoctrine()->getManager()->flush();
$this->addFlash(
'success',
'Enregistrement effectué avec succès !'
);
return $this->redirectToRoute('notification.show', ['id' => $notification->getId()]);
}
/**
* @Route("/notification/markasunread/{id}", name="notification.markasunread", methods={"GET"})
* @param Notification $notification
* @return RedirectResponse
*/
public function markAsUnread(Notification $notification): RedirectResponse
{
$userNotification = $this->getDoctrine()->getRepository(UserNotification::class)->findOneBy([
'user' => $this->getUser(),
'notification' => $notification
]);
if (!is_null($userNotification)) {
$userNotification->setIsRead(false);
$this->getDoctrine()->getManager()->persist($userNotification);
$this->getDoctrine()->getManager()->flush();
}
return $this->redirectToRoute('notification.index');
}
}