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\Address
;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface
;
16 use Symfony\Component\Routing\RouterInterface
;
17 use Symfony\Component\Translation\TranslatorInterface
;
18 use Rapsys\UserBundle\Utils\Slugger
;
20 class DefaultController
extends AbstractController
{
34 protected $translator;
37 * Inject container and translator interface
39 * @param ContainerInterface $container The container instance
40 * @param RouterInterface $router The router instance
41 * @param Slugger $slugger The slugger instance
42 * @param TranslatorInterface $translator The translator instance
44 public function __construct(ContainerInterface
$container, RouterInterface
$router, Slugger
$slugger, TranslatorInterface
$translator) {
46 $this->config
= $container->getParameter($this->getAlias());
49 $this->router
= $router;
52 $this->slugger
= $slugger;
55 $this->translator
= $translator;
59 'copy_long' => $translator->trans($this->config
['copy']['long']),
60 'copy_short' => $translator->trans($this->config
['copy']['short']),
61 'site_ico' => $this->config
['site']['ico'],
62 'site_logo' => $this->config
['site']['logo'],
63 'site_png' => $this->config
['site']['png'],
64 'site_svg' => $this->config
['site']['svg'],
65 'site_title' => $translator->trans($this->config
['site']['title']),
66 'site_url' => $router->generate($this->config
['site']['url'])
73 * @desc Send a contact mail to configured contact
75 * @param Request $request The request instance
76 * @param MailerInterface $mailer The mailer instance
78 * @return Response The rendered view or redirection
80 public function contact(Request
$request, MailerInterface
$mailer) {
82 $section = $this->translator
->trans('Contact');
85 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
87 //Create the form according to the FormType created previously.
88 //And give the proper parameters
89 $form = $this->createForm('Rapsys\AirBundle\Form\ContactType', null, [
90 'action' => $this->generateUrl('rapsys_air_contact'),
94 if ($request->isMethod('POST')) {
95 // Refill the fields in case the form is not valid.
96 $form->handleRequest($request);
98 if ($form->isValid()) {
100 $data = $form->getData();
103 $message = (new TemplatedEmail())
105 ->from(new Address($data['mail'], $data['name']))
107 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
108 ->to(new Address($this->config
['contact']['mail'], $this->config
['contact']['name']))
110 ->subject($data['subject'])
112 //Set path to twig templates
113 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
114 ->textTemplate('@RapsysAir/mail/contact.text.twig')
119 'subject' => $data['subject'],
120 'message' => strip_tags($data['message']),
124 //Try sending message
125 //XXX: mail delivery may silently fail
128 $mailer->send($message);
130 //Redirect on the same route with sent=1 to cleanup form
131 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+
$request->get('_route_params'));
132 //Catch obvious transport exception
133 } catch(TransportExceptionInterface
$e) {
134 if ($message = $e->getMessage()) {
135 //Add error message mail unreachable
136 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%: %message%', ['%mail%' => $this->config
['contact']['mail'], '%message%' => $this->translator
->trans($message)])));
138 //Add error message mail unreachable
139 $form->get('mail')->addError(new FormError($this->translator
->trans('Unable to contact: %mail%', ['%mail%' => $this->config
['contact']['mail']])));
146 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query
->get('sent', 0)]+
$this->context
);
152 * @desc Welcome the user
154 * @return Response The rendered view
156 public function index() {
158 $section = $this->translator
->trans('Index');
161 $title = $section.' - '.$this->context
['site_title'];
164 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
168 * The regulation page
170 * @desc Display the regulation policy
172 * @return Response The rendered view
174 public function regulation() {
176 $section = $this->translator
->trans('Regulation');
179 $title = $section.' - '.$this->context
['site_title'];
182 return $this->render('@RapsysAir/default/regulation.html.twig', ['title' => $title, 'section' => $section]+
$this->context
);
186 * Return the bundle alias
190 public function getAlias() {