]> Raphaël G. Git Repositories - airbundle/blob - Controller/SessionController.php
New layout
[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'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
79
80 //Fetch locations
81 //XXX: we want to display all active locations anyway
82 $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/);
83
84 //Render the view
85 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
86 }
87
88 /**
89 * Display session
90 *
91 * @desc Display session by id with an application or login form
92 *
93 * @param Request $request The request instance
94 * @param int $id The session id
95 *
96 * @return Response The rendered view
97 */
98 public function view(Request $request, $id) {
99 //Fetch doctrine
100 $doctrine = $this->getDoctrine();
101
102 //Fetch session
103 $session = $doctrine->getRepository(Session::class)->fetchOneById($id);
104
105 //Set section
106 $section = $this->translator->trans($session['l_title']);
107
108 //Set title
109 $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator->trans($this->config['site']['title']);
110
111 //Init context
112 $context = [];
113
114 //Create application form for role_guest
115 if ($this->isGranted('ROLE_GUEST')) {
116 //Create ApplicationType form
117 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
118 //Set the action
119 'action' => $this->generateUrl('rapsys_air_application_add'),
120 //Set the form attribute
121 'attr' => [ 'class' => 'col' ],
122 //Set admin
123 'admin' => $this->isGranted('ROLE_ADMIN'),
124 //Set default user to current
125 'user' => $this->getUser()->getId(),
126 //Set default slot to current
127 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById($session['t_id']),
128 //Set default location to current
129 'location' => $this->getDoctrine()->getRepository(Location::class)->findOneById($session['l_id']),
130 ]);
131
132 //Add form to context
133 $context['application'] = $application->createView();
134 //Create login form for anonymous
135 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
136 //Create ApplicationType form
137 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
138 //Set the action
139 'action' => $this->generateUrl('rapsys_user_login'),
140 //Set the form attribute
141 'attr' => [ 'class' => 'col' ]
142 ]);
143
144 //Add form to context
145 $context['login'] = $login->createView();
146 }
147
148 //Add session in context
149 $context['session'] = [
150 'id' => $id,
151 'date' => $session['date'],
152 'begin' => $session['begin'],
153 'start' => $session['start'],
154 'length' => $session['length'],
155 'stop' => $session['stop'],
156 'rainfall' => $session['rainfall'] !== null ? $session['rainfall'].' mm' : $session['rainfall'],
157 'rainrisk' => $session['rainrisk'] !== null ? ($session['rainrisk']*100).' %' : $session['rainrisk'],
158 'realfeel' => $session['realfeel'] !== null ? $session['realfeel'].' °C' : $session['realfeel'],
159 'realfeelmin' => $session['realfeelmin'] !== null ? $session['realfeelmin'].' °C' : $session['realfeelmin'],
160 'realfeelmax' => $session['realfeelmax'] !== null ? $session['realfeelmax'].' °C' : $session['realfeelmax'],
161 'temperature' => $session['temperature'] !== null ? $session['temperature'].' °C' : $session['temperature'],
162 'temperaturemin' => $session['temperaturemin'] !== null ? $session['temperaturemin'].' °C' : $session['temperaturemin'],
163 'temperaturemax' => $session['temperaturemax'] !== null ? $session['temperaturemax'].' °C' : $session['temperaturemax'],
164 'created' => $session['created'],
165 'updated' => $session['updated'],
166 'title' => $this->translator->trans('Session %id%', ['%id%' => $id]),
167 'application' => null,
168 'location' => [
169 'id' => $session['l_id'],
170 'at' => $this->translator->trans('at '.$session['l_title']),
171 'short' => $this->translator->trans($session['l_short']),
172 'title' => $this->translator->trans($session['l_title']),
173 'address' => $session['l_address'],
174 'zipcode' => $session['l_zipcode'],
175 'city' => $session['l_city'],
176 'latitude' => $session['l_latitude'],
177 'longitude' => $session['l_longitude']
178 ],
179 'slot' => [
180 'id' => $session['t_id'],
181 'title' => $this->translator->trans($session['t_title'])
182 ],
183 'applications' => null
184 ];
185
186 //With application
187 if (!empty($session['a_id'])) {
188 $context['session']['application'] = [
189 'user' => [
190 'id' => $session['au_id'],
191 'title' => $session['au_pseudonym']
192 ],
193 'id' => $session['a_id'],
194 'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]),
195 ];
196 }
197
198 //With applications
199 if (!empty($session['sa_id'])) {
200 //Extract applications id
201 $session['sa_id'] = explode("\n", $session['sa_id']);
202 //Extract applications score
203 //XXX: score may be null before grant or for bad behaviour, replace NULL with 'NULL' to avoid silent drop in mysql
204 $session['sa_score'] = array_map(function($v){return $v==='NULL'?null:$v;}, explode("\n", $session['sa_score']));
205 //Extract applications created
206 $session['sa_created'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_created']));
207 //Extract applications updated
208 $session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated']));
209 //Extract applications canceled
210 //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql
211 $session['sa_canceled'] = array_map(function($v){return $v==='NULL'?null:$v;}, explode("\n", $session['sa_canceled']));
212
213 //Extract applications user id
214 $session['sau_id'] = explode("\n", $session['sau_id']);
215 //Extract applications user pseudonym
216 $session['sau_pseudonym'] = explode("\n", $session['sau_pseudonym']);
217
218 //Init applications
219 $context['session']['applications'] = [];
220 foreach($session['sa_id'] as $i => $sa_id) {
221 $context['session']['applications'][$sa_id] = [
222 'user' => null,
223 'score' => $session['sa_score'][$i],
224 'created' => $session['sa_created'][$i],
225 'updated' => $session['sa_updated'][$i],
226 'canceled' => $session['sa_canceled'][$i]
227 ];
228 if (!empty($session['sau_id'][$i])) {
229 $context['session']['applications'][$sa_id]['user'] = [
230 'id' => $session['sau_id'][$i],
231 'title' => $session['sau_pseudonym'][$i]
232 ];
233 }
234 }
235 }
236
237 //Render the view
238 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+$context+$this->context);
239 }
240 }