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