]> Raphaël G. Git Repositories - airbundle/blob - Controller/UserController.php
Moved to Handler
[airbundle] / Controller / UserController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\Routing\RequestContext;
8 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
9
10 use Rapsys\AirBundle\Entity\Civility;
11 use Rapsys\AirBundle\Entity\Location;
12 use Rapsys\AirBundle\Entity\Session;
13 use Rapsys\AirBundle\Entity\Slot;
14 use Rapsys\AirBundle\Entity\Snippet;
15 use Rapsys\AirBundle\Entity\User;
16
17 class UserController extends DefaultController {
18 /**
19 * List all users
20 *
21 * @desc Display all user with a group listed as users
22 *
23 * @param Request $request The request instance
24 *
25 * @return Response The rendered view
26 */
27 public function index(Request $request): Response {
28 //Fetch doctrine
29 $doctrine = $this->getDoctrine();
30
31 //Set section
32 $section = $this->translator->trans('Libre Air users');
33
34 //Set description
35 $this->context['description'] = $this->translator->trans('Libre Air user list');
36
37 //Set keywords
38 $this->context['keywords'] = [
39 $this->translator->trans('users'),
40 $this->translator->trans('user list'),
41 $this->translator->trans('listing'),
42 $this->translator->trans('Libre Air')
43 ];
44
45 //Set title
46 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
47
48 //Fetch users
49 $users = $doctrine->getRepository(User::class)->findUserGroupedByTranslatedGroup($this->translator);
50
51 //Compute period
52 $period = new \DatePeriod(
53 //Start from first monday of week
54 new \DateTime('Monday this week'),
55 //Iterate on each day
56 new \DateInterval('P1D'),
57 //End with next sunday and 4 weeks
58 new \DateTime(
59 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
60 )
61 );
62
63 //Without admin role
64 if (!$this->isGranted('ROLE_ADMIN')) {
65 //Remove users
66 unset($users[$this->translator->trans('User')]);
67 }
68
69 //Fetch locations
70 //XXX: we want to display all active locations anyway
71 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
72
73 //Render the view
74 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'users' => $users, 'locations' => $locations]+$this->context);
75 }
76
77 /**
78 * List all sessions for the user
79 *
80 * @desc Display all sessions for the user with an application or login form
81 *
82 * @param Request $request The request instance
83 * @param int $id The user id
84 *
85 * @return Response The rendered view
86 */
87 public function view(Request $request, $id): Response {
88 //Fetch doctrine
89 $doctrine = $this->getDoctrine();
90
91 //Fetch user
92 if (empty($user = $doctrine->getRepository(User::class)->findOneById($id))) {
93 throw $this->createNotFoundException($this->translator->trans('Unable to find user: %id%', ['%id%' => $id]));
94 }
95
96 //Get user token
97 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles());
98
99 //Check if guest
100 $isGuest = $this->get('rapsys_user.access_decision_manager')->decide($token, ['ROLE_GUEST']);
101
102 //Prevent access when not admin, user is not guest and not currently logged user
103 if (!$this->isGranted('ROLE_ADMIN') && empty($isGuest) && $user != $this->getUser()) {
104 throw $this->createAccessDeniedException($this->translator->trans('Unable to access user: %id%', ['%id%' => $id]));
105 }
106
107 //Set section
108 $section = $user->getPseudonym();
109
110 //Set title
111 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
112
113 //Set description
114 $this->context['description'] = $this->translator->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]);
115
116 //Set keywords
117 $this->context['keywords'] = [
118 $user->getPseudonym(),
119 $this->translator->trans('outdoor'),
120 $this->translator->trans('Argentine Tango'),
121 $this->translator->trans('calendar')
122 ];
123
124 //Compute period
125 $period = new \DatePeriod(
126 //Start from first monday of week
127 new \DateTime('Monday this week'),
128 //Iterate on each day
129 new \DateInterval('P1D'),
130 //End with next sunday and 4 weeks
131 new \DateTime(
132 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
133 )
134 );
135
136 //Fetch calendar
137 //TODO: highlight with current session route parameter
138 $calendar = $doctrine->getRepository(Session::class)->fetchUserCalendarByDatePeriod($this->translator, $period, $isGuest?$id:null, $request->get('session'));
139
140 //Fetch locations
141 //XXX: we want to display all active locations anyway
142 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $id);
143
144 //Create user form for admin or current user
145 if ($this->isGranted('ROLE_ADMIN') || $user == $this->getUser()) {
146 //Create SnippetType form
147 $userForm = $this->createForm('Rapsys\AirBundle\Form\RegisterType', $user, [
148 //Set action
149 'action' => $this->generateUrl('rapsys_air_user_view', ['id' => $id]),
150 //Set the form attribute
151 'attr' => [ 'class' => 'col' ],
152 //Set civility class
153 'class_civility' => Civility::class,
154 //Disable mail
155 'mail' => $this->isGranted('ROLE_ADMIN'),
156 //Disable password
157 'password' => false
158 ]);
159
160 //Init user to context
161 $this->context['forms']['user'] = $userForm->createView();
162
163 //Check if submitted
164 if ($request->isMethod('POST')) {
165 //Refill the fields in case the form is not valid.
166 $userForm->handleRequest($request);
167
168 //Handle invalid form
169 if (!$userForm->isSubmitted() || !$userForm->isValid()) {
170 //Render the view
171 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context);
172 }
173
174 //Get data
175 $data = $userForm->getData();
176
177 //Get manager
178 $manager = $doctrine->getManager();
179
180 //Queue snippet save
181 $manager->persist($data);
182
183 //Flush to get the ids
184 $manager->flush();
185
186 //Add notice
187 $this->addFlash('notice', $this->translator->trans('User %id% updated', ['%id%' => $id]));
188
189 //Extract and process referer
190 if ($referer = $request->headers->get('referer')) {
191 //Create referer request instance
192 $req = Request::create($referer);
193
194 //Get referer path
195 $path = $req->getPathInfo();
196
197 //Get referer query string
198 $query = $req->getQueryString();
199
200 //Remove script name
201 $path = str_replace($request->getScriptName(), '', $path);
202
203 //Try with referer path
204 try {
205 //Save old context
206 $oldContext = $this->router->getContext();
207
208 //Force clean context
209 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
210 $this->router->setContext(new RequestContext());
211
212 //Retrieve route matching path
213 $route = $this->router->match($path);
214
215 //Reset context
216 $this->router->setContext($oldContext);
217
218 //Clear old context
219 unset($oldContext);
220
221 //Extract name
222 $name = $route['_route'];
223
224 //Remove route and controller from route defaults
225 unset($route['_route'], $route['_controller']);
226
227 //Check if user view route
228 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) {
229 //Replace id
230 $route['id'] = $data->getId();
231 //Other routes
232 } else {
233 //Set user
234 $route['user'] = $data->getId();
235 }
236
237 //Generate url
238 return $this->redirectToRoute($name, $route);
239 //No route matched
240 } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
241 //Unset referer to fallback to default route
242 unset($referer);
243 }
244 }
245
246 //Redirect to cleanup the form
247 return $this->redirectToRoute('rapsys_air', ['user' => $data->getId()]);
248 }
249 }
250
251 //Create snippet forms for role_guest
252 if ($this->isGranted('ROLE_ADMIN') || ($this->isGranted('ROLE_GUEST') && $user == $this->getUser())) {
253 //Fetch all user snippet
254 $snippets = $doctrine->getRepository(Snippet::class)->findByLocaleUserId($request->getLocale(), $id);
255
256 //Rekey by location id
257 $snippets = array_reduce($snippets, function($carry, $item){$carry[$item->getLocation()->getId()] = $item; return $carry;}, []);
258
259 //Init snippets to context
260 $this->context['forms']['snippets'] = [];
261
262 //Iterate on locations
263 foreach($locations as $locationId => $location) {
264 //Init snippet
265 $snippet = new Snippet();
266
267 //Set default locale
268 $snippet->setLocale($request->getLocale());
269
270 //Set default user
271 $snippet->setUser($user);
272
273 //Set default location
274 $snippet->setLocation($doctrine->getRepository(Location::class)->findOneById($locationId));
275
276 //Get snippet
277 if (!empty($snippets[$locationId])) {
278 $snippet = $snippets[$locationId];
279 }
280
281 //Create SnippetType form
282 #$form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', $snippet, [
283 $form = $this->container->get('form.factory')->createNamed('snipped_'.$request->getLocale().'_'.$locationId, 'Rapsys\AirBundle\Form\SnippetType', $snippet, [
284 //Set the action
285 //TODO: voir si on peut pas faire sauter ça ici
286 'action' =>
287 !empty($snippet->getId()) ?
288 $this->generateUrl('rapsys_air_snippet_edit', ['id' => $snippet->getId()]) :
289 $this->generateUrl('rapsys_air_snippet_add', ['location' => $locationId]),
290 #'action' => $this->generateUrl('rapsys_air_snippet_add'),
291 //Set the form attribute
292 'attr' => [],
293 //Set csrf_token_id
294 //TODO: would maybe need a signature field
295 //'csrf_token_id' => $request->getLocale().'_'.$id.'_'.$locationId
296 ]);
297 #return $this->container->get('form.factory')->create($type, $data, $options);
298 #public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []);
299
300 //Add form to context
301 $this->context['forms']['snippets'][$locationId] = $form->createView();
302 }
303 }
304
305 //Render the view
306 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context);
307 }
308 }