]>
Commit | Line | Data |
---|---|---|
152fcccd JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\UserBundle\Controller; | |
4 | ||
5 | use FOS\UserBundle\Event\UserEvent; | |
6 | use FOS\UserBundle\FOSUserEvents; | |
7 | use Symfony\Component\HttpFoundation\Request; | |
8 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
9 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
10 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
11 | use Wallabag\UserBundle\Entity\User; | |
12 | use Wallabag\CoreBundle\Entity\Config; | |
c37515f8 | 13 | use Wallabag\UserBundle\Form\SearchUserType; |
152fcccd JB |
14 | |
15 | /** | |
16 | * User controller. | |
17 | */ | |
18 | class ManageController extends Controller | |
19 | { | |
20 | /** | |
21 | * Lists all User entities. | |
22 | * | |
23 | * @Route("/", name="user_index") | |
24 | * @Method("GET") | |
25 | */ | |
26 | public function indexAction() | |
27 | { | |
28 | $em = $this->getDoctrine()->getManager(); | |
29 | ||
30 | $users = $em->getRepository('WallabagUserBundle:User')->findAll(); | |
31 | ||
32 | return $this->render('WallabagUserBundle:Manage:index.html.twig', array( | |
33 | 'users' => $users, | |
34 | )); | |
35 | } | |
36 | ||
37 | /** | |
38 | * Creates a new User entity. | |
39 | * | |
40 | * @Route("/new", name="user_new") | |
41 | * @Method({"GET", "POST"}) | |
42 | */ | |
43 | public function newAction(Request $request) | |
44 | { | |
45 | $userManager = $this->container->get('fos_user.user_manager'); | |
46 | ||
47 | $user = $userManager->createUser(); | |
48 | // enable created user by default | |
49 | $user->setEnabled(true); | |
50 | ||
51 | $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [ | |
52 | 'validation_groups' => ['Profile'], | |
53 | ]); | |
54 | $form->handleRequest($request); | |
55 | ||
56 | if ($form->isSubmitted() && $form->isValid()) { | |
57 | $userManager->updateUser($user); | |
58 | ||
59 | // dispatch a created event so the associated config will be created | |
60 | $event = new UserEvent($user, $request); | |
61 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | |
62 | ||
63 | $this->get('session')->getFlashBag()->add( | |
64 | 'notice', | |
65 | $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()]) | |
66 | ); | |
67 | ||
68 | return $this->redirectToRoute('user_edit', array('id' => $user->getId())); | |
69 | } | |
70 | ||
71 | return $this->render('WallabagUserBundle:Manage:new.html.twig', array( | |
72 | 'user' => $user, | |
73 | 'form' => $form->createView(), | |
74 | )); | |
75 | } | |
76 | ||
77 | /** | |
78 | * Displays a form to edit an existing User entity. | |
79 | * | |
80 | * @Route("/{id}/edit", name="user_edit") | |
81 | * @Method({"GET", "POST"}) | |
82 | */ | |
83 | public function editAction(Request $request, User $user) | |
84 | { | |
85 | $deleteForm = $this->createDeleteForm($user); | |
86 | $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user); | |
87 | $editForm->handleRequest($request); | |
88 | ||
89 | if ($editForm->isSubmitted() && $editForm->isValid()) { | |
90 | $em = $this->getDoctrine()->getManager(); | |
91 | $em->persist($user); | |
92 | $em->flush(); | |
93 | ||
94 | $this->get('session')->getFlashBag()->add( | |
95 | 'notice', | |
96 | $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()]) | |
97 | ); | |
98 | ||
99 | return $this->redirectToRoute('user_edit', array('id' => $user->getId())); | |
100 | } | |
101 | ||
102 | return $this->render('WallabagUserBundle:Manage:edit.html.twig', array( | |
103 | 'user' => $user, | |
104 | 'edit_form' => $editForm->createView(), | |
105 | 'delete_form' => $deleteForm->createView(), | |
106 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
107 | )); | |
108 | } | |
109 | ||
110 | /** | |
111 | * Deletes a User entity. | |
112 | * | |
113 | * @Route("/{id}", name="user_delete") | |
114 | * @Method("DELETE") | |
115 | */ | |
116 | public function deleteAction(Request $request, User $user) | |
117 | { | |
118 | $form = $this->createDeleteForm($user); | |
119 | $form->handleRequest($request); | |
120 | ||
121 | if ($form->isSubmitted() && $form->isValid()) { | |
122 | $this->get('session')->getFlashBag()->add( | |
123 | 'notice', | |
124 | $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()]) | |
125 | ); | |
126 | ||
127 | $em = $this->getDoctrine()->getManager(); | |
128 | $em->remove($user); | |
129 | $em->flush(); | |
130 | } | |
131 | ||
132 | return $this->redirectToRoute('user_index'); | |
133 | } | |
134 | ||
135 | /** | |
136 | * Creates a form to delete a User entity. | |
137 | * | |
138 | * @param User $user The User entity | |
139 | * | |
140 | * @return \Symfony\Component\Form\Form The form | |
141 | */ | |
142 | private function createDeleteForm(User $user) | |
143 | { | |
144 | return $this->createFormBuilder() | |
145 | ->setAction($this->generateUrl('user_delete', array('id' => $user->getId()))) | |
146 | ->setMethod('DELETE') | |
147 | ->getForm() | |
148 | ; | |
149 | } | |
c37515f8 TC |
150 | |
151 | /** | |
152 | * @param Request $request | |
153 | * @param int $page | |
154 | * | |
155 | * @Route("/search/{page}", name="user-search", defaults={"page" = 1}) | |
156 | * | |
157 | * Default parameter for page is hardcoded (in duplication of the defaults from the Route) | |
158 | * because this controller is also called inside the layout template without any page as argument | |
159 | * | |
160 | * @return \Symfony\Component\HttpFoundation\Response | |
161 | */ | |
162 | public function searchFormAction(Request $request, $page = 1, $currentRoute = null) | |
163 | { | |
164 | // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template) | |
165 | if (null === $currentRoute && $request->query->has('currentRoute')) { | |
166 | $currentRoute = $request->query->get('currentRoute'); | |
167 | } | |
168 | ||
169 | $form = $this->createForm(SearchUserType::class); | |
170 | ||
171 | $form->handleRequest($request); | |
172 | ||
173 | if ($form->isSubmitted() && $form->isValid()) { | |
174 | $this->get('logger')->info('searching users'); | |
175 | $em = $this->getDoctrine()->getManager(); | |
176 | ||
177 | $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : ''); | |
178 | ||
179 | $users = $em->getRepository('WallabagUserBundle:User')->getUsersForSearch($searchTerm); | |
180 | ||
181 | return $this->render('WallabagUserBundle:Manage:index.html.twig', array( | |
182 | 'users' => $users, | |
183 | )); | |
184 | } | |
185 | ||
186 | return $this->render('WallabagUserBundle:Manage:search_form.html.twig', [ | |
187 | 'form' => $form->createView(), | |
188 | 'currentRoute' => $currentRoute, | |
189 | ]); | |
190 | } | |
152fcccd | 191 | } |