3 namespace Rapsys\AirBundle\Controller
;
5 use Rapsys\AirBundle\Entity\Application
;
6 use Rapsys\AirBundle\Entity\Location
;
7 use Rapsys\AirBundle\Entity\Session
;
8 use Rapsys\AirBundle\Entity\Slot
;
9 use Rapsys\AirBundle\Entity\User
;
10 use Symfony\Bridge\Twig\Mime\TemplatedEmail
;
11 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
;
12 use Symfony\Component\DependencyInjection\ContainerInterface
;
13 use Symfony\Component\Form\FormError
;
14 use Symfony\Component\HttpFoundation\Request
;
15 use Symfony\Component\HttpFoundation\RequestStack
;
16 use Symfony\Component\HttpFoundation\Response
;
17 use Symfony\Component\Mailer\Exception\TransportExceptionInterface
;
18 use Symfony\Component\Mailer\MailerInterface
;
19 use Symfony\Component\Mime\Address
;
20 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
21 use Symfony\Component\Routing\RouterInterface
;
22 use Symfony\Component\Translation\TranslatorInterface
;
23 use Rapsys\UserBundle\Utils\Slugger
;
24 use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait
;
25 use Symfony\Component\DependencyInjection\ContainerAwareTrait
;
28 class DefaultController
{
30 //Rename render as _render
31 render
as protected _render
;
43 ///Translator instance
44 protected $translator;
47 * @var ContainerInterface
52 * Inject container and translator interface
54 * @param ContainerInterface $container The container instance
55 * @param RouterInterface $router The router instance
56 * @param TranslatorInterface $translator The translator instance
58 public function __construct(ContainerInterface
$container, RouterInterface
$router, RequestStack
$requestStack, TranslatorInterface
$translator) {
60 $this->config
= $container->getParameter($this->getAlias());
63 $this->container
= $container;
66 $this->router
= $router;
69 $this->translator
= $translator;
74 'by' => $translator->trans($this->config
['copy']['by']),
75 'link' => $this->config
['copy']['link'],
76 'long' => $translator->trans($this->config
['copy']['long']),
77 'short' => $translator->trans($this->config
['copy']['short']),
78 'title' => $this->config
['copy']['title']
81 'ico' => $this->config
['site']['ico'],
82 'logo' => $this->config
['site']['logo'],
83 'png' => $this->config
['site']['png'],
84 'svg' => $this->config
['site']['svg'],
85 'title' => $translator->trans($this->config
['site']['title']),
86 'url' => $router->generate($this->config
['site']['url']),
94 #$currentLocale = $router->getContext()->getParameters()['_locale'];
95 $currentLocale = $requestStack->getCurrentRequest()->getLocale();
97 //Set translator locale
98 //XXX: allow LocaleSubscriber on the fly locale change for first page
99 $this->translator
->setLocale($currentLocale);
101 //Iterate on locales excluding current one
102 foreach($this->config
['locales'] as $locale) {
106 //Iterate on other locales
107 foreach(array_diff($this->config
['locales'], [$locale]) as $other) {
108 $titles[$other] = $translator->trans($this->config
['languages'][$locale], [], null, $other);
112 $path = $router->getContext()->getPathInfo();
114 //Retrieve route matching path
115 $route = $router->match($path);
118 $name = $route['_route'];
121 unset($route['_route']);
123 //With current locale
124 if ($locale == $currentLocale) {
125 //Set locale locales context
126 $this->context
['canonical'] = $router->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
);
128 //Set locale locales context
129 $this->context
['alternates'][] = [
131 'absolute' => $router->generate($name, ['_locale' => $locale]+
$route, UrlGeneratorInterface
::ABSOLUTE_URL
),
132 'relative' => $router->generate($name, ['_locale' => $locale]+
$route),
133 'title' => implode('/', $titles),
134 'translated' => $translator->trans($this->config
['languages'][$locale], [], null, $locale)
143 * @desc Send a contact mail to configured contact
145 * @param Request $request The request instance
146 * @param MailerInterface $mailer The mailer instance
148 * @return Response The rendered view or redirection
150 public function contact(Request
$request, MailerInterface
$mailer): Response
{
152 $section = $this->translator
->trans('Contact');
155 $this->context
['description'] = $this->translator
->trans('Contact Libre Air');
158 $this->context
['keywords'] = [
159 $this->translator
->trans('contact'),
160 $this->translator
->trans('Libre Air'),
161 $this->translator
->trans('outdoor'),
162 $this->translator
->trans('Argentine Tango'),
163 $this->translator
->trans('calendar')
167 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
169 //Create the form according to the FormType created previously.
170 //And give the proper parameters
171 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
172 'action' => $this->generateUrl('rapsys_air_contact'),
176 if ($request->isMethod('POST')) {
177 // Refill the fields in case the form is not valid.
178 $form->handleRequest($request);
180 if ($form->isValid()) {
182 $data = $form->getData();
185 $message = (new TemplatedEmail())
187 ->from(new Address($data['mail'], $data['name']))
189 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
190 ->to(new Address($this->config
['contact']['mail'], $this->config
['contact']['name']))
192 ->subject($data['subject'])
194 //Set path to twig templates
195 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
196 ->textTemplate('@RapsysAir/mail/contact.text.twig')
201 'subject' => $data['subject'],
202 'message' => strip_tags($data['message']),
206 //Try sending message
207 //XXX: mail delivery may silently fail
210 $mailer->send($message);
212 //Redirect on the same route with sent=1 to cleanup form
213 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params'));
214 //Catch obvious transport exception
215 } catch(TransportExceptionInterface
$e) {
216 if ($message = $e->getMessage()) {
217 //Add error message mail unreachable
218 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config
['contact']['mail'], '%message%' => $this->translator
->trans($message)])));
220 //Add error message mail unreachable
221 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%', ['%mail%' => $this->config
['contact']['mail']])));
228 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
);
234 * @desc Display all granted sessions with an application or login form
236 * @param Request $request The request instance
238 * @return Response The rendered view
240 public function index(Request
$request): Response
{
242 $doctrine = $this->getDoctrine();
245 $section = $this->translator
->trans('Argentine Tango in Paris');
248 $this->context
['description'] = $this->translator
->trans('Outdoor Argentine Tango session calendar in Paris');
251 $this->context
['keywords'] = [
252 $this->translator
->trans('Argentine Tango'),
253 $this->translator
->trans('Paris'),
254 $this->translator
->trans('outdoor'),
255 $this->translator
->trans('calendar'),
256 $this->translator
->trans('Libre Air')
260 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
263 $period = new \
DatePeriod(
264 //Start from first monday of week
265 new \
DateTime('Monday this week'),
266 //Iterate on each day
267 new \
DateInterval('P1D'),
268 //End with next sunday and 4 weeks
270 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 4 week':'Monday this week + 2 week'
275 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
278 //XXX: we want to display all active locations anyway
279 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period);
282 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
);
284 //Set Cache-Control must-revalidate directive
285 #$response->setPublic(true);
286 #$response->setMaxAge(300);
287 #$response->mustRevalidate();
288 ##$response->setCache(['public' => true, 'max_age' => 300]);
290 //Return the response
295 * The organizer regulation page
297 * @desc Display the organizer regulation policy
299 * @return Response The rendered view
301 public function organizerRegulation(): Response
{
303 $section = $this->translator
->trans('Organizer regulation');
306 $this->context
['description'] = $this->translator
->trans('Libre Air organizer regulation');
309 $this->context
['keywords'] = [
310 $this->translator
->trans('organizer regulation'),
311 $this->translator
->trans('Libre Air')
315 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
318 return $this->render('@RapsysAir/default/organizer_regulation.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
322 * The terms of service page
324 * @desc Display the terms of service policy
326 * @return Response The rendered view
328 public function termsOfService(): Response
{
330 $section = $this->translator
->trans('Terms of service');
333 $this->context
['description'] = $this->translator
->trans('Libre Air terms of service');
336 $this->context
['keywords'] = [
337 $this->translator
->trans('terms of service'),
338 $this->translator
->trans('Libre Air')
342 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
345 return $this->render('@RapsysAir/default/terms_of_service.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
349 * The frequently asked questions page
351 * @desc Display the frequently asked questions
353 * @return Response The rendered view
355 public function frequentlyAskedQuestions(): Response
{
357 $section = $this->translator
->trans('Frequently asked questions');
360 $this->context
['description'] = $this->translator
->trans('Libre Air frequently asked questions');
363 $this->context
['keywords'] = [
364 $this->translator
->trans('frequently asked questions'),
365 $this->translator
->trans('faq'),
366 $this->translator
->trans('Libre Air')
370 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section;
373 return $this->render('@RapsysAir/default/frequently_asked_questions.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
377 * Return the bundle alias
381 public function getAlias(): string {
390 protected function render(string $view, array $parameters = [], Response
$response = null): Response
{
391 //Create application form for role_guest
392 if ($this->isGranted('ROLE_GUEST')) {
393 //Without application form
394 if (empty($parameters['forms']['application'])) {
396 $doctrine = $this->getDoctrine();
398 //Create ApplicationType form
399 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
401 'action' => $this->generateUrl('rapsys_air_application_add'),
402 //Set the form attribute
403 'attr' => [ 'class' => 'col' ],
405 'admin' => $this->isGranted('ROLE_ADMIN'),
406 //Set default user to current
407 'user' => $this->getUser()->getId(),
408 //Set default slot to evening
409 //XXX: default to Evening (3)
410 'slot' => $doctrine->getRepository(Slot
::class)->findOneById(3)
413 //Add form to context
414 $parameters['forms']['application'] = $application->createView();
416 //Create login form for anonymous
417 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
418 //Create ApplicationType form
419 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
421 'action' => $this->generateUrl('rapsys_user_login'),
422 //Set the form attribute
423 'attr' => [ 'class' => 'col' ]
426 //Add form to context
427 $parameters['forms']['login'] = $login->createView();
431 return $this->_render($view, $parameters, $response);