2019-03-01 12:18:06 +01:00
|
|
|
<?php
|
|
|
|
|
2019-03-01 12:49:26 +01:00
|
|
|
namespace Bluesquare\NotificationsBundle\Controller;
|
2019-03-01 12:18:06 +01:00
|
|
|
|
2019-03-01 16:02:12 +01:00
|
|
|
use Bluesquare\NotificationsBundle\Entity\Notification;
|
2019-03-01 12:18:06 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
2019-03-01 16:02:12 +01:00
|
|
|
/**
|
|
|
|
* Class MainController
|
|
|
|
* @package Bluesquare\NotificationsBundle\Controller
|
|
|
|
*/
|
2019-03-01 12:18:06 +01:00
|
|
|
class MainController extends AbstractController
|
|
|
|
{
|
2019-03-01 16:26:21 +01:00
|
|
|
/**
|
|
|
|
* Get all the current user's notifications
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
|
|
|
*/
|
2019-03-01 16:02:12 +01:00
|
|
|
public function getAll()
|
2019-03-01 12:18:06 +01:00
|
|
|
{
|
2019-03-01 16:26:21 +01:00
|
|
|
if(!($user = $this->getUser()))
|
|
|
|
{
|
|
|
|
return $this->json(["message" => "Please login"], 403);
|
|
|
|
}
|
2019-03-01 14:20:14 +01:00
|
|
|
|
2019-03-01 13:19:12 +01:00
|
|
|
$notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv');
|
2019-03-01 14:20:14 +01:00
|
|
|
$notifs = $notifssrv->getForUser($user, true);
|
2019-03-01 16:26:21 +01:00
|
|
|
|
2019-03-01 14:20:14 +01:00
|
|
|
return $this->json($notifs);
|
2019-03-01 12:18:06 +01:00
|
|
|
}
|
2019-03-01 16:02:12 +01:00
|
|
|
|
2019-03-01 16:26:21 +01:00
|
|
|
/**
|
|
|
|
* Delete a notifiation that belong to the current user.
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
|
|
|
*/
|
2019-03-01 16:02:12 +01:00
|
|
|
public function delete($id)
|
|
|
|
{
|
2019-03-01 16:26:21 +01:00
|
|
|
if(!($user = $this->getUser()))
|
|
|
|
{
|
|
|
|
return $this->json(["message" => "Please login"], 403);
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:02:12 +01:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$notif = $em->getRepository(Notification::class)->find($id);
|
|
|
|
|
|
|
|
if (!$notif)
|
2019-03-01 16:26:21 +01:00
|
|
|
return $this->json(["message" => "Notification not found"], 404);
|
2019-03-01 16:02:12 +01:00
|
|
|
|
|
|
|
if ($notif->getUser() != $user)
|
2019-03-01 16:26:21 +01:00
|
|
|
return $this->json(["message" => "Access denied"], 403);
|
2019-03-01 16:02:12 +01:00
|
|
|
|
|
|
|
$em->remove($notif);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json(["message" => 'OK']);
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:26:21 +01:00
|
|
|
/**
|
|
|
|
* Mark a notification of the current user as 'Seen'
|
|
|
|
*
|
|
|
|
* @param $id
|
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
|
|
|
*/
|
2019-03-01 16:02:12 +01:00
|
|
|
public function markSeen($id)
|
|
|
|
{
|
2019-03-01 16:26:21 +01:00
|
|
|
if(!($user = $this->getUser()))
|
|
|
|
{
|
|
|
|
return $this->json(["message" => "Please login"], 403);
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:02:12 +01:00
|
|
|
$em = $this->getDoctrine()->getManager();
|
|
|
|
$notif = $em->getRepository(Notification::class)->find($id);
|
|
|
|
|
|
|
|
if (!$notif)
|
2019-03-01 16:26:21 +01:00
|
|
|
return $this->json(["message" => "Notification not found"], 404);
|
2019-03-01 16:02:12 +01:00
|
|
|
|
|
|
|
if ($notif->getUser() != $user)
|
2019-03-01 16:26:21 +01:00
|
|
|
return $this->json(["message" => "Access denied"], 403);
|
2019-03-01 16:02:12 +01:00
|
|
|
|
|
|
|
$notif->setSeenAt(new \DateTime());
|
|
|
|
|
|
|
|
$em->persist($notif);
|
|
|
|
$em->flush();
|
|
|
|
|
|
|
|
return $this->json(["message" => 'OK']);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-01 16:26:21 +01:00
|
|
|
/**
|
|
|
|
* Test method that adds a dummy notification to the current user.
|
|
|
|
*
|
|
|
|
* @return \Symfony\Component\HttpFoundation\JsonResponse
|
|
|
|
*/
|
2019-03-01 16:02:12 +01:00
|
|
|
public function test()
|
|
|
|
{
|
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
|
|
|
|
$user = $this->getUser();
|
|
|
|
$notifssrv = $this->get('bluesquare.notifications_bundle.notifssrv');
|
|
|
|
|
|
|
|
$notif = new Notification();
|
|
|
|
$notif->setTitle("Bonjour");
|
|
|
|
$notif->setDescription("Skippy vous attend dans son antre");
|
|
|
|
$notif->setData("le_nom_de_la_route_en_react");
|
|
|
|
|
|
|
|
$users = [$user]; // Peut contenir plein d'users
|
|
|
|
$notifssrv->notifyUsers($users, $notif);
|
|
|
|
|
|
|
|
return $this->json(["message" => 'OK']);
|
|
|
|
}
|
2019-03-01 12:18:06 +01:00
|
|
|
}
|