]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/UserController.php
Add translated location fetch
[airbundle] / Controller / UserController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6
7 use Rapsys\AirBundle\Entity\Slot;
8 use Rapsys\AirBundle\Entity\Session;
9 use Rapsys\AirBundle\Entity\Location;
10 use Rapsys\AirBundle\Entity\User;
11
12 class UserController extends DefaultController {
13 /**
14 * List all sessions for the user
15 *
16 * @desc Display all sessions for the user with an application or login form
17 *
18 * @param Request $request The request instance
19 * @param int $id The user id
20 *
21 * @return Response The rendered view
22 */
23 public function view(Request $request, $id) {
24 //Fetch doctrine
25 $doctrine = $this->getDoctrine();
26
27 //Fetch user
28 $user = $doctrine->getRepository(User::class)->findOneById($id);
29
30 //Set section
31 $section = $user->getPseudonym();
32
33 //Set title
34 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
35
36 //Init context
37 $context = [];
38
39 //Create application form for role_guest
40 if ($this->isGranted('ROLE_GUEST')) {
41 //Create ApplicationType form
42 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
43 //Set the action
44 'action' => $this->generateUrl('rapsys_air_application_add'),
45 //Set the form attribute
46 'attr' => [ 'class' => 'col' ],
47 //Set admin
48 'admin' => $this->isGranted('ROLE_ADMIN'),
49 //Set default user to current
50 'user' => $this->getUser()->getId(),
51 //Set default slot to evening
52 //XXX: default to Evening (3)
53 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
54 ]);
55
56 //Add form to context
57 $context['application'] = $application->createView();
58 //Create login form for anonymous
59 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
60 //Create ApplicationType form
61 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
62 //Set the action
63 'action' => $this->generateUrl('rapsys_user_login'),
64 //Set the form attribute
65 'attr' => [ 'class' => 'col' ]
66 ]);
67
68 //Add form to context
69 $context['login'] = $login->createView();
70 }
71
72 //Compute period
73 $period = new \DatePeriod(
74 //Start from first monday of week
75 new \DateTime('Monday this week'),
76 //Iterate on each day
77 new \DateInterval('P1D'),
78 //End with next sunday and 4 weeks
79 new \DateTime('Monday this week + 5 week')
80 );
81
82 //Fetch calendar
83 //TODO: highlight with current session route parameter
84 $calendar = $doctrine->getRepository(Session::class)->fetchUserCalendarByDatePeriod($this->translator, $period, $id, $request->get('session'));
85
86 //Fetch locations
87 //XXX: we want to display all active locations anyway
88 $locations = $doctrine->getRepository(Location::class)->fetchTranslatedUserLocationByDatePeriod($this->translator, $period, $id);
89
90 //Render the view
91 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
92 }
93 }