From cb3f09d622dfc42f91e66e5144eb334864c7384f Mon Sep 17 00:00:00 2001 From: Marc Ducobu Date: Tue, 27 Jul 2021 10:55:46 +0200 Subject: [PATCH] Controller for sent & received notifications --- .../Controller/NotificationController.php | 46 +++++++++++++++++-- .../Repository/NotificationRepository.php | 38 +++++++++++---- 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/NotificationController.php b/src/Bundle/ChillMainBundle/Controller/NotificationController.php index c76e19d9e..1f4d7d795 100644 --- a/src/Bundle/ChillMainBundle/Controller/NotificationController.php +++ b/src/Bundle/ChillMainBundle/Controller/NotificationController.php @@ -22,21 +22,57 @@ class NotificationController extends AbstractController $this->security = $security; } - /** - * @Route("/show", name="chill_main_notification_show") + * @Route("/list/sent", name="chill_main_send_notification_list") */ - public function showAction( + public function sentListAction( NotificationRepository $notificationRepository, NotificationRenderer $notificationRenderer, PaginatorFactory $paginatorFactory) { $currentUser = $this->security->getUser(); - $notificationsNbr = $notificationRepository->countAllForAttendee(($currentUser)); + $notificationsNbr = $notificationRepository->countAllForAddresseeOrSender(null, $currentUser); $paginator = $paginatorFactory->create($notificationsNbr); - $notifications = $notificationRepository->findAllForAttendee( + $notifications = $notificationRepository->findAllForAddresseeOrSender( + null, $currentUser, + $paginator->getItemsPerPage(), + $paginator->getCurrentPage()->getFirstItemNumber()); + + $templateData = array(); + foreach ($notifications as $notification) { + $data = [ + 'template' => $notificationRenderer->getTemplate($notification), + 'template_data' => $notificationRenderer->getTemplateData($notification), + 'notification' => $notification + ]; + $templateData[] = $data; + } + + + return $this->render('@ChillMain/Notification/show.html.twig', [ + 'datas' => $templateData, + 'notifications' => $notifications, + 'paginator' => $paginator, + ]); + } + + /** + * @Route("/list/received", name="chill_main_received_notification_list") + */ + public function receivedListAction( + NotificationRepository $notificationRepository, NotificationRenderer $notificationRenderer, + PaginatorFactory $paginatorFactory) + { + $currentUser = $this->security->getUser(); + + $notificationsNbr = $notificationRepository->countAllForAddresseeOrSender($currentUser, null); + $paginator = $paginatorFactory->create($notificationsNbr); + + $notifications = $notificationRepository->findAllForAddresseeOrSender( + $currentUser, + null, $limit=$paginator->getItemsPerPage(), $offset= $paginator->getCurrentPage()->getFirstItemNumber()); diff --git a/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php b/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php index 5ffda75dc..b3af3dd92 100644 --- a/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php @@ -61,7 +61,7 @@ final class NotificationRepository implements ObjectRepository return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } - private function queryAllForAttendee(User $addressee, bool $countQuery=False): Query + private function queryAllForAddresseeOrSender(?User $addressee=null, ?User $sender=null, bool $countQuery=False): Query { $qb = $this->repository->createQueryBuilder('n'); @@ -70,11 +70,29 @@ final class NotificationRepository implements ObjectRepository $select = 'count(n)'; } - $qb - ->select($select) + $qb = $qb + ->select($select); + + $params = array(); + + if($addressee !== null) { + $qb = $qb ->join('n.addressees', 'a') - ->where('a = :addressee') - ->setParameter('addressee', $addressee); + ->where('a = :addressee'); + + $params['addressee'] = $addressee; + } + + if($sender !== null) { + $qb = $qb + ->where('n.sender = :sender'); + + $params['sender'] = $sender; + } + + foreach ($params as $key => $value) { + $qb = $qb->setParameter($key, $value); + } return $qb->getQuery(); } @@ -82,20 +100,20 @@ final class NotificationRepository implements ObjectRepository /** * @return int */ - public function countAllForAttendee(User $addressee): int // TODO passer à attendees avec S + public function countAllForAddresseeOrSender(?User $addressee, ?User $sender): int { - $query = $this->queryAllForAttendee($addressee, $countQuery=True); + $query = $this->queryAllForAddresseeOrSender($addressee, $sender, True); return $query->getSingleScalarResult(); } - /** * @return Notification[] */ - public function findAllForAttendee(User $addressee, $limit = null, $offset = null): array // TODO passer à attendees avec S + public function findAllForAddresseeOrSender( + ?User $addressee, ?User $sender, ?int $limit=null, ?int $offset=null): array { - $query = $this->queryAllForAttendee($addressee); + $query = $this->queryAllForAddresseeOrSender($addressee, $sender, False); if($limit) { $query = $query->setMaxResults($limit);