]> Raphaël G. Git Repositories - airbundle/blob - Controller/ApplicationController.php
Fix default controller renamed as user in user bundle
[airbundle] / Controller / ApplicationController.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Controller;
13
14 use Doctrine\ORM\NoResultException;
15 use Doctrine\ORM\ORMInvalidArgumentException;
16 use Symfony\Component\Form\FormError;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
19 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
20 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
21 use Symfony\Component\Routing\RequestContext;
22
23 use Rapsys\AirBundle\Entity\Application;
24 use Rapsys\AirBundle\Entity\Dance;
25 use Rapsys\AirBundle\Entity\Location;
26 use Rapsys\AirBundle\Entity\Session;
27 use Rapsys\AirBundle\Entity\Slot;
28 use Rapsys\AirBundle\Entity\User;
29
30 /**
31 * {@inheritdoc}
32 */
33 class ApplicationController extends AbstractController {
34 /**
35 * Add application
36 *
37 * @desc Persist application and all required dependencies in database
38 *
39 * @param Request $request The request instance
40 * @param Registry $manager The doctrine registry
41 * @param EntityManagerInterface $manager The doctrine entity manager
42 *
43 * @return Response The rendered view or redirection
44 *
45 * @throws \RuntimeException When user has not at least guest role
46 */
47 public function add(Request $request) {
48 //Without guest role
49 if (!$this->checker->isGranted('ROLE_GUEST')) {
50 //Throw 403
51 throw $this->createAccessDeniedException($this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
52 }
53
54 //Get favorites dances
55 $danceFavorites = $this->doctrine->getRepository(Dance::class)->findByUserId($this->security->getUser()->getId());
56
57 //Set dance default
58 $danceDefault = !empty($danceFavorites)?current($danceFavorites):null;
59
60
61 //Get favorites locations
62 $locationFavorites = $this->doctrine->getRepository(Location::class)->findByUserId($this->security->getUser()->getId());
63
64 //Set location default
65 $locationDefault = !empty($locationFavorites)?current($locationFavorites):null;
66
67 //With admin
68 if ($this->checker->isGranted('ROLE_ADMIN')) {
69 //Get dances
70 $dances = $this->doctrine->getRepository(Dance::class)->findAll();
71
72 //Get locations
73 $locations = $this->doctrine->getRepository(Location::class)->findAll();
74 //Without admin
75 } else {
76 //Restrict to favorite dances
77 $dances = $danceFavorites;
78
79 //Reset favorites
80 $danceFavorites = [];
81
82 //Restrict to favorite locations
83 $locations = $locationFavorites;
84
85 //Reset favorites
86 $locationFavorites = [];
87 }
88
89 //Create ApplicationType form
90 $form = $this->factory->create('Rapsys\AirBundle\Form\ApplicationType', null, [
91 //Set the action
92 'action' => $this->generateUrl('rapsys_air_application_add'),
93 //Set the form attribute
94 #'attr' => [ 'class' => 'col' ],
95 //Set dance choices
96 'dance_choices' => $dances,
97 //Set dance default
98 'dance_default' => $danceDefault,
99 //Set dance favorites
100 'dance_favorites' => $danceFavorites,
101 //Set location choices
102 'location_choices' => $locations,
103 //Set location default
104 'location_default' => $locationDefault,
105 //Set location favorites
106 'location_favorites' => $locationFavorites,
107 //With user
108 'user' => $this->checker->isGranted('ROLE_ADMIN'),
109 //Set user choices
110 'user_choices' => $this->doctrine->getRepository(User::class)->findChoicesAsArray(),
111 //Set default user to current
112 'user_default' => $this->security->getUser()->getId(),
113 //Set default slot to evening
114 //XXX: default to Evening (3)
115 'slot_default' => $this->doctrine->getRepository(Slot::class)->findOneByTitle('Evening')
116 ]);
117
118 //Refill the fields in case of invalid form
119 $form->handleRequest($request);
120
121 //Handle invalid form
122 if (!$form->isSubmitted() || !$form->isValid()) {
123 //Set title
124 $title = $this->translator->trans('Application add');
125
126 //Render the view
127 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+$this->context);
128 }
129
130 //Get data
131 $data = $form->getData();
132
133 //Protect session fetching
134 try {
135 //Fetch session
136 $session = $this->doctrine->getRepository(Session::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
137 //Catch no session case
138 } catch (NoResultException $e) {
139 //Create the session
140 $session = new Session();
141 $session->setLocation($data['location']);
142 $session->setDate($data['date']);
143 $session->setSlot($data['slot']);
144
145 //Get location
146 $location = $data['location']->getTitle();
147
148 //Get slot
149 $slot = $data['slot']->getTitle();
150
151 //Get premium
152 //XXX: premium is stored only for Afternoon and Evening
153 $premium = $session->isPremium();
154
155 //Set default length at 6h
156 //XXX: date part will be truncated on save
157 $session->setLength(new \DateTime('06:00:00'));
158
159 //Check if admin
160 if ($this->checker->isGranted('ROLE_ADMIN')) {
161 //Check if morning
162 if ($slot == 'Morning') {
163 //Set begin at 9h
164 $session->setBegin(new \DateTime('09:00:00'));
165
166 //Set length at 5h
167 $session->setLength(new \DateTime('05:00:00'));
168 //Check if afternoon
169 } elseif ($slot == 'Afternoon') {
170 //Set begin at 18h
171 $session->setBegin(new \DateTime('15:30:00'));
172
173 //Set length at 5h
174 $session->setLength(new \DateTime('05:30:00'));
175 //Check if evening
176 } elseif ($slot == 'Evening') {
177 //Set begin at 19h00
178 $session->setBegin(new \DateTime('19:30:00'));
179
180 //Set length at 5h
181 $session->setLength(new \DateTime('05:30:00'));
182
183 //Check if next day is premium
184 if ($premium) {
185 //Set length at 7h
186 $session->setLength(new \DateTime('06:30:00'));
187 }
188 //Check if after
189 } else {
190 //Set begin at 1h
191 $session->setBegin(new \DateTime('01:00:00'));
192
193 //Set length at 4h
194 $session->setLength(new \DateTime('04:00:00'));
195
196 //Check if next day is premium
197 if ($premium) {
198 //Set begin at 2h
199 $session->setBegin(new \DateTime('02:00:00'));
200
201 //Set length at 3h
202 $session->setLength(new \DateTime('03:00:00'));
203 }
204 }
205 //Tino-Rossi garden => 14h -> 19h | 19h -> 01/02h
206 } elseif (in_array($location, ['Tino-Rossi garden']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
207 //Check if afternoon
208 if ($slot == 'Afternoon') {
209 //Set begin at 14h
210 $session->setBegin(new \DateTime('14:00:00'));
211
212 //Set length at 5h
213 $session->setLength(new \DateTime('05:00:00'));
214 //Check if evening
215 } elseif ($slot == 'Evening') {
216 //Set begin at 19h
217 $session->setBegin(new \DateTime('19:00:00'));
218
219 //Check if next day is premium
220 if ($premium) {
221 //Set length at 7h
222 $session->setLength(new \DateTime('07:00:00'));
223 }
224 //Check if after
225 } else {
226 //Set begin at 1h
227 $session->setBegin(new \DateTime('01:00:00'));
228
229 //Set length at 4h
230 $session->setLength(new \DateTime('04:00:00'));
231
232 //Check if next day is premium
233 if ($premium) {
234 //Set begin at 2h
235 $session->setBegin(new \DateTime('02:00:00'));
236
237 //Set length at 3h
238 $session->setLength(new \DateTime('03:00:00'));
239 }
240 }
241 //Garnier opera => 21h -> 01/02h
242 } elseif ($location == 'Garnier opera' && in_array($slot, ['Evening', 'After'])) {
243 //Check if evening
244 if ($slot == 'Evening') {
245 //Set begin at 21h
246 $session->setBegin(new \DateTime('21:00:00'));
247
248 //Set length at 5h
249 $session->setLength(new \DateTime('05:00:00'));
250
251 //Check if next day is premium
252 if ($premium) {
253 //Set length at 6h
254 $session->setLength(new \DateTime('06:00:00'));
255 }
256 //Check if after
257 } else {
258 //Set begin at 1h
259 $session->setBegin(new \DateTime('01:00:00'));
260
261 //Set length at 4h
262 $session->setLength(new \DateTime('04:00:00'));
263
264 //Check if next day is premium
265 if ($premium) {
266 //Set begin at 2h
267 $session->setBegin(new \DateTime('02:00:00'));
268
269 //Set length at 3h
270 $session->setLength(new \DateTime('03:00:00'));
271 }
272 }
273 //Trocadero esplanade|Tokyo palace|Swan island|Saint-Honore market|Orsay museum => 19h -> 01/02h
274 } elseif (in_array($location, ['Trocadero esplanade', 'Tokyo palace', 'Swan island', 'Saint-Honore market', 'Orsay museum']) && in_array($slot, ['Evening', 'After'])) {
275 //Check if evening
276 if ($slot == 'Evening') {
277 //Set begin at 19h
278 $session->setBegin(new \DateTime('19:00:00'));
279
280 //Check if next day is premium
281 if ($premium) {
282 //Set length at 7h
283 $session->setLength(new \DateTime('07:00:00'));
284 }
285 //Check if after
286 } else {
287 //Set begin at 1h
288 $session->setBegin(new \DateTime('01:00:00'));
289
290 //Set length at 4h
291 $session->setLength(new \DateTime('04:00:00'));
292
293 //Check if next day is premium
294 if ($premium) {
295 //Set begin at 2h
296 $session->setBegin(new \DateTime('02:00:00'));
297
298 //Set length at 3h
299 $session->setLength(new \DateTime('03:00:00'));
300 }
301 }
302 //Drawings' garden (Villette) => 14h -> 19h
303 } elseif ($location == 'Drawings\' garden' && $slot == 'Afternoon') {
304 //Set begin at 14h
305 $session->setBegin(new \DateTime('14:00:00'));
306
307 //Set length at 5h
308 $session->setLength(new \DateTime('05:00:00'));
309 //Colette place => 14h -> 21h
310 //TODO: add check here that it's a millegaux account ?
311 } elseif ($location == 'Colette place' && $slot == 'Afternoon') {
312 //Set begin at 14h
313 $session->setBegin(new \DateTime('14:00:00'));
314
315 //Set length at 7h
316 $session->setLength(new \DateTime('07:00:00'));
317 //Orleans gallery => 14h -> 18h
318 } elseif ($location == 'Orleans gallery' && $slot == 'Afternoon') {
319 //Set begin at 14h
320 $session->setBegin(new \DateTime('14:00:00'));
321
322 //Set length at 4h
323 $session->setLength(new \DateTime('04:00:00'));
324 //Monde garden => 14h -> 19h
325 //TODO: add check here that it's a raphael account ?
326 } elseif ($location == 'Monde garden' && $slot == 'Afternoon') {
327 //Set begin at 14h
328 $session->setBegin(new \DateTime('14:00:00'));
329
330 //Set length at 4h
331 $session->setLength(new \DateTime('05:00:00'));
332 //Combination not supported
333 //TODO: add Madeleine place|Bastille place|Vendome place ?
334 } else {
335 //Add error in flash message
336 $this->addFlash('error', $this->translator->trans('Session on %date% %location% %slot% not yet supported', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
337
338 //Set title
339 $title = $this->translator->trans('Application add');
340
341 //Render the view
342 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+$this->context);
343 }
344
345 //Check if admin
346 if (!$this->checker->isGranted('ROLE_ADMIN') && $session->getStart() < new \DateTime('00:00:00')) {
347 //Add error in flash message
348 $this->addFlash('error', $this->translator->trans('Session in the past on %date% %location% %slot% not yet supported', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
349
350 //Set title
351 $title = $this->translator->trans('Application add');
352
353 //Render the view
354 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+$this->context);
355 }
356
357 //Queue session save
358 $this->manager->persist($session);
359
360 //Flush to get the ids
361 #$this->manager->flush();
362
363 $this->addFlash('notice', $this->translator->trans('Session on %date% %location% %slot% created', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
364 }
365
366 //Set user
367 $user = $this->security->getUser();
368
369 //Replace with requested user for admin
370 if ($this->checker->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
371 $user = $this->doctrine->getRepository(User::class)->findOneById($data['user']);
372 }
373
374 //Protect application fetching
375 try {
376 //Retrieve application
377 $application = $this->doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
378
379 //Add warning in flash message
380 $this->addFlash('warning', $this->translator->trans('Application on %date% %location% %slot% already exists', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
381 //Catch no application and session without identifier (not persisted&flushed) cases
382 } catch (NoResultException|ORMInvalidArgumentException $e) {
383 //Create the application
384 $application = new Application();
385 $application->setDance($data['dance']);
386 $application->setSession($session);
387 $application->setUser($user);
388
389 //Refresh session updated field
390 $session->setUpdated(new \DateTime('now'));
391
392 //Queue session save
393 $this->manager->persist($session);
394
395 //Queue application save
396 $this->manager->persist($application);
397
398 //Flush to get the ids
399 $this->manager->flush();
400
401 //Add notice in flash message
402 $this->addFlash('notice', $this->translator->trans('Application on %date% %location% %slot% created', ['%location%' => $this->translator->trans('at '.$data['location']), '%slot%' => $this->translator->trans('the '.strtolower(strval($data['slot']))), '%date%' => $data['date']->format('Y-m-d')]));
403 }
404
405 //Extract and process referer
406 if ($referer = $request->headers->get('referer')) {
407 //Create referer request instance
408 $req = Request::create($referer);
409
410 //Get referer path
411 $path = $req->getPathInfo();
412
413 //Get referer query string
414 $query = $req->getQueryString();
415
416 //Remove script name
417 $path = str_replace($request->getScriptName(), '', $path);
418
419 //Try with referer path
420 try {
421 //Save old context
422 $oldContext = $this->router->getContext();
423
424 //Force clean context
425 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
426 $this->router->setContext(new RequestContext());
427
428 //Retrieve route matching path
429 $route = $this->router->match($path);
430
431 //Reset context
432 $this->router->setContext($oldContext);
433
434 //Clear old context
435 unset($oldContext);
436
437 //Extract name
438 $name = $route['_route'];
439
440 //Remove route and controller from route defaults
441 unset($route['_route'], $route['_controller']);
442
443 //Check if session view route
444 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
445 //Replace id
446 $route['id'] = $session->getId();
447 //Other routes
448 } else {
449 //Set session
450 $route['session'] = $session->getId();
451 }
452
453 //Generate url
454 return $this->redirectToRoute($name, $route);
455 //No route matched
456 } catch (MethodNotAllowedException|ResourceNotFoundException $e) {
457 //Unset referer to fallback to default route
458 unset($referer);
459 }
460 }
461
462 //Redirect to cleanup the form
463 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);
464 }
465 }