<?php
namespace EADPlataforma\Controller\Admin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\Notification;
use EADPlataforma\Enum\NotificationEnum;
use EADPlataforma\Enum\ErrorEnum;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class NotificationController extends AbstractController {
public function getEntityClass(){
return Notification::class;
}
/**
* @Route(
* path = "/admin/v2/user/{userId}/notifications/read",
* methods = {"GET"}
* )
*/
public function getNotificationReadPaginateNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$columns = [
"n.id",
"n.origin",
"n.reference",
"n.status",
"DATE_FORMAT(n.date, '%Y-%m-%d %H:%i:%s') AS date",
"n.title",
"n.description",
"n.link",
"n.icon",
];
$joins = [];
$userId = $this->user->getId();
$orderParam = $this->requestUtil->getField('order');
$searchText = $this->requestUtil->getField('searchText');
$limit = (int)$this->requestUtil->getField('limit');
$offset = (int)$this->requestUtil->getField('offset');
$filter = [
"n.userTo" => $userId,
"n.status" => NotificationEnum::VIEWED
];
$order = [ "n.id" => "DESC" ];
if(!empty($orderParam)){
$order = json_decode($orderParam, true);
}
$data = $this->repository->paginate(
"n",
$searchText,
$columns,
$joins,
$filter,
$order,
$limit,
$offset,
null,
false,
[],
false
);
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/user/{userId}/notifications/unread",
* methods = {"GET"}
* )
*/
public function getNotificationUnreadPaginateNew(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$columns = [
"n.id",
"n.origin",
"n.reference",
"n.status",
"DATE_FORMAT(n.date, '%Y-%m-%d %H:%i:%s') AS date",
"n.title",
"n.description",
"n.link",
"n.icon",
];
$joins = [];
$userId = $this->user->getId();
$orderParam = $this->requestUtil->getField('order');
$searchText = $this->requestUtil->getField('searchText');
$limit = (int)$this->requestUtil->getField('limit');
$offset = (int)$this->requestUtil->getField('offset');
$filter = [
"n.userTo" => $userId,
"n.status" => NotificationEnum::NOT_VIEWED
];
$order = [ "n.id" => "DESC" ];
if(!empty($orderParam)){
$order = json_decode($orderParam, true);
}
$data = $this->repository->paginate(
"n",
$searchText,
$columns,
$joins,
$filter,
$order,
$limit,
$offset,
null,
false,
[],
false
);
return $this->eadResponseNew($data);
}
/**
* @Route(
* path = "/admin/v2/user/{userId}/notifications/unread/total",
* methods = {"GET"}
* )
*/
public function getUserNumberUnreadNotificationNew(Request $request) {
$userId = $this->user->getId();
$number = $this->repository->count([
"userTo" => $userId,
"status" => NotificationEnum::NOT_VIEWED,
]);
return $this->eadResponseNew([ "total" => $number ]);
}
/**
* @Route(
* path = "/v2/user/{userId}/notifications/{notificationId}",
* methods = {"GET"},
* name = "seeNotificationNew"
* )
*/
public function seeNotificationNew(Request $request) {
$this->checkUserSession($request);
$notificationId = $request->get('notificationId');
$notification = $this->repository->find($notificationId);
if(!$notification){
return $this->redirectToRoute('notFound');
}
if($this->user->getId() != $notification->getUserTo()->getId()){
return $this->redirectToRoute('notFound');
}
$notification->setStatus(NotificationEnum::VIEWED);
$this->em->flush();
return $this->redirect("//{$request->getHost()}{$notification->getLink()}", 301);
}
/**
* @Route(
* path = "/admin/v2/user/{userId}/notifications",
* methods = {"PATCH"}
* )
*/
public function readAllNotificationNew(Request $request) {
$userId = $this->user->getId();
$notifications = $this->repository->findBy([
"userTo" => $userId,
"status" => NotificationEnum::NOT_VIEWED
]);
foreach ($notifications as $key => $notification) {
$notification->setStatus(NotificationEnum::VIEWED);
}
$this->em->flush();
return $this->eadResponseNew([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/notification/user/list/paginate",
* methods = {"GET"}
* )
*/
public function getNotificationPaginate(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$columns = [
"n.id",
"n.origin",
"n.reference",
"n.status",
"DATE_FORMAT(n.date, '%Y-%m-%d %H:%i:%s') AS date",
"n.title",
"n.description",
"n.link",
"n.icon",
];
$joins = [];
$userId = $this->user->getId();
$status = $this->requestUtil->getField('status');
$orderParam = $this->requestUtil->getField('order');
$searchText = $this->requestUtil->getField('searchText');
$limit = (int)$this->requestUtil->getField('limit');
$offset = (int)$this->requestUtil->getField('offset');
$joins = [];
$filter = [
"n.userTo" => $userId
];
if(!is_null($status)){
$filter["n.status"] = (int)$status;
}
$order = [ "n.status" => "ASC", "n.id" => "DESC" ];
if(!empty($orderParam)){
$order = json_decode($orderParam, true);
}
$data = $this->repository->paginate(
"n",
$searchText,
$columns,
$joins,
$filter,
$order,
$limit,
$offset,
null,
false,
[],
false
);
$numberTotal = $this->repository->count([
"userTo" => $userId
]);
$numberNotRead = $this->repository->count([
"userTo" => $userId,
"status" => NotificationEnum::NOT_VIEWED,
]);
$data['total'] = $numberTotal;
$data['notRead'] = $numberNotRead;
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/notification/user",
* methods = {"GET"}
* )
*/
public function getUserNotification(Request $request) {
$userId = $this->user->getId();
$data = $this->repository->findBy([
"userTo" => $userId
]);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/admin/notification/user/number",
* methods = {"GET"}
* )
*/
public function getUserNumberNotification(Request $request) {
$userId = $this->user->getId();
$number = $this->repository->count([
"userTo" => $userId,
"status" => NotificationEnum::NOT_VIEWED,
]);
return $this->eadResponse([ "total" => $number ]);
}
/**
* @Route(
* path = "/admin/notification/user/new",
* methods = {"GET"}
* )
*/
public function getUserNewNotification(Request $request) {
$userId = $this->user->getId();
$data = $this->repository->findBy(
[
"userTo" => $userId
],
[
"status" => "ASC",
"date" => "DESC"
],
5
);
return $this->eadResponse($data);
}
/**
* @Route(
* path = "/notification/see/{notificationId}",
* methods = {"GET"},
* name = "seeNotification"
* )
*/
public function seeNotification(Request $request) {
$this->checkUserSession($request);
$notificationId = $request->get('notificationId');
$notification = $this->repository->find($notificationId);
if(!$notification){
return $this->redirectToRoute('notFound');
}
if($this->user->getId() != $notification->getUserTo()->getId()){
return $this->redirectToRoute('notFound');
}
$notification->setStatus(NotificationEnum::VIEWED);
$this->em->flush();
return $this->redirect("//{$request->getHost()}{$notification->getLink()}", 301);
}
/**
* @Route(
* path = "/admin/notification/read/{notificationId}",
* methods = {"PUT"}
* )
*/
public function readNotification(Request $request) {
$notificationId = $request->get('notificationId');
$notification = $this->repository->find($notificationId);
if(!$notification){
return $this->eadResponse(null, ErrorEnum::NOT_FOUND);
}
$notification->setStatus(NotificationEnum::VIEWED);
$this->em->flush();
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/admin/notification/user/read/all",
* methods = {"PUT"}
* )
*/
public function readAllNotification(Request $request) {
$userId = $this->user->getId();
$notifications = $this->repository->findBy([
"userTo" => $userId,
"status" => NotificationEnum::NOT_VIEWED
]);
foreach ($notifications as $key => $notification) {
$notification->setStatus(NotificationEnum::VIEWED);
}
$this->em->flush();
return $this->eadResponse([ "message" => "Success" ]);
}
}