3 namespace Rapsys\AirBundle\Controller
;
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
;
14 class UserController
extends DefaultController
{
18 * @desc Display all user with a group listed as users
20 * @param Request $request The request instance
22 * @return Response The rendered view
24 public function index(Request
$request): Response
{
26 $doctrine = $this->getDoctrine();
29 $section = $this->translator
->trans('Libre Air users');
32 $this->context
['description'] = $this->translator
->trans('Libre Air user list');
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')
43 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
46 $users = $doctrine->getRepository(User
::class)->findUserGroupedByTranslatedGroup($this->translator
);
49 $period = new \
DatePeriod(
50 //Start from first monday of week
51 new \
DateTime('Monday this week'),
53 new \
DateInterval('P1D'),
54 //End with next sunday and 4 weeks
56 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
61 if (!$this->isGranted('ROLE_ADMIN')) {
63 unset($users[$this->translator
->trans('User')]);
67 //XXX: we want to display all active locations anyway
68 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period);
71 return $this->render('@RapsysAir/user/index.html.twig', ['title' => $title, 'section' => $section, 'users' => $users, 'locations' => $locations]+
$this->context
);
75 * List all sessions for the user
77 * @desc Display all sessions for the user with an application or login form
79 * @param Request $request The request instance
80 * @param int $id The user id
82 * @return Response The rendered view
84 public function view(Request
$request, $id): Response
{
86 $doctrine = $this->getDoctrine();
89 if (empty($user = $doctrine->getRepository(User
::class)->findOneById($id))) {
90 throw $this->createNotFoundException($this->translator
->trans('Unable to find user: %id%', ['%id%' => $id]));
93 //Prevent non admin access to non guest users
94 if (!$this->isGranted('ROLE_ADMIN')) {
96 $token = new UsernamePasswordToken($user, null, 'none', $user->getRoles());
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]));
105 $section = $user->getPseudonym();
108 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
111 $this->context
['description'] = $this->translator
->trans('%pseudonym% outdoor Argentine Tango session calendar', [ '%pseudonym%' => $user->getPseudonym() ]);
114 $this->context
['keywords'] = [
115 $user->getPseudonym(),
116 $this->translator
->trans('outdoor'),
117 $this->translator
->trans('Argentine Tango'),
118 $this->translator
->trans('calendar')
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
129 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
134 //TODO: highlight with current session route parameter
135 $calendar = $doctrine->getRepository(Session
::class)->fetchUserCalendarByDatePeriod($this->translator
, $period, $id, $request->get('session'));
138 //XXX: we want to display all active locations anyway
139 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period, $id);
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);
146 //Rekey by location id
147 $snippets = array_reduce($snippets, function($carry, $item){$carry
[$item
->getLocation()->getId()] = $item
; return $carry
;}, []);
149 //Init snippets to context
150 $this->context
['forms']['snippets'] = [];
152 //Iterate on locations
153 foreach($locations as $locationId => $location) {
155 $snippet = new Snippet();
158 $snippet->setLocale($request->getLocale());
161 $snippet->setUser($user);
163 //Set default location
164 $snippet->setLocation($doctrine->getRepository(Location
::class)->findOneById($locationId));
167 if (!empty($snippets[$locationId])) {
168 $snippet = $snippets[$locationId];
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, [
175 //TODO: voir si on peut pas faire sauter ça ici
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
184 //TODO: would maybe need a signature field
185 //'csrf_token_id' => $request->getLocale().'_'.$id.'_'.$locationId
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 = []);
190 //Add form to context
191 $this->context
['forms']['snippets'][$locationId] = $form->createView();
196 return $this->render('@RapsysAir/user/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);