1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Controller
;
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
;
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
;
33 class ApplicationController
extends AbstractController
{
37 * @desc Persist application and all required dependencies in database
39 * @param Request $request The request instance
40 * @param Registry $manager The doctrine registry
41 * @param EntityManagerInterface $manager The doctrine entity manager
43 * @return Response The rendered view or redirection
45 * @throws \RuntimeException When user has not at least guest role
47 public function add(Request
$request) {
49 if (!$this->checker
->isGranted('ROLE_GUEST')) {
51 throw $this->createAccessDeniedException($this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')]));
54 //Get favorites dances
55 $danceFavorites = $this->doctrine
->getRepository(Dance
::class)->findByUserId($this->security
->getUser()->getId());
58 $danceDefault = !empty($danceFavorites)?current($danceFavorites):null;
60 //Get favorites locations
61 $locationFavorites = $this->doctrine
->getRepository(Location
::class)->findByUserId($this->security
->getUser()->getId());
63 //Set location default
64 $locationDefault = !empty($locationFavorites)?current($locationFavorites):null;
67 if ($this->checker
->isGranted('ROLE_ADMIN')) {
69 $dances = $this->doctrine
->getRepository(Dance
::class)->findAll();
72 $locations = $this->doctrine
->getRepository(Location
::class)->findAll();
75 //Restrict to favorite dances
76 $dances = $danceFavorites;
81 //Restrict to favorite locations
82 $locations = $locationFavorites;
85 $locationFavorites = [];
88 //Create ApplicationType form
89 $form = $this->factory
->create('Rapsys\AirBundle\Form\ApplicationType', null, [
91 'action' => $this->generateUrl('rapsys_air_application_add'),
92 //Set the form attribute
93 #'attr' => [ 'class' => 'col' ],
95 'dance_choices' => $dances,
97 'dance_default' => $danceDefault,
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,
107 'user' => $this->checker
->isGranted('ROLE_ADMIN'),
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')
118 $this->context
['title']['page'] = $this->translator
->trans('Application add');
121 $this->context
['title']['section'] = $this->translator
->trans('Application');
123 //Refill the fields in case of invalid form
124 $form->handleRequest($request);
126 //Handle invalid form
127 if (!$form->isSubmitted() || !$form->isValid()) {
129 return $this->render('@RapsysAir/application/add.html.twig', ['form' => $form->createView()]+
$this->context
);
133 $data = $form->getData();
135 //Protect session fetching
138 $session = $this->doctrine
->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
139 //Catch no session case
140 } catch (NoResultException
$e) {
142 $session = new Session();
143 $session->setLocation($data['location']);
144 $session->setDate($data['date']);
145 $session->setSlot($data['slot']);
148 $location = $data['location']->getTitle();
151 $slot = $data['slot']->getTitle();
154 //XXX: premium is stored only for Afternoon and Evening
155 $premium = $session->isPremium();
157 //Set default length at 6h
158 //XXX: date part will be truncated on save
159 $session->setLength(new \
DateTime('06:00:00'));
162 if ($this->checker
->isGranted('ROLE_ADMIN')) {
164 if ($slot == 'Morning') {
166 $session->setBegin(new \
DateTime('09:00:00'));
169 $session->setLength(new \
DateTime('05:00:00'));
171 } elseif ($slot == 'Afternoon') {
173 $session->setBegin(new \
DateTime('15:30:00'));
176 $session->setLength(new \
DateTime('05:30:00'));
178 } elseif ($slot == 'Evening') {
180 $session->setBegin(new \
DateTime('19:30:00'));
183 $session->setLength(new \
DateTime('05:30:00'));
185 //Check if next day is premium
188 $session->setLength(new \
DateTime('06:30:00'));
193 $session->setBegin(new \
DateTime('01:00:00'));
196 $session->setLength(new \
DateTime('04:00:00'));
198 //Check if next day is premium
201 $session->setBegin(new \
DateTime('02:00:00'));
204 $session->setLength(new \
DateTime('03:00:00'));
207 //Tino-Rossi garden => 14h -> 19h | 19h -> 01/02h
208 } elseif (in_array($location, ['Tino-Rossi garden']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
210 if ($slot == 'Afternoon') {
212 $session->setBegin(new \
DateTime('14:00:00'));
215 $session->setLength(new \
DateTime('05:00:00'));
217 } elseif ($slot == 'Evening') {
219 $session->setBegin(new \
DateTime('19:00:00'));
221 //Check if next day is premium
224 $session->setLength(new \
DateTime('07:00:00'));
229 $session->setBegin(new \
DateTime('01:00:00'));
232 $session->setLength(new \
DateTime('04:00:00'));
234 //Check if next day is premium
237 $session->setBegin(new \
DateTime('02:00:00'));
240 $session->setLength(new \
DateTime('03:00:00'));
243 //Garnier opera => 21h -> 01/02h
244 } elseif ($location == 'Garnier opera' && in_array($slot, ['Evening', 'After'])) {
246 if ($slot == 'Evening') {
248 $session->setBegin(new \
DateTime('21:00:00'));
251 $session->setLength(new \
DateTime('05:00:00'));
253 //Check if next day is premium
256 $session->setLength(new \
DateTime('06:00:00'));
261 $session->setBegin(new \
DateTime('01:00:00'));
264 $session->setLength(new \
DateTime('04:00:00'));
266 //Check if next day is premium
269 $session->setBegin(new \
DateTime('02:00:00'));
272 $session->setLength(new \
DateTime('03:00:00'));
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'])) {
278 if ($slot == 'Evening') {
280 $session->setBegin(new \
DateTime('19:00:00'));
282 //Check if next day is premium
285 $session->setLength(new \
DateTime('07:00:00'));
290 $session->setBegin(new \
DateTime('01:00:00'));
293 $session->setLength(new \
DateTime('04:00:00'));
295 //Check if next day is premium
298 $session->setBegin(new \
DateTime('02:00:00'));
301 $session->setLength(new \
DateTime('03:00:00'));
304 //Drawings' garden (Villette) => 14h -> 19h
305 } elseif ($location == 'Drawings\' garden' && $slot == 'Afternoon') {
307 $session->setBegin(new \
DateTime('14:00:00'));
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') {
315 $session->setBegin(new \
DateTime('14:00:00'));
318 $session->setLength(new \
DateTime('07:00:00'));
319 //Orleans gallery => 14h -> 18h
320 } elseif ($location == 'Orleans gallery' && $slot == 'Afternoon') {
322 $session->setBegin(new \
DateTime('14:00:00'));
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') {
330 $session->setBegin(new \
DateTime('14:00:00'));
333 $session->setLength(new \
DateTime('05:00:00'));
334 //Combination not supported
335 //TODO: add Madeleine place|Bastille place|Vendome place ?
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')]));
341 return $this->render('@RapsysAir/application/add.html.twig', ['form' => $form->createView()]+
$this->context
);
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')]));
350 return $this->render('@RapsysAir/application/add.html.twig', ['form' => $form->createView()]+
$this->context
);
354 $this->manager
->persist($session);
356 //Flush to get the ids
357 #$this->manager->flush();
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')]));
363 $user = $this->security
->getUser();
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']);
370 //Protect application fetching
372 //Retrieve application
373 $application = $this->doctrine
->getRepository(Application
::class)->findOneBySessionUser($session, $user);
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);
385 //Refresh session updated field
386 $session->setUpdated(new \
DateTime('now'));
389 $this->manager
->persist($session);
391 //Queue application save
392 $this->manager
->persist($application);
394 //Flush to get the ids
395 $this->manager
->flush();
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')]));
401 //Extract and process referer
402 if ($referer = $request->headers
->get('referer')) {
403 //Create referer request instance
404 $req = Request
::create($referer);
407 $path = $req->getPathInfo();
409 //Get referer query string
410 $query = $req->getQueryString();
413 $path = str_replace($request->getScriptName(), '', $path);
415 //Try with referer path
418 $oldContext = $this->router
->getContext();
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());
424 //Retrieve route matching path
425 $route = $this->router
->match($path);
428 $this->router
->setContext($oldContext);
434 $name = $route['_route'];
436 //Remove route and controller from route defaults
437 unset($route['_route'], $route['_controller']);
439 //Check if session view route
440 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
442 $route['id'] = $session->getId();
446 $route['session'] = $session->getId();
450 return $this->redirectToRoute($name, $route);
452 } catch (MethodNotAllowedException
|ResourceNotFoundException
$e) {
453 //Unset referer to fallback to default route
458 //Redirect to cleanup the form
459 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);