]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/LocationController.php
Contextualize attribute, autoattribute and forcecancel buttons
[airbundle] / Controller / LocationController.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 LocationController extends DefaultController {
11 /**
12 * List all sessions for the location
13 *
14 * @desc Display all sessions for the location with an application or login form
15 *
16 * @param Request $request The request instance
17 * @param int $id The location id
18 *
19 * @return Response The rendered view
20 */
21 public function view(Request $request, $id) {
22 //Fetch doctrine
23 $doctrine = $this->getDoctrine();
24
25 //Fetch location
26 $location = $doctrine->getRepository(Location::class)->findOneById($id);
27
28 //Set section
29 $section = $this->translator->trans($location);
30
31 //Set title
32 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
33
34 //Init context
35 $context = [];
36
37 //Create application form for role_guest
38 if ($this->isGranted('ROLE_GUEST')) {
39 //Create ApplicationType form
40 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
41 //Set the action
42 'action' => $this->generateUrl('rapsys_air_application_add'),
43 //Set the form attribute
44 'attr' => [ 'class' => 'col' ],
45 //Set admin
46 'admin' => $this->isGranted('ROLE_ADMIN'),
47 //Set default user to current
48 'user' => $this->getUser()->getId(),
49 //Set default slot to evening
50 //XXX: default to Evening (3)
51 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3),
52 //Set default location to current one
53 'location' => $location
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 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, $id, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
84
85 //Fetch locations
86 //XXX: we want to display all active locations anyway
87 $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/);
88
89 //Render the view
90 return $this->render('@RapsysAir/location/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
91 }
92 }