3 namespace Rapsys\AirBundle\Controller
;
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
;
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
;
17 class UserController
extends DefaultController
{
21 * @desc Display all user with a group listed as users
23 * @param Request $request The request instance
25 * @return Response The rendered view
27 public function index(Request
$request): Response
{
29 $doctrine = $this->getDoctrine();
32 if ($this->isGranted('ROLE_ADMIN')) {
34 $section = $this->translator
->trans('Libre Air users');
37 $this->context
['description'] = $this->translator
->trans('Libre Air user list');
41 $section = $this->translator
->trans('Libre Air organizers');
44 $this->context
['description'] = $this->translator
->trans('Libre Air organizers list');
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')
56 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
59 $users = $doctrine->getRepository(User
::class)->findUserGroupedByTranslatedGroup($this->translator
);
62 $period = new \
DatePeriod(
63 //Start from first monday of week
64 new \
DateTime('Monday this week'),
66 new \
DateInterval('P1D'),
67 //End with next sunday and 4 weeks
69 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
74 if ($this->isGranted('ROLE_ADMIN')) {
76 $this->context
['groups'] = $users;
79 //Only display senior organizers
80 $this->context
['users'] = $users[$this->translator
->trans('Senior')];
84 //XXX: we want to display all active locations anyway
85 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period);
88 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+
$this->context
);
92 * List all sessions for the user
94 * @desc Display all sessions for the user with an application or login form
96 * @param Request $request The request instance
97 * @param int $id The user id
99 * @return Response The rendered view
101 public function view(Request
$request, $id): Response
{
103 $doctrine = $this->getDoctrine();
106 if (empty($user = $doctrine->getRepository(User
::class)->findOneById($id))) {
107 throw $this->createNotFoundException($this->translator
->trans('Unable to find user: %id%', ['%id%' => $id]));
111 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles());
114 $isGuest = $this->get('rapsys_user.access_decision_manager')->decide($token, ['ROLE_GUEST']);
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]));
122 $section = $user->getPseudonym();
125 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
128 $this->context
['description'] = $this->translator
->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]);
131 $this->context
['keywords'] = [
132 $user->getPseudonym(),
133 $this->translator
->trans('outdoor'),
134 $this->translator
->trans('Argentine Tango'),
135 $this->translator
->trans('calendar')
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
146 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
151 //TODO: highlight with current session route parameter
152 $calendar = $doctrine->getRepository(Session
::class)->fetchUserCalendarByDatePeriod($this->translator
, $period, $isGuest?$id:null, $request->get('session'));
155 //XXX: we want to display all active locations anyway
156 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period, $id);
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, [
163 'action' => $this->generateUrl('rapsys_air_user_view', ['id' => $id]),
164 //Set the form attribute
165 'attr' => [ 'class' => 'col' ],
167 'civility_class' => Civility
::class,
169 'mail' => $this->isGranted('ROLE_ADMIN'),
174 //Init user to context
175 $this->context
['forms']['user'] = $userForm->createView();
178 if ($request->isMethod('POST')) {
179 //Refill the fields in case the form is not valid.
180 $userForm->handleRequest($request);
182 //Handle invalid form
183 if (!$userForm->isSubmitted() || !$userForm->isValid()) {
185 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);
189 $data = $userForm->getData();
192 $manager = $doctrine->getManager();
195 $manager->persist($data);
197 //Flush to get the ids
201 $this->addFlash('notice', $this->translator
->trans('User %id% updated', ['%id%' => $id]));
203 //Extract and process referer
204 if ($referer = $request->headers
->get('referer')) {
205 //Create referer request instance
206 $req = Request
::create($referer);
209 $path = $req->getPathInfo();
211 //Get referer query string
212 $query = $req->getQueryString();
215 $path = str_replace($request->getScriptName(), '', $path);
217 //Try with referer path
220 $oldContext = $this->router
->getContext();
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());
226 //Retrieve route matching path
227 $route = $this->router
->match($path);
230 $this->router
->setContext($oldContext);
236 $name = $route['_route'];
238 //Remove route and controller from route defaults
239 unset($route['_route'], $route['_controller']);
241 //Check if user view route
242 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) {
244 $route['id'] = $data->getId();
248 $route['user'] = $data->getId();
252 return $this->redirectToRoute($name, $route);
254 } catch(MethodNotAllowedException
|ResourceNotFoundException
$e) {
255 //Unset referer to fallback to default route
260 //Redirect to cleanup the form
261 return $this->redirectToRoute('rapsys_air', ['user' => $data->getId()]);
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);
270 //Rekey by location id
271 $snippets = array_reduce($snippets, function($carry, $item){$carry
[$item
->getLocation()->getId()] = $item
; return $carry
;}, []);
273 //Init snippets to context
274 $this->context
['forms']['snippets'] = [];
276 //Iterate on locations
277 foreach($locations as $locationId => $location) {
279 $snippet = new Snippet();
282 $snippet->setLocale($request->getLocale());
285 $snippet->setUser($user);
287 //Set default location
288 $snippet->setLocation($doctrine->getRepository(Location
::class)->findOneById($locationId));
291 if (!empty($snippets[$locationId])) {
292 $snippet = $snippets[$locationId];
295 //Create SnippetType form
296 $form = $this->container
->get('form.factory')->createNamed('snipped_'.$request->getLocale().'_'.$locationId, 'Rapsys\AirBundle\Form\SnippetType', $snippet, [
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
306 //Add form to context
307 $this->context
['forms']['snippets'][$locationId] = $form->createView();
312 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);