3 namespace Wallabag\UserBundle\Controller
;
5 use FOS\UserBundle\Event\UserEvent
;
6 use FOS\UserBundle\FOSUserEvents
;
7 use Pagerfanta\Adapter\DoctrineORMAdapter
;
8 use Pagerfanta\Exception\OutOfRangeCurrentPageException
;
9 use Pagerfanta\Pagerfanta
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
12 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method
;
13 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
14 use Wallabag\UserBundle\Entity\User
;
15 use Wallabag\UserBundle\Form\SearchUserType
;
20 class ManageController
extends Controller
23 * Lists all User entities.
25 * @Route("/index/{page}", name="user_index")
30 * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
32 public function indexAction($page = 1)
34 $em = $this->getDoctrine()->getManager();
36 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
37 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
38 $pagerFanta = new Pagerfanta($pagerAdapter);
39 $pagerFanta->setMaxPerPage(50);
42 $pagerFanta->setCurrentPage($page);
43 } catch (OutOfRangeCurrentPageException
$e) {
45 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
49 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
50 'users' => $pagerFanta,
55 * Creates a new User entity.
57 * @Route("/new", name="user_new")
58 * @Method({"GET", "POST"})
60 public function newAction(Request
$request)
62 $userManager = $this->container
->get('fos_user.user_manager');
64 $user = $userManager->createUser();
65 // enable created user by default
66 $user->setEnabled(true);
68 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
69 'validation_groups' => ['Profile'],
71 $form->handleRequest($request);
73 if ($form->isSubmitted() && $form->isValid()) {
74 $userManager->updateUser($user);
76 // dispatch a created event so the associated config will be created
77 $event = new UserEvent($user, $request);
78 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
80 $this->get('session')->getFlashBag()->add(
82 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
85 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
88 return $this->render('WallabagUserBundle:Manage:new.html.twig', array(
90 'form' => $form->createView(),
95 * Displays a form to edit an existing User entity.
97 * @Route("/{id}/edit", name="user_edit")
98 * @Method({"GET", "POST"})
100 public function editAction(Request
$request, User
$user)
102 $deleteForm = $this->createDeleteForm($user);
103 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
104 $editForm->handleRequest($request);
106 if ($editForm->isSubmitted() && $editForm->isValid()) {
107 $em = $this->getDoctrine()->getManager();
111 $this->get('session')->getFlashBag()->add(
113 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
116 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
119 return $this->render('WallabagUserBundle:Manage:edit.html.twig', array(
121 'edit_form' => $editForm->createView(),
122 'delete_form' => $deleteForm->createView(),
123 'twofactor_auth' => $this->getParameter('twofactor_auth'),
128 * Deletes a User entity.
130 * @Route("/{id}", name="user_delete")
133 public function deleteAction(Request
$request, User
$user)
135 $form = $this->createDeleteForm($user);
136 $form->handleRequest($request);
138 if ($form->isSubmitted() && $form->isValid()) {
139 $this->get('session')->getFlashBag()->add(
141 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
144 $em = $this->getDoctrine()->getManager();
149 return $this->redirectToRoute('user_index');
153 * Creates a form to delete a User entity.
155 * @param User $user The User entity
157 * @return \Symfony\Component\Form\Form The form
159 private function createDeleteForm(User
$user)
161 return $this->createFormBuilder()
162 ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
163 ->setMethod('DELETE')
169 * @param Request $request
172 * @Route("/search/{page}", name="user-search", defaults={"page" = 1})
174 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
175 * because this controller is also called inside the layout template without any page as argument
177 * @return \Symfony\Component\HttpFoundation\Response
179 public function searchFormAction(Request
$request, $page = 1, $currentRoute = null)
181 // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template)
182 if (null === $currentRoute && $request->query
->has('currentRoute')) {
183 $currentRoute = $request->query
->get('currentRoute');
186 $form = $this->createForm(SearchUserType
::class);
188 $form->handleRequest($request);
190 if ($form->isSubmitted() && $form->isValid()) {
191 $this->get('logger')->info('searching users');
192 $em = $this->getDoctrine()->getManager();
194 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
196 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
198 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
199 $pagerFanta = new Pagerfanta($pagerAdapter);
200 $pagerFanta->setMaxPerPage(50);
203 $pagerFanta->setCurrentPage($page);
204 } catch (OutOfRangeCurrentPageException
$e) {
206 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
210 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
211 'users' => $pagerFanta,
215 return $this->render('WallabagUserBundle:Manage:search_form.html.twig', [
216 'form' => $form->createView(),
217 'currentRoute' => $currentRoute,