<?php
namespace App\Controller;
use App\Entity\Notification;
use App\Repository\NotificationsRepository;
use App\Services\NotificationService;
use Carbon\Carbon;
use Doctrine\ORM\EntityNotFoundException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;
class NotificationsController extends DictionaryController
{
/**
* @var NotificationService
*/
protected $notificationService;
/**
* @var NotificationsRepository
*/
protected $notificationsRepository;
public function __construct(Environment $twig,
TranslatorInterface $translator,
AuthenticationUtils $authenticationUtils,
SessionInterface $session,
NotificationService $notificationService,
NotificationsRepository $notificationsRepository)
{
parent::__construct($twig, $translator, $authenticationUtils, $session);
$this->notificationService = $notificationService;
$this->notificationsRepository = $notificationsRepository;
}
public function getNotifications(Request $request)
{
$customer = $this->get('security.token_storage')->getToken()->getUser();
$notifications = $this->notificationsRepository->getNotificationsForUser($customer);
foreach ($notifications as $key => $notification) {
$notifications[$key]['timeAgo'] = Carbon::createFromTimeStamp($notification['createdAt']->getTimestamp())->locale('pl_PL')->diffForHumans();
}
$count = count($notifications);
if($request->isXmlHttpRequest()) {
$html = $this->getTwig()->render('notifications.html.twig', ['notifications' => $notifications]);
return new JsonResponse(['html' => $html, 'countNotifications' => $count]);
}
return $this->render('notifications.html.twig', ['notifications' => $notifications]);
}
public function markAsRead($id, Request $request)
{
try {
$this->notificationService->markAsRead($id, $this->getUser());
} catch (EntityNotFoundException $e) {
return new JsonResponse(['status' => 'ERROR', 'message' => $e->getMessage()]);
} catch (\Exception $exception) {
return new JsonResponse(['status' => 'ERROR', 'message' => $exception->getMessage()]);
}
return new JsonResponse(['status' => 'OK', 'id' => $id], 200);
}
public function markAsUnRead($id, Request $request)
{
try {
$this->notificationService->markAsUnRead($id, $this->getUser());
} catch (EntityNotFoundException $e) {
return new JsonResponse(['status' => 'ERROR', 'message' => $e->getMessage()]);
} catch (\Exception $exception) {
return new JsonResponse(['status' => 'ERROR', 'message' => $exception->getMessage()]);
}
return new JsonResponse(['status' => 'OK', 'id' => $id], 200);
}
public function markAllAsRead()
{
try {
$this->notificationService->markAllAsRead($this->getUser());
} catch (EntityNotFoundException $e) {
return new JsonResponse(['status' => 'ERROR', 'message' => $e->getMessage()]);
} catch (\Exception $exception) {
return new JsonResponse(['status' => 'ERROR', 'message' => $exception->getMessage()]);
}
return new JsonResponse(['status' => 'OK', 'refreshPage' => true], 200);
}
public function getConfiguration()
{
return [];
}
}