From 50cfd8108b8e318fd28564d2e9d30943ab12aac0 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 27 Apr 2017 16:23:54 +0200 Subject: Add pagination Signed-off-by: Thomas Citharel --- .../UserBundle/Controller/ManageController.php | 43 ++++++++++++++++++---- .../UserBundle/Repository/UserRepository.php | 6 +-- .../Resources/views/Manage/index.html.twig | 8 +++- 3 files changed, 45 insertions(+), 12 deletions(-) (limited to 'src/Wallabag') diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php index f0e3d4de..86fcd431 100644 --- a/src/Wallabag/UserBundle/Controller/ManageController.php +++ b/src/Wallabag/UserBundle/Controller/ManageController.php @@ -4,12 +4,14 @@ namespace Wallabag\UserBundle\Controller; use FOS\UserBundle\Event\UserEvent; use FOS\UserBundle\FOSUserEvents; +use Pagerfanta\Adapter\DoctrineORMAdapter; +use Pagerfanta\Exception\OutOfRangeCurrentPageException; +use Pagerfanta\Pagerfanta; use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Wallabag\UserBundle\Entity\User; -use Wallabag\CoreBundle\Entity\Config; use Wallabag\UserBundle\Form\SearchUserType; /** @@ -20,17 +22,32 @@ class ManageController extends Controller /** * Lists all User entities. * - * @Route("/", name="user_index") + * @Route("/index/{page}", name="user_index") * @Method("GET") + * + * @param int $page + * + * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response */ - public function indexAction() + public function indexAction($page = 1) { $em = $this->getDoctrine()->getManager(); - $users = $em->getRepository('WallabagUserBundle:User')->findAll(); + $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u'); + $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); + $pagerFanta = new Pagerfanta($pagerAdapter); + $pagerFanta->setMaxPerPage(50); + + try { + $pagerFanta->setCurrentPage($page); + } catch (OutOfRangeCurrentPageException $e) { + if ($page > 1) { + return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302); + } + } return $this->render('WallabagUserBundle:Manage:index.html.twig', array( - 'users' => $users, + 'users' => $pagerFanta, )); } @@ -176,10 +193,22 @@ class ManageController extends Controller $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : ''); - $users = $em->getRepository('WallabagUserBundle:User')->getUsersForSearch($searchTerm); + $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm); + + $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); + $pagerFanta = new Pagerfanta($pagerAdapter); + $pagerFanta->setMaxPerPage(50); + + try { + $pagerFanta->setCurrentPage($page); + } catch (OutOfRangeCurrentPageException $e) { + if ($page > 1) { + return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302); + } + } return $this->render('WallabagUserBundle:Manage:index.html.twig', array( - 'users' => $users, + 'users' => $pagerFanta, )); } diff --git a/src/Wallabag/UserBundle/Repository/UserRepository.php b/src/Wallabag/UserBundle/Repository/UserRepository.php index 66bbab39..6adbe329 100644 --- a/src/Wallabag/UserBundle/Repository/UserRepository.php +++ b/src/Wallabag/UserBundle/Repository/UserRepository.php @@ -60,11 +60,9 @@ class UserRepository extends EntityRepository * * @return QueryBuilder */ - public function getUsersForSearch($term) + public function getQueryBuilderForSearch($term) { return $this->createQueryBuilder('u') - ->andWhere('lower(u.username) LIKE lower(:term) OR lower(u.email) LIKE lower(:term) OR lower(u.name) LIKE lower(:term)')->setParameter('term', '%'.$term.'%') - ->getQuery() - ->getResult(); + ->andWhere('lower(u.username) LIKE lower(:term) OR lower(u.email) LIKE lower(:term) OR lower(u.name) LIKE lower(:term)')->setParameter('term', '%'.$term.'%'); } } diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig index abc02852..7a76f157 100644 --- a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig +++ b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig @@ -7,6 +7,9 @@
+ {% if users.getNbPages > 1 %} + {{ pagerfanta(users, 'twitter_bootstrap_translated', {'proximity': 1}) }} + {% endif %}

{{ 'user.description'|trans|raw }}

@@ -22,7 +25,7 @@ {{ 'user.form.username_label'|trans }} {{ 'user.form.email_label'|trans }} - {{ 'user.form2017-03-10 16:51:07.last_login_label'|trans }} + {{ 'user.form.last_login_label'|trans }} {{ 'user.list.actions'|trans }} @@ -43,6 +46,9 @@

{{ 'user.list.create_new_one'|trans }}

+ {% if users.getNbPages > 1 %} + {{ pagerfanta(users, 'twitter_bootstrap_translated', {'proximity': 1}) }} + {% endif %}
-- cgit v1.2.3