]> Raphaël G. Git Repositories - airbundle/blob - Controller/SessionController.php
Rename Title in Civility
[airbundle] / Controller / SessionController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\Routing\RequestContext;
7 use Symfony\Component\Routing\Exception\MethodNotAllowedException;
8 use Symfony\Component\Routing\Exception\ResourceNotFoundException;
9 use Rapsys\AirBundle\Entity\Application;
10 use Rapsys\AirBundle\Entity\User;
11 use Rapsys\AirBundle\Entity\Slot;
12 use Rapsys\AirBundle\Entity\Session;
13 use Rapsys\AirBundle\Entity\Location;
14
15 class SessionController extends DefaultController {
16 /**
17 * Edit session
18 *
19 * @desc Persist session and all required dependencies in database
20 *
21 * @param Request $request The request instance
22 *
23 * @return Response The rendered view or redirection
24 *
25 * @throws \RuntimeException When user has not at least guest role
26 */
27 public function edit(Request $request, $id) {
28 //Prevent non-guest to access here
29 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')]));
30
31 //Reject non post requests
32 if (!$request->isMethod('POST')) {
33 throw new \RuntimeException('Request method MUST be POST');
34 }
35
36 //Get doctrine
37 $doctrine = $this->getDoctrine();
38
39 //Fetch session
40 $session = $doctrine->getRepository(Session::class)->fetchOneById($id, $request->getLocale());
41
42 //Check if
43 if (
44 //we are admin
45 !$this->isGranted('ROLE_ADMIN') &&
46 //or attributed user
47 $this->getUser()->getId() != $session['au_id'] &&
48 //or application without attributed user
49 $session['au_id'] !== null && !in_array($this->getUser()->getId(), explode("\n", $session['sau_id']))
50 ) {
51 //Prevent non admin and non attributed user access
52 throw $this->createAccessDeniedException();
53 }
54
55 //Set now
56 $now = new \DateTime('now');
57
58 //Create SessionEditType form
59 $form = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [
60 //Set the action
61 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]),
62 //Set the form attribute
63 'attr' => [],
64 //Set admin
65 'admin' => $this->isGranted('ROLE_ADMIN'),
66 //Set default user to current
67 'user' => $this->getUser()->getId(),
68 //Set begin
69 'begin' => $session['begin'],
70 //Set length
71 'length' => $session['length'],
72 //Set raincancel
73 'raincancel' => ($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id']) && $session['rainfall'] >= 2,
74 //Set cancel
75 'cancel' => $this->isGranted('ROLE_ADMIN') || in_array($this->getUser()->getId(), explode("\n", $session['sau_id'])),
76 //Set modify
77 'modify' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_REGULAR'),
78 //Set move
79 'move' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_SENIOR'),
80 //Set attribute
81 'attribute' => $this->isGranted('ROLE_ADMIN') && $session['locked'] === null,
82 //Set session
83 'session' => $session['id']
84 ]);
85
86 //Refill the fields in case of invalid form
87 $form->handleRequest($request);
88
89 //Handle invalid data
90 #if (true) { $form->isValid();
91 //TODO: mettre une contrainte sur un des boutons submit, je sais pas encore comment
92 if (!$form->isValid()) {
93 //Set section
94 $section = $this->translator->trans('Session %id%', ['%id%' => $id]);
95
96 //Set title
97 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
98
99 //Add session in context
100 $context['session'] = [
101 'id' => $id,
102 'title' => $this->translator->trans('Session %id%', ['%id%' => $id]),
103 'location' => [
104 'id' => $session['l_id'],
105 'at' => $this->translator->trans('at '.$session['l_title'])
106 ]
107 ];
108 //Render the view
109 return $this->render('@RapsysAir/session/edit.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$context+$this->context);
110 }
111
112 //Get manager
113 $manager = $doctrine->getManager();
114
115 //Get data
116 $data = $form->getData();
117
118 //Fetch session
119 $session = $doctrine->getRepository(Session::class)->findOneById($id);
120
121 //Set user
122 $user = $this->getUser();
123
124 //Replace with requested user for admin
125 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
126 $user = $doctrine->getRepository(User::class)->findOneById($data['user']);
127 }
128
129 //Set datetime
130 $datetime = new \DateTime('now');
131
132 //Set canceled time at start minus one day
133 $canceled = (clone $session->getStart())->sub(new \DateInterval('P1D'));
134
135 //Set action
136 $action = [
137 'raincancel' => $form->has('raincancel') && $form->get('raincancel')->isClicked(),
138 'modify' => $form->has('modify') && $form->get('modify')->isClicked(),
139 'move' => $form->has('move') && $form->get('move')->isClicked(),
140 'cancel' => $form->has('cancel') && $form->get('cancel')->isClicked(),
141 'forcecancel' => $form->has('forcecancel') && $form->get('forcecancel')->isClicked(),
142 'attribute' => $form->has('attribute') && $form->get('attribute')->isClicked(),
143 'autoattribute' => $form->has('autoattribute') && $form->get('autoattribute')->isClicked(),
144 'lock' => $form->has('lock') && $form->get('lock')->isClicked(),
145 ];
146
147 //With raincancel and application and (rainfall or admin)
148 if ($action['raincancel'] && ($application = $session->getApplication()) && ($session->getRainfall() >= 2 || $this->isGranted('ROLE_ADMIN'))) {
149 //Cancel application at start minus one day
150 $application->setCanceled($canceled);
151
152 //Update time
153 $application->setUpdated($datetime);
154
155 //Insufficient rainfall
156 //XXX: is admin
157 if ($session->getRainfall() < 2) {
158 //Set score
159 //XXX: magic cheat score 42
160 $application->setScore(42);
161 }
162
163 //Queue application save
164 $manager->persist($application);
165
166 //Add notice in flash message
167 $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
168
169 //Update time
170 $session->setUpdated($datetime);
171
172 //Queue session save
173 $manager->persist($session);
174
175 //Add notice in flash message
176 $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
177 //With modify
178 } elseif ($action['modify']) {
179 //Set begin
180 $session->setBegin($data['begin']);
181
182 //Set length
183 $session->setLength($data['length']);
184
185 //Update time
186 $session->setUpdated($datetime);
187
188 //Queue session save
189 $manager->persist($session);
190
191 //Add notice in flash message
192 $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
193 //With move
194 } elseif ($action['move']) {
195 //Set location
196 $session->setLocation($doctrine->getRepository(Location::class)->findOneById($data['location']));
197
198 //Update time
199 $session->setUpdated($datetime);
200
201 //Queue session save
202 $manager->persist($session);
203
204 //Add notice in flash message
205 $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
206 //With cancel or forcecancel
207 } elseif ($action['cancel'] || $action['forcecancel']) {
208 //Get application
209 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
210
211 //Not already canceled
212 if ($application->getCanceled() === null) {
213 //Cancel application
214 $application->setCanceled($datetime);
215
216 //Check if application is session application and (canceled 24h before start or forcecancel (as admin))
217 #if ($session->getApplication() == $application && ($datetime < $canceled || $action['forcecancel'])) {
218 if ($session->getApplication() == $application && $action['forcecancel']) {
219 //Set score
220 //XXX: magic cheat score 42
221 $application->setScore(42);
222
223 //Unattribute session
224 $session->setApplication(null);
225
226 //Update time
227 $session->setUpdated($datetime);
228
229 //Queue session save
230 $manager->persist($session);
231
232 //Add notice in flash message
233 $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
234 }
235 //Already canceled
236 } else {
237 //Uncancel application
238 $application->setCanceled(null);
239 }
240
241 //Update time
242 $application->setUpdated($datetime);
243
244 //Queue application save
245 $manager->persist($application);
246
247 //Add notice in flash message
248 $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
249 //With attribute
250 } elseif ($action['attribute']) {
251 //Get application
252 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $user);
253
254 //Already canceled
255 if ($application->getCanceled() !== null) {
256 //Uncancel application
257 $application->setCanceled(null);
258 }
259
260 //Set score
261 //XXX: magic cheat score 42
262 $application->setScore(42);
263
264 //Update time
265 $application->setUpdated($datetime);
266
267 //Queue application save
268 $manager->persist($application);
269
270 //Add notice in flash message
271 $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
272
273 //Unattribute session
274 $session->setApplication($application);
275
276 //Update time
277 $session->setUpdated($datetime);
278
279 //Queue session save
280 $manager->persist($session);
281
282 //Add notice in flash message
283 $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
284 //With autoattribute
285 } elseif ($action['autoattribute']) {
286 //Get best application
287 //XXX: best application may not issue result while grace time or bad behaviour
288 if (!empty($application = $doctrine->getRepository(Session::class)->findBestApplicationById($id))) {
289 //Attribute session
290 $session->setApplication($application);
291
292 //Update time
293 $session->setUpdated($datetime);
294
295 //Queue session save
296 $manager->persist($session);
297
298 //Add notice in flash message
299 $this->addFlash('notice', $this->translator->trans('Session %id% auto attributed', ['%id%' => $id]));
300 //No application
301 } else {
302 //Add warning in flash message
303 $this->addFlash('warning', $this->translator->trans('Session %id% not auto attributed', ['%id%' => $id]));
304 }
305 //With lock
306 } elseif ($action['lock']) {
307 //Already locked
308 if ($session->getLocked() !== null) {
309 //Set uncanceled
310 $canceled = null;
311
312 //Unlock session
313 $session->setLocked(null);
314 //Not locked
315 } else {
316 //Get application
317 if ($application = $session->getApplication()) {
318 //Set score
319 //XXX: magic cheat score 42
320 $application->setScore(42);
321
322 //Update time
323 $application->setUpdated($datetime);
324
325 //Queue application save
326 $manager->persist($application);
327
328 //Add notice in flash message
329 $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
330 }
331
332 //Unattribute session
333 $session->setApplication(null);
334
335 //Lock session
336 $session->setLocked($datetime);
337 }
338
339 # //Get applications
340 # $applications = $doctrine->getRepository(Application::class)->findBySession($session);
341 #
342 # //Not empty
343 # if (!empty($applications)) {
344 # //Iterate on each applications
345 # foreach($applications as $application) {
346 # //Cancel application
347 # $application->setCanceled($canceled);
348 #
349 # //Update time
350 # $application->setUpdated($datetime);
351 #
352 # //Queue application save
353 # $manager->persist($application);
354 #
355 # //Add notice in flash message
356 # $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()]));
357 # }
358 # }
359
360 //Update time
361 $session->setUpdated($datetime);
362
363 //Queue session save
364 $manager->persist($session);
365
366 //Add notice in flash message
367 $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id]));
368 //Unknown action
369 } else {
370 //Add warning in flash message
371 $this->addFlash('warning', $this->translator->trans('Session %id% not updated', ['%id%' => $id]));
372 }
373
374 //Flush to get the ids
375 $manager->flush();
376
377 //Extract and process referer
378 if ($referer = $request->headers->get('referer')) {
379 //Create referer request instance
380 $req = Request::create($referer);
381
382 //Get referer path
383 $path = $req->getPathInfo();
384
385 //Get referer query string
386 $query = $req->getQueryString();
387
388 //Remove script name
389 $path = str_replace($request->getScriptName(), '', $path);
390
391 //Try with referer path
392 try {
393 //Save old context
394 $oldContext = $this->router->getContext();
395
396 //Force clean context
397 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
398 $this->router->setContext(new RequestContext());
399
400 //Retrieve route matching path
401 $route = $this->router->match($path);
402
403 //Reset context
404 $this->router->setContext($oldContext);
405
406 //Clear old context
407 unset($oldContext);
408
409 //Extract name
410 $name = $route['_route'];
411
412 //Remove route and controller from route defaults
413 unset($route['_route'], $route['_controller']);
414
415 //Generate url
416 return $this->redirectToRoute($name, $route);
417 //No route matched
418 } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
419 //Unset referer to fallback to default route
420 unset($referer);
421 }
422 }
423
424 //Redirect to cleanup the form
425 return $this->redirectToRoute('rapsys_air_session_view', ['id' => $id]);
426 }
427
428 /**
429 * List all sessions
430 *
431 * @desc Display all sessions with an application or login form
432 *
433 * @param Request $request The request instance
434 *
435 * @return Response The rendered view
436 */
437 public function index(Request $request) {
438 //Fetch doctrine
439 $doctrine = $this->getDoctrine();
440
441 //Set section
442 $section = $this->translator->trans('Sessions');
443
444 //Set description
445 $this->context['description'] = $this->translator->trans('Libre Air session list');
446
447 //Set keywords
448 $this->context['keywords'] = [
449 $this->translator->trans('sessions'),
450 $this->translator->trans('session list'),
451 $this->translator->trans('listing'),
452 $this->translator->trans('Libre Air')
453 ];
454
455 //Set title
456 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
457
458 //Init context
459 $context = [];
460
461 //Create application form for role_guest
462 if ($this->isGranted('ROLE_GUEST')) {
463 //Create ApplicationType form
464 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
465 //Set the action
466 'action' => $this->generateUrl('rapsys_air_application_add'),
467 //Set the form attribute
468 'attr' => [ 'class' => 'col' ],
469 //Set admin
470 'admin' => $this->isGranted('ROLE_ADMIN'),
471 //Set default user to current
472 'user' => $this->getUser()->getId(),
473 //Set default slot to evening
474 //XXX: default to Evening (3)
475 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
476 ]);
477
478 //Add form to context
479 $context['application'] = $application->createView();
480 //Create login form for anonymous
481 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
482 //Create ApplicationType form
483 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
484 //Set the action
485 'action' => $this->generateUrl('rapsys_user_login'),
486 //Set the form attribute
487 'attr' => [ 'class' => 'col' ]
488 ]);
489
490 //Add form to context
491 $context['login'] = $login->createView();
492 }
493
494 //Compute period
495 $period = new \DatePeriod(
496 //Start from first monday of week
497 new \DateTime('Monday this week'),
498 //Iterate on each day
499 new \DateInterval('P1D'),
500 //End with next sunday and 4 weeks
501 new \DateTime(
502 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
503 )
504 );
505
506 //Fetch calendar
507 //TODO: highlight with current session route parameter
508 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
509
510 //Fetch locations
511 //XXX: we want to display all active locations anyway
512 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
513
514 //Render the view
515 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
516 }
517
518 /**
519 * Display session
520 *
521 * @desc Display session by id with an application or login form
522 *
523 * @param Request $request The request instance
524 * @param int $id The session id
525 *
526 * @return Response The rendered view
527 */
528 public function view(Request $request, $id) {
529 //Fetch doctrine
530 $doctrine = $this->getDoctrine();
531
532 //Fetch session
533 if (empty($session = $doctrine->getRepository(Session::class)->fetchOneById($id, $request->getLocale()))) {
534 throw $this->createNotFoundException($this->translator->trans('Unable to find session: %id%', ['%id%' => $id]));
535 }
536
537 //Set section
538 $section = $this->translator->trans($session['l_title']);
539
540 //Set localization date formater
541 $intl = new \IntlDateFormatter($request->getLocale(), \IntlDateFormatter::GREGORIAN, \IntlDateFormatter::SHORT);
542
543 //Set description
544 $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session the %date%', [ '%date%' => $intl->format($session['start']) ]);
545
546 //Set keywords
547 $this->context['keywords'] = [
548 $this->translator->trans('outdoor'),
549 $this->translator->trans('Argentine Tango'),
550 ];
551
552 //With granted session
553 if (!empty($session['au_id'])) {
554 $this->context['keywords'][0] = $session['au_pseudonym'];
555 }
556 //Set title
557 $title = $this->translator->trans($this->config['site']['title']).' - '.$section.' - '.$this->translator->trans(!empty($session['au_id'])?'Session %id% by %pseudonym%':'Session %id%', ['%id%' => $id, '%pseudonym%' => $session['au_pseudonym']]);
558
559 //Init context
560 $context = [];
561
562 //Create application form for role_guest
563 if ($this->isGranted('ROLE_GUEST')) {
564 //Create ApplicationType form
565 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
566 //Set the action
567 'action' => $this->generateUrl('rapsys_air_application_add'),
568 //Set the form attribute
569 'attr' => [ 'class' => 'col' ],
570 //Set admin
571 'admin' => $this->isGranted('ROLE_ADMIN'),
572 //Set default user to current
573 'user' => $this->getUser()->getId(),
574 //Set default slot to current
575 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById($session['t_id']),
576 //Set default location to current
577 'location' => $this->getDoctrine()->getRepository(Location::class)->findOneById($session['l_id']),
578 ]);
579
580 //Add form to context
581 $context['application'] = $application->createView();
582
583 //Set now
584 $now = new \DateTime('now');
585
586 //Create SessionEditType form
587 $session_edit = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [
588 //Set the action
589 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]),
590 //Set the form attribute
591 'attr' => [ 'class' => 'col' ],
592 //Set admin
593 'admin' => $this->isGranted('ROLE_ADMIN'),
594 //Set default user to current
595 'user' => $this->getUser()->getId(),
596 //Set begin
597 'begin' => $session['begin'],
598 //Set length
599 'length' => $session['length'],
600 //Set raincancel
601 'raincancel' => ($this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id']) && $session['rainfall'] >= 2,
602 //Set cancel
603 'cancel' => $this->isGranted('ROLE_ADMIN') || in_array($this->getUser()->getId(), explode("\n", $session['sau_id'])),
604 //Set modify
605 'modify' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_REGULAR'),
606 //Set move
607 'move' => $this->isGranted('ROLE_ADMIN') || $this->getUser()->getId() == $session['au_id'] && $session['stop'] >= $now && $this->isGranted('ROLE_SENIOR'),
608 //Set attribute
609 'attribute' => $this->isGranted('ROLE_ADMIN') && $session['locked'] === null,
610 //Set session
611 'session' => $session['id']
612 ]);
613
614 //Add form to context
615 $context['session_edit'] = $session_edit->createView();
616 //Create login form for anonymous
617 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
618 //Create ApplicationType form
619 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
620 //Set the action
621 'action' => $this->generateUrl('rapsys_user_login'),
622 //Set the form attribute
623 'attr' => [ 'class' => 'col' ]
624 ]);
625
626 //Add form to context
627 $context['login'] = $login->createView();
628 }
629
630 //Add session in context
631 $context['session'] = [
632 'id' => $id,
633 'date' => $session['date'],
634 'begin' => $session['begin'],
635 'start' => $session['start'],
636 'length' => $session['length'],
637 'stop' => $session['stop'],
638 'rainfall' => $session['rainfall'] !== null ? $session['rainfall'].' mm' : $session['rainfall'],
639 'rainrisk' => $session['rainrisk'] !== null ? ($session['rainrisk']*100).' %' : $session['rainrisk'],
640 'realfeel' => $session['realfeel'] !== null ? $session['realfeel'].' °C' : $session['realfeel'],
641 'realfeelmin' => $session['realfeelmin'] !== null ? $session['realfeelmin'].' °C' : $session['realfeelmin'],
642 'realfeelmax' => $session['realfeelmax'] !== null ? $session['realfeelmax'].' °C' : $session['realfeelmax'],
643 'temperature' => $session['temperature'] !== null ? $session['temperature'].' °C' : $session['temperature'],
644 'temperaturemin' => $session['temperaturemin'] !== null ? $session['temperaturemin'].' °C' : $session['temperaturemin'],
645 'temperaturemax' => $session['temperaturemax'] !== null ? $session['temperaturemax'].' °C' : $session['temperaturemax'],
646 'locked' => $session['locked'],
647 'created' => $session['created'],
648 'updated' => $session['updated'],
649 'title' => $this->translator->trans('Session %id%', ['%id%' => $id]),
650 'application' => null,
651 'location' => [
652 'id' => $session['l_id'],
653 'at' => $this->translator->trans('at '.$session['l_title']),
654 'short' => $this->translator->trans($session['l_short']),
655 'title' => $this->translator->trans($session['l_title']),
656 'address' => $session['l_address'],
657 'zipcode' => $session['l_zipcode'],
658 'city' => $session['l_city'],
659 'latitude' => $session['l_latitude'],
660 'longitude' => $session['l_longitude']
661 ],
662 'slot' => [
663 'id' => $session['t_id'],
664 'title' => $this->translator->trans($session['t_title'])
665 ],
666 'snippet' => [
667 'id' => $session['p_id'],
668 'description' => $session['p_description'],
669 'class' => $session['p_class'],
670 'contact' => $session['p_contact'],
671 'donate' => $session['p_donate'],
672 'link' => $session['p_link'],
673 'social' => $session['p_social']
674 ],
675 'applications' => null
676 ];
677
678 //With application
679 if (!empty($session['a_id'])) {
680 $context['session']['application'] = [
681 'user' => [
682 'id' => $session['au_id'],
683 'by' => $this->translator->trans('by %pseudonym%', [ '%pseudonym%' => $session['au_pseudonym'] ]),
684 'title' => $session['au_pseudonym']
685 ],
686 'id' => $session['a_id'],
687 'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]),
688 ];
689 }
690
691 //With applications
692 if (!empty($session['sa_id'])) {
693 //Extract applications id
694 $session['sa_id'] = explode("\n", $session['sa_id']);
695 //Extract applications score
696 //XXX: score may be null before grant or for bad behaviour, replace NULL with 'NULL' to avoid silent drop in mysql
697 $session['sa_score'] = array_map(function($v){return $v==='NULL'?null:$v;}, explode("\n", $session['sa_score']));
698 //Extract applications created
699 $session['sa_created'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_created']));
700 //Extract applications updated
701 $session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated']));
702 //Extract applications canceled
703 //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql
704 $session['sa_canceled'] = array_map(function($v){return $v==='NULL'?null:new \DateTime($v);}, explode("\n", $session['sa_canceled']));
705
706 //Extract applications user id
707 $session['sau_id'] = explode("\n", $session['sau_id']);
708 //Extract applications user pseudonym
709 $session['sau_pseudonym'] = explode("\n", $session['sau_pseudonym']);
710
711 //Init applications
712 $context['session']['applications'] = [];
713 foreach($session['sa_id'] as $i => $sa_id) {
714 $context['session']['applications'][$sa_id] = [
715 'user' => null,
716 'score' => $session['sa_score'][$i],
717 'created' => $session['sa_created'][$i],
718 'updated' => $session['sa_updated'][$i],
719 'canceled' => $session['sa_canceled'][$i]
720 ];
721 if (!empty($session['sau_id'][$i])) {
722 $context['session']['applications'][$sa_id]['user'] = [
723 'id' => $session['sau_id'][$i],
724 'title' => $session['sau_pseudonym'][$i]
725 ];
726 }
727 }
728 }
729
730 //Compute period
731 $period = new \DatePeriod(
732 //Start from first monday of week
733 new \DateTime('Monday this week'),
734 //Iterate on each day
735 new \DateInterval('P1D'),
736 //End with next sunday and 4 weeks
737 new \DateTime(
738 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
739 )
740 );
741
742 //Fetch locations
743 //XXX: we want to display all active locations anyway
744 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $session['au_id']);
745
746 //Render the view
747 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+$context+$this->context);
748 }
749 }