From c37515f880bd05b86e3e848cc184018295ec1920 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 27 Apr 2017 15:58:32 +0200 Subject: Add filter to users management page Signed-off-by: Thomas Citharel --- .../UserBundle/Controller/ManageController.php | 42 +++++++++++++++ src/Wallabag/UserBundle/Form/SearchUserType.php | 29 ++++++++++ .../UserBundle/Repository/UserRepository.php | 15 ++++++ .../Resources/views/Manage/index.html.twig | 61 ++++++++++++---------- .../Resources/views/Manage/search_form.html.twig | 15 ++++++ 5 files changed, 134 insertions(+), 28 deletions(-) create mode 100644 src/Wallabag/UserBundle/Form/SearchUserType.php create mode 100644 src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig (limited to 'src/Wallabag/UserBundle') diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php index 92ee2b41..f0e3d4de 100644 --- a/src/Wallabag/UserBundle/Controller/ManageController.php +++ b/src/Wallabag/UserBundle/Controller/ManageController.php @@ -10,6 +10,7 @@ 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; /** * User controller. @@ -146,4 +147,45 @@ class ManageController extends Controller ->getForm() ; } + + /** + * @param Request $request + * @param int $page + * + * @Route("/search/{page}", name="user-search", defaults={"page" = 1}) + * + * Default parameter for page is hardcoded (in duplication of the defaults from the Route) + * because this controller is also called inside the layout template without any page as argument + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function searchFormAction(Request $request, $page = 1, $currentRoute = null) + { + // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template) + if (null === $currentRoute && $request->query->has('currentRoute')) { + $currentRoute = $request->query->get('currentRoute'); + } + + $form = $this->createForm(SearchUserType::class); + + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->get('logger')->info('searching users'); + $em = $this->getDoctrine()->getManager(); + + $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : ''); + + $users = $em->getRepository('WallabagUserBundle:User')->getUsersForSearch($searchTerm); + + return $this->render('WallabagUserBundle:Manage:index.html.twig', array( + 'users' => $users, + )); + } + + return $this->render('WallabagUserBundle:Manage:search_form.html.twig', [ + 'form' => $form->createView(), + 'currentRoute' => $currentRoute, + ]); + } } diff --git a/src/Wallabag/UserBundle/Form/SearchUserType.php b/src/Wallabag/UserBundle/Form/SearchUserType.php new file mode 100644 index 00000000..9ce46ee1 --- /dev/null +++ b/src/Wallabag/UserBundle/Form/SearchUserType.php @@ -0,0 +1,29 @@ +setMethod('GET') + ->add('term', TextType::class, [ + 'required' => true, + 'label' => 'user.new.form_search.term_label', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'csrf_protection' => false, + ]); + } +} diff --git a/src/Wallabag/UserBundle/Repository/UserRepository.php b/src/Wallabag/UserBundle/Repository/UserRepository.php index f913f52d..66bbab39 100644 --- a/src/Wallabag/UserBundle/Repository/UserRepository.php +++ b/src/Wallabag/UserBundle/Repository/UserRepository.php @@ -52,4 +52,19 @@ class UserRepository extends EntityRepository ->getQuery() ->getSingleScalarResult(); } + + /** + * Retrieves users filtered with a search term. + * + * @param string $term + * + * @return QueryBuilder + */ + public function getUsersForSearch($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(); + } } diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig index daba29e4..abc02852 100644 --- a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig +++ b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig @@ -8,36 +8,41 @@
-
+

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

- - - - - - - - - - - - {% for user in users %} - - - - - - - {% endfor %} - -
{{ 'user.form.username_label'|trans }}{{ 'user.form.email_label'|trans }}{{ 'user.form.last_login_label'|trans }}{{ 'user.list.actions'|trans }}
{{ user.username }}{{ user.email }}{% if user.lastLogin %}{{ user.lastLogin|date('Y-m-d H:i:s') }}{% endif %} - {{ 'user.list.edit_action'|trans }} -
-
-

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

+
+
+ {{ render(controller("WallabagUserBundle:Manage:searchForm", {'currentRoute': app.request.attributes.get('_route')})) }} +
+
+ + + + + + + + + + + + {% for user in users %} + + + + + + + {% endfor %} + +
{{ 'user.form.username_label'|trans }}{{ 'user.form.email_label'|trans }}{{ 'user.form2017-03-10 16:51:07.last_login_label'|trans }}{{ 'user.list.actions'|trans }}
{{ user.username }}{{ user.email }}{% if user.lastLogin %}{{ user.lastLogin|date('Y-m-d H:i:s') }}{% endif %} + {{ 'user.list.edit_action'|trans }} +
+
+

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

diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig new file mode 100644 index 00000000..f59388d0 --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig @@ -0,0 +1,15 @@ +
+ {% if form_errors(form) %} + {{ form_errors(form) }} + {% endif %} + + {% if form_errors(form.term) %} + {{ form_errors(form.term) }} + {% endif %} + + search + {{ form_widget(form.term, { 'attr': {'autocomplete': 'off', 'placeholder': 'user.search.placeholder'} }) }} + + + {{ form_rest(form) }} +
-- cgit v1.2.3 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/UserBundle') 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 From a2daa32d0c6f71aa8553aed3c6ed58d7aa861baa Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 27 Apr 2017 16:40:18 +0200 Subject: Fix tests Signed-off-by: Thomas Citharel --- src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/UserBundle') diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig index f59388d0..1d4df204 100644 --- a/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig +++ b/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig @@ -1,4 +1,4 @@ -
+ {% if form_errors(form) %} {{ form_errors(form) }} {% endif %} -- cgit v1.2.3 From d01dc5a81e7fde37d98fe6b10b9329b53add5b6a Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Sat, 29 Apr 2017 19:39:51 +0200 Subject: rename index to list Signed-off-by: Thomas Citharel --- src/Wallabag/UserBundle/Controller/ManageController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/UserBundle') diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php index 86fcd431..3ea773ee 100644 --- a/src/Wallabag/UserBundle/Controller/ManageController.php +++ b/src/Wallabag/UserBundle/Controller/ManageController.php @@ -22,7 +22,7 @@ class ManageController extends Controller /** * Lists all User entities. * - * @Route("/index/{page}", name="user_index") + * @Route("/list/{page}", name="user_index") * @Method("GET") * * @param int $page -- cgit v1.2.3 From b5b6877976bc32f23e51c2fb0f3f973f0d571b10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Tue, 2 May 2017 15:27:58 +0200 Subject: Merged list and search methods --- .../UserBundle/Controller/ManageController.php | 72 +++++----------------- .../Resources/views/Manage/index.html.twig | 14 ++++- .../Resources/views/Manage/search_form.html.twig | 15 ----- 3 files changed, 29 insertions(+), 72 deletions(-) delete mode 100644 src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig (limited to 'src/Wallabag/UserBundle') diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php index 3ea773ee..1c5c86d4 100644 --- a/src/Wallabag/UserBundle/Controller/ManageController.php +++ b/src/Wallabag/UserBundle/Controller/ManageController.php @@ -19,38 +19,6 @@ use Wallabag\UserBundle\Form\SearchUserType; */ class ManageController extends Controller { - /** - * Lists all User entities. - * - * @Route("/list/{page}", name="user_index") - * @Method("GET") - * - * @param int $page - * - * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response - */ - public function indexAction($page = 1) - { - $em = $this->getDoctrine()->getManager(); - - $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' => $pagerFanta, - )); - } - /** * Creates a new User entity. * @@ -169,52 +137,44 @@ class ManageController extends Controller * @param Request $request * @param int $page * - * @Route("/search/{page}", name="user-search", defaults={"page" = 1}) + * @Route("/list/{page}", name="user_index", defaults={"page" = 1}) * * Default parameter for page is hardcoded (in duplication of the defaults from the Route) * because this controller is also called inside the layout template without any page as argument * * @return \Symfony\Component\HttpFoundation\Response */ - public function searchFormAction(Request $request, $page = 1, $currentRoute = null) + public function searchFormAction(Request $request, $page = 1) { - // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template) - if (null === $currentRoute && $request->query->has('currentRoute')) { - $currentRoute = $request->query->get('currentRoute'); - } + $em = $this->getDoctrine()->getManager(); + $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u'); $form = $this->createForm(SearchUserType::class); - $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $this->get('logger')->info('searching users'); - $em = $this->getDoctrine()->getManager(); $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : ''); $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm); + } - $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); - $pagerFanta = new Pagerfanta($pagerAdapter); - $pagerFanta->setMaxPerPage(50); + $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); - } + 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' => $pagerFanta, - )); } - return $this->render('WallabagUserBundle:Manage:search_form.html.twig', [ - 'form' => $form->createView(), - 'currentRoute' => $currentRoute, + return $this->render('WallabagUserBundle:Manage:index.html.twig', [ + 'searchForm' => $form->createView(), + 'users' => $pagerFanta, ]); } } diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig index 7a76f157..15002632 100644 --- a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig +++ b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig @@ -16,7 +16,19 @@
- {{ render(controller("WallabagUserBundle:Manage:searchForm", {'currentRoute': app.request.attributes.get('_route')})) }} + + {% if form_errors(searchForm) %} + {{ form_errors(searchForm) }} + {% endif %} + + {% if form_errors(searchForm.term) %} + {{ form_errors(searchForm.term) }} + {% endif %} + + {{ form_widget(searchForm.term, { 'attr': {'autocomplete': 'off', 'placeholder': 'user.search.placeholder'} }) }} + + {{ form_rest(searchForm) }} +
diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig deleted file mode 100644 index 1d4df204..00000000 --- a/src/Wallabag/UserBundle/Resources/views/Manage/search_form.html.twig +++ /dev/null @@ -1,15 +0,0 @@ -
- {% if form_errors(form) %} - {{ form_errors(form) }} - {% endif %} - - {% if form_errors(form.term) %} - {{ form_errors(form.term) }} - {% endif %} - - search - {{ form_widget(form.term, { 'attr': {'autocomplete': 'off', 'placeholder': 'user.search.placeholder'} }) }} - - - {{ form_rest(form) }} -
-- cgit v1.2.3