]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/SessionController.php
Remove X-Robot: noindex header
[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 * List all sessions
13 *
14 * @desc Display all sessions with an application or login form
15 *
16 * @param Request $request The request instance
17 *
18 * @return Response The rendered view
19 */
20 public function index(Request $request = null) {
21 //Fetch doctrine
22 $doctrine = $this->getDoctrine();
23
24 //Set section
25 $section = $this->translator->trans('Sessions');
26
27 //Set title
28 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
29
30 //Init context
31 $context = [];
32
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, [
37 //Set the action
38 'action' => $this->generateUrl('rapsys_air_application_add'),
39 //Set the form attribute
40 'attr' => [ 'class' => 'col' ],
41 //Set admin
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)
48 ]);
49
50 //Add form to context
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, [
56 //Set the action
57 'action' => $this->generateUrl('rapsys_user_login'),
58 //Set the form attribute
59 'attr' => [ 'class' => 'col' ]
60 ]);
61
62 //Add form to context
63 $context['login'] = $login->createView();
64 }
65
66 //Compute period
67 $period = new \DatePeriod(
68 //Start from first monday of week
69 new \DateTime('Monday this week'),
70 //Iterate on each day
71 new \DateInterval('P1D'),
72 //End with next sunday and 4 weeks
73 new \DateTime('Monday this week + 5 week')
74 );
75
76 //Fetch calendar
77 //TODO: highlight with current session route parameter
78 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'));
79
80 //Fetch locations
81 $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period);
82
83 //Render the view
84 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
85 }
86
87 /**
88 * Display session
89 *
90 * @desc Display session by id with an application or login form
91 *
92 * @param Request $request The request instance
93 * @param int $id The session id
94 *
95 * @return Response The rendered view
96 */
97 public function view(Request $request, $id) {
98 //Fetch doctrine
99 $doctrine = $this->getDoctrine();
100
101 //Fetch session
102 //TODO: genereate a custom request to fetch everything in a single request ???
103 $session = $doctrine->getRepository(Session::class)->findOneById($id);
104
105 //Fetch session
106 //TODO: genereate a custom request to fetch everything in a single request ???
107 $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
108
109 //Set section
110 //TODO: replace with $session['location']['title']
111 $section = $this->translator->trans($location);
112
113 //Set title
114 $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator->trans($this->config['site']['title']);
115
116 //Init context
117 $context = [];
118
119 //Create application form for role_guest
120 if ($this->isGranted('ROLE_GUEST')) {
121 //Create ApplicationType form
122 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
123 //Set the action
124 'action' => $this->generateUrl('rapsys_air_application_add'),
125 //Set the form attribute
126 'attr' => [ 'class' => 'col' ],
127 //Set admin
128 'admin' => $this->isGranted('ROLE_ADMIN'),
129 //Set default user to current
130 'user' => $this->getUser()->getId(),
131 //Set default slot to evening
132 //XXX: default to Evening (3)
133 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
134 ]);
135
136 //Add form to context
137 $context['application'] = $application->createView();
138 //Create login form for anonymous
139 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
140 //Create ApplicationType form
141 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
142 //Set the action
143 'action' => $this->generateUrl('rapsys_user_login'),
144 //Set the form attribute
145 'attr' => [ 'class' => 'col' ]
146 ]);
147
148 //Add form to context
149 $context['login'] = $login->createView();
150 }
151
152 //Extract session date
153 $sessionDate = $session->getDate();
154
155 //Init session begin and end
156 $sessionBegin = $sessionEnd = null;
157
158 //Check session begin and end availability
159 if (($sessionBegin = $session->getBegin()) && ($sessionLength = $session->getLength())) {
160 $sessionBegin = new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP'));
161 #$sessionEnd = (new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP')))->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
162 $sessionEnd = (clone $sessionBegin)->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
163 }
164
165 //Add session in context
166 $context['session'] = [
167 'id' => ($sessionId = $session->getId()),
168 'date' => $sessionDate,
169 'begin' => $sessionBegin,
170 'end' => $sessionEnd,
171 #'begin' => new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')),
172 #'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'))),
173 #'length' => $session->getLength(),
174 'created' => $session->getCreated(),
175 'updated' => $session->getUpdated(),
176 'title' => $this->translator->trans('Session %id%', ['%id%' => $sessionId]),
177 'application' => null,
178 'location' => [
179 'id' => ($location = $session->getLocation())->getId(),
180 'title' => $this->translator->trans($location),
181 ],
182 'slot' => [
183 'id' => ($slot = $session->getSlot())->getId(),
184 'title' => $this->translator->trans($slot),
185 ],
186 'applications' => null,
187 ];
188
189 if ($application = $session->getApplication()) {
190 $context['session']['application'] = [
191 'user' => [
192 'id' => ($user = $application->getUser())->getId(),
193 'title' => (string) $user->getPseudonym(),
194 ],
195 'id' => ($applicationId = $application->getId()),
196 'title' => $this->translator->trans('Application %id%', [ '%id%' => $applicationId ]),
197 ];
198 }
199
200 if ($applications = $session->getApplications()) {
201 $context['session']['applications'] = [];
202 foreach($applications as $application) {
203 $context['session']['applications'][$applicationId = $application->getId()] = [
204 'user' => null,
205 'created' => $application->getCreated(),
206 'updated' => $application->getUpdated(),
207 ];
208 if ($user = $application->getUser()) {
209 $context['session']['applications'][$applicationId]['user'] = [
210 'id' => $user->getId(),
211 'title' => (string) $user->getPseudonym(),
212 ];
213 }
214 }
215 }
216
217 //Add location in context
218 #$context['location'] = [
219 # 'id' => $location->getId(),
220 # 'title' => $location->getTitle(),
221 #];
222
223 //Render the view
224 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+$context+$this->context);
225 }
226 }