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 session by id with an application or login form
16 * @param Request $request The request instance
17 * @param int $id The session id
19 * @return Response The rendered view
21 public function view(Request
$request, $id) {
23 $doctrine = $this->getDoctrine();
26 //TODO: genereate a custom request to fetch everything in a single request ???
27 $session = $doctrine->getRepository(Session
::class)->findOneById($id);
30 //TODO: genereate a custom request to fetch everything in a single request ???
31 $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
34 //TODO: replace with $session['location']['title']
35 $section = $this->translator
->trans($location);
38 $title = $this->translator
->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator
->trans($this->config
['site']['title']);
43 //Create application form for role_guest
44 if ($this->isGranted('ROLE_GUEST')) {
45 //Create ApplicationType form
46 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
48 'action' => $this->generateUrl('rapsys_air_application_add'),
49 //Set the form attribute
50 'attr' => [ 'class' => 'col' ],
52 'admin' => $this->isGranted('ROLE_ADMIN'),
53 //Set default user to current
54 'user' => $this->getUser()->getId(),
55 //Set default slot to evening
56 //XXX: default to Evening (3)
57 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3)
61 $context['application'] = $application->createView();
62 //Create login form for anonymous
63 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
64 //Create ApplicationType form
65 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
67 'action' => $this->generateUrl('rapsys_user_login'),
68 //Set the form attribute
69 'attr' => [ 'class' => 'col' ]
73 $context['login'] = $login->createView();
76 //Extract session date
77 $sessionDate = $session->getDate();
79 //Init session begin and end
80 $sessionBegin = $sessionEnd = null;
82 //Check session begin and end availability
83 if (($sessionBegin = $session->getBegin()) && ($sessionLength = $session->getLength())) {
84 $sessionBegin = new \
DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP'));
85 #$sessionEnd = (new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP')))->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
86 $sessionEnd = (clone $sessionBegin)->add(new \
DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
89 //Add session in context
90 $context['session'] = [
91 'id' => ($sessionId = $session->getId()),
92 'date' => $sessionDate,
93 'begin' => $sessionBegin,
95 #'begin' => new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')),
96 #'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'))),
97 #'length' => $session->getLength(),
98 'created' => $session->getCreated(),
99 'updated' => $session->getUpdated(),
100 'title' => $this->translator
->trans('Session %id%', ['%id%' => $sessionId]),
101 'application' => null,
103 'id' => ($location = $session->getLocation())->getId(),
104 'title' => $this->translator
->trans($location),
107 'id' => ($slot = $session->getSlot())->getId(),
108 'title' => $this->translator
->trans($slot),
110 'applications' => null,
113 if ($application = $session->getApplication()) {
114 $context['session']['application'] = [
116 'id' => ($user = $application->getUser())->getId(),
117 'title' => (string) $user->getPseudonym(),
119 'id' => ($applicationId = $application->getId()),
120 'title' => $this->translator
->trans('Application %id%', [ '%id%' => $applicationId ]),
124 if ($applications = $session->getApplications()) {
125 $context['session']['applications'] = [];
126 foreach($applications as $application) {
127 $context['session']['applications'][$applicationId = $application->getId()] = [
129 'created' => $application->getCreated(),
130 'updated' => $application->getUpdated(),
132 if ($user = $application->getUser()) {
133 $context['session']['applications'][$applicationId]['user'] = [
134 'id' => $user->getId(),
135 'title' => (string) $user->getPseudonym(),
141 //Add location in context
142 #$context['location'] = [
143 # 'id' => $location->getId(),
144 # 'title' => $location->getTitle(),
148 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+
$context+
$this->context
);