src/Controller/NotificationController.php line 107

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notification\UserNotification;
  4. use App\Entity\NotificationEventList;
  5. use App\Entity\User;
  6. use App\Form\NotificationListType;
  7. use App\Repository\Notification\UserNotificationRepository;
  8. use App\Security\Voters\UserNotificationVoter;
  9. use App\Service\NotificationService\DefferedMailService;
  10. use App\Service\NotificationService\NotificationReadService;
  11. use Knp\Component\Pager\PaginatorInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  13. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. #[Route('/notifications'name'notification_'options: ['expose' => true])]
  18. class NotificationController extends AbstractController
  19. {
  20.     #[Security("is_granted('ROLE_ADMIN') or is_granted('ROLE_MANAGER_ADMIN') or is_granted('ROLE_CLIENT_MANAGER') or is_granted('ROLE_QUALITY_MANAGER')")]
  21.     #[Route('/{type}{page}'name'main'requirements: ['page' => '\d+''type' => 'all|orders|reclamations|errors'], defaults: ['page' => 1'type' => 'all'])]
  22.     public function actionIndex(int $pagestring $typePaginatorInterface $paginatorRequestStack $requestStackUserNotificationRepository $notificationRepository): Response
  23.     {
  24.         $request $requestStack->getCurrentRequest();
  25.         $notificationForm $this->createForm(NotificationListType::class, options: [
  26.             'action' => $this->generateUrl('notification_main'),
  27. //            'user' => $canGetByContractor ? null : $user,
  28.         ]);
  29.         $notificationForm->handleRequest($request);
  30.         $typeIds = match ($type) {
  31.             'orders' => UserNotification::GROUP_ADMIN_ORDERS,
  32.             'reclamations' => UserNotification::GROUP_ADMIN_RECLAMATIONS,
  33.             'errors' => UserNotification::GROUP_ADMIN_ERRORS,
  34.             default => array_merge(
  35.                 UserNotification::GROUP_ADMIN_ORDERS,
  36.                 UserNotification::GROUP_ADMIN_RECLAMATIONS,
  37.                 UserNotification::GROUP_ADMIN_ERRORS
  38.             ),
  39.         };
  40.         if ($notificationForm->isSubmitted() && $notificationForm->isValid()) {
  41.             $user $notificationForm->get('user')->getData();
  42. //            $id = (int)$notificationForm->get('id')->getData();
  43. //
  44. //            if (!$canGetByContractor && $contractor) {
  45. //                $this->denyAccessUnlessGranted(ContractorVoter::VIEW, $contractor);
  46. //            }
  47.             $ordersQuery $notificationRepository->getByUser($user$typeIdstrue);
  48.             $pagination $paginator->paginate($ordersQuery$page);
  49.             /** @var UserNotification[] $notifications */
  50.             $notifications $pagination->getItems();
  51.             return $this->render('notification/notification-list.html.twig'compact('notifications''pagination'));
  52.         }
  53.         $ordersQuery $notificationRepository->getByUser(typeIds$typeIdsqueryOnlytrue);
  54.         $pagination $paginator->paginate($ordersQuery$page);
  55.         /** @var UserNotification[] $notifications */
  56.         $notifications $pagination->getItems();
  57.         return $this->render('notification/index.html.twig', [
  58.             'notificationForm' => $notificationForm->createView(),
  59.             'notifications' => $notifications,
  60.             'pagination' => $pagination,
  61.             'seo' => [
  62.                 'title' => 'Уведомления'
  63.             ]
  64.         ]);
  65.     }
  66.     #[Route('/get/{type}/{page}'name'get'requirements: ['type' => 'all|orders|reclamations','page' => '\d+'], defaults: ['type' => 'all''page' => 1], methods: ['POST'])]
  67.     public function actionGet(PaginatorInterface $paginatorUserNotificationRepository $notificationRepositorystring $typeint $page): Response
  68.     {
  69.         $typeIds = match ($type) {
  70.             'orders' => UserNotification::GROUP_CLIENT_ORDERS,
  71.             'reclamations' => UserNotification::GROUP_CLIENT_RECLAMATIONS,
  72.             'decors' => UserNotification::GROUP_CLIENT_DECORS,
  73.             default => array_merge(UserNotification::GROUP_CLIENT_ORDERSUserNotification::GROUP_CLIENT_RECLAMATIONSUserNotification::GROUP_CLIENT_DECORS),
  74.         };
  75.         $notifications $notificationRepository->getByUser($this->getUser(), $typeIdstrue);
  76.         $paginator $paginator->paginate($notifications$page20);
  77.         $dates = [];
  78.         /** @var UserNotification $item */
  79.         foreach ($paginator->getItems() as $item) {
  80.             $dates[$item->getCreatedAt()->format('d.m.Y')][] = $this->renderView('notification/single-notify.html.twig'compact('item'));
  81.         }
  82.         return $this->json([
  83.             'items' => $dates,
  84.             'page' => $page,
  85.             'type' => $type,
  86.             'nextPage' => (ceil($paginator->getTotalItemCount() / $paginator->getItemNumberPerPage()) - $paginator->getCurrentPageNumber()) > 0
  87.         ]);
  88.     }
  89.     #[Route('/check-new'name'check_new'methods: ['POST'])]
  90.     public function actionCheckNew(UserNotificationRepository $notificationRepository): Response
  91.     {
  92.         /** @var User $user */
  93.         if (! $user $this->getUser()) {
  94.              return $this->json([
  95.                  'success' => false,
  96.                  'message' => 'Ошибка не найден пользователь.'
  97.              ]);
  98.         }
  99.         $notifications $notificationRepository->checkNew($user);
  100.         $response = ['orders' => false'reclamations' => false'decors' => false'all' => false];
  101.         foreach (UserNotification::GROUP_CLIENT_ORDERS as $typeId) {
  102.             if (array_key_exists($typeId$notifications)) {
  103.                 $response['orders'] = true;
  104.             }
  105.         }
  106.         foreach (UserNotification::GROUP_CLIENT_RECLAMATIONS as $typeId) {
  107.             if (array_key_exists($typeId$notifications)) {
  108.                 $response['reclamations'] = true;
  109.             }
  110.         }
  111.         foreach (UserNotification::GROUP_CLIENT_DECORS as $typeId) {
  112.             if (array_key_exists($typeId$notifications)) {
  113.                 $response['decors'] = true;
  114.             }
  115.         }
  116.         if (in_array(true$response)) {
  117.             $response['all'] = true;
  118.         }
  119.         return $this->json($response);
  120.     }
  121.     #[Route('/set-checked/{id}'name'set_checked'methods: ['POST'])]
  122.     public function actionSetChecked(UserNotification $notificationDefferedMailService $defferedMailService): Response
  123.     {
  124.         $this->denyAccessUnlessGranted(UserNotificationVoter::VIEW$notification);
  125.         $notification->setReadedAt(($notification->getReadedAt() ? null : new \DateTime()));
  126.         $parentNotification $notification->getParentNotification();
  127.         if ($parentNotification) {
  128.             $defferedMailService->removeDefferedMail($parentNotification);
  129.         }
  130.         $parentNotification?->getReclamationMessageDeliveryStatus()?->setReadedAt($notification->getReadedAt());
  131.         $this->getDoctrine()->getManager()->flush();
  132.         return $this->json(['result' => true]);
  133.     }
  134.     #[Route('/set-read-all'name'set_checked_all'methods: ['POST'])]
  135.     public function actionSetReadAll(UserNotificationRepository $notificationRepositoryRequestStack $requestStackDefferedMailService $defferedMailServiceNotificationReadService $notificationReadService): Response
  136.     {
  137.         $request $requestStack->getCurrentRequest();
  138.         $type $request?->request->get('type') ?? 'orders';
  139.         $typeIds = match ($type) {
  140.             'orders' => UserNotification::GROUP_CLIENT_ORDERS,
  141.             'reclamations' => UserNotification::GROUP_CLIENT_RECLAMATIONS,
  142.             'decors' => UserNotification::GROUP_CLIENT_DECORS,
  143.             default => array_merge(UserNotification::GROUP_CLIENT_ORDERSUserNotification::GROUP_CLIENT_RECLAMATIONSUserNotification::GROUP_CLIENT_DECORS),
  144.         };
  145.         $notificationReadService->setReadAll($this->getUser(), $typeIds);
  146. //        $unreadedNotifications = $notificationRepository->getAllUnreaded($this->getUser(), $typeIds);
  147. //
  148. //        /** @var UserNotification $unreadedNotification */
  149. //        foreach ($unreadedNotifications as $notification){
  150. //            $notification->setReadedAt(($notification->getReadedAt() ? null : new \DateTime()));
  151. //            /** @var NotificationEventList $parentNotification */
  152. //            $parentNotification = $notification->getParentNotification();
  153. //            if ($parentNotification){
  154. //                $parentNotification->setReadedAt(($parentNotification->getReadedAt() ? null : new \DateTime()));
  155. //                $defferedMailService->removeDefferedMail($parentNotification);
  156. //            }
  157. //            $parentNotification?->getReclamationMessageDeliveryStatus()?->setReadedAt($notification->getReadedAt());
  158. //        }
  159. //        $this->getDoctrine()->getManager()->flush();
  160. //
  161. //        $notificationRepository->setReadAll($this->getUser(), $typeIds);
  162.         return $this->json(['result' => true]);
  163.     }
  164. }