]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/LocationController.php
Rewrite user in organizer route
[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 if (empty($location = $doctrine->getRepository(Location::class)->findOneById($id))) {
27 throw $this->createNotFoundException($this->translator->trans('Unable to find location: %id%', ['%id%' => $id]));
28 }
29
30 //Set section
31 $section = $this->translator->trans('Argentine Tango at '.$location);
32
33 //Set title
34 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
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 //Set default location to current one
55 'location' => $location
56 ]);
57
58 //Add form to context
59 $context['application'] = $application->createView();
60 //Create login form for anonymous
61 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
62 //Create ApplicationType form
63 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
64 //Set the action
65 'action' => $this->generateUrl('rapsys_user_login'),
66 //Set the form attribute
67 'attr' => [ 'class' => 'col' ]
68 ]);
69
70 //Add form to context
71 $context['login'] = $login->createView();
72 }
73
74 //Compute period
75 $period = new \DatePeriod(
76 //Start from first monday of week
77 new \DateTime('Monday this week'),
78 //Iterate on each day
79 new \DateInterval('P1D'),
80 //End with next sunday and 4 weeks
81 new \DateTime('Monday this week + 5 week')
82 );
83
84 //Fetch calendar
85 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, $id, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
86
87 //Fetch locations
88 //XXX: we want to display all active locations anyway
89 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
90
91 //Render the view
92 return $this->render('@RapsysAir/location/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
93 }
94 }