]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/SessionController.php
Add location.view, session.index and user.view twig templates
[airbundle] / Controller / SessionController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Rapsys\AirBundle\Entity\Slot;
7 use Rapsys\AirBundle\Entity\Session;
8 use Rapsys\AirBundle\Entity\Location;
9
10 class SessionController extends DefaultController {
11 /**
12 * Display session
13 *
14 * @desc Display session by id with an application or login form
15 *
16 * @param Request $request The request instance
17 * @param int $id The session id
18 *
19 * @return Response The rendered view
20 */
21 public function view(Request $request, $id) {
22 //Fetch doctrine
23 $doctrine = $this->getDoctrine();
24
25 //Fetch session
26 //TODO: genereate a custom request to fetch everything in a single request ???
27 $session = $doctrine->getRepository(Session::class)->findOneById($id);
28
29 //Fetch session
30 //TODO: genereate a custom request to fetch everything in a single request ???
31 $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
32
33 //Set section
34 //TODO: replace with $session['location']['title']
35 $section = $this->translator->trans($location);
36
37 //Set title
38 $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator->trans($this->config['site']['title']);
39
40 //Init context
41 $context = [];
42
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, [
47 //Set the action
48 'action' => $this->generateUrl('rapsys_air_application_add'),
49 //Set the form attribute
50 'attr' => [ 'class' => 'col' ],
51 //Set admin
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)
58 ]);
59
60 //Add form to context
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, [
66 //Set the action
67 'action' => $this->generateUrl('rapsys_user_login'),
68 //Set the form attribute
69 'attr' => [ 'class' => 'col' ]
70 ]);
71
72 //Add form to context
73 $context['login'] = $login->createView();
74 }
75
76 //Extract session date
77 $sessionDate = $session->getDate();
78
79 //Init session begin and end
80 $sessionBegin = $sessionEnd = null;
81
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')));
87 }
88
89 //Add session in context
90 $context['session'] = [
91 'id' => ($sessionId = $session->getId()),
92 'date' => $sessionDate,
93 'begin' => $sessionBegin,
94 'end' => $sessionEnd,
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,
102 'location' => [
103 'id' => ($location = $session->getLocation())->getId(),
104 'title' => $this->translator->trans($location),
105 ],
106 'slot' => [
107 'id' => ($slot = $session->getSlot())->getId(),
108 'title' => $this->translator->trans($slot),
109 ],
110 'applications' => null,
111 ];
112
113 if ($application = $session->getApplication()) {
114 $context['session']['application'] = [
115 'user' => [
116 'id' => ($user = $application->getUser())->getId(),
117 'title' => (string) $user->getPseudonym(),
118 ],
119 'id' => ($applicationId = $application->getId()),
120 'title' => $this->translator->trans('Application %id%', [ '%id%' => $applicationId ]),
121 ];
122 }
123
124 if ($applications = $session->getApplications()) {
125 $context['session']['applications'] = [];
126 foreach($applications as $application) {
127 $context['session']['applications'][$applicationId = $application->getId()] = [
128 'user' => null,
129 'created' => $application->getCreated(),
130 'updated' => $application->getUpdated(),
131 ];
132 if ($user = $application->getUser()) {
133 $context['session']['applications'][$applicationId]['user'] = [
134 'id' => $user->getId(),
135 'title' => (string) $user->getPseudonym(),
136 ];
137 }
138 }
139 }
140
141 //Add location in context
142 #$context['location'] = [
143 # 'id' => $location->getId(),
144 # 'title' => $location->getTitle(),
145 #];
146
147 //Render the view
148 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+$context+$this->context);
149 }
150 }