<?php
namespace App\Controller;
use App\Entity\Notification;
use App\Service\NotificationService;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* @var NotificationService
*/
protected NotificationService $notificationService;
/**
* @param NotificationService $notificationService
*/
public function __construct(NotificationService $notificationService)
{
$this->notificationService = $notificationService;
}
/**
* @Route(
* "/",
* name="app.home"
* )
*
* @return RedirectResponse
*/
public function indexAction(): RedirectResponse
{
if ($this->isLoggedIn()) {
if ($this->isGranted('ROLE_INVENTORY')) {
return $this->redirectResponse('app.product.inventory');
} else {
return $this->redirectResponse('app.sales_case.index');
}
}
return $this->redirectResponse('nucleos_user_security_login');
}
/**
* @Route(
* "/notifications/{notification}/acknowledge",
* name="app.notifications.acknowledge",
* requirements={"notification" = "\d+"}
* )
*
* @param Notification $notification
*
* @return RedirectResponse
*/
public function notificationsAcknowledgeAction(Notification $notification): RedirectResponse
{
try {
$this->notificationService->delete($notification);
$this->flashSuccess('entity.success.acknowledge', [], 'Notification');
} catch (Exception $e) {
$this->flashError('entity.error.acknowledge', [], 'Notification');
if ($this->getCurrentUser()->isSuperAdmin()) {
$this->flashError($e->getMessage());
}
}
return $this->redirectResponse('app.notifications');
}
/**
* @Route(
* "/notifications",
* name="app.notifications"
* )
*
* @return Response
*/
public function notificationsAction(): Response
{
return $this->render(
'Default/notifications.html.twig',
[
'present' => $this->notificationService->getPresent(),
'upcoming' => $this->notificationService->getUpcoming(),
]
);
}
/**
* @Route(
* "/notifications/alert",
* name="app.notifications.alert"
* )
*
* @return Response
*/
public function notificationsAlertAction(): Response
{
if (!$this->isGranted('ROLE_BASIC')) {
return new Response('');
}
return $this->render(
'Default/notificationsAlert.html.twig',
[
'notifications' => $this->notificationService->getPresent(),
]
);
}
}