]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Controller/ManageController.php
Merge remote-tracking branch 'origin/master' into 2.3
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Controller / ManageController.php
1 <?php
2
3 namespace Wallabag\UserBundle\Controller;
4
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;
16
17 /**
18 * User controller.
19 */
20 class ManageController extends Controller
21 {
22 /**
23 * Creates a new User entity.
24 *
25 * @Route("/new", name="user_new")
26 * @Method({"GET", "POST"})
27 */
28 public function newAction(Request $request)
29 {
30 $userManager = $this->container->get('fos_user.user_manager');
31
32 $user = $userManager->createUser();
33 // enable created user by default
34 $user->setEnabled(true);
35
36 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
37 'validation_groups' => ['Profile'],
38 ]);
39 $form->handleRequest($request);
40
41 if ($form->isSubmitted() && $form->isValid()) {
42 $userManager->updateUser($user);
43
44 // dispatch a created event so the associated config will be created
45 $event = new UserEvent($user, $request);
46 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
47
48 $this->get('session')->getFlashBag()->add(
49 'notice',
50 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
51 );
52
53 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
54 }
55
56 return $this->render('WallabagUserBundle:Manage:new.html.twig', array(
57 'user' => $user,
58 'form' => $form->createView(),
59 ));
60 }
61
62 /**
63 * Displays a form to edit an existing User entity.
64 *
65 * @Route("/{id}/edit", name="user_edit")
66 * @Method({"GET", "POST"})
67 */
68 public function editAction(Request $request, User $user)
69 {
70 $deleteForm = $this->createDeleteForm($user);
71 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
72 $editForm->handleRequest($request);
73
74 if ($editForm->isSubmitted() && $editForm->isValid()) {
75 $em = $this->getDoctrine()->getManager();
76 $em->persist($user);
77 $em->flush();
78
79 $this->get('session')->getFlashBag()->add(
80 'notice',
81 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
82 );
83
84 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
85 }
86
87 return $this->render('WallabagUserBundle:Manage:edit.html.twig', array(
88 'user' => $user,
89 'edit_form' => $editForm->createView(),
90 'delete_form' => $deleteForm->createView(),
91 'twofactor_auth' => $this->getParameter('twofactor_auth'),
92 ));
93 }
94
95 /**
96 * Deletes a User entity.
97 *
98 * @Route("/{id}", name="user_delete")
99 * @Method("DELETE")
100 */
101 public function deleteAction(Request $request, User $user)
102 {
103 $form = $this->createDeleteForm($user);
104 $form->handleRequest($request);
105
106 if ($form->isSubmitted() && $form->isValid()) {
107 $this->get('session')->getFlashBag()->add(
108 'notice',
109 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
110 );
111
112 $em = $this->getDoctrine()->getManager();
113 $em->remove($user);
114 $em->flush();
115 }
116
117 return $this->redirectToRoute('user_index');
118 }
119
120 /**
121 * Creates a form to delete a User entity.
122 *
123 * @param User $user The User entity
124 *
125 * @return \Symfony\Component\Form\Form The form
126 */
127 private function createDeleteForm(User $user)
128 {
129 return $this->createFormBuilder()
130 ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
131 ->setMethod('DELETE')
132 ->getForm()
133 ;
134 }
135
136 /**
137 * @param Request $request
138 * @param int $page
139 *
140 * @Route("/list/{page}", name="user_index", defaults={"page" = 1})
141 *
142 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
143 * because this controller is also called inside the layout template without any page as argument
144 *
145 * @return \Symfony\Component\HttpFoundation\Response
146 */
147 public function searchFormAction(Request $request, $page = 1)
148 {
149 $em = $this->getDoctrine()->getManager();
150 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
151
152 $form = $this->createForm(SearchUserType::class);
153 $form->handleRequest($request);
154
155 if ($form->isSubmitted() && $form->isValid()) {
156 $this->get('logger')->info('searching users');
157
158 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
159
160 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
161 }
162
163 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
164 $pagerFanta = new Pagerfanta($pagerAdapter);
165 $pagerFanta->setMaxPerPage(50);
166
167 try {
168 $pagerFanta->setCurrentPage($page);
169 } catch (OutOfRangeCurrentPageException $e) {
170 if ($page > 1) {
171 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
172 }
173 }
174
175 return $this->render('WallabagUserBundle:Manage:index.html.twig', [
176 'searchForm' => $form->createView(),
177 'users' => $pagerFanta,
178 ]);
179 }
180 }