]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/UserBundle/Controller/ManageController.php
Add filter to users management page
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Controller / ManageController.php
index 92ee2b4162649fad67e3158ca91c105d13e58ff4..f0e3d4dea8a183f2d8f2c06001da2f96872afc82 100644 (file)
@@ -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,
+        ]);
+    }
 }