3 namespace Rapsys\AirBundle\Controller
; 
   5 use Symfony\Component\HttpFoundation\Request
; 
   6 use Rapsys\AirBundle\Entity\Slot
; 
   7 use Rapsys\AirBundle\Entity\Session
; 
   8 use Rapsys\AirBundle\Entity\Location
; 
  10 class SessionController 
extends DefaultController 
{ 
  14          * @desc Display all sessions with an application or login form 
  16          * @param Request $request The request instance 
  18          * @return Response The rendered view 
  20         public function index(Request 
$request = null) { 
  22                 $doctrine = $this->getDoctrine(); 
  25                 $section = $this->translator
->trans('Sessions'); 
  28                 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
  33                 //Create application form for role_guest 
  34                 if ($this->isGranted('ROLE_GUEST')) { 
  35                         //Create ApplicationType form 
  36                         $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
  38                                 'action' => $this->generateUrl('rapsys_air_application_add'), 
  39                                 //Set the form attribute 
  40                                 'attr' => [ 'class' => 'col' ], 
  42                                 'admin' => $this->isGranted('ROLE_ADMIN'), 
  43                                 //Set default user to current 
  44                                 'user' => $this->getUser()->getId(), 
  45                                 //Set default slot to evening 
  46                                 //XXX: default to Evening (3) 
  47                                 'slot' => $doctrine->getRepository(Slot
::class)->findOneById(3) 
  51                         $context['application'] = $application->createView(); 
  52                 //Create login form for anonymous 
  53                 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
  54                         //Create ApplicationType form 
  55                         $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ 
  57                                 'action' => $this->generateUrl('rapsys_user_login'), 
  58                                 //Set the form attribute 
  59                                 'attr' => [ 'class' => 'col' ] 
  63                         $context['login'] = $login->createView(); 
  67                 $period = new \
DatePeriod( 
  68                         //Start from first monday of week 
  69                         new \
DateTime('Monday this week'), 
  71                         new \
DateInterval('P1D'), 
  72                         //End with next sunday and 4 weeks 
  73                         new \
DateTime('Monday this week + 5 week') 
  77                 //TODO: highlight with current session route parameter 
  78                 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')); 
  81                 //XXX: we want to display all active locations anyway 
  82                 $locations = $doctrine->getRepository(Location
::class)->fetchTranslatedLocationByDatePeriod($this->translator
, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/); 
  85                 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$context+
$this->context
); 
  91          * @desc Display session by id with an application or login form 
  93          * @param Request $request The request instance 
  94          * @param int $id The session id 
  96          * @return Response The rendered view 
  98         public function view(Request 
$request, $id) { 
 100                 $doctrine = $this->getDoctrine(); 
 103                 $session = $doctrine->getRepository(Session
::class)->fetchOneById($id); 
 106                 $section = $this->translator
->trans($session['l_title']); 
 109                 $title = $this->translator
->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator
->trans($this->config
['site']['title']); 
 114                 //Create application form for role_guest 
 115                 if ($this->isGranted('ROLE_GUEST')) { 
 116                         //Create ApplicationType form 
 117                         $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 119                                 'action' => $this->generateUrl('rapsys_air_application_add'), 
 120                                 //Set the form attribute 
 121                                 'attr' => [ 'class' => 'col' ], 
 123                                 'admin' => $this->isGranted('ROLE_ADMIN'), 
 124                                 //Set default user to current 
 125                                 'user' => $this->getUser()->getId(), 
 126                                 //Set default slot to current 
 127                                 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById($session['t_id']), 
 128                                 //Set default location to current 
 129                                 'location' => $this->getDoctrine()->getRepository(Location
::class)->findOneById($session['l_id']), 
 132                         //Add form to context 
 133                         $context['application'] = $application->createView(); 
 134                 //Create login form for anonymous 
 135                 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
 136                         //Create ApplicationType form 
 137                         $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ 
 139                                 'action' => $this->generateUrl('rapsys_user_login'), 
 140                                 //Set the form attribute 
 141                                 'attr' => [ 'class' => 'col' ] 
 144                         //Add form to context 
 145                         $context['login'] = $login->createView(); 
 148                 //Add session in context 
 149                 $context['session'] = [ 
 151                         'date' => $session['date'], 
 152                         'begin' => $session['begin'], 
 153                         'start' => $session['start'], 
 154                         'length' => $session['length'], 
 155                         'stop' => $session['stop'], 
 156                         'rainfall' => $session['rainfall'] !== null ? $session['rainfall'].' mm' : $session['rainfall'], 
 157                         'rainrisk' => $session['rainrisk'] !== null ? ($session['rainrisk']*100).' %' : $session['rainrisk'], 
 158                         'realfeel' => $session['realfeel'] !== null ? $session['realfeel'].' °C' : $session['realfeel'], 
 159                         'realfeelmin' => $session['realfeelmin'] !== null ? $session['realfeelmin'].' °C' : $session['realfeelmin'], 
 160                         'realfeelmax' => $session['realfeelmax'] !== null ? $session['realfeelmax'].' °C' : $session['realfeelmax'], 
 161                         'temperature' => $session['temperature'] !== null ? $session['temperature'].' °C' : $session['temperature'], 
 162                         'temperaturemin' => $session['temperaturemin'] !== null ? $session['temperaturemin'].' °C' : $session['temperaturemin'], 
 163                         'temperaturemax' => $session['temperaturemax'] !== null ? $session['temperaturemax'].' °C' : $session['temperaturemax'], 
 164                         'created' => $session['created'], 
 165                         'updated' => $session['updated'], 
 166                         'title' => $this->translator
->trans('Session %id%', ['%id%' => $id]), 
 167                         'application' => null, 
 169                                 'id' => $session['l_id'], 
 170                                 'at' => $this->translator
->trans('at '.$session['l_title']), 
 171                                 'short' => $this->translator
->trans($session['l_short']), 
 172                                 'title' => $this->translator
->trans($session['l_title']), 
 173                                 'address' => $session['l_address'], 
 174                                 'zipcode' => $session['l_zipcode'], 
 175                                 'city' => $session['l_city'], 
 176                                 'latitude' => $session['l_latitude'], 
 177                                 'longitude' => $session['l_longitude'] 
 180                                 'id' => $session['t_id'], 
 181                                 'title' => $this->translator
->trans($session['t_title']) 
 183                         'applications' => null 
 187                 if (!empty($session['a_id'])) { 
 188                         $context['session']['application'] = [ 
 190                                         'id' => $session['au_id'], 
 191                                         'title' => $session['au_pseudonym'] 
 193                                 'id' => $session['a_id'], 
 194                                 'title' => $this->translator
->trans('Application %id%', [ '%id%' => $session['a_id'] ]), 
 199                 if (!empty($session['sa_id'])) { 
 200                         //Extract applications id 
 201                         $session['sa_id'] = explode("\n", $session['sa_id']); 
 202                         //Extract applications score 
 203                         //XXX: score may be null before grant or for bad behaviour, replace NULL with 'NULL' to avoid silent drop in mysql 
 204                         $session['sa_score'] = array_map(function($v){return $v
==='NULL'?null:$v
;}, explode("\n", $session['sa_score'])); 
 205                         //Extract applications created 
 206                         $session['sa_created'] = array_map(function($v){return new \
DateTime($v
);}, explode("\n", $session['sa_created'])); 
 207                         //Extract applications updated 
 208                         $session['sa_updated'] = array_map(function($v){return new \
DateTime($v
);}, explode("\n", $session['sa_updated'])); 
 209                         //Extract applications canceled 
 210                         //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql 
 211                         $session['sa_canceled'] = array_map(function($v){return $v
==='NULL'?null:$v
;}, explode("\n", $session['sa_canceled'])); 
 213                         //Extract applications user id 
 214                         $session['sau_id'] = explode("\n", $session['sau_id']); 
 215                         //Extract applications user pseudonym 
 216                         $session['sau_pseudonym'] = explode("\n", $session['sau_pseudonym']); 
 219                         $context['session']['applications'] = []; 
 220                         foreach($session['sa_id'] as $i => $sa_id) { 
 221                                 $context['session']['applications'][$sa_id] = [ 
 223                                         'score' => $session['sa_score'][$i], 
 224                                         'created' => $session['sa_created'][$i], 
 225                                         'updated' => $session['sa_updated'][$i], 
 226                                         'canceled' => $session['sa_canceled'][$i] 
 228                                 if (!empty($session['sau_id'][$i])) { 
 229                                         $context['session']['applications'][$sa_id]['user'] = [ 
 230                                                 'id' => $session['sau_id'][$i], 
 231                                                 'title' => $session['sau_pseudonym'][$i] 
 238                 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+
$context+
$this->context
);