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
); 
 235          * @desc Generate a dispute document 
 237          * @param Request $request The request instance 
 238          * @param MailerInterface $mailer The mailer instance 
 240          * @return Response The rendered view or redirection 
 242         public function dispute(Request 
$request, MailerInterface 
$mailer): Response 
{ 
 243                 //Prevent non-guest to access here 
 244                 $this->denyAccessUnlessGranted('ROLE_USER', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('User')])); 
 246                 $section = $this->translator
->trans('Dispute'); 
 249                 $this->context
['description'] = $this->translator
->trans('Libre Air dispute'); 
 252                 $this->context
['keywords'] = [ 
 253                         $this->translator
->trans('dispute'), 
 254                         $this->translator
->trans('Libre Air'), 
 255                         $this->translator
->trans('outdoor'), 
 256                         $this->translator
->trans('Argentine Tango'), 
 257                         $this->translator
->trans('calendar') 
 261                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 263                 //Create the form according to the FormType created previously. 
 264                 //And give the proper parameters 
 265                 $form = $this->createForm('Rapsys\AirBundle\Form\DisputeType', null, [ 
 266                         'action' => $this->generateUrl('rapsys_air_dispute'), 
 270                 if ($request->isMethod('POST')) { 
 271                         // Refill the fields in case the form is not valid. 
 272                         $form->handleRequest($request); 
 274                         if ($form->isValid()) { 
 276                                 $data = $form->getData(); 
 278                                 header('Content-Type: text/plain'); 
 281                                 var_dump($data['offense']); 
 284                                 var_dump($data['notice']); 
 287                                 var_dump($data['agent']); 
 290                                 var_dump($data['service']); 
 293                                 var_dump($data['mask']); 
 296                                 var_dump($this->translator
->trans($this->getUser()->getCivility()->getTitle())); 
 299                                 var_dump($this->getUser()->getForename()); 
 302                                 var_dump($this->getUser()->getSurname()); 
 305                                 var_dump($this->getUser()->getMail()); 
 310 #                               $message = (new TemplatedEmail()) 
 312 #                                       ->from(new Address($data['mail'], $data['name'])) 
 314 #                                       //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 
 315 #                                       ->to(new Address($this->config['contact']['mail'], $this->config['contact']['name'])) 
 317 #                                       ->subject($data['subject']) 
 319 #                                       //Set path to twig templates 
 320 #                                       ->htmlTemplate('@RapsysAir/mail/contact.html.twig') 
 321 #                                       ->textTemplate('@RapsysAir/mail/contact.text.twig') 
 326 #                                                       'subject' => $data['subject'], 
 327 #                                                       'message' => strip_tags($data['message']), 
 331 #                               //Try sending message 
 332 #                               //XXX: mail delivery may silently fail 
 335 #                                       $mailer->send($message); 
 337 #                                       //Redirect on the same route with sent=1 to cleanup form 
 338 #                                       return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params')); 
 339 #                               //Catch obvious transport exception 
 340 #                               } catch(TransportExceptionInterface $e) { 
 341 #                                       if ($message = $e->getMessage()) { 
 342 #                                               //Add error message mail unreachable 
 343 #                                               $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config['contact']['mail'], '%message%' => $this->translator->trans($message)]))); 
 345 #                                               //Add error message mail unreachable 
 346 #                                               $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']]))); 
 353                 return $this->render('@RapsysAir/form/dispute.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
); 
 359          * @desc Display all granted sessions with an application or login form 
 361          * @param Request $request The request instance 
 363          * @return Response The rendered view 
 365         public function index(Request 
$request): Response 
{ 
 367                 $doctrine = $this->getDoctrine(); 
 370                 $section = $this->translator
->trans('Argentine Tango in Paris'); 
 373                 $this->context
['description'] = $this->translator
->trans('Outdoor Argentine Tango session calendar in Paris'); 
 376                 $this->context
['keywords'] = [ 
 377                         $this->translator
->trans('Argentine Tango'), 
 378                         $this->translator
->trans('Paris'), 
 379                         $this->translator
->trans('outdoor'), 
 380                         $this->translator
->trans('calendar'), 
 381                         $this->translator
->trans('Libre Air') 
 385                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 388                 $period = new \
DatePeriod( 
 389                         //Start from first monday of week 
 390                         new \
DateTime('Monday this week'), 
 391                         //Iterate on each day 
 392                         new \
DateInterval('P1D'), 
 393                         //End with next sunday and 4 weeks 
 395                                 $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' 
 400                 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')); 
 403                 //XXX: we want to display all active locations anyway 
 404                 $locations = $doctrine->getRepository(Location
::class)->findTranslatedSortedByPeriod($this->translator
, $period); 
 407                 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$this->context
); 
 409                 //Set Cache-Control must-revalidate directive 
 410                 //TODO: add a javascript forced refresh after 1h ? or header refresh ? 
 411                 #$response->setPublic(true); 
 412                 #$response->setMaxAge(300); 
 413                 #$response->mustRevalidate(); 
 414                 ##$response->setCache(['public' => true, 'max_age' => 300]); 
 416                 //Return the response 
 421          * The organizer regulation page 
 423          * @desc Display the organizer regulation policy 
 425          * @return Response The rendered view 
 427         public function organizerRegulation(): Response 
{ 
 429                 $section = $this->translator
->trans('Organizer regulation'); 
 432                 $this->context
['description'] = $this->translator
->trans('Libre Air organizer regulation'); 
 435                 $this->context
['keywords'] = [ 
 436                         $this->translator
->trans('organizer regulation'), 
 437                         $this->translator
->trans('Libre Air') 
 441                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 444                 return $this->render('@RapsysAir/default/organizer_regulation.html.twig', ['title' => $title, 'section' => $section]+
$this->context
); 
 448          * The terms of service page 
 450          * @desc Display the terms of service policy 
 452          * @return Response The rendered view 
 454         public function termsOfService(): Response 
{ 
 456                 $section = $this->translator
->trans('Terms of service'); 
 459                 $this->context
['description'] = $this->translator
->trans('Libre Air terms of service'); 
 462                 $this->context
['keywords'] = [ 
 463                         $this->translator
->trans('terms of service'), 
 464                         $this->translator
->trans('Libre Air') 
 468                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 471                 return $this->render('@RapsysAir/default/terms_of_service.html.twig', ['title' => $title, 'section' => $section]+
$this->context
); 
 475          * The frequently asked questions page 
 477          * @desc Display the frequently asked questions 
 479          * @return Response The rendered view 
 481         public function frequentlyAskedQuestions(): Response 
{ 
 483                 $section = $this->translator
->trans('Frequently asked questions'); 
 486                 $this->context
['description'] = $this->translator
->trans('Libre Air frequently asked questions'); 
 489                 $this->context
['keywords'] = [ 
 490                         $this->translator
->trans('frequently asked questions'), 
 491                         $this->translator
->trans('faq'), 
 492                         $this->translator
->trans('Libre Air') 
 496                 $title = $this->translator
->trans($this->config
['site']['title']).' - '.$section; 
 499                 return $this->render('@RapsysAir/default/frequently_asked_questions.html.twig', ['title' => $title, 'section' => $section]+
$this->context
); 
 503          * Return the bundle alias 
 507         public function getAlias(): string { 
 516         protected function render(string $view, array $parameters = [], Response 
$response = null): Response 
{ 
 517                 //Create application form for role_guest 
 518                 if ($this->isGranted('ROLE_GUEST')) { 
 519                         //Without application form 
 520                         if (empty($parameters['forms']['application'])) { 
 522                                 $doctrine = $this->getDoctrine(); 
 524                                 //Create ApplicationType form 
 525                                 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 527                                         'action' => $this->generateUrl('rapsys_air_application_add'), 
 528                                         //Set the form attribute 
 529                                         'attr' => [ 'class' => 'col' ], 
 531                                         'admin' => $this->isGranted('ROLE_ADMIN'), 
 532                                         //Set default user to current 
 533                                         'user' => $this->getUser()->getId(), 
 534                                         //Set default slot to evening 
 535                                         //XXX: default to Evening (3) 
 536                                         'slot' => $doctrine->getRepository(Slot
::class)->findOneById(3) 
 539                                 //Add form to context 
 540                                 $parameters['forms']['application'] = $application->createView(); 
 542                 //Create login form for anonymous 
 543                 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
 544                         //Create ApplicationType form 
 545                         $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ 
 547                                 'action' => $this->generateUrl('rapsys_user_login'), 
 548                                 //Set the form attribute 
 549                                 'attr' => [ 'class' => 'col' ] 
 552                         //Add form to context 
 553                         $parameters['forms']['login'] = $login->createView(); 
 557                 return $this->_render($view, $parameters, $response);