3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Symfony\Component\HttpFoundation\Response
; 
   7 use Symfony\Component\Routing\RequestContext
; 
   8 use Symfony\Component\Form\FormError
; 
   9 use Symfony\Component\Routing\Exception\MethodNotAllowedException
; 
  10 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
  11 use Rapsys\AirBundle\Entity\Slot
; 
  12 use Rapsys\AirBundle\Entity\User
; 
  13 use Rapsys\AirBundle\Entity\Session
; 
  14 use Rapsys\AirBundle\Entity\Application
; 
  16 class ApplicationController 
extends DefaultController 
{ 
  20          * @desc Persist application and all required dependencies in database 
  22          * @param Request $request The request instance 
  24          * @return Response The rendered view or redirection 
  26          * @throws \RuntimeException When user has not at least guest role 
  28         public function add(Request 
$request) { 
  29                 //Prevent non-guest to access here 
  30                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  32                 //Create ApplicationType form 
  33                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  35                         'action' => $this->generateUrl('rapsys_air_application_add'), 
  36                         //Set the form attribute 
  37                         #'attr' => [ 'class' => 'col' ], 
  39                         'admin' => $this->isGranted('ROLE_ADMIN'), 
  40                         //Set default user to current 
  41                         'user' => $this->getUser()->getId(), 
  42                         //Set default slot to evening 
  43                         //XXX: default to Evening (3) 
  44                         'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3) 
  47                 //Refill the fields in case of invalid form 
  48                 $form->handleRequest($request); 
  51                 if (!$form->isSubmitted() || !$form->isValid()) { 
  53                         $section = $this->translator
->trans('Application add'); 
  56                         $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
  59                         return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
  63                 $doctrine = $this->getDoctrine(); 
  66                 $manager = $doctrine->getManager(); 
  69                 $data = $form->getData(); 
  71                 //Protect session fetching 
  74                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
  75                 //Catch no session case 
  76                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
  78                         $session = new Session(); 
  79                         $session->setLocation($data['location']); 
  80                         $session->setDate($data['date']); 
  81                         $session->setSlot($data['slot']); 
  82                         $session->setCreated(new \
DateTime('now')); 
  83                         $session->setUpdated(new \
DateTime('now')); 
  86                         $short = $data['location']->getShort(); 
  89                         $slot = $data['slot']->getTitle(); 
  92                         $session->setPremium($premium = false); 
  94                         //Check if slot is afternoon 
  95                         //XXX: premium is stored only for Afternoon and Evening 
  96                         if ($slot == 'Afternoon') { 
  98                                 //XXX: a session is considered premium a day off 
  99                                 $session->setPremium($premium = $this->isPremium($data['date'])); 
 100                         //Check if slot is evening 
 101                         //XXX: premium is stored only for Afternoon and Evening 
 102                         } elseif ($slot == 'Evening') { 
 104                                 //XXX: a session is considered premium the eve of a day off 
 105                                 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D')))); 
 106                         //Check if slot is after 
 107                         } elseif ($slot == 'After') { 
 109                                 //XXX: a session is considered premium the eve of a day off 
 110                                 $premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D'))); 
 113                         //Set default length at 6h 
 114                         //XXX: date part will be truncated on save 
 115                         $session->setLength(new \
DateTime('06:00:00')); 
 118                         if ($this->isGranted('ROLE_ADMIN')) { 
 120                                 if ($slot == 'Morning') { 
 122                                         $session->setBegin(new \
DateTime('09:00:00')); 
 125                                         $session->setLength(new \
DateTime('05:00:00')); 
 127                                 } elseif ($slot == 'Afternoon') { 
 129                                         $session->setBegin(new \
DateTime('14:30:00')); 
 132                                         $session->setLength(new \
DateTime('04:30:00')); 
 134                                 } elseif ($slot == 'Evening') { 
 136                                         $session->setBegin(new \
DateTime('19:00:00')); 
 138                                         //Check if next day is premium 
 141                                                 $session->setLength(new \
DateTime('07:00:00')); 
 146                                         $session->setBegin(new \
DateTime('01:00:00')); 
 149                                         $session->setLength(new \
DateTime('04:00:00')); 
 151                                         //Check if next day is premium 
 154                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 157                                                 $session->setLength(new \
DateTime('03:00:00')); 
 160                         //Docks => 14h -> 19h | 19h -> 01/02h 
 161                         //XXX: remove Garnier from here to switch back to 21h 
 162                         } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) { 
 164                                 if ($slot == 'Afternoon') { 
 166                                         $session->setBegin(new \
DateTime('14:00:00')); 
 169                                         $session->setLength(new \
DateTime('05:00:00')); 
 171                                 } elseif ($slot == 'Evening') { 
 173                                         $session->setBegin(new \
DateTime('19:00:00')); 
 175                                         //Check if next day is premium 
 178                                                 $session->setLength(new \
DateTime('07:00:00')); 
 183                                         $session->setBegin(new \
DateTime('01:00:00')); 
 186                                         $session->setLength(new \
DateTime('04:00:00')); 
 188                                         //Check if next day is premium 
 191                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 194                                                 $session->setLength(new \
DateTime('03:00:00')); 
 197                         //Garnier => 21h -> 01/02h 
 198                         } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) { 
 200                                 if ($slot == 'Evening') { 
 202                                         $session->setBegin(new \
DateTime('21:00:00')); 
 205                                         $session->setLength(new \
DateTime('05:00:00')); 
 207                                         //Check if next day is premium 
 210                                                 $session->setLength(new \
DateTime('06:00:00')); 
 215                                         $session->setBegin(new \
DateTime('01:00:00')); 
 218                                         $session->setLength(new \
DateTime('04:00:00')); 
 220                                         //Check if next day is premium 
 223                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 226                                                 $session->setLength(new \
DateTime('03:00:00')); 
 229                         //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h 
 230                         } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) { 
 232                                 if ($slot == 'Evening') { 
 234                                         $session->setBegin(new \
DateTime('19:00:00')); 
 236                                         //Check if next day is premium 
 239                                                 $session->setLength(new \
DateTime('07:00:00')); 
 244                                         $session->setBegin(new \
DateTime('01:00:00')); 
 247                                         $session->setLength(new \
DateTime('04:00:00')); 
 249                                         //Check if next day is premium 
 252                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 255                                                 $session->setLength(new \
DateTime('03:00:00')); 
 258                         //La Villette => 14h -> 19h 
 259                         } elseif ($short == 'Villette' && $slot == 'Afternoon') { 
 261                                 $session->setBegin(new \
DateTime('14:00:00')); 
 264                                 $session->setLength(new \
DateTime('05:00:00')); 
 265                         //Place Colette => 14h -> 21h 
 266                         //TODO: add check here that it's a millegaux account ? 
 267                         } elseif ($short == 'Colette' && $slot == 'Afternoon') { 
 269                                 $session->setBegin(new \
DateTime('14:00:00')); 
 272                                 $session->setLength(new \
DateTime('07:00:00')); 
 273                         //Galerie d'OrlƩans => 14h -> 18h 
 274                         } elseif ($short == 'Orleans' && $slot == 'Afternoon') { 
 276                                 $session->setBegin(new \
DateTime('14:00:00')); 
 279                                 $session->setLength(new \
DateTime('04:00:00')); 
 280                         //Jardin du Monde => 14h -> 15h 
 281                         } elseif ($short == 'Monde' && $slot == 'Morning') { 
 283                                 $session->setBegin(new \
DateTime('14:00:00')); 
 286                                 $session->setLength(new \
DateTime('01:00:00')); 
 287                         //Combination not supported 
 289                                 //Add error in flash message 
 290                                 $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($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
 293                                 $section = $this->translator
->trans('Application add'); 
 296                                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 299                                 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
 303                         if (!$this->isGranted('ROLE_ADMIN') && $session->getStart() < new \
DateTime('00:00:00')) { 
 304                                 //Add error in flash message 
 305                                 $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($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
 308                                 $section = $this->translator
->trans('Application add'); 
 311                                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 314                                 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$this->context
); 
 318                         $manager->persist($session); 
 320                         //Flush to get the ids 
 323                         $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($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
 327                 $user = $this->getUser(); 
 329                 //Replace with requested user for admin 
 330                 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) { 
 331                         $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']); 
 334                 //Protect application fetching 
 336                         //Retrieve application 
 337                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 339                         //Add warning in flash message 
 340                         $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($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
 341                 //Catch no application and session without identifier (not persisted&flushed) cases 
 342                 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 343                         //Create the application 
 344                         $application = new Application(); 
 345                         $application->setSession($session); 
 346                         $application->setUser($user); 
 347                         $application->setCreated(new \
DateTime('now')); 
 348                         $application->setUpdated(new \
DateTime('now')); 
 350                         //Refresh session updated field 
 351                         $session->setUpdated(new \
DateTime('now')); 
 354                         $manager->persist($session); 
 356                         //Queue application save 
 357                         $manager->persist($application); 
 359                         //Flush to get the ids 
 362                         //Add notice in flash message 
 363                         $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($data['slot'])), '%date%' => $data['date']->format('Y-m-d')])); 
 366                 //Extract and process referer 
 367                 if ($referer = $request->headers
->get('referer')) { 
 368                         //Create referer request instance 
 369                         $req = Request
::create($referer); 
 372                         $path = $req->getPathInfo(); 
 374                         //Get referer query string 
 375                         $query = $req->getQueryString(); 
 378                         $path = str_replace($request->getScriptName(), '', $path); 
 380                         //Try with referer path 
 383                                 $oldContext = $this->router
->getContext(); 
 385                                 //Force clean context 
 386                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 387                                 $this->router
->setContext(new RequestContext()); 
 389                                 //Retrieve route matching path 
 390                                 $route = $this->router
->match($path); 
 393                                 $this->router
->setContext($oldContext); 
 399                                 $name = $route['_route']; 
 401                                 //Remove route and controller from route defaults 
 402                                 unset($route['_route'], $route['_controller']); 
 404                                 //Check if session view route 
 405                                 if ($name == 'rapsys_air_session_view' && !empty($route['id'])) { 
 407                                         $route['id'] = $session->getId(); 
 411                                         $route['session'] = $session->getId(); 
 415                                 return $this->redirectToRoute($name, $route); 
 417                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 418                                 //Unset referer to fallback to default route 
 423                 //Redirect to cleanup the form 
 424                 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]); 
 428          * Compute eastern for selected year 
 430          * @param int $year The eastern year 
 432          * @return DateTime The eastern date 
 434         function getEastern($year) { 
 437                 //Check if already computed 
 438                 if (isset($data[$year])) { 
 439                         //Return computed eastern 
 441                 //Check if data is null 
 442                 } elseif (is_null($data)) { 
 446                 $d = (19 * ($year % 
19) + 
24) % 
30; 
 447                 $e = (2 * ($year % 
4) + 
4 * ($year % 
7) + 
6 * $d + 
5) % 
7; 
 455                 } elseif ($d == 29 && $e == 6) { 
 458                 } elseif ($d == 28 && $e == 6) { 
 463                 //Store eastern in data 
 464                 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day))); 
 468          * Check if date is a premium day 
 470          * @desc Consider as premium a day off 
 472          * @param DateTime $date The date to check 
 473          * @return bool Whether the date is off or not 
 475         function isPremium($date) { 
 477                 $w = $date->format('w'); 
 479                 //Check if weekend day 
 480                 if ($w == 0 || $w == 6) { 
 481                         //Date is weekend day 
 486                 $d = $date->format('d'); 
 489                 $m = $date->format('m'); 
 491                 //Check if fixed holiday 
 493                         //Check if 1st january 
 494                         ($d == 1 && $m == 1) || 
 496                         ($d == 1 && $m == 5) || 
 498                         ($d == 8 && $m == 5) || 
 500                         ($d == 14 && $m == 7) || 
 501                         //Check if 15st august 
 502                         ($d == 15 && $m == 8) || 
 503                         //Check if 1st november 
 504                         ($d == 1 && $m == 11) || 
 505                         //Check if 11st november 
 506                         ($d == 11 && $m == 11) || 
 507                         //Check if 25st december 
 508                         ($d == 25 && $m == 12) 
 510                         //Date is a fixed holiday 
 515                 $eastern = $this->getEastern($date->format('Y')); 
 517                 //Check dynamic holidays 
 519                         (clone $eastern)->add(new \
DateInterval('P1D')) == $date || 
 520                         (clone $eastern)->add(new \
DateInterval('P39D')) == $date || 
 521                         (clone $eastern)->add(new \
DateInterval('P50D')) == $date 
 523                         //Date is a dynamic holiday 
 527                 //Date is not a holiday and week day