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 Sensio\Bundle\FrameworkExtraBundle\Configuration\Method
;
11 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
12 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
13 use Symfony\Component\HttpFoundation\Request
;
14 use Wallabag\UserBundle\Entity\User
;
15 use Wallabag\UserBundle\Form\SearchUserType
;
20 class ManageController
extends Controller
23 * Creates a new User entity.
25 * @Route("/new", name="user_new")
26 * @Method({"GET", "POST"})
28 public function newAction(Request
$request)
30 $userManager = $this->container
->get('fos_user.user_manager');
32 $user = $userManager->createUser();
33 // enable created user by default
34 $user->setEnabled(true);
36 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user);
37 $form->handleRequest($request);
39 if ($form->isSubmitted() && $form->isValid()) {
40 $userManager->updateUser($user);
42 // dispatch a created event so the associated config will be created
43 $event = new UserEvent($user, $request);
44 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
46 $this->get('session')->getFlashBag()->add(
48 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
51 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
54 return $this->render('WallabagUserBundle:Manage:new.html.twig', [
56 'form' => $form->createView(),
61 * Displays a form to edit an existing User entity.
63 * @Route("/{id}/edit", name="user_edit")
64 * @Method({"GET", "POST"})
66 public function editAction(Request
$request, User
$user)
68 $deleteForm = $this->createDeleteForm($user);
69 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
70 $editForm->handleRequest($request);
72 if ($editForm->isSubmitted() && $editForm->isValid()) {
73 $em = $this->getDoctrine()->getManager();
77 $this->get('session')->getFlashBag()->add(
79 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
82 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
85 return $this->render('WallabagUserBundle:Manage:edit.html.twig', [
87 'edit_form' => $editForm->createView(),
88 'delete_form' => $deleteForm->createView(),
89 'twofactor_auth' => $this->getParameter('twofactor_auth'),
94 * Deletes a User entity.
96 * @Route("/{id}", name="user_delete")
99 public function deleteAction(Request
$request, User
$user)
101 $form = $this->createDeleteForm($user);
102 $form->handleRequest($request);
104 if ($form->isSubmitted() && $form->isValid()) {
105 $this->get('session')->getFlashBag()->add(
107 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
110 $em = $this->getDoctrine()->getManager();
115 return $this->redirectToRoute('user_index');
119 * @param Request $request
122 * @Route("/list/{page}", name="user_index", defaults={"page" = 1})
124 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
125 * because this controller is also called inside the layout template without any page as argument
127 * @return \Symfony\Component\HttpFoundation\Response
129 public function searchFormAction(Request
$request, $page = 1)
131 $em = $this->getDoctrine()->getManager();
132 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
134 $form = $this->createForm(SearchUserType
::class);
135 $form->handleRequest($request);
137 if ($form->isSubmitted() && $form->isValid()) {
138 $this->get('logger')->info('searching users');
140 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
142 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
145 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
146 $pagerFanta = new Pagerfanta($pagerAdapter);
147 $pagerFanta->setMaxPerPage(50);
150 $pagerFanta->setCurrentPage($page);
151 } catch (OutOfRangeCurrentPageException
$e) {
153 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
157 return $this->render('WallabagUserBundle:Manage:index.html.twig', [
158 'searchForm' => $form->createView(),
159 'users' => $pagerFanta,
164 * Creates a form to delete a User entity.
166 * @param User $user The User entity
168 * @return \Symfony\Component\Form\Form The form
170 private function createDeleteForm(User
$user)
172 return $this->createFormBuilder()
173 ->setAction($this->generateUrl('user_delete', ['id' => $user->getId()]))
174 ->setMethod('DELETE')