]> Raphaël G. Git Repositories - airbundle/blobdiff - Controller/SessionController.php
Block unsupported session timing for non admin
[airbundle] / Controller / SessionController.php
index c184f4de8117588cd350bb717f8adb14a6247e08..7bdb11775b751546867b5a082f72a75ebe437898 100644 (file)
@@ -8,6 +8,83 @@ use Rapsys\AirBundle\Entity\Session;
 use Rapsys\AirBundle\Entity\Location;
 
 class SessionController extends DefaultController {
+       /**
+        * List all sessions
+        *
+        * @desc Display all sessions with an application or login form
+        *
+        * @param Request $request The request instance
+        *
+        * @return Response The rendered view
+        */
+       public function index(Request $request = null) {
+               //Fetch doctrine
+               $doctrine = $this->getDoctrine();
+
+               //Set section
+               $section = $this->translator->trans('Sessions');
+
+               //Set title
+               $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
+
+               //Init context
+               $context = [];
+
+               //Create application form for role_guest
+               if ($this->isGranted('ROLE_GUEST')) {
+                       //Create ApplicationType form
+                       $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
+                               //Set the action
+                               'action' => $this->generateUrl('rapsys_air_application_add'),
+                               //Set the form attribute
+                               'attr' => [ 'class' => 'col' ],
+                               //Set admin
+                               'admin' => $this->isGranted('ROLE_ADMIN'),
+                               //Set default user to current
+                               'user' => $this->getUser()->getId(),
+                               //Set default slot to evening
+                               //XXX: default to Evening (3)
+                               'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
+                       ]);
+
+                       //Add form to context
+                       $context['application'] = $application->createView();
+               //Create login form for anonymous
+               } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
+                       //Create ApplicationType form
+                       $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
+                               //Set the action
+                               'action' => $this->generateUrl('rapsys_user_login'),
+                               //Set the form attribute
+                               'attr' => [ 'class' => 'col' ]
+                       ]);
+
+                       //Add form to context
+                       $context['login'] = $login->createView();
+               }
+
+               //Compute period
+               $period = new \DatePeriod(
+                       //Start from first monday of week
+                       new \DateTime('Monday this week'),
+                       //Iterate on each day
+                       new \DateInterval('P1D'),
+                       //End with next sunday and 4 weeks
+                       new \DateTime('Monday this week + 5 week')
+               );
+
+               //Fetch calendar
+               //TODO: highlight with current session route parameter
+               $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
+
+               //Fetch locations
+               //XXX: we want to display all active locations anyway
+               $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/);
+
+               //Render the view
+               return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
+       }
+
        /**
         * Display session
         *
@@ -23,16 +100,10 @@ class SessionController extends DefaultController {
                $doctrine = $this->getDoctrine();
 
                //Fetch session
-               //TODO: genereate a custom request to fetch everything in a single request ???
-               $session = $doctrine->getRepository(Session::class)->findOneById($id);
-
-               //Fetch session
-               //TODO: genereate a custom request to fetch everything in a single request ???
-               $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
+               $session = $doctrine->getRepository(Session::class)->fetchOneById($id);
 
                //Set section
-               //TODO: replace with $session['location']['title']
-               $section = $this->translator->trans($location);
+               $section = $this->translator->trans($session['l_title']);
 
                //Set title
                $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator->trans($this->config['site']['title']);
@@ -52,9 +123,10 @@ class SessionController extends DefaultController {
                                'admin' => $this->isGranted('ROLE_ADMIN'),
                                //Set default user to current
                                'user' => $this->getUser()->getId(),
-                               //Set default slot to evening
-                               //XXX: default to Evening (3)
-                               'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
+                               //Set default slot to current
+                               'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById($session['t_id']),
+                               //Set default location to current
+                               'location' => $this->getDoctrine()->getRepository(Location::class)->findOneById($session['l_id']),
                        ]);
 
                        //Add form to context
@@ -73,77 +145,95 @@ class SessionController extends DefaultController {
                        $context['login'] = $login->createView();
                }
 
-               //Extract session date
-               $sessionDate = $session->getDate();
-
-               //Init session begin and end
-               $sessionBegin = $sessionEnd = null;
-
-               //Check session begin and end availability
-               if (($sessionBegin = $session->getBegin()) && ($sessionLength = $session->getLength())) {
-                       $sessionBegin = new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP'));
-                       #$sessionEnd = (new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP')))->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
-                       $sessionEnd = (clone $sessionBegin)->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
-               }
-
                //Add session in context
                $context['session'] = [
-                       'id' => ($sessionId = $session->getId()),
-                       'date' => $sessionDate,
-                       'begin' => $sessionBegin,
-                       'end' => $sessionEnd,
-                       #'begin' => new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')),
-                       #'end' => (new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')))->add(new \DateInterval($session->getLength()->format('\P\TH\Hi\Ms\S'))),
-                       #'length' => $session->getLength(),
-                       'created' => $session->getCreated(),
-                       'updated' => $session->getUpdated(),
-                       'title' => $this->translator->trans('Session %id%', ['%id%' => $sessionId]),
+                       'id' => $id,
+                       'date' => $session['date'],
+                       'begin' => $session['begin'],
+                       'start' => $session['start'],
+                       'length' => $session['length'],
+                       'stop' => $session['stop'],
+                       'rainfall' => $session['rainfall'] !== null ? $session['rainfall'].' mm' : $session['rainfall'],
+                       'rainrisk' => $session['rainrisk'] !== null ? ($session['rainrisk']*100).' %' : $session['rainrisk'],
+                       'realfeel' => $session['realfeel'] !== null ? $session['realfeel'].' °C' : $session['realfeel'],
+                       'realfeelmin' => $session['realfeelmin'] !== null ? $session['realfeelmin'].' °C' : $session['realfeelmin'],
+                       'realfeelmax' => $session['realfeelmax'] !== null ? $session['realfeelmax'].' °C' : $session['realfeelmax'],
+                       'temperature' => $session['temperature'] !== null ? $session['temperature'].' °C' : $session['temperature'],
+                       'temperaturemin' => $session['temperaturemin'] !== null ? $session['temperaturemin'].' °C' : $session['temperaturemin'],
+                       'temperaturemax' => $session['temperaturemax'] !== null ? $session['temperaturemax'].' °C' : $session['temperaturemax'],
+                       'created' => $session['created'],
+                       'updated' => $session['updated'],
+                       'title' => $this->translator->trans('Session %id%', ['%id%' => $id]),
                        'application' => null,
                        'location' => [
-                               'id' => ($location = $session->getLocation())->getId(),
-                               'title' => $this->translator->trans($location),
+                               'id' => $session['l_id'],
+                               'at' => $this->translator->trans('at '.$session['l_title']),
+                               'short' => $this->translator->trans($session['l_short']),
+                               'title' => $this->translator->trans($session['l_title']),
+                               'address' => $session['l_address'],
+                               'zipcode' => $session['l_zipcode'],
+                               'city' => $session['l_city'],
+                               'latitude' => $session['l_latitude'],
+                               'longitude' => $session['l_longitude']
                        ],
                        'slot' => [
-                               'id' => ($slot = $session->getSlot())->getId(),
-                               'title' => $this->translator->trans($slot),
+                               'id' => $session['t_id'],
+                               'title' => $this->translator->trans($session['t_title'])
                        ],
-                       'applications' => null,
+                       'applications' => null
                ];
 
-               if ($application = $session->getApplication()) {
+               //With application
+               if (!empty($session['a_id'])) {
                        $context['session']['application'] = [
                                'user' => [
-                                       'id' => ($user = $application->getUser())->getId(),
-                                       'title' => (string) $user->getPseudonym(),
+                                       'id' => $session['au_id'],
+                                       'title' => $session['au_pseudonym']
                                ],
-                               'id' => ($applicationId = $application->getId()),
-                               'title' => $this->translator->trans('Application %id%', [ '%id%' => $applicationId ]),
+                               'id' => $session['a_id'],
+                               'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]),
                        ];
                }
 
-               if ($applications = $session->getApplications()) {
+               //With applications
+               if (!empty($session['sa_id'])) {
+                       //Extract applications id
+                       $session['sa_id'] = explode("\n", $session['sa_id']);
+                       //Extract applications score
+                       //XXX: score may be null before grant or for bad behaviour, replace NULL with 'NULL' to avoid silent drop in mysql
+                       $session['sa_score'] = array_map(function($v){return $v==='NULL'?null:$v;}, explode("\n", $session['sa_score']));
+                       //Extract applications created
+                       $session['sa_created'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_created']));
+                       //Extract applications updated
+                       $session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated']));
+                       //Extract applications canceled
+                       //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql
+                       $session['sa_canceled'] = array_map(function($v){return $v==='NULL'?null:$v;}, explode("\n", $session['sa_canceled']));
+
+                       //Extract applications user id
+                       $session['sau_id'] = explode("\n", $session['sau_id']);
+                       //Extract applications user pseudonym
+                       $session['sau_pseudonym'] = explode("\n", $session['sau_pseudonym']);
+
+                       //Init applications
                        $context['session']['applications'] = [];
-                       foreach($applications as $application) {
-                               $context['session']['applications'][$applicationId = $application->getId()] = [
+                       foreach($session['sa_id'] as $i => $sa_id) {
+                               $context['session']['applications'][$sa_id] = [
                                        'user' => null,
-                                       'created' => $application->getCreated(),
-                                       'updated' => $application->getUpdated(),
+                                       'score' => $session['sa_score'][$i],
+                                       'created' => $session['sa_created'][$i],
+                                       'updated' => $session['sa_updated'][$i],
+                                       'canceled' => $session['sa_canceled'][$i]
                                ];
-                               if ($user = $application->getUser()) {
-                                       $context['session']['applications'][$applicationId]['user'] = [
-                                               'id' => $user->getId(),
-                                               'title' => (string) $user->getPseudonym(),
+                               if (!empty($session['sau_id'][$i])) {
+                                       $context['session']['applications'][$sa_id]['user'] = [
+                                               'id' => $session['sau_id'][$i],
+                                               'title' => $session['sau_pseudonym'][$i]
                                        ];
                                }
                        }
                }
 
-               //Add location in context
-               #$context['location'] = [
-               #       'id' => $location->getId(),
-               #       'title' => $location->getTitle(),
-               #];
-
                //Render the view
                return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+$context+$this->context);
        }