Notification with paginitation

This commit is contained in:
Marc Ducobu 2021-06-30 12:02:18 +02:00
parent 559d3dd756
commit 1de370bab1
4 changed files with 64 additions and 17 deletions

View File

@ -3,12 +3,16 @@
namespace Chill\MainBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Repository\NotificationRepository;
use Chill\MainBundle\Notification\NotificationRenderer;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Routing\Annotation\Route;
use Chill\MainBundle\Pagination\PaginatorFactory;
/**
* @Route("/{_locale}/notification")
*/
class NotificationController extends AbstractController
{
private $security;
@ -18,11 +22,23 @@ class NotificationController extends AbstractController
$this->security = $security;
}
public function showAction(NotificationRepository $notificationRepository, NotificationRenderer $notificationRenderer)
/**
* @Route("/show", name="chill_main_notification_show")
*/
public function showAction(
NotificationRepository $notificationRepository, NotificationRenderer $notificationRenderer,
PaginatorFactory $paginatorFactory)
{
$currentUser = $this->security->getUser();
$notifications = $notificationRepository->findAllForAttendee($currentUser);
$notificationsNbr = $notificationRepository->countAllForAttendee(($currentUser));
$paginator = $paginatorFactory->create($notificationsNbr);
$notifications = $notificationRepository->findAllForAttendee(
$currentUser,
$limit=$paginator->getItemsPerPage(),
$offset= $paginator->getCurrentPage()->getFirstItemNumber());
$templateData = array();
foreach ($notifications as $notification) {
@ -34,9 +50,10 @@ class NotificationController extends AbstractController
$templateData[] = $data;
}
return $this->render('ChillMainBundle:Notification:show.html.twig', [
return $this->render('@ChillMain/Notification/show.html.twig', [
'datas' => $templateData,
'notifications' => $notifications,
'paginator' => $paginator,
]);
}
}

View File

@ -24,6 +24,7 @@ use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ObjectRepository;
use Chill\MainBundle\Entity\User;
use Doctrine\ORM\Query;
final class NotificationRepository implements ObjectRepository
{
@ -60,21 +61,51 @@ final class NotificationRepository implements ObjectRepository
return $this->repository->findBy($criteria, $orderBy, $limit, $offset);
}
/**
* @return Notification[]
*/
public function findAllForAttendee(User $addressee) // TODO passer à attendees avec S
private function queryAllForAttendee(User $addressee, bool $countQuery=False): Query
{
$qb = $this->repository->createQueryBuilder('n');
$select = 'n';
if($countQuery) {
$select = 'count(n)';
}
$qb
->select('n')
->select($select)
->join('n.addressees', 'a')
->where('a = :addressee')
->setParameter('addressee', $addressee);
$query = $qb->getQuery();
return $qb->getQuery()->getResult();
return $qb->getQuery();
}
/**
* @return int
*/
public function countAllForAttendee(User $addressee): int // TODO passer à attendees avec S
{
$query = $this->queryAllForAttendee($addressee, $countQuery=True);
return $query->getSingleScalarResult();
}
/**
* @return Notification[]
*/
public function findAllForAttendee(User $addressee, $limit = null, $offset = null): array // TODO passer à attendees avec S
{
$query = $this->queryAllForAttendee($addressee);
if($limit) {
$query = $query->setMaxResults($limit);
}
if($offset) {
$query = $query->setFirstResult($offset);
}
return $query->getResult();
}
public function getClassName() {

View File

@ -1,3 +0,0 @@
chill_main_notification_show:
path: /show
controller: Chill\MainBundle\Controller\NotificationController::showAction

View File

@ -2,6 +2,8 @@ services:
chill_main.paginator_factory:
class: Chill\MainBundle\Pagination\PaginatorFactory
public: true
autowire: true
autoconfigure: true
arguments:
- "@request_stack"
- "@router"