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