]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/DefaultController.php
Switch from Mime\NamedAddress to Mime\Address as it do not exists anymore in symfony 4.4
[airbundle] / Controller / DefaultController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
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
19 class DefaultController extends AbstractController {
20 //Config array
21 protected $config;
22
23 //Context array
24 protected $context;
25
26 //Translator instance
27 protected $translator;
28
29 /**
30 * Inject container and translator interface
31 */
32 public function __construct(ContainerInterface $container, TranslatorInterface $translator, RouterInterface $router) {
33 //Retrieve config
34 $this->config = $container->getParameter($this->getAlias());
35
36 //Set the translator
37 $this->translator = $translator;
38
39 //Set the context
40 $this->context = [
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')
49 ];
50 }
51
52 /**
53 * The contact page
54 */
55 public function contact(Request $request, MailerInterface $mailer) {
56 //Set section
57 $section = $this->translator->trans('Contact');
58
59 //Set title
60 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
61
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'),
67 'method' => 'POST'
68 ]);
69
70 if ($request->isMethod('POST')) {
71 // Refill the fields in case the form is not valid.
72 $form->handleRequest($request);
73
74 if ($form->isValid()) {
75 //Get data
76 $data = $form->getData();
77
78 //Create message
79 $message = (new TemplatedEmail())
80 //Set sender
81 ->from(new Address($data['mail'], $data['name']))
82 //Set recipient
83 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
84 ->to(new Address($this->config['contact']['mail'], $this->config['contact']['name']))
85 //Set subject
86 ->subject($data['subject'])
87
88 //Set path to twig templates
89 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
90 ->textTemplate('@RapsysAir/mail/contact.text.twig')
91
92 //Set context
93 ->context(
94 [
95 'subject' => $data['subject'],
96 'message' => strip_tags($data['message']),
97 ]+$this->context
98 );
99
100 //Try sending message
101 //XXX: mail delivery may silently fail
102 try {
103 //Send message
104 $mailer->send($message);
105
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)])));
113 } else {
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']])));
116 }
117 }
118 }
119 }
120
121 //Render template
122 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
123 }
124
125 /**
126 * The index page
127 */
128 public function index() {
129 //Set section
130 $section = $this->translator->trans('Index');
131
132 //Set title
133 $title = $section.' - '.$this->context['site_title'];
134
135 //Render template
136 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section]+$this->context);
137 }
138
139 /**
140 * The policy page
141 */
142 public function policy() {
143 //Set section
144 $section = $this->translator->trans('Policy');
145
146 //Set title
147 $title = $section.' - '.$this->context['site_title'];
148
149 //Render template
150 return $this->render('@RapsysAir/default/policy.html.twig', ['title' => $title, 'section' => $section]+$this->context);
151 }
152
153 /**
154 * Return the bundle alias
155 *
156 * {@inheritdoc}
157 */
158 public function getAlias() {
159 return 'rapsys_air';
160 }
161 }