]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add pagination
authorThomas Citharel <tcit@tcit.fr>
Thu, 27 Apr 2017 14:23:54 +0000 (16:23 +0200)
committerNicolas LÅ“uillet <nicolas@loeuillet.org>
Tue, 2 May 2017 13:28:39 +0000 (15:28 +0200)
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
src/Wallabag/UserBundle/Controller/ManageController.php
src/Wallabag/UserBundle/Repository/UserRepository.php
src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig

index f0e3d4dea8a183f2d8f2c06001da2f96872afc82..86fcd4317a36326fac8efa24277be6132d0e43b8 100644 (file)
@@ -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,
             ));
         }
 
index 66bbab39b819496cbe9246eb990a33d0f9528237..6adbe329fb5519f9308be0aafc39968451ad8f88 100644 (file)
@@ -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.'%');
     }
 }
index abc028521b12be4fea0303b1fe451b510491cbb8..7a76f15742996c355c6c4ece8ea9805ea3954f0a 100644 (file)
@@ -7,6 +7,9 @@
     <div class="row">
         <div class="col s12">
             <div class="card-panel">
+                {% if users.getNbPages > 1 %}
+                    {{ pagerfanta(users, 'twitter_bootstrap_translated', {'proximity': 1}) }}
+                {% endif %}
                 <div class="row">
                     <div class="col s6">
                         <p class="help">{{ 'user.description'|trans|raw }}</p>
@@ -22,7 +25,7 @@
                             <tr>
                                 <th>{{ 'user.form.username_label'|trans }}</th>
                                 <th>{{ 'user.form.email_label'|trans }}</th>
-                                <th>{{ 'user.form2017-03-10 16:51:07.last_login_label'|trans }}</th>
+                                <th>{{ 'user.form.last_login_label'|trans }}</th>
                                 <th>{{ 'user.list.actions'|trans }}</th>
                             </tr>
                         </thead>
@@ -43,6 +46,9 @@
                     <p>
                         <a href="{{ path('user_new') }}" class="waves-effect waves-light btn">{{ 'user.list.create_new_one'|trans }}</a>
                     </p>
+                    {% if users.getNbPages > 1 %}
+                        {{ pagerfanta(users, 'twitter_bootstrap_translated', {'proximity': 1}) }}
+                    {% endif %}
                 </div>
             </div>
         </div>