Controller for sent & received notifications

This commit is contained in:
Marc Ducobu 2021-07-27 10:55:46 +02:00
parent 5c2c509805
commit cb3f09d622
2 changed files with 69 additions and 15 deletions

View File

@ -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());

View File

@ -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);