From 33c1d5701a7dc0e6e54fc0fd016782811d1d4f95 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Wed, 14 Oct 2020 03:50:48 +0200 Subject: [PATCH] Add session controller with view function --- Controller/SessionController.php | 150 +++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 Controller/SessionController.php diff --git a/Controller/SessionController.php b/Controller/SessionController.php new file mode 100644 index 0000000..c184f4d --- /dev/null +++ b/Controller/SessionController.php @@ -0,0 +1,150 @@ +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); + + //Set section + //TODO: replace with $session['location']['title'] + $section = $this->translator->trans($location); + + //Set title + $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$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' => $this->getDoctrine()->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(); + } + + //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]), + 'application' => null, + 'location' => [ + 'id' => ($location = $session->getLocation())->getId(), + 'title' => $this->translator->trans($location), + ], + 'slot' => [ + 'id' => ($slot = $session->getSlot())->getId(), + 'title' => $this->translator->trans($slot), + ], + 'applications' => null, + ]; + + if ($application = $session->getApplication()) { + $context['session']['application'] = [ + 'user' => [ + 'id' => ($user = $application->getUser())->getId(), + 'title' => (string) $user->getPseudonym(), + ], + 'id' => ($applicationId = $application->getId()), + 'title' => $this->translator->trans('Application %id%', [ '%id%' => $applicationId ]), + ]; + } + + if ($applications = $session->getApplications()) { + $context['session']['applications'] = []; + foreach($applications as $application) { + $context['session']['applications'][$applicationId = $application->getId()] = [ + 'user' => null, + 'created' => $application->getCreated(), + 'updated' => $application->getUpdated(), + ]; + if ($user = $application->getUser()) { + $context['session']['applications'][$applicationId]['user'] = [ + 'id' => $user->getId(), + 'title' => (string) $user->getPseudonym(), + ]; + } + } + } + + //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); + } +} -- 2.41.1