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