X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/574f848e49b8f555159c2eac67fcea0104f0fe8b..41477ffe9567f40bd2ff3ad83d2a54fd0f206a53:/Controller/DefaultController.php?ds=inline diff --git a/Controller/DefaultController.php b/Controller/DefaultController.php index 31d4b89..078b180 100644 --- a/Controller/DefaultController.php +++ b/Controller/DefaultController.php @@ -2,23 +2,40 @@ namespace Rapsys\AirBundle\Controller; -use Symfony\Bundle\FrameworkBundle\Controller\Controller; -use Symfony\Component\Routing\Generator\UrlGeneratorInterface; -use Symfony\Component\HttpFoundation\Request; -use Rapsys\AirBundle\Entity\Session; use Rapsys\AirBundle\Entity\Application; +use Rapsys\AirBundle\Entity\Session; +use Symfony\Bridge\Twig\Mime\TemplatedEmail; +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Form\FormError; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; +use Symfony\Component\Mailer\MailerInterface; +use Symfony\Component\Mime\NamedAddress; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Translation\TranslatorInterface; -class DefaultController extends Controller { - public function contactAction(Request $request) { - //Get translator - $trans = $this->get('translator'); +class DefaultController extends AbstractController { + //Config array + protected $config; + //Translator instance + protected $translator; + + public function __construct(ContainerInterface $container, TranslatorInterface $translator) { + //Retrieve config + $this->config = $container->getParameter($this->getAlias()); + + //Set the translator + $this->translator = $translator; + } + + public function contact(Request $request, MailerInterface $mailer) { //Set section - $section = $trans->trans('Contact'); + $section = $this->translator->trans('Contact'); //Set title - $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); + $title = $section.' - '.$this->translator->trans($this->config['site']['title']); //Create the form according to the FormType created previously. //And give the proper parameters @@ -36,45 +53,50 @@ class DefaultController extends Controller { //Get data $data = $form->getData(); - //Get contact name - $contactName = $this->getParameter('rapsys_air.contact_name'); - - //Get contact mail - $contactMail = $this->getParameter('rapsys_air.contact_mail'); - - //Get logo - $logo = $this->getParameter('rapsys_air.logo'); - - //Get title - $title = $trans->trans($this->getParameter('rapsys_air.title')); - - //Get subtitle - $subtitle = $trans->trans('Hi,').' '.$contactName; - - $message = \Swift_Message::newInstance() - ->setSubject($data['subject']) - ->setFrom([$data['mail'] => $data['name']]) - ->setTo([$contactMail => $contactName]) - ->setBody($data['message']) - ->addPart( - $this->renderView( - '@RapsysAir/mail/generic.html.twig', - [ - 'logo' => $logo, - 'title' => $title, - 'subtitle' => $subtitle, - 'home' => $this->get('router')->generate('rapsys_air_homepage', [], UrlGeneratorInterface::ABSOLUTE_URL), - 'subject' => $data['subject'], - 'contact_name' => $contactName, - 'message' => strip_tags($data['message']) - ] - ), - 'text/html' + //Create message + $message = (new TemplatedEmail()) + //Set sender + ->from(new NamedAddress($data['mail'], $data['name'])) + //Set recipient + //XXX: remove the debug set in vendor/symfony/mime/Address.php +46 + ->to(new NamedAddress($this->config['contact']['mail'], $this->config['contact']['name'])) + //Set subject + ->subject($data['subject']) + + //Set path to twig templates + ->htmlTemplate('@RapsysAir/mail/contact.html.twig') + ->textTemplate('@RapsysAir/mail/contact.text.twig') + + //Set context + ->context( + [ + 'site_logo' => $this->config['site']['logo'], + 'site_title' => $this->config['site']['title'], + 'site_url' => $this->get('router')->generate('rapsys_air_homepage', [], UrlGeneratorInterface::ABSOLUTE_URL), + 'copy_long' => $this->config['copy']['long'], + 'copy_short' => $this->config['copy']['short'], + 'subject' => $data['subject'], + 'message' => strip_tags($data['message']), + ] ); - //Send message - if ($this->get('mailer')->send($message)) { - //Redirect to cleanup the form - return $this->redirectToRoute('rapsys_air_contact', ['sent' => 1]); + + //Try sending message + //XXX: mail delivery may silently fail + try { + //Send message + $mailer->send($message); + + //Redirect on the same route with sent=1 to cleanup form + return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params')); + //Catch obvious transport exception + } catch(TransportExceptionInterface $e) { + if ($message = $e->getMessage()) { + //Add error message mail unreachable + $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config['contact']['mail'], '%message%' => $this->translator->trans($message)]))); + } else { + //Add error message mail unreachable + $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']]))); + } } } } @@ -83,31 +105,30 @@ class DefaultController extends Controller { return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]); } - public function indexAction() { - //Get translator - $trans = $this->get('translator'); - + public function index() { //Set section - $section = $trans->trans('Index'); + $section = $this->translator->trans('Index'); //Set title - $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); + $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + //Render template return $this->render('@RapsysAir/page/index.html.twig', ['title' => $title, 'section' => $section]); } - public function adminAction(Request $request) { - $this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!'); - - //Get translator - $trans = $this->get('translator'); + public function admin(Request $request) { + //Prevent non-admin to access here + $this->denyAccessUnlessGranted('ROLE_GUEST', null, 'Unable to access this page without ROLE_GUEST!'); //Set section - $section = $trans->trans('Admin'); + $section = $this->translator->trans('Admin'); //Set title - $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); + $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + header('Content-Type: text/plain'); + var_dump('TODO'); + exit; //Create the form according to the FormType created previously. //And give the proper parameters $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ @@ -158,7 +179,7 @@ class DefaultController extends Controller { $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $this->getUser()); //Add error message to mail field - $form->get('slot')->addError(new FormError($trans->trans('Application already exists'))); + $form->get('slot')->addError(new FormError($this->translator->trans('Application already exists'))); //Catch no application cases //XXX: combine these catch when php 7.1 is available } catch (\Doctrine\ORM\NoResultException $e) { @@ -181,7 +202,7 @@ class DefaultController extends Controller { $manager->flush(); //Add notice in flash message - $this->addFlash('notice', $trans->trans('Application request the %date% for %location% on the slot %slot% saved', ['%location%' => $data['location']->getTitle(), '%slot%' => $data['slot']->getTitle(), '%date%' => $data['date']->format('Y-m-d')])); + $this->addFlash('notice', $this->translator->trans('Application request the %date% for %location% on the slot %slot% saved', ['%location%' => $data['location']->getTitle(), '%slot%' => $data['slot']->getTitle(), '%date%' => $data['date']->format('Y-m-d')])); //Redirect to cleanup the form return $this->redirectToRoute('rapsys_air_admin'); @@ -271,19 +292,16 @@ class DefaultController extends Controller { return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]); } - public function sessionAction(Request $request, $id) { + public function session(Request $request, $id) { /*header('Content-Type: text/plain'); var_dump($calendar); exit;*/ - //Get translator - $trans = $this->get('translator'); - //Set section - $section = $trans->trans('Session %id%', ['%id%' => $id]); + $section = $this->translator->trans('Session %id%', ['%id%' => $id]); //Set title - $title = $section.' - '.$trans->trans($this->getParameter('rapsys_air.title')); + $title = $section.' - '.$this->translator->trans($this->config['site']['title']); //Create the form according to the FormType created previously. //And give the proper parameters @@ -302,4 +320,11 @@ class DefaultController extends Controller { return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]); } + + /** + * {@inheritdoc} + */ + public function getAlias() { + return 'rapsys_air'; + } }