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