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                 $section = $this->translator
->trans('Libre Air users'); 
  35                 $this->context
['description'] = $this->translator
->trans('Libre Air user list'); 
  38                 $this->context
['keywords'] = [ 
  39                         $this->translator
->trans('users'), 
  40                         $this->translator
->trans('user list'), 
  41                         $this->translator
->trans('listing'), 
  42                         $this->translator
->trans('Libre Air') 
  46                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
  49                 $users = $doctrine->getRepository(User
::class)->findUserGroupedByTranslatedGroup($this->translator
); 
  52                 $period = new \
DatePeriod( 
  53                         //Start from first monday of week 
  54                         new \
DateTime('Monday this week'), 
  56                         new \
DateInterval('P1D'), 
  57                         //End with next sunday and 4 weeks 
  59                                 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' 
  64                 if (!$this->isGranted('ROLE_ADMIN')) { 
  66                         unset($users[$this->translator
->trans('User')]); 
  70                 //XXX: we want to display all active locations anyway 
  71                 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period); 
  74                 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'users' => $users, 'locations' => $locations]+
$this->context
); 
  78          * List all sessions for the user 
  80          * @desc Display all sessions for the user with an application or login form 
  82          * @param Request $request The request instance 
  83          * @param int $id The user id 
  85          * @return Response The rendered view 
  87         public function view(Request 
$request, $id): Response 
{ 
  89                 $doctrine = $this->getDoctrine(); 
  92                 if (empty($user = $doctrine->getRepository(User
::class)->findOneById($id))) { 
  93                         throw $this->createNotFoundException($this->translator
->trans('Unable to find user: %id%', ['%id%' => $id])); 
  97                 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles()); 
 100                 $isGuest = $this->get('rapsys_user.access_decision_manager')->decide($token, ['ROLE_GUEST']); 
 102                 //Prevent access when not admin, user is not guest and not currently logged user 
 103                 if (!$this->isGranted('ROLE_ADMIN') && empty($isGuest) && $user != $this->getUser()) { 
 104                         throw $this->createAccessDeniedException($this->translator
->trans('Unable to access user: %id%', ['%id%' => $id])); 
 108                 $section = $user->getPseudonym(); 
 111                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 114                 $this->context
['description'] = $this->translator
->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]); 
 117                 $this->context
['keywords'] = [ 
 118                         $user->getPseudonym(), 
 119                         $this->translator
->trans('outdoor'), 
 120                         $this->translator
->trans('Argentine Tango'), 
 121                         $this->translator
->trans('calendar') 
 125                 $period = new \
DatePeriod( 
 126                         //Start from first monday of week 
 127                         new \
DateTime('Monday this week'), 
 128                         //Iterate on each day 
 129                         new \
DateInterval('P1D'), 
 130                         //End with next sunday and 4 weeks 
 132                                 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' 
 137                 //TODO: highlight with current session route parameter 
 138                 $calendar = $doctrine->getRepository(Session
::class)->fetchUserCalendarByDatePeriod($this->translator
, $period, $isGuest?$id:null, $request->get('session')); 
 141                 //XXX: we want to display all active locations anyway 
 142                 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period, $id); 
 144                 //Create user form for admin or current user 
 145                 if ($this->isGranted('ROLE_ADMIN') || $user == $this->getUser()) { 
 146                         //Create SnippetType form 
 147                         $userForm = $this->createForm('Rapsys\AirBundle\Form\RegisterType', $user, [ 
 149                                 'action' => $this->generateUrl('rapsys_air_user_view', ['id' => $id]), 
 150                                 //Set the form attribute 
 151                                 'attr' => [ 'class' => 'col' ], 
 153                                 'class_civility' => Civility
::class, 
 155                                 'mail' => $this->isGranted('ROLE_ADMIN'), 
 160                         //Init user to context 
 161                         $this->context
['forms']['user'] = $userForm->createView(); 
 164                         if ($request->isMethod('POST')) { 
 165                                 //Refill the fields in case the form is not valid. 
 166                                 $userForm->handleRequest($request); 
 168                                 //Handle invalid form 
 169                                 if (!$userForm->isSubmitted() || !$userForm->isValid()) { 
 171                                         return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
); 
 175                                 $data = $userForm->getData(); 
 178                                 $manager = $doctrine->getManager(); 
 181                                 $manager->persist($data); 
 183                                 //Flush to get the ids 
 187                                 $this->addFlash('notice', $this->translator
->trans('User %id% updated', ['%id%' => $id])); 
 189                                 //Extract and process referer 
 190                                 if ($referer = $request->headers
->get('referer')) { 
 191                                         //Create referer request instance 
 192                                         $req = Request
::create($referer); 
 195                                         $path = $req->getPathInfo(); 
 197                                         //Get referer query string 
 198                                         $query = $req->getQueryString(); 
 201                                         $path = str_replace($request->getScriptName(), '', $path); 
 203                                         //Try with referer path 
 206                                                 $oldContext = $this->router
->getContext(); 
 208                                                 //Force clean context 
 209                                                 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42 
 210                                                 $this->router
->setContext(new RequestContext()); 
 212                                                 //Retrieve route matching path 
 213                                                 $route = $this->router
->match($path); 
 216                                                 $this->router
->setContext($oldContext); 
 222                                                 $name = $route['_route']; 
 224                                                 //Remove route and controller from route defaults 
 225                                                 unset($route['_route'], $route['_controller']); 
 227                                                 //Check if user view route 
 228                                                 if ($name == 'rapsys_air_user_view' && !empty($route['id'])) { 
 230                                                         $route['id'] = $data->getId(); 
 234                                                         $route['user'] = $data->getId(); 
 238                                                 return $this->redirectToRoute($name, $route); 
 240                                         } catch(MethodNotAllowedException
|ResourceNotFoundException 
$e) { 
 241                                                 //Unset referer to fallback to default route 
 246                                 //Redirect to cleanup the form 
 247                                 return $this->redirectToRoute('rapsys_air', ['user' => $data->getId()]); 
 251                 //Create snippet forms for role_guest 
 252                 if ($this->isGranted('ROLE_ADMIN') || ($this->isGranted('ROLE_GUEST') && $user == $this->getUser())) { 
 253                         //Fetch all user snippet 
 254                         $snippets = $doctrine->getRepository(Snippet
::class)->findByLocaleUserId($request->getLocale(), $id); 
 256                         //Rekey by location id 
 257                         $snippets = array_reduce($snippets, function($carry, $item){$carry
[$item
->getLocation()->getId()] = $item
; return $carry
;}, []); 
 259                         //Init snippets to context 
 260                         $this->context
['forms']['snippets'] = []; 
 262                         //Iterate on locations 
 263                         foreach($locations as $locationId => $location) { 
 265                                 $snippet = new Snippet(); 
 268                                 $snippet->setLocale($request->getLocale()); 
 271                                 $snippet->setUser($user); 
 273                                 //Set default location 
 274                                 $snippet->setLocation($doctrine->getRepository(Location
::class)->findOneById($locationId)); 
 277                                 if (!empty($snippets[$locationId])) { 
 278                                         $snippet = $snippets[$locationId]; 
 281                                 //Create SnippetType form 
 282                                 #$form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', $snippet, [ 
 283                                 $form = $this->container
->get('form.factory')->createNamed('snipped_'.$request->getLocale().'_'.$locationId, 'Rapsys\AirBundle\Form\SnippetType', $snippet, [ 
 285                                         //TODO: voir si on peut pas faire sauter ça ici 
 287                                                 !empty($snippet->getId()) ? 
 288                                                 $this->generateUrl('rapsys_air_snippet_edit', ['id' => $snippet->getId()]) : 
 289                                                 $this->generateUrl('rapsys_air_snippet_add', ['location' => $locationId]), 
 290                                         #'action' => $this->generateUrl('rapsys_air_snippet_add'), 
 291                                         //Set the form attribute 
 294                                         //TODO: would maybe need a signature field 
 295                                         //'csrf_token_id' => $request->getLocale().'_'.$id.'_'.$locationId 
 297                                 #return $this->container->get('form.factory')->create($type, $data, $options); 
 298                                 #public function createNamed($name, $type = 'Symfony\Component\Form\Extension\Core\Type\FormType', $data = null, array $options = []); 
 300                                 //Add form to context 
 301                                 $this->context
['forms']['snippets'][$locationId] = $form->createView(); 
 306                 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);