]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Controller/ManageController.php
rename index to list
[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 * Lists all User entities.
24 *
25 * @Route("/list/{page}", name="user_index")
26 * @Method("GET")
27 *
28 * @param int $page
29 *
30 * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
31 */
32 public function indexAction($page = 1)
33 {
34 $em = $this->getDoctrine()->getManager();
35
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);
40
41 try {
42 $pagerFanta->setCurrentPage($page);
43 } catch (OutOfRangeCurrentPageException $e) {
44 if ($page > 1) {
45 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
46 }
47 }
48
49 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
50 'users' => $pagerFanta,
51 ));
52 }
53
54 /**
55 * Creates a new User entity.
56 *
57 * @Route("/new", name="user_new")
58 * @Method({"GET", "POST"})
59 */
60 public function newAction(Request $request)
61 {
62 $userManager = $this->container->get('fos_user.user_manager');
63
64 $user = $userManager->createUser();
65 // enable created user by default
66 $user->setEnabled(true);
67
68 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
69 'validation_groups' => ['Profile'],
70 ]);
71 $form->handleRequest($request);
72
73 if ($form->isSubmitted() && $form->isValid()) {
74 $userManager->updateUser($user);
75
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);
79
80 $this->get('session')->getFlashBag()->add(
81 'notice',
82 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
83 );
84
85 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
86 }
87
88 return $this->render('WallabagUserBundle:Manage:new.html.twig', array(
89 'user' => $user,
90 'form' => $form->createView(),
91 ));
92 }
93
94 /**
95 * Displays a form to edit an existing User entity.
96 *
97 * @Route("/{id}/edit", name="user_edit")
98 * @Method({"GET", "POST"})
99 */
100 public function editAction(Request $request, User $user)
101 {
102 $deleteForm = $this->createDeleteForm($user);
103 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
104 $editForm->handleRequest($request);
105
106 if ($editForm->isSubmitted() && $editForm->isValid()) {
107 $em = $this->getDoctrine()->getManager();
108 $em->persist($user);
109 $em->flush();
110
111 $this->get('session')->getFlashBag()->add(
112 'notice',
113 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
114 );
115
116 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
117 }
118
119 return $this->render('WallabagUserBundle:Manage:edit.html.twig', array(
120 'user' => $user,
121 'edit_form' => $editForm->createView(),
122 'delete_form' => $deleteForm->createView(),
123 'twofactor_auth' => $this->getParameter('twofactor_auth'),
124 ));
125 }
126
127 /**
128 * Deletes a User entity.
129 *
130 * @Route("/{id}", name="user_delete")
131 * @Method("DELETE")
132 */
133 public function deleteAction(Request $request, User $user)
134 {
135 $form = $this->createDeleteForm($user);
136 $form->handleRequest($request);
137
138 if ($form->isSubmitted() && $form->isValid()) {
139 $this->get('session')->getFlashBag()->add(
140 'notice',
141 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
142 );
143
144 $em = $this->getDoctrine()->getManager();
145 $em->remove($user);
146 $em->flush();
147 }
148
149 return $this->redirectToRoute('user_index');
150 }
151
152 /**
153 * Creates a form to delete a User entity.
154 *
155 * @param User $user The User entity
156 *
157 * @return \Symfony\Component\Form\Form The form
158 */
159 private function createDeleteForm(User $user)
160 {
161 return $this->createFormBuilder()
162 ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
163 ->setMethod('DELETE')
164 ->getForm()
165 ;
166 }
167
168 /**
169 * @param Request $request
170 * @param int $page
171 *
172 * @Route("/search/{page}", name="user-search", defaults={"page" = 1})
173 *
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
176 *
177 * @return \Symfony\Component\HttpFoundation\Response
178 */
179 public function searchFormAction(Request $request, $page = 1, $currentRoute = null)
180 {
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');
184 }
185
186 $form = $this->createForm(SearchUserType::class);
187
188 $form->handleRequest($request);
189
190 if ($form->isSubmitted() && $form->isValid()) {
191 $this->get('logger')->info('searching users');
192 $em = $this->getDoctrine()->getManager();
193
194 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
195
196 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
197
198 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
199 $pagerFanta = new Pagerfanta($pagerAdapter);
200 $pagerFanta->setMaxPerPage(50);
201
202 try {
203 $pagerFanta->setCurrentPage($page);
204 } catch (OutOfRangeCurrentPageException $e) {
205 if ($page > 1) {
206 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
207 }
208 }
209
210 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
211 'users' => $pagerFanta,
212 ));
213 }
214
215 return $this->render('WallabagUserBundle:Manage:search_form.html.twig', [
216 'form' => $form->createView(),
217 'currentRoute' => $currentRoute,
218 ]);
219 }
220 }