3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Symfony\Component\Routing\RequestContext
; 
   7 use Symfony\Component\Form\FormError
; 
   8 use Rapsys\AirBundle\Entity\Slot
; 
   9 use Rapsys\AirBundle\Entity\User
; 
  10 use Rapsys\AirBundle\Entity\Session
; 
  11 use Rapsys\AirBundle\Entity\Application
; 
  13 class ApplicationController 
extends DefaultController 
{ 
  17          * @desc Persist application and all required dependencies in database 
  19          * @param Request $request The request instance 
  21          * @return Response The rendered view or redirection 
  23          * @throws \RuntimeException When user has not at least guest role 
  25         public function add(Request 
$request) { 
  26                 //Prevent non-guest to access here 
  27                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  29                 //Create ApplicationType form 
  30                 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  32                         'action' => $this->generateUrl('rapsys_air_application_add'), 
  33                         //Set the form attribute 
  34                         #'attr' => [ 'class' => 'col' ], 
  36                         'admin' => $this->isGranted('ROLE_ADMIN'), 
  37                         //Set default user to current 
  38                         'user' => $this->getUser()->getId(), 
  39                         //Set default slot to evening 
  40                         //XXX: default to Evening (3) 
  41                         'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3) 
  44                 //Reject non post requests 
  45                 if (!$request->isMethod('POST')) { 
  46                         throw new \
RuntimeException('Request method MUST be POST'); 
  49                 //Refill the fields in case of invalid form 
  50                 $form->handleRequest($request); 
  53                 if (!$form->isValid()) { 
  55                         $section = $this->translator
->trans('Application Add'); 
  58                         $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
  61                         return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form]+
$this->context
); 
  65                 $doctrine = $this->getDoctrine(); 
  68                 $manager = $doctrine->getManager(); 
  71                 $data = $form->getData(); 
  73                 //Protect session fetching 
  76                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
  77                 //Catch no session case 
  78                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
  80                         $session = new Session(); 
  81                         $session->setLocation($data['location']); 
  82                         $session->setDate($data['date']); 
  83                         $session->setSlot($data['slot']); 
  84                         $session->setCreated(new \
DateTime('now')); 
  85                         $session->setUpdated(new \
DateTime('now')); 
  88                         $short = $data['location']->getShort(); 
  91                         $slot = $data['slot']->getTitle(); 
  94                         $session->setPremium($premium = false); 
  96                         //Check if slot is afternoon 
  97                         //XXX: premium is stored only for Afternoon and Evening 
  98                         if ($slot == 'Afternoon') { 
 100                                 //XXX: a session is considered premium a day off 
 101                                 $session->setPremium($premium = $this->isPremium($data['date'])); 
 102                         //Check if slot is evening 
 103                         //XXX: premium is stored only for Afternoon and Evening 
 104                         } elseif ($slot == 'Evening') { 
 106                                 //XXX: a session is considered premium the eve of a day off 
 107                                 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D')))); 
 108                         //Check if slot is after 
 109                         } elseif ($slot == 'After') { 
 111                                 //XXX: a session is considered premium the eve of a day off 
 112                                 $premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D'))); 
 115                         //Set default length at 6h 
 116                         //XXX: date part will be truncated on save 
 117                         $session->setLength(new \
DateTime('06:00:00')); 
 120                         if ($this->isGranted('ROLE_ADMIN')) { 
 122                                 if ($slot == 'Morning') { 
 124                                         $session->setBegin(new \
DateTime('09:00:00')); 
 127                                         $session->setLength(new \
DateTime('05:00:00')); 
 129                                 } elseif ($slot == 'Afternoon') { 
 131                                         $session->setBegin(new \
DateTime('14:00:00')); 
 134                                         $session->setLength(new \
DateTime('05:00:00')); 
 136                                 } elseif ($slot == 'Evening') { 
 138                                         $session->setBegin(new \
DateTime('19:00:00')); 
 140                                         //Check if next day is premium 
 143                                                 $session->setLength(new \
DateTime('07:00:00')); 
 148                                         $session->setBegin(new \
DateTime('01:00:00')); 
 151                                         $session->setLength(new \
DateTime('04:00:00')); 
 153                                         //Check if next day is premium 
 156                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 159                                                 $session->setLength(new \
DateTime('03:00:00')); 
 162                         //Docks => 14h -> 19h | 19h -> 01/02h 
 163                         //XXX: remove Garnier from here to switch back to 21h 
 164                         } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) { 
 166                                 if ($slot == 'Afternoon') { 
 168                                         $session->setBegin(new \
DateTime('14:00:00')); 
 171                                         $session->setLength(new \
DateTime('05:00:00')); 
 173                                 } elseif ($slot == 'Evening') { 
 175                                         $session->setBegin(new \
DateTime('19:00:00')); 
 177                                         //Check if next day is premium 
 180                                                 $session->setLength(new \
DateTime('07:00:00')); 
 185                                         $session->setBegin(new \
DateTime('01:00:00')); 
 188                                         $session->setLength(new \
DateTime('04:00:00')); 
 190                                         //Check if next day is premium 
 193                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 196                                                 $session->setLength(new \
DateTime('03:00:00')); 
 199                         //Garnier => 21h -> 01/02h 
 200                         } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) { 
 202                                 if ($slot == 'Evening') { 
 204                                         $session->setBegin(new \
DateTime('21:00:00')); 
 207                                         $session->setLength(new \
DateTime('05:00:00')); 
 209                                         //Check if next day is premium 
 212                                                 $session->setLength(new \
DateTime('06:00:00')); 
 217                                         $session->setBegin(new \
DateTime('01:00:00')); 
 220                                         $session->setLength(new \
DateTime('04:00:00')); 
 222                                         //Check if next day is premium 
 225                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 228                                                 $session->setLength(new \
DateTime('03:00:00')); 
 231                         //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h 
 232                         } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) { 
 234                                 if ($slot == 'Evening') { 
 236                                         $session->setBegin(new \
DateTime('19:00:00')); 
 238                                         //Check if next day is premium 
 241                                                 $session->setLength(new \
DateTime('07:00:00')); 
 246                                         $session->setBegin(new \
DateTime('01:00:00')); 
 249                                         $session->setLength(new \
DateTime('04:00:00')); 
 251                                         //Check if next day is premium 
 254                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 257                                                 $session->setLength(new \
DateTime('03:00:00')); 
 260                         //La Villette => 14h -> 19h 
 261                         } elseif ($short == 'Villette' && $slot == 'Afternoon') { 
 263                                 $session->setBegin(new \
DateTime('14:00:00')); 
 266                                 $session->setLength(new \
DateTime('05:00:00')); 
 267                         //Place Colette => 14h -> 21h 
 268                         //TODO: add check here that it's a millegaux account ? 
 269                         } elseif ($short == 'Colette' && $slot == 'Afternoon') { 
 271                                 $session->setBegin(new \
DateTime('14:00:00')); 
 274                                 $session->setLength(new \
DateTime('07:00:00')); 
 275                         //Galerie d'OrlƩans => 14h -> 18h 
 276                         } elseif ($short == 'Orleans' && $slot == 'Afternoon') { 
 278                                 $session->setBegin(new \
DateTime('14:00:00')); 
 281                                 $session->setLength(new \
DateTime('04:00:00')); 
 282                         //Combination not supported 
 284                                 //Add error in flash message 
 285                                 $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')])); 
 289                         $manager->persist($session); 
 291                         //Flush to get the ids 
 294                         $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')])); 
 298                 $user = $this->getUser(); 
 300                 //Replace with requested user for admin 
 301                 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) { 
 302                         $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']); 
 305                 //Protect application fetching 
 307                         //Retrieve application 
 308                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 310                         //Add warning in flash message 
 311                         $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')])); 
 312                 //Catch no application and session without identifier (not persisted&flushed) cases 
 313                 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 314                         //Create the application 
 315                         $application = new Application(); 
 316                         $application->setSession($session); 
 317                         $application->setUser($user); 
 318                         $application->setCreated(new \
DateTime('now')); 
 319                         $application->setUpdated(new \
DateTime('now')); 
 321                         //Refresh session updated field 
 322                         $session->setUpdated(new \
DateTime('now')); 
 325                         $manager->persist($session); 
 327                         //Queue application save 
 328                         $manager->persist($application); 
 330                         //Flush to get the ids 
 333                         //Add notice in flash message 
 334                         $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')])); 
 337                 //Extract and process referer 
 338                 if ($referer = $request->headers
->get('referer')) { 
 339                         //Create referer request instance 
 340                         $req = Request
::create($referer); 
 343                         $path = $req->getPathInfo(); 
 345                         //Get referer query string 
 346                         $query = $req->getQueryString(); 
 349                         $path = str_replace($request->getScriptName(), '', $path); 
 351                         //Try with referer path 
 354                                 $oldContext = $this->router
->getContext(); 
 356                                 //Force clean context 
 357                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 358                                 $this->router
->setContext(new RequestContext()); 
 360                                 //Retrieve route matching path 
 361                                 $route = $this->router
->match($path); 
 364                                 $this->router
->setContext($oldContext); 
 370                                 $name = $route['_route']; 
 372                                 //Remove route and controller from route defaults 
 373                                 unset($route['_route'], $route['_controller']); 
 376                                 return $this->redirectToRoute($name, ['session' => $session->getId()]+
$route); 
 378                         } catch(ResourceNotFoundException 
$e) { 
 379                                 //Unset referer to fallback to default route 
 384                 //Redirect to cleanup the form 
 385                 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]); 
 389          * Compute eastern for selected year 
 391          * @param int $year The eastern year 
 393          * @return DateTime The eastern date 
 395         function getEastern($year) { 
 398                 //Check if already computed 
 399                 if (isset($data[$year])) { 
 400                         //Return computed eastern 
 402                 //Check if data is null 
 403                 } elseif (is_null($data)) { 
 407                 $d = (19 * ($year % 
19) + 
24) % 
30; 
 408                 $e = (2 * ($year % 
4) + 
4 * ($year % 
7) + 
6 * $d + 
5) % 
7; 
 416                 } elseif ($d == 29 && $e == 6) { 
 419                 } elseif ($d == 28 && $e == 6) { 
 424                 //Store eastern in data 
 425                 return ($data[$year] = new \
DateTime(sprintf('%04d-%02d-%02d', $year, $month, $day))); 
 429          * Check if date is a premium day 
 431          * @desc Consider as premium a day off 
 433          * @param DateTime $date The date to check 
 434          * @return bool Whether the date is off or not 
 436         function isPremium($date) { 
 438                 $w = $date->format('w'); 
 440                 //Check if weekend day 
 441                 if ($w == 0 || $w == 6) { 
 442                         //Date is weekend day 
 447                 $d = $date->format('d'); 
 450                 $m = $date->format('m'); 
 452                 //Check if fixed holiday 
 454                         //Check if 1st january 
 455                         ($d == 1 && $m == 1) || 
 457                         ($d == 1 && $m == 5) || 
 459                         ($d == 8 && $m == 5) || 
 461                         ($d == 14 && $m == 7) || 
 462                         //Check if 15st august 
 463                         ($d == 15 && $m == 8) || 
 464                         //Check if 1st november 
 465                         ($d == 1 && $m == 11) || 
 466                         //Check if 11st november 
 467                         ($d == 11 && $m == 11) || 
 468                         //Check if 25st december 
 469                         ($d == 25 && $m == 12) 
 471                         //Date is a fixed holiday 
 476                 $eastern = $this->getEastern($date->format('Y')); 
 478                 //Check dynamic holidays 
 480                         (clone $eastern)->add(new \
DateInterval('P1D')) == $date || 
 481                         (clone $eastern)->add(new \
DateInterval('P39D')) == $date || 
 482                         (clone $eastern)->add(new \
DateInterval('P50D')) == $date 
 484                         //Date is a dynamic holiday 
 488                 //Date is not a holiday and week day