From c37515f880bd05b86e3e848cc184018295ec1920 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Thu, 27 Apr 2017 15:58:32 +0200 Subject: [PATCH] Add filter to users management page Signed-off-by: Thomas Citharel --- .../Resources/translations/messages.da.yml | 3 + .../Resources/translations/messages.de.yml | 3 + .../Resources/translations/messages.en.yml | 3 + .../Resources/translations/messages.es.yml | 3 + .../Resources/translations/messages.fa.yml | 3 + .../Resources/translations/messages.fr.yml | 3 + .../Resources/translations/messages.it.yml | 3 + .../Resources/translations/messages.oc.yml | 3 + .../Resources/translations/messages.pl.yml | 3 + .../Resources/translations/messages.pt.yml | 3 + .../Resources/translations/messages.ro.yml | 3 + .../Resources/translations/messages.tr.yml | 3 + .../Controller/ManageController.php | 42 +++++++++++++ .../UserBundle/Form/SearchUserType.php | 29 +++++++++ .../UserBundle/Repository/UserRepository.php | 15 +++++ .../Resources/views/Manage/index.html.twig | 61 ++++++++++--------- .../views/Manage/search_form.html.twig | 15 +++++ 17 files changed, 170 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 diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml index 72493fe39..68fcec5c8 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml @@ -512,6 +512,9 @@ user: # delete: Delete # delete_confirm: Are you sure? # back_to_list: Back to list + search: + # label: Filter + # placeholder: Filter by username or email error: # page_title: An error occurred diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml index dbad8b16f..be1172316 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml @@ -512,6 +512,9 @@ user: delete: Löschen delete_confirm: Bist du sicher? back_to_list: Zurück zur Liste + search: + # label: Filter + # placeholder: Filter by username or email error: page_title: Ein Fehler ist aufgetreten diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml index 42a12b2a7..b38508933 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml @@ -512,6 +512,9 @@ user: delete: Delete delete_confirm: Are you sure? back_to_list: Back to list + search: + label: Filter + placeholder: Filter by username or email error: page_title: An error occurred diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml index 6e21614e2..ebf6745a2 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml @@ -512,6 +512,9 @@ user: delete: Eliminar delete_confirm: ¿Estás seguro? back_to_list: Volver a la lista + search: + # label: Filter + # placeholder: Filter by username or email error: page_title: Ha ocurrido un error diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml index b938c80aa..46460e130 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml @@ -512,6 +512,9 @@ user: # delete: Delete # delete_confirm: Are you sure? # back_to_list: Back to list + search: + # label: Filter + # placeholder: Filter by username or email error: # page_title: An error occurred diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml index 9abcda45c..c968f7abf 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml @@ -512,6 +512,9 @@ user: delete: "Supprimer" delete_confirm: "Voulez-vous vraiment ?" back_to_list: "Revenir à la liste" + search: + label: Filtrer + placeholder: Filtrer par nom d'utilisateur ou email error: page_title: Une erreur est survenue diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml index 58d0962a0..53b5e1b9e 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml @@ -512,6 +512,9 @@ user: # delete: Delete # delete_confirm: Are you sure? # back_to_list: Back to list + search: + # label: Filter + # placeholder: Filter by username or email error: # page_title: An error occurred diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml index 825a0efd1..c6614a63b 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml @@ -512,6 +512,9 @@ user: delete: 'Suprimir' delete_confirm: 'Sètz segur ?' back_to_list: 'Tornar a la lista' + search: + # label: Filter + # placeholder: Filter by username or email error: page_title: Una error s'es produsida diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml index b02aa4ec4..b464d0f66 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml @@ -512,6 +512,9 @@ user: delete: Usuń delete_confirm: Jesteś pewien? back_to_list: Powrót do listy + search: + # label: Filter + # placeholder: Filter by username or email error: page_title: Wystąpił błąd diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml index 8aa7e5af8..2b0b16868 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml @@ -512,6 +512,9 @@ user: delete: 'Apagar' delete_confirm: 'Tem certeza?' back_to_list: 'Voltar para a lista' + search: + # label: Filter + # placeholder: Filter by username or email error: # page_title: An error occurred diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml index ce8d8d521..210978d03 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml @@ -512,6 +512,9 @@ user: # delete: Delete # delete_confirm: Are you sure? # back_to_list: Back to list + search: + # label: Filter + # placeholder: Filter by username or email error: # page_title: An error occurred diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml index d89036086..5ff5e012d 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.tr.yml @@ -512,6 +512,9 @@ user: # delete: Delete # delete_confirm: Are you sure? # back_to_list: Back to list + search: + # label: Filter + # placeholder: Filter by username or email error: # page_title: An error occurred diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php index 92ee2b416..f0e3d4dea 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 000000000..9ce46ee17 --- /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 f913f52dd..66bbab39b 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 daba29e43..abc028521 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 000000000..f59388d09 --- /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) }} +