em = $em; $this->security = $security; $this->notificationRepository = $notificationRepository; $this->notificationHandlerManager = $notificationHandlerManager; $this->paginatorFactory = $paginatorFactory; $this->translator = $translator; } /** * @Route("/create", name="chill_main_notification_create") */ public function createAction(Request $request): Response { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); if (!$this->security->getUser() instanceof User) { throw new AccessDeniedHttpException('You must be authenticated and a user to create a notification'); } if (!$request->query->has('entityClass')) { throw new BadRequestHttpException('Missing entityClass parameter'); } if (!$request->query->has('entityId')) { throw new BadRequestHttpException('missing entityId parameter'); } $notification = new Notification(); $notification ->setRelatedEntityClass($request->query->get('entityClass')) ->setRelatedEntityId($request->query->getInt('entityId')) ->setSender($this->security->getUser()); try { $handler = $this->notificationHandlerManager->getHandler($notification); } catch (NotificationHandlerNotFound $e) { throw new BadRequestHttpException('no handler for this notification'); } $form = $this->createForm(NotificationType::class, $notification); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->em->persist($notification); $this->em->flush(); $this->addFlash('success', $this->translator->trans('notification.Notification created')); if ($request->query->has('returnPath')) { return new RedirectResponse($request->query->get('returnPath')); } return $this->redirectToRoute('chill_main_homepage'); } return $this->render('@ChillMain/Notification/create.html.twig', [ 'form' => $form->createView(), 'handler' => $handler, 'notification' => $notification, ]); } /** * @Route("/my", name="chill_main_notification_my") */ public function myAction(): Response { $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); $currentUser = $this->security->getUser(); $notificationsNbr = $this->notificationRepository->countAllForAttendee(($currentUser)); $paginator = $this->paginatorFactory->create($notificationsNbr); $notifications = $this->notificationRepository->findAllForAttendee( $currentUser, $limit = $paginator->getItemsPerPage(), $offset = $paginator->getCurrentPage()->getFirstItemNumber() ); $templateData = []; foreach ($notifications as $notification) { $data = [ 'template' => $this->notificationHandlerManager->getTemplate($notification), 'template_data' => $this->notificationHandlerManager->getTemplateData($notification), 'notification' => $notification, ]; $templateData[] = $data; } return $this->render('@ChillMain/Notification/show.html.twig', [ 'datas' => $templateData, 'notifications' => $notifications, 'paginator' => $paginator, ]); } }