mirror of
				https://gitlab.com/Chill-Projet/chill-bundles.git
				synced 2025-10-31 01:08:26 +00:00 
			
		
		
		
	Notification with paginitation
This commit is contained in:
		| @@ -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, | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -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() { | ||||
|   | ||||
| @@ -1,3 +0,0 @@ | ||||
| chill_main_notification_show: | ||||
|     path: /show | ||||
|     controller: Chill\MainBundle\Controller\NotificationController::showAction | ||||
|   | ||||
| @@ -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: | ||||
|   | ||||
		Reference in New Issue
	
	Block a user