]> Raphaël G. Git Repositories - airbundle/blob - Controller/DefaultController.php
Remove short title
[airbundle] / Controller / DefaultController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Bridge\Twig\Mime\TemplatedEmail;
6 use Symfony\Component\Form\FormError;
7 use Symfony\Component\HttpFoundation\Request;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
10 use Symfony\Component\Mailer\MailerInterface;
11 use Symfony\Component\Mime\Address;
12
13 use Rapsys\AirBundle\Entity\Location;
14 use Rapsys\AirBundle\Entity\Session;
15 use Rapsys\AirBundle\Pdf\DisputePdf;
16
17 class DefaultController extends AbstractController {
18 /**
19 * The about page
20 *
21 * @desc Display the about informations
22 *
23 * @param Request $request The request instance
24 * @return Response The rendered view
25 */
26 public function about(Request $request): Response {
27 //Set page
28 $this->context['title'] = $this->translator->trans('About');
29
30 //Set description
31 $this->context['description'] = $this->translator->trans('Libre Air about');
32
33 //Set keywords
34 $this->context['keywords'] = [
35 $this->translator->trans('about'),
36 $this->translator->trans('Libre Air')
37 ];
38
39 //Render template
40 $response = $this->render('@RapsysAir/default/about.html.twig', $this->context);
41 $response->setEtag(md5($response->getContent()));
42 $response->setPublic();
43 $response->isNotModified($request);
44
45 //Return response
46 return $response;
47 }
48
49 /**
50 * The contact page
51 *
52 * @desc Send a contact mail to configured contact
53 *
54 * @param Request $request The request instance
55 * @param MailerInterface $mailer The mailer instance
56 *
57 * @return Response The rendered view or redirection
58 */
59 public function contact(Request $request, MailerInterface $mailer): Response {
60 //Set page
61 $this->context['title'] = $this->translator->trans('Contact');
62
63 //Set description
64 $this->context['description'] = $this->translator->trans('Contact Libre Air');
65
66 //Set keywords
67 $this->context['keywords'] = [
68 $this->translator->trans('contact'),
69 $this->translator->trans('Libre Air'),
70 $this->translator->trans('outdoor'),
71 $this->translator->trans('Argentine Tango'),
72 $this->translator->trans('calendar')
73 ];
74
75 //Create the form according to the FormType created previously.
76 //And give the proper parameters
77 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
78 'action' => $this->generateUrl('rapsys_air_contact'),
79 'method' => 'POST'
80 ]);
81
82 if ($request->isMethod('POST')) {
83 // Refill the fields in case the form is not valid.
84 $form->handleRequest($request);
85
86 if ($form->isValid()) {
87 //Get data
88 $data = $form->getData();
89
90 //Create message
91 $message = (new TemplatedEmail())
92 //Set sender
93 ->from(new Address($data['mail'], $data['name']))
94 //Set recipient
95 ->to(new Address($this->context['contact']['mail'], $this->context['contact']['title']))
96 //Set subject
97 ->subject($data['subject'])
98
99 //Set path to twig templates
100 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
101 ->textTemplate('@RapsysAir/mail/contact.text.twig')
102
103 //Set context
104 ->context(
105 [
106 'subject' => $data['subject'],
107 'message' => strip_tags($data['message']),
108 ]+$this->context
109 );
110
111 //Try sending message
112 //XXX: mail delivery may silently fail
113 try {
114 //Send message
115 $mailer->send($message);
116
117 //Redirect on the same route with sent=1 to cleanup form
118 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
119 //Catch obvious transport exception
120 } catch(TransportExceptionInterface $e) {
121 if ($message = $e->getMessage()) {
122 //Add error message mail unreachable
123 $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->context['contact']['mail'], '%message%' => $this->translator->trans($message)])));
124 } else {
125 //Add error message mail unreachable
126 $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->context['contact']['mail']])));
127 }
128 }
129 }
130 }
131
132 //Render template
133 return $this->render('@RapsysAir/form/contact.html.twig', ['form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
134 }
135
136 /**
137 * The dispute page
138 *
139 * @desc Generate a dispute document
140 *
141 * @param Request $request The request instance
142 * @param MailerInterface $mailer The mailer instance
143 *
144 * @return Response The rendered view or redirection
145 */
146 public function dispute(Request $request, MailerInterface $mailer): Response {
147 //Prevent non-guest to access here
148 $this->denyAccessUnlessGranted('ROLE_USER', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('User')]));
149
150 //Set page
151 $this->context['title'] = $this->translator->trans('Dispute');
152
153 //Set description
154 $this->context['description'] = $this->translator->trans('Libre Air dispute');
155
156 //Set keywords
157 $this->context['keywords'] = [
158 $this->translator->trans('dispute'),
159 $this->translator->trans('Libre Air'),
160 $this->translator->trans('outdoor'),
161 $this->translator->trans('Argentine Tango'),
162 $this->translator->trans('calendar')
163 ];
164
165 //Create the form according to the FormType created previously.
166 //And give the proper parameters
167 $form = $this->createForm('Rapsys\AirBundle\Form\DisputeType', ['court' => 'Paris', 'abstract' => 'Pour constater cette prétendue infraction, les agents verbalisateurs ont pénétré dans un jardin privatif, sans visibilité depuis la voie publique, situé derrière un batiment privé, pour ce faire ils ont franchi au moins un grillage de chantier ou des potteaux métalliques séparant le terrain privé de la voie publique de l\'autre côté du batiment.'], [
168 'action' => $this->generateUrl('rapsys_air_dispute'),
169 'method' => 'POST'
170 ]);
171
172 if ($request->isMethod('POST')) {
173 // Refill the fields in case the form is not valid.
174 $form->handleRequest($request);
175
176 if ($form->isValid()) {
177 //Get data
178 $data = $form->getData();
179
180 //Gathering offense
181 if (!empty($data['offense']) && $data['offense'] == 'gathering') {
182 //Add gathering
183 $output = DisputePdf::genGathering($data['court'], $data['notice'], $data['agent'], $data['service'], $data['abstract'], $this->translator->trans($this->getUser()->getCivility()->getTitle()), $this->getUser()->getForename(), $this->getUser()->getSurname());
184 //Traffic offense
185 } elseif (!empty($data['offense'] && $data['offense'] == 'traffic')) {
186 //Add traffic
187 $output = DisputePdf::genTraffic($data['court'], $data['notice'], $data['agent'], $data['service'], $data['abstract'], $this->translator->trans($this->getUser()->getCivility()->getTitle()), $this->getUser()->getForename(), $this->getUser()->getSurname());
188 //Unsupported offense
189 } else {
190 header('Content-Type: text/plain');
191 die('TODO');
192 exit;
193 }
194
195 //Send common headers
196 header('Content-Type: application/pdf');
197
198 //Send remaining headers
199 header('Cache-Control: private, max-age=0, must-revalidate');
200 header('Pragma: public');
201
202 //Send content-length
203 header('Content-Length: '.strlen($output));
204
205 //Display the pdf
206 echo $output;
207
208 //Die for now
209 exit;
210
211 # //Create message
212 # $message = (new TemplatedEmail())
213 # //Set sender
214 # ->from(new Address($data['mail'], $data['name']))
215 # //Set recipient
216 # //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
217 # ->to(new Address($this->config['contact']['mail'], $this->config['contact']['title']))
218 # //Set subject
219 # ->subject($data['subject'])
220 #
221 # //Set path to twig templates
222 # ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
223 # ->textTemplate('@RapsysAir/mail/contact.text.twig')
224 #
225 # //Set context
226 # ->context(
227 # [
228 # 'subject' => $data['subject'],
229 # 'message' => strip_tags($data['message']),
230 # ]+$this->context
231 # );
232 #
233 # //Try sending message
234 # //XXX: mail delivery may silently fail
235 # try {
236 # //Send message
237 # $mailer->send($message);
238 #
239 # //Redirect on the same route with sent=1 to cleanup form
240 # return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
241 # //Catch obvious transport exception
242 # } catch(TransportExceptionInterface $e) {
243 # if ($message = $e->getMessage()) {
244 # //Add error message mail unreachable
245 # $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config['contact']['mail'], '%message%' => $this->translator->trans($message)])));
246 # } else {
247 # //Add error message mail unreachable
248 # $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']])));
249 # }
250 # }
251 }
252 }
253
254 //Render template
255 return $this->render('@RapsysAir/default/dispute.html.twig', ['form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
256 }
257
258 /**
259 * The index page
260 *
261 * @desc Display all granted sessions with an application or login form
262 *
263 * @param Request $request The request instance
264 * @return Response The rendered view
265 */
266 public function index(Request $request): Response {
267 //Fetch doctrine
268 $doctrine = $this->getDoctrine();
269
270 //Set page
271 $this->context['title'] = $this->translator->trans('Argentine Tango in Paris');
272
273 //Set description
274 $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session calendar in Paris');
275
276 //Set keywords
277 $this->context['keywords'] = [
278 $this->translator->trans('Argentine Tango'),
279 $this->translator->trans('Paris'),
280 $this->translator->trans('outdoor'),
281 $this->translator->trans('calendar'),
282 $this->translator->trans('Libre Air')
283 ];
284
285 //Set facebook type
286 //XXX: only valid for home page
287 $this->context['facebook']['metas']['og:type'] = 'website';
288
289 //Compute period
290 $period = new \DatePeriod(
291 //Start from first monday of week
292 new \DateTime('Monday this week'),
293 //Iterate on each day
294 new \DateInterval('P1D'),
295 //End with next sunday and 4 weeks
296 new \DateTime(
297 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week'
298 )
299 );
300
301 //Fetch calendar
302 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'), $request->getLocale());
303
304 //Fetch locations
305 //XXX: we want to display all active locations anyway
306 $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period);
307
308 //Render the view
309 return $this->render('@RapsysAir/default/index.html.twig', ['calendar' => $calendar, 'locations' => $locations]+$this->context);
310
311 //Set Cache-Control must-revalidate directive
312 //TODO: add a javascript forced refresh after 1h ? or header refresh ?
313 #$response->setPublic(true);
314 #$response->setMaxAge(300);
315 #$response->mustRevalidate();
316 ##$response->setCache(['public' => true, 'max_age' => 300]);
317
318 //Return the response
319 #return $response;
320 }
321
322 /**
323 * The organizer regulation page
324 *
325 * @desc Display the organizer regulation policy
326 *
327 * @param Request $request The request instance
328 * @return Response The rendered view
329 */
330 public function organizerRegulation(Request $request): Response {
331 //Set page
332 $this->context['title'] = $this->translator->trans('Organizer regulation');
333
334 //Set description
335 $this->context['description'] = $this->translator->trans('Libre Air organizer regulation');
336
337 //Set keywords
338 $this->context['keywords'] = [
339 $this->translator->trans('organizer regulation'),
340 $this->translator->trans('Libre Air')
341 ];
342
343 //Render template
344 $response = $this->render('@RapsysAir/default/organizer_regulation.html.twig', $this->context);
345
346 //Set as cachable
347 $response->setEtag(md5($response->getContent()));
348 $response->setPublic();
349 $response->isNotModified($request);
350
351 //Return response
352 return $response;
353 }
354
355 /**
356 * The terms of service page
357 *
358 * @desc Display the terms of service policy
359 *
360 * @param Request $request The request instance
361 * @return Response The rendered view
362 */
363 public function termsOfService(Request $request): Response {
364 //Set page
365 $this->context['title'] = $this->translator->trans('Terms of service');
366
367 //Set description
368 $this->context['description'] = $this->translator->trans('Libre Air terms of service');
369
370 //Set keywords
371 $this->context['keywords'] = [
372 $this->translator->trans('terms of service'),
373 $this->translator->trans('Libre Air')
374 ];
375
376 //Render template
377 $response = $this->render('@RapsysAir/default/terms_of_service.html.twig', $this->context);
378
379 //Set as cachable
380 $response->setEtag(md5($response->getContent()));
381 $response->setPublic();
382 $response->isNotModified($request);
383
384 //Return response
385 return $response;
386 }
387
388 /**
389 * The frequently asked questions page
390 *
391 * @desc Display the frequently asked questions
392 *
393 * @param Request $request The request instance
394 * @return Response The rendered view
395 */
396 public function frequentlyAskedQuestions(Request $request): Response {
397 //Set page
398 $this->context['title'] = $this->translator->trans('Frequently asked questions');
399
400 //Set description
401 $this->context['description'] = $this->translator->trans('Libre Air frequently asked questions');
402
403 //Set keywords
404 $this->context['keywords'] = [
405 $this->translator->trans('frequently asked questions'),
406 $this->translator->trans('faq'),
407 $this->translator->trans('Libre Air')
408 ];
409
410 //Render template
411 $response = $this->render('@RapsysAir/default/frequently_asked_questions.html.twig', $this->context);
412
413 //Set as cachable
414 $response->setEtag(md5($response->getContent()));
415 $response->setPublic();
416 $response->isNotModified($request);
417
418 //Return response
419 return $response;
420 }
421
422 /**
423 * Renders a view
424 *
425 * {@inheritdoc}
426 */
427 /*protected function render(string $view, array $parameters = [], Response $response = null): Response {
428 //Call parent render
429 return parent::render($view, $parameters, $response);
430 }*/
431 }