<?php
namespace App\Controller;
use App\Entity\Notification\UserNotification;
use App\Entity\NotificationEventList;
use App\Entity\User;
use App\Form\NotificationListType;
use App\Repository\Notification\UserNotificationRepository;
use App\Security\Voters\UserNotificationVoter;
use App\Service\NotificationService\DefferedMailService;
use App\Service\NotificationService\NotificationReadService;
use Knp\Component\Pager\PaginatorInterface;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/notifications', name: 'notification_', options: ['expose' => true])]
class NotificationController extends AbstractController
{
#[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_MANAGER_ADMIN') or is_granted('ROLE_CLIENT_MANAGER') or is_granted('ROLE_QUALITY_MANAGER')")]
#[Route('/{type}{page}', name: 'main', requirements: ['page' => '\d+', 'type' => 'all|orders|reclamations|errors'], defaults: ['page' => 1, 'type' => 'all'])]
public function actionIndex(int $page, string $type, PaginatorInterface $paginator, RequestStack $requestStack, UserNotificationRepository $notificationRepository): Response
{
$request = $requestStack->getCurrentRequest();
$notificationForm = $this->createForm(NotificationListType::class, options: [
'action' => $this->generateUrl('notification_main'),
// 'user' => $canGetByContractor ? null : $user,
]);
$notificationForm->handleRequest($request);
$typeIds = match ($type) {
'orders' => UserNotification::GROUP_ADMIN_ORDERS,
'reclamations' => UserNotification::GROUP_ADMIN_RECLAMATIONS,
'errors' => UserNotification::GROUP_ADMIN_ERRORS,
default => array_merge(
UserNotification::GROUP_ADMIN_ORDERS,
UserNotification::GROUP_ADMIN_RECLAMATIONS,
UserNotification::GROUP_ADMIN_ERRORS
),
};
if ($notificationForm->isSubmitted() && $notificationForm->isValid()) {
$user = $notificationForm->get('user')->getData();
// $id = (int)$notificationForm->get('id')->getData();
//
// if (!$canGetByContractor && $contractor) {
// $this->denyAccessUnlessGranted(ContractorVoter::VIEW, $contractor);
// }
$ordersQuery = $notificationRepository->getByUser($user, $typeIds, true);
$pagination = $paginator->paginate($ordersQuery, $page);
/** @var UserNotification[] $notifications */
$notifications = $pagination->getItems();
return $this->render('notification/notification-list.html.twig', compact('notifications', 'pagination'));
}
$ordersQuery = $notificationRepository->getByUser(typeIds: $typeIds, queryOnly: true);
$pagination = $paginator->paginate($ordersQuery, $page);
/** @var UserNotification[] $notifications */
$notifications = $pagination->getItems();
return $this->render('notification/index.html.twig', [
'notificationForm' => $notificationForm->createView(),
'notifications' => $notifications,
'pagination' => $pagination,
'seo' => [
'title' => 'Уведомления'
]
]);
}
#[Route('/get/{type}/{page}', name: 'get', requirements: ['type' => 'all|orders|reclamations','page' => '\d+'], defaults: ['type' => 'all', 'page' => 1], methods: ['POST'])]
public function actionGet(PaginatorInterface $paginator, UserNotificationRepository $notificationRepository, string $type, int $page): Response
{
$typeIds = match ($type) {
'orders' => UserNotification::GROUP_CLIENT_ORDERS,
'reclamations' => UserNotification::GROUP_CLIENT_RECLAMATIONS,
'decors' => UserNotification::GROUP_CLIENT_DECORS,
default => array_merge(UserNotification::GROUP_CLIENT_ORDERS, UserNotification::GROUP_CLIENT_RECLAMATIONS, UserNotification::GROUP_CLIENT_DECORS),
};
$notifications = $notificationRepository->getByUser($this->getUser(), $typeIds, true);
$paginator = $paginator->paginate($notifications, $page, 20);
$dates = [];
/** @var UserNotification $item */
foreach ($paginator->getItems() as $item) {
$dates[$item->getCreatedAt()->format('d.m.Y')][] = $this->renderView('notification/single-notify.html.twig', compact('item'));
}
return $this->json([
'items' => $dates,
'page' => $page,
'type' => $type,
'nextPage' => (ceil($paginator->getTotalItemCount() / $paginator->getItemNumberPerPage()) - $paginator->getCurrentPageNumber()) > 0
]);
}
#[Route('/check-new', name: 'check_new', methods: ['POST'])]
public function actionCheckNew(UserNotificationRepository $notificationRepository): Response
{
/** @var User $user */
if (! $user = $this->getUser()) {
return $this->json([
'success' => false,
'message' => 'Ошибка не найден пользователь.'
]);
}
$notifications = $notificationRepository->checkNew($user);
$response = ['orders' => false, 'reclamations' => false, 'decors' => false, 'all' => false];
foreach (UserNotification::GROUP_CLIENT_ORDERS as $typeId) {
if (array_key_exists($typeId, $notifications)) {
$response['orders'] = true;
}
}
foreach (UserNotification::GROUP_CLIENT_RECLAMATIONS as $typeId) {
if (array_key_exists($typeId, $notifications)) {
$response['reclamations'] = true;
}
}
foreach (UserNotification::GROUP_CLIENT_DECORS as $typeId) {
if (array_key_exists($typeId, $notifications)) {
$response['decors'] = true;
}
}
if (in_array(true, $response)) {
$response['all'] = true;
}
return $this->json($response);
}
#[Route('/set-checked/{id}', name: 'set_checked', methods: ['POST'])]
public function actionSetChecked(UserNotification $notification, DefferedMailService $defferedMailService): Response
{
$this->denyAccessUnlessGranted(UserNotificationVoter::VIEW, $notification);
$notification->setReadedAt(($notification->getReadedAt() ? null : new \DateTime()));
$parentNotification = $notification->getParentNotification();
if ($parentNotification) {
$defferedMailService->removeDefferedMail($parentNotification);
}
$parentNotification?->getReclamationMessageDeliveryStatus()?->setReadedAt($notification->getReadedAt());
$this->getDoctrine()->getManager()->flush();
return $this->json(['result' => true]);
}
#[Route('/set-read-all', name: 'set_checked_all', methods: ['POST'])]
public function actionSetReadAll(UserNotificationRepository $notificationRepository, RequestStack $requestStack, DefferedMailService $defferedMailService, NotificationReadService $notificationReadService): Response
{
$request = $requestStack->getCurrentRequest();
$type = $request?->request->get('type') ?? 'orders';
$typeIds = match ($type) {
'orders' => UserNotification::GROUP_CLIENT_ORDERS,
'reclamations' => UserNotification::GROUP_CLIENT_RECLAMATIONS,
'decors' => UserNotification::GROUP_CLIENT_DECORS,
default => array_merge(UserNotification::GROUP_CLIENT_ORDERS, UserNotification::GROUP_CLIENT_RECLAMATIONS, UserNotification::GROUP_CLIENT_DECORS),
};
$notificationReadService->setReadAll($this->getUser(), $typeIds);
// $unreadedNotifications = $notificationRepository->getAllUnreaded($this->getUser(), $typeIds);
//
// /** @var UserNotification $unreadedNotification */
// foreach ($unreadedNotifications as $notification){
// $notification->setReadedAt(($notification->getReadedAt() ? null : new \DateTime()));
// /** @var NotificationEventList $parentNotification */
// $parentNotification = $notification->getParentNotification();
// if ($parentNotification){
// $parentNotification->setReadedAt(($parentNotification->getReadedAt() ? null : new \DateTime()));
// $defferedMailService->removeDefferedMail($parentNotification);
// }
// $parentNotification?->getReclamationMessageDeliveryStatus()?->setReadedAt($notification->getReadedAt());
// }
// $this->getDoctrine()->getManager()->flush();
//
// $notificationRepository->setReadAll($this->getUser(), $typeIds);
return $this->json(['result' => true]);
}
}