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) { 
  48                 //Prevent non-guest to access here 
  49                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  51                 //Get favorites dances 
  52                 $danceFavorites = $this->doctrine
->getRepository(Dance
::class)->findByUserId($this->getUser()->getId()); 
  55                 $danceDefault = !empty($danceFavorites)?current($danceFavorites):null; 
  58                 //Get favorites locations 
  59                 $locationFavorites = $this->doctrine
->getRepository(Location
::class)->findByUserId($this->getUser()->getId()); 
  61                 //Set location default 
  62                 $locationDefault = !empty($locationFavorites)?current($locationFavorites):null; 
  65                 if ($this->isGranted('ROLE_ADMIN')) { 
  67                         $dances = $this->doctrine
->getRepository(Dance
::class)->findAll(); 
  70                         $locations = $this->doctrine
->getRepository(Location
::class)->findAll(); 
  73                         //Restrict to favorite dances 
  74                         $dances = $danceFavorites; 
  79                         //Restrict to favorite locations 
  80                         $locations = $locationFavorites; 
  83                         $locationFavorites = []; 
  86                 //Create ApplicationType form 
  87                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  89                         'action' => $this->generateUrl('rapsys_air_application_add'), 
  90                         //Set the form attribute 
  91                         #'attr' => [ 'class' => 'col' ], 
  93                         'dance_choices' => $dances, 
  95                         'dance_default' => $danceDefault, 
  97                         'dance_favorites' => $danceFavorites, 
  98                         //Set location choices 
  99                         'location_choices' => $locations, 
 100                         //Set location default 
 101                         'location_default' => $locationDefault, 
 102                         //Set location favorites 
 103                         'location_favorites' => $locationFavorites, 
 105                         'user' => $this->isGranted('ROLE_ADMIN'), 
 107                         'user_choices' => $this->doctrine
->getRepository(User
::class)->findChoicesAsArray(), 
 108                         //Set default user to current 
 109                         'user_default' => $this->getUser()->getId(), 
 110                         //Set default slot to evening 
 111                         //XXX: default to Evening (3) 
 112                         'slot_default' => $this->doctrine
->getRepository(Slot
::class)->findOneByTitle('Evening') 
 115                 //Refill the fields in case of invalid form 
 116                 $form->handleRequest($request); 
 118                 //Handle invalid form 
 119                 if (!$form->isSubmitted() || !$form->isValid()) { 
 121                         $title = $this->translator
->trans('Application add'); 
 124                         return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+
$this->context
); 
 128                 $data = $form->getData(); 
 130                 //Protect session fetching 
 133                         $session = $this->doctrine
->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
 134                 //Catch no session case 
 135                 } catch (NoResultException 
$e) { 
 137                         $session = new Session(); 
 138                         $session->setLocation($data['location']); 
 139                         $session->setDate($data['date']); 
 140                         $session->setSlot($data['slot']); 
 143                         $location = $data['location']->getTitle(); 
 146                         $slot = $data['slot']->getTitle(); 
 149                         //XXX: premium is stored only for Afternoon and Evening 
 150                         $premium = $session->isPremium(); 
 152                         //Set default length at 6h 
 153                         //XXX: date part will be truncated on save 
 154                         $session->setLength(new \
DateTime('06:00:00')); 
 157                         if ($this->isGranted('ROLE_ADMIN')) { 
 159                                 if ($slot == 'Morning') { 
 161                                         $session->setBegin(new \
DateTime('09:00:00')); 
 164                                         $session->setLength(new \
DateTime('05:00:00')); 
 166                                 } elseif ($slot == 'Afternoon') { 
 168                                         $session->setBegin(new \
DateTime('15:30:00')); 
 171                                         $session->setLength(new \
DateTime('05:30:00')); 
 173                                 } elseif ($slot == 'Evening') { 
 175                                         $session->setBegin(new \
DateTime('19:30:00')); 
 178                                         $session->setLength(new \
DateTime('05:30:00')); 
 180                                         //Check if next day is premium 
 183                                                 $session->setLength(new \
DateTime('06:30:00')); 
 188                                         $session->setBegin(new \
DateTime('01:00:00')); 
 191                                         $session->setLength(new \
DateTime('04:00:00')); 
 193                                         //Check if next day is premium 
 196                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 199                                                 $session->setLength(new \
DateTime('03:00:00')); 
 202                         //Tino-Rossi garden => 14h -> 19h | 19h -> 01/02h 
 203                         } elseif (in_array($location, ['Tino-Rossi garden']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) { 
 205                                 if ($slot == 'Afternoon') { 
 207                                         $session->setBegin(new \
DateTime('14:00:00')); 
 210                                         $session->setLength(new \
DateTime('05:00:00')); 
 212                                 } elseif ($slot == 'Evening') { 
 214                                         $session->setBegin(new \
DateTime('19:00:00')); 
 216                                         //Check if next day is premium 
 219                                                 $session->setLength(new \
DateTime('07:00:00')); 
 224                                         $session->setBegin(new \
DateTime('01:00:00')); 
 227                                         $session->setLength(new \
DateTime('04:00:00')); 
 229                                         //Check if next day is premium 
 232                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 235                                                 $session->setLength(new \
DateTime('03:00:00')); 
 238                         //Garnier opera => 21h -> 01/02h 
 239                         } elseif ($location == 'Garnier opera' && in_array($slot, ['Evening', 'After'])) { 
 241                                 if ($slot == 'Evening') { 
 243                                         $session->setBegin(new \
DateTime('21:00:00')); 
 246                                         $session->setLength(new \
DateTime('05:00:00')); 
 248                                         //Check if next day is premium 
 251                                                 $session->setLength(new \
DateTime('06:00:00')); 
 256                                         $session->setBegin(new \
DateTime('01:00:00')); 
 259                                         $session->setLength(new \
DateTime('04:00:00')); 
 261                                         //Check if next day is premium 
 264                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 267                                                 $session->setLength(new \
DateTime('03:00:00')); 
 270                         //Trocadero esplanade|Tokyo palace|Swan island|Saint-Honore market|Orsay museum => 19h -> 01/02h 
 271                         } elseif (in_array($location, ['Trocadero esplanade', 'Tokyo palace', 'Swan island', 'Saint-Honore market', 'Orsay museum']) && in_array($slot, ['Evening', 'After'])) { 
 273                                 if ($slot == 'Evening') { 
 275                                         $session->setBegin(new \
DateTime('19:00:00')); 
 277                                         //Check if next day is premium 
 280                                                 $session->setLength(new \
DateTime('07:00:00')); 
 285                                         $session->setBegin(new \
DateTime('01:00:00')); 
 288                                         $session->setLength(new \
DateTime('04:00:00')); 
 290                                         //Check if next day is premium 
 293                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 296                                                 $session->setLength(new \
DateTime('03:00:00')); 
 299                         //Drawings' garden (Villette) => 14h -> 19h 
 300                         } elseif ($location == 'Drawings\' garden' && $slot == 'Afternoon') { 
 302                                 $session->setBegin(new \
DateTime('14:00:00')); 
 305                                 $session->setLength(new \
DateTime('05:00:00')); 
 306                         //Colette place => 14h -> 21h 
 307                         //TODO: add check here that it's a millegaux account ? 
 308                         } elseif ($location == 'Colette place' && $slot == 'Afternoon') { 
 310                                 $session->setBegin(new \
DateTime('14:00:00')); 
 313                                 $session->setLength(new \
DateTime('07:00:00')); 
 314                         //Orleans gallery => 14h -> 18h 
 315                         } elseif ($location == 'Orleans gallery' && $slot == 'Afternoon') { 
 317                                 $session->setBegin(new \
DateTime('14:00:00')); 
 320                                 $session->setLength(new \
DateTime('04:00:00')); 
 321                         //Monde garden => 14h -> 19h 
 322                         //TODO: add check here that it's a raphael account ? 
 323                         } elseif ($location == 'Monde garden' && $slot == 'Afternoon') { 
 325                                 $session->setBegin(new \
DateTime('14:00:00')); 
 328                                 $session->setLength(new \
DateTime('05:00:00')); 
 329                         //Combination not supported 
 330                         //TODO: add Madeleine place|Bastille place|Vendome place ? 
 332                                 //Add error in flash message 
 333                                 $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')])); 
 336                                 $title = $this->translator
->trans('Application add'); 
 339                                 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+
$this->context
); 
 343                         if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \
DateTime('00:00:00')) { 
 344                                 //Add error in flash message 
 345                                 $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                                 $title = $this->translator
->trans('Application add'); 
 351                                 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'form' => $form->createView()]+
$this->context
); 
 355                         $this->manager
->persist($session); 
 357                         //Flush to get the ids 
 358                         #$this->manager->flush(); 
 360                         $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                 $user = $this->getUser(); 
 366                 //Replace with requested user for admin 
 367                 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) { 
 368                         $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']); 
 371                 //Protect application fetching 
 373                         //Retrieve application 
 374                         $application = $this->doctrine
->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 376                         //Add warning in flash message 
 377                         $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')])); 
 378                 //Catch no application and session without identifier (not persisted&flushed) cases 
 379                 } catch (NoResultException
|ORMInvalidArgumentException 
$e) { 
 380                         //Create the application 
 381                         $application = new Application(); 
 382                         $application->setDance($data['dance']); 
 383                         $application->setSession($session); 
 384                         $application->setUser($user); 
 386                         //Refresh session updated field 
 387                         $session->setUpdated(new \
DateTime('now')); 
 390                         $this->manager
->persist($session); 
 392                         //Queue application save 
 393                         $this->manager
->persist($application); 
 395                         //Flush to get the ids 
 396                         $this->manager
->flush(); 
 398                         //Add notice in flash message 
 399                         $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')])); 
 402                 //Extract and process referer 
 403                 if ($referer = $request->headers
->get('referer')) { 
 404                         //Create referer request instance 
 405                         $req = Request
::create($referer); 
 408                         $path = $req->getPathInfo(); 
 410                         //Get referer query string 
 411                         $query = $req->getQueryString(); 
 414                         $path = str_replace($request->getScriptName(), '', $path); 
 416                         //Try with referer path 
 419                                 $oldContext = $this->router
->getContext(); 
 421                                 //Force clean context 
 422                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 423                                 $this->router
->setContext(new RequestContext()); 
 425                                 //Retrieve route matching path 
 426                                 $route = $this->router
->match($path); 
 429                                 $this->router
->setContext($oldContext); 
 435                                 $name = $route['_route']; 
 437                                 //Remove route and controller from route defaults 
 438                                 unset($route['_route'], $route['_controller']); 
 440                                 //Check if session view route 
 441                                 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) { 
 443                                         $route['id'] = $session->getId(); 
 447                                         $route['session'] = $session->getId(); 
 451                                 return $this->redirectToRoute($name, $route); 
 453                         } catch (MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 454                                 //Unset referer to fallback to default route 
 459                 //Redirect to cleanup the form 
 460                 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);