3 namespace Rapsys\AirBundle\Controller
;
5 use Rapsys\AirBundle\Entity\Application
;
6 use Rapsys\AirBundle\Entity\Session
;
7 use Symfony\Bridge\Twig\Mime\TemplatedEmail
;
8 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController
;
9 use Symfony\Component\DependencyInjection\ContainerInterface
;
10 use Symfony\Component\Form\FormError
;
11 use Symfony\Component\HttpFoundation\Request
;
12 use Symfony\Component\Mailer\Exception\TransportExceptionInterface
;
13 use Symfony\Component\Mailer\MailerInterface
;
14 use Symfony\Component\Mime\NamedAddress
;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
16 use Symfony\Component\Routing\RouterInterface
;
17 use Symfony\Component\Translation\TranslatorInterface
;
19 class DefaultController
extends AbstractController
{
27 protected $translator;
30 * Inject container and translator interface
32 public function __construct(ContainerInterface
$container, TranslatorInterface
$translator, RouterInterface
$router) {
34 $this->config
= $container->getParameter($this->getAlias());
37 $this->translator
= $translator;
41 'copy_long' => $translator->trans($this->config
['copy']['long']),
42 'copy_short' => $translator->trans($this->config
['copy']['short']),
43 'site_ico' => $this->config
['site']['ico'],
44 'site_logo' => $this->config
['site']['logo'],
45 'site_png' => $this->config
['site']['png'],
46 'site_svg' => $this->config
['site']['svg'],
47 'site_title' => $translator->trans($this->config
['site']['title']),
48 'site_url' => $router->generate('rapsys_air_index')
55 public function contact(Request
$request, MailerInterface
$mailer) {
57 $section = $this->translator
->trans('Contact');
60 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
62 //Create the form according to the FormType created previously.
63 //And give the proper parameters
64 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
65 // To set the action use $this->generateUrl('route_identifier')
66 'action' => $this->generateUrl('rapsys_air_contact'),
70 if ($request->isMethod('POST')) {
71 // Refill the fields in case the form is not valid.
72 $form->handleRequest($request);
74 if ($form->isValid()) {
76 $data = $form->getData();
79 $message = (new TemplatedEmail())
81 ->from(new NamedAddress($data['mail'], $data['name']))
83 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
84 ->to(new NamedAddress($this->config
['contact']['mail'], $this->config
['contact']['name']))
86 ->subject($data['subject'])
88 //Set path to twig templates
89 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
90 ->textTemplate('@RapsysAir/mail/contact.text.twig')
95 'subject' => $data['subject'],
96 'message' => strip_tags($data['message']),
100 //Try sending message
101 //XXX: mail delivery may silently fail
104 $mailer->send($message);
106 //Redirect on the same route with sent=1 to cleanup form
107 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params'));
108 //Catch obvious transport exception
109 } catch(TransportExceptionInterface
$e) {
110 if ($message = $e->getMessage()) {
111 //Add error message mail unreachable
112 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config
['contact']['mail'], '%message%' => $this->translator
->trans($message)])));
114 //Add error message mail unreachable
115 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%', ['%mail%' => $this->config
['contact']['mail']])));
122 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
);
128 public function index() {
130 $section = $this->translator
->trans('Index');
133 $title = $section.' - '.$this->context
['site_title'];
136 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
142 public function policy() {
144 $section = $this->translator
->trans('Policy');
147 $title = $section.' - '.$this->context
['site_title'];
150 return $this->render('@RapsysAir/default/policy.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
154 * Return the bundle alias
158 public function getAlias() {