]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/UserController.php
New page scheme
[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\Routing\RequestContext;
8 use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
9
10 use Rapsys\AirBundle\Entity\Civility;
11 use Rapsys\AirBundle\Entity\Location;
12 use Rapsys\AirBundle\Entity\Session;
13 use Rapsys\AirBundle\Entity\Slot;
14 use Rapsys\AirBundle\Entity\Snippet;
15 use Rapsys\AirBundle\Entity\User;
16
17 class UserController extends DefaultController {
18 /**
19 * List all users
20 *
21 * @desc Display all user with a group listed as users
22 *
23 * @param Request $request The request instance
24 *
25 * @return Response The rendered view
26 */
27 public function index(Request $request): Response {
28 //Fetch doctrine
29 $doctrine = $this->getDoctrine();
30
31 //With admin role
32 if ($this->isGranted('ROLE_ADMIN')) {
33 //Set section
34 $section = $this->translator->trans('Libre Air users');
35
36 //Set description
37 $this->context['description'] = $this->translator->trans('Libre Air user list');
38 //Without admin role
39 } else {
40 //Set section
41 $section = $this->translator->trans('Libre Air organizers');
42
43 //Set description
44 $this->context['description'] = $this->translator->trans('Libre Air organizers list');
45 }
46
47 //Set keywords
48 $this->context['keywords'] = [
49 $this->translator->trans('users'),
50 $this->translator->trans('user list'),
51 $this->translator->trans('listing'),
52 $this->translator->trans('Libre Air')
53 ];
54
55 //Set title
56 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
57
58 //Fetch users
59 $users = $doctrine->getRepository(User::class)->findUserGroupedByTranslatedGroup($this->translator);
60
61 //Compute period
62 $period = new \DatePeriod(
63 //Start from first monday of week
64 new \DateTime('Monday this week'),
65 //Iterate on each day
66 new \DateInterval('P1D'),
67 //End with next sunday and 4 weeks
68 new \DateTime(
69 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
70 )
71 );
72
73 //With admin role
74 if ($this->isGranted('ROLE_ADMIN')) {
75 //Display all users
76 $this->context['groups'] = $users;
77 //Without admin role
78 } else {
79 //Only display senior organizers
80 $this->context['users'] = $users[$this->translator->trans('Senior')];
81 }
82
83 //Fetch locations
84 //XXX: we want to display all active locations anyway
85 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
86
87 //Render the view
88 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+$this->context);
89 }
90
91 /**
92 * List all sessions for the user
93 *
94 * @desc Display all sessions for the user with an application or login form
95 *
96 * @param Request $request The request instance
97 * @param int $id The user id
98 *
99 * @return Response The rendered view
100 */
101 public function view(Request $request, $id): Response {
102 //Fetch doctrine
103 $doctrine = $this->getDoctrine();
104
105 //Fetch user
106 if (empty($user = $doctrine->getRepository(User::class)->findOneById($id))) {
107 throw $this->createNotFoundException($this->translator->trans('Unable to find user: %id%', ['%id%' => $id]));
108 }
109
110 //Get user token
111 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles());
112
113 //Check if guest
114 $isGuest = $this->get('rapsys_user.access_decision_manager')->decide($token, ['ROLE_GUEST']);
115
116 //Prevent access when not admin, user is not guest and not currently logged user
117 if (!$this->isGranted('ROLE_ADMIN') && empty($isGuest) && $user != $this->getUser()) {
118 throw $this->createAccessDeniedException($this->translator->trans('Unable to access user: %id%', ['%id%' => $id]));
119 }
120
121 //Set section
122 $section = $user->getPseudonym();
123
124 //Set title
125 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
126
127 //Set description
128 $this->context['description'] = $this->translator->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]);
129
130 //Set keywords
131 $this->context['keywords'] = [
132 $user->getPseudonym(),
133 $this->translator->trans('outdoor'),
134 $this->translator->trans('Argentine Tango'),
135 $this->translator->trans('calendar')
136 ];
137
138 //Compute period
139 $period = new \DatePeriod(
140 //Start from first monday of week
141 new \DateTime('Monday this week'),
142 //Iterate on each day
143 new \DateInterval('P1D'),
144 //End with next sunday and 4 weeks
145 new \DateTime(
146 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
147 )
148 );
149
150 //Fetch calendar
151 //TODO: highlight with current session route parameter
152 $calendar = $doctrine->getRepository(Session::class)->fetchUserCalendarByDatePeriod($this->translator, $period, $isGuest?$id:null, $request->get('session'));
153
154 //Fetch locations
155 //XXX: we want to display all active locations anyway
156 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $id);
157
158 //Create user form for admin or current user
159 if ($this->isGranted('ROLE_ADMIN') || $user == $this->getUser()) {
160 //Create SnippetType form
161 $userForm = $this->createForm('Rapsys\AirBundle\Form\RegisterType', $user, [
162 //Set action
163 'action' => $this->generateUrl('rapsys_air_user_view', ['id' => $id]),
164 //Set the form attribute
165 'attr' => [ 'class' => 'col' ],
166 //Set civility class
167 'civility_class' => Civility::class,
168 //Disable mail
169 'mail' => $this->isGranted('ROLE_ADMIN'),
170 //Disable password
171 'password' => false
172 ]);
173
174 //Init user to context
175 $this->context['forms']['user'] = $userForm->createView();
176
177 //Check if submitted
178 if ($request->isMethod('POST')) {
179 //Refill the fields in case the form is not valid.
180 $userForm->handleRequest($request);
181
182 //Handle invalid form
183 if (!$userForm->isSubmitted() || !$userForm->isValid()) {
184 //Render the view
185 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context);
186 }
187
188 //Get data
189 $data = $userForm->getData();
190
191 //Get manager
192 $manager = $doctrine->getManager();
193
194 //Queue snippet save
195 $manager->persist($data);
196
197 //Flush to get the ids
198 $manager->flush();
199
200 //Add notice
201 $this->addFlash('notice', $this->translator->trans('User %id% updated', ['%id%' => $id]));
202
203 //Extract and process referer
204 if ($referer = $request->headers->get('referer')) {
205 //Create referer request instance
206 $req = Request::create($referer);
207
208 //Get referer path
209 $path = $req->getPathInfo();
210
211 //Get referer query string
212 $query = $req->getQueryString();
213
214 //Remove script name
215 $path = str_replace($request->getScriptName(), '', $path);
216
217 //Try with referer path
218 try {
219 //Save old context
220 $oldContext = $this->router->getContext();
221
222 //Force clean context
223 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
224 $this->router->setContext(new RequestContext());
225
226 //Retrieve route matching path
227 $route = $this->router->match($path);
228
229 //Reset context
230 $this->router->setContext($oldContext);
231
232 //Clear old context
233 unset($oldContext);
234
235 //Extract name
236 $name = $route['_route'];
237
238 //Remove route and controller from route defaults
239 unset($route['_route'], $route['_controller']);
240
241 //Check if user view route
242 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) {
243 //Replace id
244 $route['id'] = $data->getId();
245 //Other routes
246 } else {
247 //Set user
248 $route['user'] = $data->getId();
249 }
250
251 //Generate url
252 return $this->redirectToRoute($name, $route);
253 //No route matched
254 } catch(MethodNotAllowedException|ResourceNotFoundException $e) {
255 //Unset referer to fallback to default route
256 unset($referer);
257 }
258 }
259
260 //Redirect to cleanup the form
261 return $this->redirectToRoute('rapsys_air', ['user' => $data->getId()]);
262 }
263 }
264
265 //Create snippet forms for role_guest
266 if ($this->isGranted('ROLE_ADMIN') || ($this->isGranted('ROLE_GUEST') && $user == $this->getUser())) {
267 //Fetch all user snippet
268 $snippets = $doctrine->getRepository(Snippet::class)->findByLocaleUserId($request->getLocale(), $id);
269
270 //Rekey by location id
271 $snippets = array_reduce($snippets, function($carry, $item){$carry[$item->getLocation()->getId()] = $item; return $carry;}, []);
272
273 //Init snippets to context
274 $this->context['forms']['snippets'] = [];
275
276 //Iterate on locations
277 foreach($locations as $locationId => $location) {
278 //Init snippet
279 $snippet = new Snippet();
280
281 //Set default locale
282 $snippet->setLocale($request->getLocale());
283
284 //Set default user
285 $snippet->setUser($user);
286
287 //Set default location
288 $snippet->setLocation($doctrine->getRepository(Location::class)->findOneById($locationId));
289
290 //Get snippet
291 if (!empty($snippets[$locationId])) {
292 $snippet = $snippets[$locationId];
293 }
294
295 //Create SnippetType form
296 $form = $this->container->get('form.factory')->createNamed('snipped_'.$request->getLocale().'_'.$locationId, 'Rapsys\AirBundle\Form\SnippetType', $snippet, [
297 //Set the action
298 'action' =>
299 !empty($snippet->getId()) ?
300 $this->generateUrl('rapsys_air_snippet_edit', ['id' => $snippet->getId()]) :
301 $this->generateUrl('rapsys_air_snippet_add', ['location' => $locationId]),
302 //Set the form attribute
303 'attr' => []
304 ]);
305
306 //Add form to context
307 $this->context['forms']['snippets'][$locationId] = $form->createView();
308 }
309 }
310
311 //Render the view
312 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context);
313 }
314 }