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
;
9 use Rapsys\AirBundle\Entity\Slot
;
10 use Rapsys\AirBundle\Entity\Session
;
11 use Rapsys\AirBundle\Entity\Location
;
12 use Rapsys\AirBundle\Entity\User
;
13 use Rapsys\AirBundle\Entity\Snippet
;
15 class UserController
extends DefaultController
{
19 * @desc Display all user with a group listed as users
21 * @param Request $request The request instance
23 * @return Response The rendered view
25 public function index(Request
$request): Response
{
27 $doctrine = $this->getDoctrine();
30 $section = $this->translator
->trans('Libre Air users');
33 $this->context
['description'] = $this->translator
->trans('Libre Air user list');
36 $this->context
['keywords'] = [
37 $this->translator
->trans('users'),
38 $this->translator
->trans('user list'),
39 $this->translator
->trans('listing'),
40 $this->translator
->trans('Libre Air')
44 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
47 $users = $doctrine->getRepository(User
::class)->findUserGroupedByTranslatedGroup($this->translator
);
50 $period = new \
DatePeriod(
51 //Start from first monday of week
52 new \
DateTime('Monday this week'),
54 new \
DateInterval('P1D'),
55 //End with next sunday and 4 weeks
57 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
62 if (!$this->isGranted('ROLE_ADMIN')) {
64 unset($users[$this->translator
->trans('User')]);
68 //XXX: we want to display all active locations anyway
69 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period);
72 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'users' => $users, 'locations' => $locations]+
$this->context
);
76 * List all sessions for the user
78 * @desc Display all sessions for the user with an application or login form
80 * @param Request $request The request instance
81 * @param int $id The user id
83 * @return Response The rendered view
85 public function view(Request
$request, $id): Response
{
87 $doctrine = $this->getDoctrine();
90 if (empty($user = $doctrine->getRepository(User
::class)->findOneById($id))) {
91 throw $this->createNotFoundException($this->translator
->trans('Unable to find user: %id%', ['%id%' => $id]));
95 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles());
98 $isGuest = $this->get('rapsys_user.access_decision_manager')->decide($token, ['ROLE_GUEST']);
100 //Prevent access when not admin, user is not guest and not currently logged user
101 if (!$this->isGranted('ROLE_ADMIN') && empty($isGuest) && $user != $this->getUser()) {
102 throw $this->createAccessDeniedException($this->translator
->trans('Unable to access user: %id%', ['%id%' => $id]));
106 $section = $user->getPseudonym();
109 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
112 $this->context
['description'] = $this->translator
->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]);
115 $this->context
['keywords'] = [
116 $user->getPseudonym(),
117 $this->translator
->trans('outdoor'),
118 $this->translator
->trans('Argentine Tango'),
119 $this->translator
->trans('calendar')
123 $period = new \
DatePeriod(
124 //Start from first monday of week
125 new \
DateTime('Monday this week'),
126 //Iterate on each day
127 new \
DateInterval('P1D'),
128 //End with next sunday and 4 weeks
130 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
135 //TODO: highlight with current session route parameter
136 $calendar = $doctrine->getRepository(Session
::class)->fetchUserCalendarByDatePeriod($this->translator
, $period, $isGuest?$id:null, $request->get('session'));
139 //XXX: we want to display all active locations anyway
140 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period, $id);
142 //Create user form for admin or current user
143 if ($this->isGranted('ROLE_ADMIN') || $user == $this->getUser()) {
144 //Create SnippetType form
145 $userForm = $this->createForm('Rapsys\AirBundle\Form\RegisterType', $user, [
147 'action' => $this->generateUrl('rapsys_air_user_view', ['id' => $id]),
148 //Set the form attribute
149 'attr' => [ 'class' => 'col' ],
151 'mail' => $this->isGranted('ROLE_ADMIN'),
156 //Init user to context
157 $this->context
['forms']['user'] = $userForm->createView();
160 if ($request->isMethod('POST')) {
161 //Refill the fields in case the form is not valid.
162 $userForm->handleRequest($request);
164 //Handle invalid form
165 if (!$userForm->isSubmitted() || !$userForm->isValid()) {
167 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);
171 $data = $userForm->getData();
174 $manager = $doctrine->getManager();
177 $manager->persist($data);
179 //Flush to get the ids
183 $this->addFlash('notice', $this->translator
->trans('User %id% updated', ['%id%' => $id]));
185 //Extract and process referer
186 if ($referer = $request->headers
->get('referer')) {
187 //Create referer request instance
188 $req = Request
::create($referer);
191 $path = $req->getPathInfo();
193 //Get referer query string
194 $query = $req->getQueryString();
197 $path = str_replace($request->getScriptName(), '', $path);
199 //Try with referer path
202 $oldContext = $this->router
->getContext();
204 //Force clean context
205 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
206 $this->router
->setContext(new RequestContext());
208 //Retrieve route matching path
209 $route = $this->router
->match($path);
212 $this->router
->setContext($oldContext);
218 $name = $route['_route'];
220 //Remove route and controller from route defaults
221 unset($route['_route'], $route['_controller']);
223 //Check if user view route
224 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) {
226 $route['id'] = $data->getId();
230 $route['user'] = $data->getId();
234 return $this->redirectToRoute($name, $route);
236 } catch(MethodNotAllowedException
|ResourceNotFoundException
$e) {
237 //Unset referer to fallback to default route
242 //Redirect to cleanup the form
243 return $this->redirectToRoute('rapsys_air', ['user' => $data->getId()]);
247 //Create snippet forms for role_guest
248 if ($this->isGranted('ROLE_ADMIN') || ($this->isGranted('ROLE_GUEST') && $user == $this->getUser())) {
249 //Fetch all user snippet
250 $snippets = $doctrine->getRepository(Snippet
::class)->findByLocaleUserId($request->getLocale(), $id);
252 //Rekey by location id
253 $snippets = array_reduce($snippets, function($carry, $item){$carry
[$item
->getLocation()->getId()] = $item
; return $carry
;}, []);
255 //Init snippets to context
256 $this->context
['forms']['snippets'] = [];
258 //Iterate on locations
259 foreach($locations as $locationId => $location) {
261 $snippet = new Snippet();
264 $snippet->setLocale($request->getLocale());
267 $snippet->setUser($user);
269 //Set default location
270 $snippet->setLocation($doctrine->getRepository(Location
::class)->findOneById($locationId));
273 if (!empty($snippets[$locationId])) {
274 $snippet = $snippets[$locationId];
277 //Create SnippetType form
278 #$form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', $snippet, [
279 $form = $this->container
->get('form.factory')->createNamed('snipped_'.$request->getLocale().'_'.$locationId, 'Rapsys\AirBundle\Form\SnippetType', $snippet, [
281 //TODO: voir si on peut pas faire sauter ça ici
283 !empty($snippet->getId()) ?
284 $this->generateUrl('rapsys_air_snippet_edit', ['id' => $snippet->getId()]) :
285 $this->generateUrl('rapsys_air_snippet_add', ['location' => $locationId]),
286 #'action' => $this->generateUrl('rapsys_air_snippet_add'),
287 //Set the form attribute
290 //TODO: would maybe need a signature field
291 //'csrf_token_id' => $request->getLocale().'_'.$id.'_'.$locationId
293 #return $this->container->get('form.factory')->create($type, $data, $options);
294 #public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []);
296 //Add form to context
297 $this->context
['forms']['snippets'][$locationId] = $form->createView();
302 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);