3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Symfony\Component\Routing\RequestContext
; 
   7 use Symfony\Component\Routing\Exception\MethodNotAllowedException
; 
   8 use Symfony\Component\Routing\Exception\ResourceNotFoundException
; 
   9 use Rapsys\AirBundle\Entity\Application
; 
  10 use Rapsys\AirBundle\Entity\User
; 
  11 use Rapsys\AirBundle\Entity\Slot
; 
  12 use Rapsys\AirBundle\Entity\Session
; 
  13 use Rapsys\AirBundle\Entity\Location
; 
  15 class SessionController 
extends DefaultController 
{ 
  19          * @desc Persist session and all required dependencies in database 
  21          * @param Request $request The request instance 
  23          * @return Response The rendered view or redirection 
  25          * @throws \RuntimeException When user has not at least guest role 
  27         public function edit(Request 
$request, $id) { 
  28                 //Prevent non-guest to access here 
  29                 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')])); 
  31                 //Reject non post requests 
  32                 if (!$request->isMethod('POST')) { 
  33                         throw new \
RuntimeException('Request method MUST be POST'); 
  37                 $doctrine = $this->getDoctrine(); 
  40                 $session = $doctrine->getRepository(Session
::class)->fetchOneById($id); 
  45                         !$this->isGranted('ROLE_ADMIN') && 
  47                         $this->getUser()->getId() != $session['au_id'] && 
  48                         //or application without attributed user 
  49                         $session['au_id'] !== null && !in_array($this->getUser()->getId(), explode("\n", $session['sau_id'])) 
  51                         //Prevent non admin and non attributed user access 
  52                         throw $this->createAccessDeniedException(); 
  56                 $now = new \
DateTime('now'); 
  58                 //Create SessionEditType form 
  59                 $form = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [ 
  61                         'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]), 
  62                         //Set the form attribute 
  65                         'admin' => $this->isGranted('ROLE_ADMIN'), 
  66                         //Set default user to current 
  67                         'user' => $this->getUser()->getId(), 
  69                         'begin' => $session['begin'], 
  71                         'length' => $session['length'], 
  73                         'raincancel' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['rainfall'] >= 2, 
  75                         'cancel' => $this->isGranted('ROLE_ADMIN') || in_array($this->getUser()->getId(), explode("\n", $session['sau_id'])), 
  77                         'modify' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_REGULAR'), 
  79                         'move' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_SENIOR'), 
  81                         'session' => $session['id'] 
  84                 //Refill the fields in case of invalid form 
  85                 $form->handleRequest($request); 
  88                 #if (true) { $form->isValid(); 
  89                 //TODO: mettre une contrainte sur un des boutons submit, je sais pas encore comment 
  90                 if (!$form->isValid()) { 
  92                         $section = $this->translator
->trans('Session %id%', ['%id%' => $id]); 
  95                         $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
  97                         //Add session in context 
  98                         $context['session'] = [ 
 100                                 'title' => $this->translator
->trans('Session %id%', ['%id%' => $id]), 
 102                                         'id' => $session['l_id'], 
 103                                         'at' => $this->translator
->trans('at '.$session['l_title']) 
 107                         return $this->render('@RapsysAir/session/edit.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+
$context+
$this->context
); 
 111                 $manager = $doctrine->getManager(); 
 114                 $data = $form->getData(); 
 117                 $session = $doctrine->getRepository(Session
::class)->findOneById($id); 
 120                 $user = $this->getUser(); 
 122                 //Replace with requested user for admin 
 123                 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) { 
 124                         $user = $doctrine->getRepository(User
::class)->findOneById($data['user']); 
 128                 $datetime = new \
DateTime('now'); 
 131                 if ($form->has('raincancel') && $form->get('raincancel')->isClicked()) { 
 133                         if ($this->isGranted('ROLE_ADMIN') || $session->getRainfall() >= 2) { 
 134                                 //Check that application is attributed 
 135                                 if (!empty($application = $session->getApplication())) { 
 137                                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $application->getUser()); 
 139                                         //Set canceled time at start minus one day 
 140                                         $canceled = (clone $session->getStart())->sub(new \
DateInterval('P1D')); 
 143                                         $application->setCanceled($canceled); 
 146                                         $application->setUpdated($datetime); 
 148                                         //Queue application save 
 149                                         $manager->persist($application); 
 151                                         //Add notice in flash message 
 152                                         $this->addFlash('notice', $this->translator
->trans('Application %id% updated', ['%id%' => $application->getId()])); 
 154                                         $session->setUpdated($datetime); 
 157                                         $manager->persist($session); 
 159                                         //Add notice in flash message 
 160                                         $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 163                                         //Add notice in flash message 
 164                                         $this->addFlash('warning', $this->translator
->trans('Session %id% not updated', ['%id%' => $id])); 
 166                         //Not enough rainfall 
 168                                 //Add notice in flash message 
 169                                 $this->addFlash('warning', $this->translator
->trans('Session %id% not updated', ['%id%' => $id])); 
 172                 } elseif ($form->has('modify') && $form->get('modify')->isClicked()) { 
 174                         $session->setBegin($data['begin']); 
 177                         $session->setLength($data['length']); 
 180                         $session->setUpdated($datetime); 
 183                         $manager->persist($session); 
 185                         //Add notice in flash message 
 186                         $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 188                 } elseif ($form->has('move') && $form->get('move')->isClicked()) { 
 190                         $session->setLocation($doctrine->getRepository(Location
::class)->findOneById($data['location'])); 
 193                         $session->setUpdated($datetime); 
 196                         $manager->persist($session); 
 198                         //Add notice in flash message 
 199                         $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 201                 } elseif ($form->has('cancel') && $form->get('cancel')->isClicked()) { 
 203                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 205                         //Not already canceled 
 206                         if ($application->getCanceled() === null) { 
 208                                 $application->setCanceled($datetime); 
 210                                 //Check if application is session application 
 211                                 if ($session->getApplication() == $application) { 
 212                                         //Unattribute session 
 213                                         $session->setApplication(null); 
 216                                         $session->setUpdated($datetime); 
 219                                         $manager->persist($session); 
 221                                         //Add notice in flash message 
 222                                         $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 226                                 //Uncancel application 
 227                                 $application->setCanceled(null); 
 231                         $application->setUpdated($datetime); 
 233                         //Queue application save 
 234                         $manager->persist($application); 
 236                         //Add notice in flash message 
 237                         $this->addFlash('notice', $this->translator
->trans('Application %id% updated', ['%id%' => $application->getId()])); 
 239                 } elseif ($form->has('attribute') && $form->get('attribute')->isClicked()) { 
 241                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 244                         if ($application->getCanceled() !== null) { 
 245                                 //Uncancel application 
 246                                 $application->setCanceled(null); 
 250                         //XXX: magic cheat score 42 
 251                         $application->setScore(42); 
 254                         $application->setUpdated($datetime); 
 256                         //Queue application save 
 257                         $manager->persist($application); 
 259                         //Add notice in flash message 
 260                         $this->addFlash('notice', $this->translator
->trans('Application %id% updated', ['%id%' => $application->getId()])); 
 262                         //Unattribute session 
 263                         $session->setApplication($application); 
 266                         $session->setUpdated($datetime); 
 269                         $manager->persist($session); 
 271                         //Add notice in flash message 
 272                         $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 274                 } elseif ($form->has('autoattribute') && $form->get('autoattribute')->isClicked()) { 
 275                         //Get best application 
 276                         //XXX: best application may not issue result while grace time or bad behaviour 
 277                         if (!empty($application = $doctrine->getRepository(Session
::class)->findBestApplicationById($id))) { 
 279                                 $session->setApplication($application); 
 282                                 $session->setUpdated($datetime); 
 285                                 $manager->persist($session); 
 287                                 //Add notice in flash message 
 288                                 $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 291                                 //Add notice in flash message 
 292                                 $this->addFlash('warning', $this->translator
->trans('Session %id% not updated', ['%id%' => $id])); 
 295                 } elseif ($form->has('lock') && $form->get('lock')->isClicked()) { 
 297                         if ($session->getLocked() !== null) { 
 302                                 $session->setLocked(null); 
 305                                 //Set canceled time at start minus one day 
 306                                 $canceled = (clone $session->getStart())->sub(new \
DateInterval('P1D')); 
 308                                 //Unattribute session 
 309                                 $session->setApplication(null); 
 312                                 $session->setLocked($datetime); 
 316                         $applications = $doctrine->getRepository(Application
::class)->findBySession($session); 
 319                         if (!empty($applications)) { 
 320                                 //Iterate on each applications 
 321                                 foreach($applications as $application) { 
 323                                         $application->setCanceled($canceled); 
 326                                         $application->setUpdated($datetime); 
 328                                         //Queue application save 
 329                                         $manager->persist($application); 
 331                                         //Add notice in flash message 
 332                                         $this->addFlash('notice', $this->translator
->trans('Application %id% updated', ['%id%' => $application->getId()])); 
 337                         $session->setUpdated($datetime); 
 340                         $manager->persist($session); 
 342                         //Add notice in flash message 
 343                         $this->addFlash('notice', $this->translator
->trans('Session %id% updated', ['%id%' => $id])); 
 346                 //Flush to get the ids 
 349                 //Extract and process referer 
 350                 if ($referer = $request->headers
->get('referer')) { 
 351                         //Create referer request instance 
 352                         $req = Request
::create($referer); 
 355                         $path = $req->getPathInfo(); 
 357                         //Get referer query string 
 358                         $query = $req->getQueryString(); 
 361                         $path = str_replace($request->getScriptName(), '', $path); 
 363                         //Try with referer path 
 366                                 $oldContext = $this->router
->getContext(); 
 368                                 //Force clean context 
 369                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 370                                 $this->router
->setContext(new RequestContext()); 
 372                                 //Retrieve route matching path 
 373                                 $route = $this->router
->match($path); 
 376                                 $this->router
->setContext($oldContext); 
 382                                 $name = $route['_route']; 
 384                                 //Remove route and controller from route defaults 
 385                                 unset($route['_route'], $route['_controller']); 
 388                                 return $this->redirectToRoute($name, ['session' => $id]+
$route); 
 390                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 391                                 //Unset referer to fallback to default route 
 396                 //Redirect to cleanup the form 
 397                 return $this->redirectToRoute('rapsys_air_session_view', ['id' => $id]); 
 399                 //Protect session fetching 
 402                         $session = $doctrine->getRepository(Session
::class)->findOneById($id); 
 405                         $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']); 
 406                 //Catch no session case 
 407                 } catch (\Doctrine\ORM\NoResultException 
$e) { 
 409                         $session = new Session(); 
 410                         $session->setLocation($data['location']); 
 411                         $session->setDate($data['date']); 
 412                         $session->setSlot($data['slot']); 
 413                         $session->setCreated(new \
DateTime('now')); 
 414                         $session->setUpdated(new \
DateTime('now')); 
 417                         $short = $data['location']->getShort(); 
 420                         $slot = $data['slot']->getTitle(); 
 423                         $session->setPremium($premium = false); 
 425                         //Check if slot is afternoon 
 426                         //XXX: premium is stored only for Afternoon and Evening 
 427                         if ($slot == 'Afternoon') { 
 429                                 //XXX: a session is considered premium a day off 
 430                                 $session->setPremium($premium = $this->isPremium($data['date'])); 
 431                         //Check if slot is evening 
 432                         //XXX: premium is stored only for Afternoon and Evening 
 433                         } elseif ($slot == 'Evening') { 
 435                                 //XXX: a session is considered premium the eve of a day off 
 436                                 $session->setPremium($premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D')))); 
 437                         //Check if slot is after 
 438                         } elseif ($slot == 'After') { 
 440                                 //XXX: a session is considered premium the eve of a day off 
 441                                 $premium = $this->isPremium((clone $data['date'])->add(new \
DateInterval('P1D'))); 
 444                         //Set default length at 6h 
 445                         //XXX: date part will be truncated on save 
 446                         $session->setLength(new \
DateTime('06:00:00')); 
 449                         if ($this->isGranted('ROLE_ADMIN')) { 
 451                                 if ($slot == 'Morning') { 
 453                                         $session->setBegin(new \
DateTime('09:00:00')); 
 456                                         $session->setLength(new \
DateTime('05:00:00')); 
 458                                 } elseif ($slot == 'Afternoon') { 
 460                                         $session->setBegin(new \
DateTime('14:00:00')); 
 463                                         $session->setLength(new \
DateTime('05:00:00')); 
 465                                 } elseif ($slot == 'Evening') { 
 467                                         $session->setBegin(new \
DateTime('19:00:00')); 
 469                                         //Check if next day is premium 
 472                                                 $session->setLength(new \
DateTime('07:00:00')); 
 477                                         $session->setBegin(new \
DateTime('01:00:00')); 
 480                                         $session->setLength(new \
DateTime('04:00:00')); 
 482                                         //Check if next day is premium 
 485                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 488                                                 $session->setLength(new \
DateTime('03:00:00')); 
 491                         //Docks => 14h -> 19h | 19h -> 01/02h 
 492                         //XXX: remove Garnier from here to switch back to 21h 
 493                         } elseif (in_array($short, ['Docks', 'Garnier']) && in_array($slot, ['Afternoon', 'Evening', 'After'])) { 
 495                                 if ($slot == 'Afternoon') { 
 497                                         $session->setBegin(new \
DateTime('14:00:00')); 
 500                                         $session->setLength(new \
DateTime('05:00:00')); 
 502                                 } elseif ($slot == 'Evening') { 
 504                                         $session->setBegin(new \
DateTime('19:00:00')); 
 506                                         //Check if next day is premium 
 509                                                 $session->setLength(new \
DateTime('07:00:00')); 
 514                                         $session->setBegin(new \
DateTime('01:00:00')); 
 517                                         $session->setLength(new \
DateTime('04:00:00')); 
 519                                         //Check if next day is premium 
 522                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 525                                                 $session->setLength(new \
DateTime('03:00:00')); 
 528                         //Garnier => 21h -> 01/02h 
 529                         } elseif ($short == 'Garnier' && in_array($slot, ['Evening', 'After'])) { 
 531                                 if ($slot == 'Evening') { 
 533                                         $session->setBegin(new \
DateTime('21:00:00')); 
 536                                         $session->setLength(new \
DateTime('05:00:00')); 
 538                                         //Check if next day is premium 
 541                                                 $session->setLength(new \
DateTime('06:00:00')); 
 546                                         $session->setBegin(new \
DateTime('01:00:00')); 
 549                                         $session->setLength(new \
DateTime('04:00:00')); 
 551                                         //Check if next day is premium 
 554                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 557                                                 $session->setLength(new \
DateTime('03:00:00')); 
 560                         //Trocadero|Tokyo|Swan|Honore|Orsay => 19h -> 01/02h 
 561                         } elseif (in_array($short, ['Trocadero', 'Tokyo', 'Swan', 'Honore', 'Orsay']) && in_array($slot, ['Evening', 'After'])) { 
 563                                 if ($slot == 'Evening') { 
 565                                         $session->setBegin(new \
DateTime('19:00:00')); 
 567                                         //Check if next day is premium 
 570                                                 $session->setLength(new \
DateTime('07:00:00')); 
 575                                         $session->setBegin(new \
DateTime('01:00:00')); 
 578                                         $session->setLength(new \
DateTime('04:00:00')); 
 580                                         //Check if next day is premium 
 583                                                 $session->setBegin(new \
DateTime('02:00:00')); 
 586                                                 $session->setLength(new \
DateTime('03:00:00')); 
 589                         //La Villette => 14h -> 19h 
 590                         } elseif ($short == 'Villette' && $slot == 'Afternoon') { 
 592                                 $session->setBegin(new \
DateTime('14:00:00')); 
 595                                 $session->setLength(new \
DateTime('05:00:00')); 
 596                         //Place Colette => 14h -> 21h 
 597                         //TODO: add check here that it's a millegaux account ? 
 598                         } elseif ($short == 'Colette' && $slot == 'Afternoon') { 
 600                                 $session->setBegin(new \
DateTime('14:00:00')); 
 603                                 $session->setLength(new \
DateTime('07:00:00')); 
 604                         //Galerie d'Orléans => 14h -> 18h 
 605                         } elseif ($short == 'Orleans' && $slot == 'Afternoon') { 
 607                                 $session->setBegin(new \
DateTime('14:00:00')); 
 610                                 $session->setLength(new \
DateTime('04:00:00')); 
 611                         //Combination not supported 
 613                                 //Add error in flash message 
 614                                 $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')])); 
 618                         $manager->persist($session); 
 620                         //Flush to get the ids 
 623                         $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')])); 
 627                 $user = $this->getUser(); 
 629                 //Replace with requested user for admin 
 630                 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) { 
 631                         $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']); 
 634                 //Protect application fetching 
 636                         //Retrieve application 
 637                         $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user); 
 639                         //Add warning in flash message 
 640                         $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')])); 
 641                 //Catch no application and session without identifier (not persisted&flushed) cases 
 642                 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException 
$e) { 
 643                         //Create the application 
 644                         $application = new Application(); 
 645                         $application->setSession($session); 
 646                         $application->setUser($user); 
 647                         $application->setCreated(new \
DateTime('now')); 
 648                         $application->setUpdated(new \
DateTime('now')); 
 650                         //Refresh session updated field 
 651                         $session->setUpdated(new \
DateTime('now')); 
 654                         $manager->persist($session); 
 656                         //Queue application save 
 657                         $manager->persist($application); 
 659                         //Flush to get the ids 
 662                         //Add notice in flash message 
 663                         $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')])); 
 670          * @desc Display all sessions with an application or login form 
 672          * @param Request $request The request instance 
 674          * @return Response The rendered view 
 676         public function index(Request 
$request = null) { 
 678                 $doctrine = $this->getDoctrine(); 
 681                 $section = $this->translator
->trans('Sessions'); 
 684                 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
 689                 //Create application form for role_guest 
 690                 if ($this->isGranted('ROLE_GUEST')) { 
 691                         //Create ApplicationType form 
 692                         $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 694                                 'action' => $this->generateUrl('rapsys_air_application_add'), 
 695                                 //Set the form attribute 
 696                                 'attr' => [ 'class' => 'col' ], 
 698                                 'admin' => $this->isGranted('ROLE_ADMIN'), 
 699                                 //Set default user to current 
 700                                 'user' => $this->getUser()->getId(), 
 701                                 //Set default slot to evening 
 702                                 //XXX: default to Evening (3) 
 703                                 'slot' => $doctrine->getRepository(Slot
::class)->findOneById(3) 
 706                         //Add form to context 
 707                         $context['application'] = $application->createView(); 
 708                 //Create login form for anonymous 
 709                 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
 710                         //Create ApplicationType form 
 711                         $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ 
 713                                 'action' => $this->generateUrl('rapsys_user_login'), 
 714                                 //Set the form attribute 
 715                                 'attr' => [ 'class' => 'col' ] 
 718                         //Add form to context 
 719                         $context['login'] = $login->createView(); 
 723                 $period = new \
DatePeriod( 
 724                         //Start from first monday of week 
 725                         new \
DateTime('Monday this week'), 
 726                         //Iterate on each day 
 727                         new \
DateInterval('P1D'), 
 728                         //End with next sunday and 4 weeks 
 729                         new \
DateTime('Monday this week + 5 week') 
 733                 //TODO: highlight with current session route parameter 
 734                 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')); 
 737                 //XXX: we want to display all active locations anyway 
 738                 $locations = $doctrine->getRepository(Location
::class)->fetchTranslatedLocationByDatePeriod($this->translator
, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/); 
 741                 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$context+
$this->context
); 
 747          * @desc Display session by id with an application or login form 
 749          * @param Request $request The request instance 
 750          * @param int $id The session id 
 752          * @return Response The rendered view 
 754         public function view(Request 
$request, $id) { 
 756                 $doctrine = $this->getDoctrine(); 
 759                 $session = $doctrine->getRepository(Session
::class)->fetchOneById($id); 
 762                 $section = $this->translator
->trans($session['l_title']); 
 765                 $title = $this->translator
->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator
->trans($this->config
['site']['title']); 
 770                 //Create application form for role_guest 
 771                 if ($this->isGranted('ROLE_GUEST')) { 
 772                         //Create ApplicationType form 
 773                         $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 775                                 'action' => $this->generateUrl('rapsys_air_application_add'), 
 776                                 //Set the form attribute 
 777                                 'attr' => [ 'class' => 'col' ], 
 779                                 'admin' => $this->isGranted('ROLE_ADMIN'), 
 780                                 //Set default user to current 
 781                                 'user' => $this->getUser()->getId(), 
 782                                 //Set default slot to current 
 783                                 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById($session['t_id']), 
 784                                 //Set default location to current 
 785                                 'location' => $this->getDoctrine()->getRepository(Location
::class)->findOneById($session['l_id']), 
 788                         //Add form to context 
 789                         $context['application'] = $application->createView(); 
 792                         $now = new \
DateTime('now'); 
 794                         //Create SessionEditType form 
 795                         $session_edit = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [ 
 797                                 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]), 
 798                                 //Set the form attribute 
 799                                 'attr' => [ 'class' => 'col' ], 
 801                                 'admin' => $this->isGranted('ROLE_ADMIN'), 
 802                                 //Set default user to current 
 803                                 'user' => $this->getUser()->getId(), 
 805                                 'begin' => $session['begin'], 
 807                                 'length' => $session['length'], 
 809                                 'raincancel' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['rainfall'] >= 2, 
 811                                 'cancel' => $this->isGranted('ROLE_ADMIN') || in_array($this->getUser()->getId(), explode("\n", $session['sau_id'])), 
 813                                 'modify' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_REGULAR'), 
 815                                 'move' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_SENIOR'), 
 817                                 'session' => $session['id'] 
 820                         //Add form to context 
 821                         $context['session_edit'] = $session_edit->createView(); 
 822                 //Create login form for anonymous 
 823                 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
 824                         //Create ApplicationType form 
 825                         $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ 
 827                                 'action' => $this->generateUrl('rapsys_user_login'), 
 828                                 //Set the form attribute 
 829                                 'attr' => [ 'class' => 'col' ] 
 832                         //Add form to context 
 833                         $context['login'] = $login->createView(); 
 836                 //Add session in context 
 837                 $context['session'] = [ 
 839                         'date' => $session['date'], 
 840                         'begin' => $session['begin'], 
 841                         'start' => $session['start'], 
 842                         'length' => $session['length'], 
 843                         'stop' => $session['stop'], 
 844                         'rainfall' => $session['rainfall'] !== null ? $session['rainfall'].' mm' : $session['rainfall'], 
 845                         'rainrisk' => $session['rainrisk'] !== null ? ($session['rainrisk']*100).' %' : $session['rainrisk'], 
 846                         'realfeel' => $session['realfeel'] !== null ? $session['realfeel'].' °C' : $session['realfeel'], 
 847                         'realfeelmin' => $session['realfeelmin'] !== null ? $session['realfeelmin'].' °C' : $session['realfeelmin'], 
 848                         'realfeelmax' => $session['realfeelmax'] !== null ? $session['realfeelmax'].' °C' : $session['realfeelmax'], 
 849                         'temperature' => $session['temperature'] !== null ? $session['temperature'].' °C' : $session['temperature'], 
 850                         'temperaturemin' => $session['temperaturemin'] !== null ? $session['temperaturemin'].' °C' : $session['temperaturemin'], 
 851                         'temperaturemax' => $session['temperaturemax'] !== null ? $session['temperaturemax'].' °C' : $session['temperaturemax'], 
 852                         'locked' => $session['locked'], 
 853                         'created' => $session['created'], 
 854                         'updated' => $session['updated'], 
 855                         'title' => $this->translator
->trans('Session %id%', ['%id%' => $id]), 
 856                         'application' => null, 
 858                                 'id' => $session['l_id'], 
 859                                 'at' => $this->translator
->trans('at '.$session['l_title']), 
 860                                 'short' => $this->translator
->trans($session['l_short']), 
 861                                 'title' => $this->translator
->trans($session['l_title']), 
 862                                 'address' => $session['l_address'], 
 863                                 'zipcode' => $session['l_zipcode'], 
 864                                 'city' => $session['l_city'], 
 865                                 'latitude' => $session['l_latitude'], 
 866                                 'longitude' => $session['l_longitude'] 
 869                                 'id' => $session['t_id'], 
 870                                 'title' => $this->translator
->trans($session['t_title']) 
 872                         'applications' => null 
 876                 if (!empty($session['a_id'])) { 
 877                         $context['session']['application'] = [ 
 879                                         'id' => $session['au_id'], 
 880                                         'title' => $session['au_pseudonym'] 
 882                                 'id' => $session['a_id'], 
 883                                 'title' => $this->translator
->trans('Application %id%', [ '%id%' => $session['a_id'] ]), 
 888                 if (!empty($session['sa_id'])) { 
 889                         //Extract applications id 
 890                         $session['sa_id'] = explode("\n", $session['sa_id']); 
 891                         //Extract applications score 
 892                         //XXX: score may be null before grant or for bad behaviour, replace NULL with 'NULL' to avoid silent drop in mysql 
 893                         $session['sa_score'] = array_map(function($v){return $v
==='NULL'?null:$v
;}, explode("\n", $session['sa_score'])); 
 894                         //Extract applications created 
 895                         $session['sa_created'] = array_map(function($v){return new \
DateTime($v
);}, explode("\n", $session['sa_created'])); 
 896                         //Extract applications updated 
 897                         $session['sa_updated'] = array_map(function($v){return new \
DateTime($v
);}, explode("\n", $session['sa_updated'])); 
 898                         //Extract applications canceled 
 899                         //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql 
 900                         $session['sa_canceled'] = array_map(function($v){return $v
==='NULL'?null:new \
DateTime($v
);}, explode("\n", $session['sa_canceled'])); 
 902                         //Extract applications user id 
 903                         $session['sau_id'] = explode("\n", $session['sau_id']); 
 904                         //Extract applications user pseudonym 
 905                         $session['sau_pseudonym'] = explode("\n", $session['sau_pseudonym']); 
 908                         $context['session']['applications'] = []; 
 909                         foreach($session['sa_id'] as $i => $sa_id) { 
 910                                 $context['session']['applications'][$sa_id] = [ 
 912                                         'score' => $session['sa_score'][$i], 
 913                                         'created' => $session['sa_created'][$i], 
 914                                         'updated' => $session['sa_updated'][$i], 
 915                                         'canceled' => $session['sa_canceled'][$i] 
 917                                 if (!empty($session['sau_id'][$i])) { 
 918                                         $context['session']['applications'][$sa_id]['user'] = [ 
 919                                                 'id' => $session['sau_id'][$i], 
 920                                                 'title' => $session['sau_pseudonym'][$i] 
 927                 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+
$context+
$this->context
);