]> Raphaël G. Git Repositories - airbundle/blob - Controller/OrganizerController.php
Revert to phone only extra field
[airbundle] / Controller / OrganizerController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6
7 use Rapsys\AirBundle\Entity\Slot;
8 use Rapsys\AirBundle\Entity\Session;
9 use Rapsys\AirBundle\Entity\Location;
10 use Rapsys\AirBundle\Entity\User;
11 use Rapsys\AirBundle\Entity\Snippet;
12
13 class OrganizerController extends DefaultController {
14 /**
15 * List all organizers
16 *
17 * @desc Display all user with a group listed as organizers
18 *
19 * @param Request $request The request instance
20 * @param int $id The user id
21 *
22 * @return Response The rendered view
23 */
24 public function index(Request $request) {
25 //Fetch doctrine
26 $doctrine = $this->getDoctrine();
27
28 //Set section
29 $section = $this->translator->trans('Argentine Tango organizers');
30
31 //Set description
32 $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango organizer list');
33
34 //Set keywords
35 $this->context['keywords'] = [
36 $this->translator->trans('Argentine Tango'),
37 $this->translator->trans('outdoor'),
38 $this->translator->trans('organizer'),
39 $this->translator->trans('Libre Air')
40 ];
41
42 //Set title
43 $title = $this->translator->trans($this->config['site']['title']).' - '.$section;
44
45 //Init context
46 $context = [];
47
48 //Create application form for role_guest
49 if ($this->isGranted('ROLE_GUEST')) {
50 //Create ApplicationType form
51 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
52 //Set the action
53 'action' => $this->generateUrl('rapsys_air_application_add'),
54 //Set the form attribute
55 'attr' => [ 'class' => 'col' ],
56 //Set admin
57 'admin' => $this->isGranted('ROLE_ADMIN'),
58 //Set default user to current
59 'user' => $this->getUser()->getId(),
60 //Set default slot to evening
61 //XXX: default to Evening (3)
62 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
63 ]);
64
65 //Add form to context
66 $context['application'] = $application->createView();
67 //Create login form for anonymous
68 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
69 //Create ApplicationType form
70 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
71 //Set the action
72 'action' => $this->generateUrl('rapsys_user_login'),
73 //Set the form attribute
74 'attr' => [ 'class' => 'col' ]
75 ]);
76
77 //Add form to context
78 $context['login'] = $login->createView();
79 }
80
81 //Fetch organizers
82 $organizers = $doctrine->getRepository(User::class)->findOrganizerGroupedByGroup($this->translator);
83
84 //Compute period
85 $period = new \DatePeriod(
86 //Start from first monday of week
87 new \DateTime('Monday this week'),
88 //Iterate on each day
89 new \DateInterval('P1D'),
90 //End with next sunday and 4 weeks
91 new \DateTime('Monday this week + 5 week')
92 );
93
94 //Fetch locations
95 //XXX: we want to display all active locations anyway
96 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
97
98 //Render the view
99 return $this->render('@RapsysAir/organizer/index.html.twig', ['title' => $title, 'section' => $section, 'organizers' => $organizers, 'locations' => $locations]+$context+$this->context);
100 }
101
102 /**
103 * List all sessions for the organizer
104 *
105 * @desc Display all sessions for the user with an application or login form
106 *
107 * @param Request $request The request instance
108 * @param int $id The user id
109 *
110 * @return Response The rendered view
111 */
112 public function view(Request $request, $id) {
113 //Fetch doctrine
114 $doctrine = $this->getDoctrine();
115
116 //Fetch user
117 if (empty($user = $doctrine->getRepository(User::class)->findOneById($id))) {
118 throw $this->createNotFoundException($this->translator->trans('Unable to find organizer: %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 //Init context
128 $context = [];
129
130 //Create application form for role_guest
131 if ($this->isGranted('ROLE_GUEST')) {
132 //Create ApplicationType form
133 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
134 //Set the action
135 'action' => $this->generateUrl('rapsys_air_application_add'),
136 //Set the form attribute
137 'attr' => [ 'class' => 'col' ],
138 //Set admin
139 'admin' => $this->isGranted('ROLE_ADMIN'),
140 //Set default user to current
141 'user' => $this->getUser()->getId(),
142 //Set default slot to evening
143 //XXX: default to Evening (3)
144 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
145 ]);
146
147 //Add form to context
148 $context['application'] = $application->createView();
149 //Create login form for anonymous
150 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
151 //Create ApplicationType form
152 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
153 //Set the action
154 'action' => $this->generateUrl('rapsys_user_login'),
155 //Set the form attribute
156 'attr' => [ 'class' => 'col' ]
157 ]);
158
159 //Add form to context
160 $context['login'] = $login->createView();
161 }
162
163 //Compute period
164 $period = new \DatePeriod(
165 //Start from first monday of week
166 new \DateTime('Monday this week'),
167 //Iterate on each day
168 new \DateInterval('P1D'),
169 //End with next sunday and 4 weeks
170 new \DateTime('Monday this week + 5 week')
171 );
172
173 //Fetch calendar
174 //TODO: highlight with current session route parameter
175 $calendar = $doctrine->getRepository(Session::class)->fetchUserCalendarByDatePeriod($this->translator, $period, $id, $request->get('session'));
176
177 //Fetch locations
178 //XXX: we want to display all active locations anyway
179 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $id);
180
181 //Create snippet forms for role_guest
182 if ($this->isGranted('ROLE_GUEST')) {
183 //Fetch all user snippet
184 $snippets = $doctrine->getRepository(Snippet::class)->findByLocaleUserId($request->getLocale(), $id);
185
186 //Rekey by location id
187 $snippets = array_reduce($snippets, function($carry, $item){$carry[$item->getLocation()->getId()] = $item; return $carry;}, []);
188
189 //Init snippets to context
190 $context['snippets'] = [];
191
192 //Iterate on locations
193 foreach($locations as $locationId => $location) {
194 //Init snippet
195 $snippet = new Snippet();
196
197 //Set default locale
198 $snippet->setLocale($request->getLocale());
199
200 //Set default user
201 $snippet->setUser($user);
202
203 //Set default location
204 $snippet->setLocation($doctrine->getRepository(Location::class)->findOneById($locationId));
205
206 //Get snippet
207 if (!empty($snippets[$locationId])) {
208 $snippet = $snippets[$locationId];
209 }
210
211 //Create SnippetType form
212 $form = $this->createForm('Rapsys\AirBundle\Form\SnippetType', $snippet, [
213 //Set the action
214 //TODO: voir si on peut pas faire sauter ça ici
215 'action' => !empty($snippet->getId())?$this->generateUrl('rapsys_air_snippet_edit', ['id' => $snippet->getId()]):$this->generateUrl('rapsys_air_snippet_add'),
216 #'action' => $this->generateUrl('rapsys_air_snippet_add'),
217 //Set the form attribute
218 'attr' => [],
219 //Set csrf_token_id
220 //TODO: would maybe need a signature field
221 //'csrf_token_id' => $request->getLocale().'_'.$id.'_'.$locationId
222 ]);
223
224 //Add form to context
225 $context['snippets'][$locationId] = $form->createView();
226 }
227 }
228
229 //Render the view
230 return $this->render('@RapsysAir/organizer/view.html.twig', ['id' => $id, 'title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
231 }
232 }