]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/DefaultController.php
Improve method documentation
[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 use Rapsys\UserBundle\Utils\Slugger;
19
20 class DefaultController extends AbstractController {
21 //Config array
22 protected $config;
23
24 //Context array
25 protected $context;
26
27 //Router instance
28 protected $router;
29
30 //Slugger instance
31 protected $slugger;
32
33 //Translator instance
34 protected $translator;
35
36 /**
37 * Inject container and translator interface
38 *
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
43 */
44 public function __construct(ContainerInterface $container, RouterInterface $router, Slugger $slugger, TranslatorInterface $translator) {
45 //Retrieve config
46 $this->config = $container->getParameter($this->getAlias());
47
48 //Set the router
49 $this->router = $router;
50
51 //Set the slugger
52 $this->slugger = $slugger;
53
54 //Set the translator
55 $this->translator = $translator;
56
57 //Set the context
58 $this->context = [
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'])
67 ];
68 }
69
70 /**
71 * The contact page
72 *
73 * @desc Send a contact mail to configured contact
74 *
75 * @param Request $request The request instance
76 * @param MailerInterface $mailer The mailer instance
77 *
78 * @return Response The rendered view or redirection
79 */
80 public function contact(Request $request, MailerInterface $mailer) {
81 //Set section
82 $section = $this->translator->trans('Contact');
83
84 //Set title
85 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
86
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 // To set the action use $this->generateUrl('route_identifier')
91 'action' => $this->generateUrl('rapsys_air_contact'),
92 'method' => 'POST'
93 ]);
94
95 if ($request->isMethod('POST')) {
96 // Refill the fields in case the form is not valid.
97 $form->handleRequest($request);
98
99 if ($form->isValid()) {
100 //Get data
101 $data = $form->getData();
102
103 //Create message
104 $message = (new TemplatedEmail())
105 //Set sender
106 ->from(new Address($data['mail'], $data['name']))
107 //Set recipient
108 //XXX: remove the debug set in vendor/symfony/mime/Address.php +46
109 ->to(new Address($this->config['contact']['mail'], $this->config['contact']['name']))
110 //Set subject
111 ->subject($data['subject'])
112
113 //Set path to twig templates
114 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
115 ->textTemplate('@RapsysAir/mail/contact.text.twig')
116
117 //Set context
118 ->context(
119 [
120 'subject' => $data['subject'],
121 'message' => strip_tags($data['message']),
122 ]+$this->context
123 );
124
125 //Try sending message
126 //XXX: mail delivery may silently fail
127 try {
128 //Send message
129 $mailer->send($message);
130
131 //Redirect on the same route with sent=1 to cleanup form
132 return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
133 //Catch obvious transport exception
134 } catch(TransportExceptionInterface $e) {
135 if ($message = $e->getMessage()) {
136 //Add error message mail unreachable
137 $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 } else {
139 //Add error message mail unreachable
140 $form->get('mail')->addError(new FormError($this->translator->trans('Unable to contact: %mail%', ['%mail%' => $this->config['contact']['mail']])));
141 }
142 }
143 }
144 }
145
146 //Render template
147 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
148 }
149
150 /**
151 * The index page
152 *
153 * @desc Welcome the user
154 *
155 * @return Response The rendered view
156 */
157 public function index() {
158 //Set section
159 $section = $this->translator->trans('Index');
160
161 //Set title
162 $title = $section.' - '.$this->context['site_title'];
163
164 //Render template
165 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section]+$this->context);
166 }
167
168 /**
169 * The regulation page
170 *
171 * @desc Display the regulation policy
172 *
173 * @return Response The rendered view
174 */
175 public function regulation() {
176 //Set section
177 $section = $this->translator->trans('Regulation');
178
179 //Set title
180 $title = $section.' - '.$this->context['site_title'];
181
182 //Render template
183 return $this->render('@RapsysAir/default/regulation.html.twig', ['title' => $title, 'section' => $section]+$this->context);
184 }
185
186 /**
187 * Return the bundle alias
188 *
189 * {@inheritdoc}
190 */
191 public function getAlias() {
192 return 'rapsys_air';
193 }
194 }