]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/DefaultController.php
Replace location.index by session.index
[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 'action' => $this->generateUrl('rapsys_air_contact'),
91 'method' => 'POST'
92 ]);
93
94 if ($request->isMethod('POST')) {
95 // Refill the fields in case the form is not valid.
96 $form->handleRequest($request);
97
98 if ($form->isValid()) {
99 //Get data
100 $data = $form->getData();
101
102 //Create message
103 $message = (new TemplatedEmail())
104 //Set sender
105 ->from(new Address($data['mail'], $data['name']))
106 //Set recipient
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']))
109 //Set subject
110 ->subject($data['subject'])
111
112 //Set path to twig templates
113 ->htmlTemplate('@RapsysAir/mail/contact.html.twig')
114 ->textTemplate('@RapsysAir/mail/contact.text.twig')
115
116 //Set context
117 ->context(
118 [
119 'subject' => $data['subject'],
120 'message' => strip_tags($data['message']),
121 ]+$this->context
122 );
123
124 //Try sending message
125 //XXX: mail delivery may silently fail
126 try {
127 //Send message
128 $mailer->send($message);
129
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)])));
137 } else {
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']])));
140 }
141 }
142 }
143 }
144
145 //Render template
146 return $this->render('@RapsysAir/form/contact.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'sent' => $request->query->get('sent', 0)]+$this->context);
147 }
148
149 /**
150 * The index page
151 *
152 * @desc Welcome the user
153 *
154 * @return Response The rendered view
155 */
156 public function index() {
157 //Set section
158 $section = $this->translator->trans('Index');
159
160 //Set title
161 $title = $section.' - '.$this->context['site_title'];
162
163 //Render template
164 return $this->render('@RapsysAir/default/index.html.twig', ['title' => $title, 'section' => $section]+$this->context);
165 }
166
167 /**
168 * The regulation page
169 *
170 * @desc Display the regulation policy
171 *
172 * @return Response The rendered view
173 */
174 public function regulation() {
175 //Set section
176 $section = $this->translator->trans('Regulation');
177
178 //Set title
179 $title = $section.' - '.$this->context['site_title'];
180
181 //Render template
182 return $this->render('@RapsysAir/default/regulation.html.twig', ['title' => $title, 'section' => $section]+$this->context);
183 }
184
185 /**
186 * Return the bundle alias
187 *
188 * {@inheritdoc}
189 */
190 public function getAlias() {
191 return 'rapsys_air';
192 }
193 }