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\Mailer\Exception\TransportExceptionInterface
; 
  16 use Symfony\Component\Mailer\MailerInterface
; 
  17 use Symfony\Component\Mime\Address
; 
  18 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
; 
  19 use Symfony\Component\Routing\RouterInterface
; 
  20 use Symfony\Component\Translation\TranslatorInterface
; 
  21 use Rapsys\UserBundle\Utils\Slugger
; 
  24 class DefaultController 
extends AbstractController 
{ 
  38         protected $translator; 
  41          * Inject container and translator interface 
  43          * @param ContainerInterface $container The container instance 
  44          * @param RouterInterface $router The router instance 
  45          * @param Slugger $slugger The slugger instance 
  46          * @param TranslatorInterface $translator The translator instance 
  48         public function __construct(ContainerInterface 
$container, RouterInterface 
$router, Slugger 
$slugger, TranslatorInterface 
$translator) { 
  50                 $this->config 
= $container->getParameter($this->getAlias()); 
  53                 $this->router 
= $router; 
  56                 $this->slugger 
= $slugger; 
  59                 $this->translator 
= $translator; 
  63                         'copy_long' => $translator->trans($this->config
['copy']['long']), 
  64                         'copy_short' => $translator->trans($this->config
['copy']['short']), 
  65                         'site_ico' => $this->config
['site']['ico'], 
  66                         'site_logo' => $this->config
['site']['logo'], 
  67                         'site_png' => $this->config
['site']['png'], 
  68                         'site_svg' => $this->config
['site']['svg'], 
  69                         'site_title' => $translator->trans($this->config
['site']['title']), 
  70                         'site_url' => $router->generate($this->config
['site']['url']) 
  77          * @desc Send a contact mail to configured contact 
  79          * @param Request $request The request instance 
  80          * @param MailerInterface $mailer The mailer instance 
  82          * @return Response The rendered view or redirection 
  84         public function contact(Request 
$request, MailerInterface 
$mailer) { 
  86                 $section = $this->translator
->trans('Contact'); 
  89                 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']); 
  91                 //Create the form according to the FormType created previously. 
  92                 //And give the proper parameters 
  93                 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [ 
  94                         'action' => $this->generateUrl('rapsys_air_contact'), 
  98                 if ($request->isMethod('POST')) { 
  99                         // Refill the fields in case the form is not valid. 
 100                         $form->handleRequest($request); 
 102                         if ($form->isValid()) { 
 104                                 $data = $form->getData(); 
 107                                 $message = (new TemplatedEmail()) 
 109                                         ->from(new Address($data['mail'], $data['name'])) 
 111                                         //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 
 112                                         ->to(new Address($this->config
['contact']['mail'], $this->config
['contact']['name'])) 
 114                                         ->subject($data['subject']) 
 116                                         //Set path to twig templates 
 117                                         ->htmlTemplate('@RapsysAir/mail/contact.html.twig') 
 118                                         ->textTemplate('@RapsysAir/mail/contact.text.twig') 
 123                                                         'subject' => $data['subject'], 
 124                                                         'message' => strip_tags($data['message']), 
 128                                 //Try sending message 
 129                                 //XXX: mail delivery may silently fail 
 132                                         $mailer->send($message); 
 134                                         //Redirect on the same route with sent=1 to cleanup form 
 135                                         return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params')); 
 136                                 //Catch obvious transport exception 
 137                                 } catch(TransportExceptionInterface 
$e) { 
 138                                         if ($message = $e->getMessage()) { 
 139                                                 //Add error message mail unreachable 
 140                                                 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config
['contact']['mail'], '%message%' => $this->translator
->trans($message)]))); 
 142                                                 //Add error message mail unreachable 
 143                                                 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%', ['%mail%' => $this->config
['contact']['mail']]))); 
 150                 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
); 
 156          * @desc Display all granted sessions with an application or login form 
 158          * @param Request $request The request instance 
 160          * @return Response The rendered view 
 162         public function index(Request 
$request = null) { 
 164                 $doctrine = $this->getDoctrine(); 
 167                 $section = $this->translator
->trans('Index'); 
 170                 $title = $section.' - '.$this->context
['site_title']; 
 175                 //Create application form for role_guest 
 176                 if ($this->isGranted('ROLE_GUEST')) { 
 177                         //Create ApplicationType form 
 178                         $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ 
 180                                 'action' => $this->generateUrl('rapsys_air_application_add'), 
 181                                 //Set the form attribute 
 182                                 'attr' => [ 'class' => 'col' ], 
 184                                 'admin' => $this->isGranted('ROLE_ADMIN'), 
 185                                 //Set default user to current 
 186                                 'user' => $this->getUser()->getId(), 
 187                                 //Set default slot to evening 
 188                                 //XXX: default to Evening (3) 
 189                                 'slot' => $doctrine->getRepository(Slot
::class)->findOneById(3) 
 192                         //Add form to context 
 193                         $context['application'] = $application->createView(); 
 194                 //Create login form for anonymous 
 195                 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { 
 196                         //Create ApplicationType form 
 197                         $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ 
 199                                 'action' => $this->generateUrl('rapsys_user_login'), 
 200                                 //Set the form attribute 
 201                                 'attr' => [ 'class' => 'col' ] 
 204                         //Add form to context 
 205                         $context['login'] = $login->createView(); 
 209                 $period = new \
DatePeriod( 
 210                         //Start from first monday of week 
 211                         new \
DateTime('Monday this week'), 
 212                         //Iterate on each day 
 213                         new \
DateInterval('P1D'), 
 214                         //End with next sunday and 4 weeks 
 215                         new \
DateTime('Monday this week + 5 week') 
 219                 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'), true); 
 222                 $locations = $doctrine->getRepository(Location
::class)->fetchTranslatedLocationByDatePeriod($this->translator
, $period, true); 
 225                 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$context+
$this->context
); 
 230          * The regulation page 
 232          * @desc Display the regulation policy 
 234          * @return Response The rendered view 
 236         public function regulation() { 
 238                 $section = $this->translator
->trans('Regulation'); 
 241                 $title = $section.' - '.$this->context
['site_title']; 
 244                 return $this->render('@RapsysAir/default/regulation.html.twig', ['title' => $title, 'section' => $section]+
$this->context
); 
 248          * Return the bundle alias 
 252         public function getAlias() {