]> Raphaël G. Git Repositories - airbundle/blob - Controller/UserController.php
Update css
[airbundle] / Controller / UserController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Symfony\Component\HttpFoundation\Response;
7 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
8 use Rapsys\AirBundle\Entity\Slot;
9 use Rapsys\AirBundle\Entity\Session;
10 use Rapsys\AirBundle\Entity\Location;
11 use Rapsys\AirBundle\Entity\User;
12 use Rapsys\AirBundle\Entity\Snippet;
13
14 class UserController extends DefaultController {
15 /**
16 * List all users
17 *
18 * @desc Display all user with a group listed as users
19 *
20 * @param Request $request The request instance
21 *
22 * @return Response The rendered view
23 */
24 public function index(Request $request): Response {
25 //Fetch doctrine
26 $doctrine = $this->getDoctrine();
27
28 //Set section
29 $section = $this->translator->trans('Libre Air users');
30
31 //Set description
32 $this->context['description'] = $this->translator->trans('Libre Air user list');
33
34 //Set keywords
35 $this->context['keywords'] = [
36 $this->translator->trans('users'),
37 $this->translator->trans('user list'),
38 $this->translator->trans('listing'),
39 $this->translator->trans('Libre Air')
40 ];
41
42 //Set title
43 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
44
45 //Fetch users
46 $users = $doctrine->getRepository(User::class)->findUserGroupedByTranslatedGroup($this->translator);
47
48 //Compute period
49 $period = new \DatePeriod(
50 //Start from first monday of week
51 new \DateTime('Monday this week'),
52 //Iterate on each day
53 new \DateInterval('P1D'),
54 //End with next sunday and 4 weeks
55 new \DateTime(
56 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
57 )
58 );
59
60 //Without admin role
61 if (!$this->isGranted('ROLE_ADMIN')) {
62 //Remove users
63 unset($users[$this->translator->trans('User')]);
64 }
65
66 //Fetch locations
67 //XXX: we want to display all active locations anyway
68 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
69
70 //Render the view
71 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'users' => $users, 'locations' => $locations]+$this->context);
72 }
73
74 /**
75 * List all sessions for the user
76 *
77 * @desc Display all sessions for the user with an application or login form
78 *
79 * @param Request $request The request instance
80 * @param int $id The user id
81 *
82 * @return Response The rendered view
83 */
84 public function view(Request $request, $id): Response {
85 //Fetch doctrine
86 $doctrine = $this->getDoctrine();
87
88 //Fetch user
89 if (empty($user = $doctrine->getRepository(User::class)->findOneById($id))) {
90 throw $this->createNotFoundException($this->translator->trans('Unable to find user: %id%', ['%id%' => $id]));
91 }
92
93 //Prevent non admin access to non guest users
94 if (!$this->isGranted('ROLE_ADMIN')) {
95 //Get user token
96 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles());
97
98 //Check if guest access
99 if (!($isGuest = $this->get('rapsys_user.access_decision_manager')->decide($token, ['ROLE_GUEST']))) {
100 throw $this->createAccessDeniedException($this->translator->trans('Unable to access user: %id%', ['%id%' => $id]));
101 }
102 }
103
104 //Set section
105 $section = $user->getPseudonym();
106
107 //Set title
108 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
109
110 //Set description
111 $this->context['description'] = $this->translator->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]);
112
113 //Set keywords
114 $this->context['keywords'] = [
115 $user->getPseudonym(),
116 $this->translator->trans('outdoor'),
117 $this->translator->trans('Argentine Tango'),
118 $this->translator->trans('calendar')
119 ];
120
121 //Compute period
122 $period = new \DatePeriod(
123 //Start from first monday of week
124 new \DateTime('Monday this week'),
125 //Iterate on each day
126 new \DateInterval('P1D'),
127 //End with next sunday and 4 weeks
128 new \DateTime(
129 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
130 )
131 );
132
133 //Fetch calendar
134 //TODO: highlight with current session route parameter
135 $calendar = $doctrine->getRepository(Session::class)->fetchUserCalendarByDatePeriod($this->translator, $period, $id, $request->get('session'));
136
137 //Fetch locations
138 //XXX: we want to display all active locations anyway
139 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $id);
140
141 //Create snippet forms for role_guest
142 if ($this->isGranted('ROLE_GUEST')) {
143 //Fetch all user snippet
144 $snippets = $doctrine->getRepository(Snippet::class)->findByLocaleUserId($request->getLocale(), $id);
145
146 //Rekey by location id
147 $snippets = array_reduce($snippets, function($carry, $item){$carry[$item->getLocation()->getId()] = $item; return $carry;}, []);
148
149 //Init snippets to context
150 $this->context['forms']['snippets'] = [];
151
152 //Iterate on locations
153 foreach($locations as $locationId => $location) {
154 //Init snippet
155 $snippet = new Snippet();
156
157 //Set default locale
158 $snippet->setLocale($request->getLocale());
159
160 //Set default user
161 $snippet->setUser($user);
162
163 //Set default location
164 $snippet->setLocation($doctrine->getRepository(Location::class)->findOneById($locationId));
165
166 //Get snippet
167 if (!empty($snippets[$locationId])) {
168 $snippet = $snippets[$locationId];
169 }
170
171 //Create SnippetType form
172 #$form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', $snippet, [
173 $form = $this->container->get('form.factory')->createNamed('snipped_'.$request->getLocale().'_'.$locationId, 'Rapsys\AirBundle\Form\SnippetType', $snippet, [
174 //Set the action
175 //TODO: voir si on peut pas faire sauter ça ici
176 'action' =>
177 !empty($snippet->getId()) ?
178 $this->generateUrl('rapsys_air_snippet_edit', ['id' => $snippet->getId()]) :
179 $this->generateUrl('rapsys_air_snippet_add', ['location' => $locationId]),
180 #'action' => $this->generateUrl('rapsys_air_snippet_add'),
181 //Set the form attribute
182 'attr' => [],
183 //Set csrf_token_id
184 //TODO: would maybe need a signature field
185 //'csrf_token_id' => $request->getLocale().'_'.$id.'_'.$locationId
186 ]);
187 #return $this->container->get('form.factory')->create($type, $data, $options);
188 #public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []);
189
190 //Add form to context
191 $this->context['forms']['snippets'][$locationId] = $form->createView();
192 }
193 }
194
195 //Render the view
196 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context);
197 }
198 }