diff --git a/src/Bundle/ChillMainBundle/Controller/NotificationController.php b/src/Bundle/ChillMainBundle/Controller/NotificationController.php index 83b77d7b6..c76e19d9e 100644 --- a/src/Bundle/ChillMainBundle/Controller/NotificationController.php +++ b/src/Bundle/ChillMainBundle/Controller/NotificationController.php @@ -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, + 'notifications' => $notifications, + 'paginator' => $paginator, ]); } } diff --git a/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php b/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php index d52817164..5ffda75dc 100644 --- a/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php +++ b/src/Bundle/ChillMainBundle/Repository/NotificationRepository.php @@ -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() { diff --git a/src/Bundle/ChillMainBundle/config/routes/notification.yaml b/src/Bundle/ChillMainBundle/config/routes/notification.yaml index 2112f882f..e69de29bb 100644 --- a/src/Bundle/ChillMainBundle/config/routes/notification.yaml +++ b/src/Bundle/ChillMainBundle/config/routes/notification.yaml @@ -1,3 +0,0 @@ -chill_main_notification_show: - path: /show - controller: Chill\MainBundle\Controller\NotificationController::showAction diff --git a/src/Bundle/ChillMainBundle/config/services/pagination.yaml b/src/Bundle/ChillMainBundle/config/services/pagination.yaml index f6282a39f..e85352728 100644 --- a/src/Bundle/ChillMainBundle/config/services/pagination.yaml +++ b/src/Bundle/ChillMainBundle/config/services/pagination.yaml @@ -2,13 +2,15 @@ services: chill_main.paginator_factory: class: Chill\MainBundle\Pagination\PaginatorFactory public: true + autowire: true + autoconfigure: true arguments: - "@request_stack" - "@router" - "%chill_main.pagination.item_per_page%" Chill\MainBundle\Pagination\PaginatorFactory: '@chill_main.paginator_factory' - + chill_main.paginator.twig_extensions: class: Chill\MainBundle\Pagination\ChillPaginationTwig tags: