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;
61 //Get favorites locations
62 $locationFavorites = $this->doctrine
->getRepository(Location
::class)->findByUserId($this->security
->getUser()->getId());
64 //Set location default
65 $locationDefault = !empty($locationFavorites)?current($locationFavorites):null;
68 if ($this->checker
->isGranted('ROLE_ADMIN')) {
70 $dances = $this->doctrine
->getRepository(Dance
::class)->findAll();
73 $locations = $this->doctrine
->getRepository(Location
::class)->findAll();
76 //Restrict to favorite dances
77 $dances = $danceFavorites;
82 //Restrict to favorite locations
83 $locations = $locationFavorites;
86 $locationFavorites = [];
89 //Create ApplicationType form
90 $form = $this->factory
->create('Rapsys\AirBundle\Form\ApplicationType', null, [
92 'action' => $this->generateUrl('rapsys_air_application_add'),
93 //Set the form attribute
94 #'attr' => [ 'class' => 'col' ],
96 'dance_choices' => $dances,
98 'dance_default' => $danceDefault,
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,
108 'user' => $this->checker
->isGranted('ROLE_ADMIN'),
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')
118 //Refill the fields in case of invalid form
119 $form->handleRequest($request);
121 //Handle invalid form
122 if (!$form->isSubmitted() || !$form->isValid()) {
124 $title = $this->translator
->trans('Application add');
127 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+
$this->context
);
131 $data = $form->getData();
133 //Protect session fetching
136 $session = $this->doctrine
->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
137 //Catch no session case
138 } catch (NoResultException
$e) {
140 $session = new Session();
141 $session->setLocation($data['location']);
142 $session->setDate($data['date']);
143 $session->setSlot($data['slot']);
146 $location = $data['location']->getTitle();
149 $slot = $data['slot']->getTitle();
152 //XXX: premium is stored only for Afternoon and Evening
153 $premium = $session->isPremium();
155 //Set default length at 6h
156 //XXX: date part will be truncated on save
157 $session->setLength(new \
DateTime('06:00:00'));
160 if ($this->checker
->isGranted('ROLE_ADMIN')) {
162 if ($slot == 'Morning') {
164 $session->setBegin(new \
DateTime('09:00:00'));
167 $session->setLength(new \
DateTime('05:00:00'));
169 } elseif ($slot == 'Afternoon') {
171 $session->setBegin(new \
DateTime('15:30:00'));
174 $session->setLength(new \
DateTime('05:30:00'));
176 } elseif ($slot == 'Evening') {
178 $session->setBegin(new \
DateTime('19:30:00'));
181 $session->setLength(new \
DateTime('05:30:00'));
183 //Check if next day is premium
186 $session->setLength(new \
DateTime('06:30:00'));
191 $session->setBegin(new \
DateTime('01:00:00'));
194 $session->setLength(new \
DateTime('04:00:00'));
196 //Check if next day is premium
199 $session->setBegin(new \
DateTime('02:00:00'));
202 $session->setLength(new \
DateTime('03:00:00'));
205 //Tino-Rossi garden => 14h -> 19h | 19h -> 01/02h
206 } elseif (in_array($location, ['Tino-Rossi garden']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) {
208 if ($slot == 'Afternoon') {
210 $session->setBegin(new \
DateTime('14:00:00'));
213 $session->setLength(new \
DateTime('05:00:00'));
215 } elseif ($slot == 'Evening') {
217 $session->setBegin(new \
DateTime('19:00:00'));
219 //Check if next day is premium
222 $session->setLength(new \
DateTime('07:00:00'));
227 $session->setBegin(new \
DateTime('01:00:00'));
230 $session->setLength(new \
DateTime('04:00:00'));
232 //Check if next day is premium
235 $session->setBegin(new \
DateTime('02:00:00'));
238 $session->setLength(new \
DateTime('03:00:00'));
241 //Garnier opera => 21h -> 01/02h
242 } elseif ($location == 'Garnier opera' && in_array($slot, ['Evening', 'After'])) {
244 if ($slot == 'Evening') {
246 $session->setBegin(new \
DateTime('21:00:00'));
249 $session->setLength(new \
DateTime('05:00:00'));
251 //Check if next day is premium
254 $session->setLength(new \
DateTime('06:00:00'));
259 $session->setBegin(new \
DateTime('01:00:00'));
262 $session->setLength(new \
DateTime('04:00:00'));
264 //Check if next day is premium
267 $session->setBegin(new \
DateTime('02:00:00'));
270 $session->setLength(new \
DateTime('03:00:00'));
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'])) {
276 if ($slot == 'Evening') {
278 $session->setBegin(new \
DateTime('19:00:00'));
280 //Check if next day is premium
283 $session->setLength(new \
DateTime('07:00:00'));
288 $session->setBegin(new \
DateTime('01:00:00'));
291 $session->setLength(new \
DateTime('04:00:00'));
293 //Check if next day is premium
296 $session->setBegin(new \
DateTime('02:00:00'));
299 $session->setLength(new \
DateTime('03:00:00'));
302 //Drawings' garden (Villette) => 14h -> 19h
303 } elseif ($location == 'Drawings\' garden' && $slot == 'Afternoon') {
305 $session->setBegin(new \
DateTime('14:00:00'));
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') {
313 $session->setBegin(new \
DateTime('14:00:00'));
316 $session->setLength(new \
DateTime('07:00:00'));
317 //Orleans gallery => 14h -> 18h
318 } elseif ($location == 'Orleans gallery' && $slot == 'Afternoon') {
320 $session->setBegin(new \
DateTime('14:00:00'));
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') {
328 $session->setBegin(new \
DateTime('14:00:00'));
331 $session->setLength(new \
DateTime('05:00:00'));
332 //Combination not supported
333 //TODO: add Madeleine place|Bastille place|Vendome place ?
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')]));
339 $title = $this->translator
->trans('Application add');
342 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+
$this->context
);
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')]));
351 $title = $this->translator
->trans('Application add');
354 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+
$this->context
);
358 $this->manager
->persist($session);
360 //Flush to get the ids
361 #$this->manager->flush();
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')]));
367 $user = $this->security
->getUser();
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']);
374 //Protect application fetching
376 //Retrieve application
377 $application = $this->doctrine
->getRepository(Application
::class)->findOneBySessionUser($session, $user);
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);
389 //Refresh session updated field
390 $session->setUpdated(new \
DateTime('now'));
393 $this->manager
->persist($session);
395 //Queue application save
396 $this->manager
->persist($application);
398 //Flush to get the ids
399 $this->manager
->flush();
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')]));
405 //Extract and process referer
406 if ($referer = $request->headers
->get('referer')) {
407 //Create referer request instance
408 $req = Request
::create($referer);
411 $path = $req->getPathInfo();
413 //Get referer query string
414 $query = $req->getQueryString();
417 $path = str_replace($request->getScriptName(), '', $path);
419 //Try with referer path
422 $oldContext = $this->router
->getContext();
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());
428 //Retrieve route matching path
429 $route = $this->router
->match($path);
432 $this->router
->setContext($oldContext);
438 $name = $route['_route'];
440 //Remove route and controller from route defaults
441 unset($route['_route'], $route['_controller']);
443 //Check if session view route
444 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) {
446 $route['id'] = $session->getId();
450 $route['session'] = $session->getId();
454 return $this->redirectToRoute($name, $route);
456 } catch (MethodNotAllowedException
|ResourceNotFoundException
$e) {
457 //Unset referer to fallback to default route
462 //Redirect to cleanup the form
463 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);