- public function rootAction() {
- //Set default locale
- $locale = 'en';
-
- //Supported application languages
- $supportedLanguage = explode('|', $this->getParameter('blog.locales'));
-
- //Language list
- if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
- //Init array
- $httpAcceptLanguage = [];
-
- //Extract languages
- foreach(explode(',',str_replace('-','_',$_SERVER['HTTP_ACCEPT_LANGUAGE'])) as $candidate) {
- //Extract candidate and optional weight
- @list($candidate, $weight) = explode(';', $candidate);
- if (!empty($candidate)) {
- $httpAcceptLanguage[!empty($weight)?$weight:1][] = $candidate;
+ public function contact(Request $request): Response {
+ //Set title
+ $this->context['title'] = $this->translator->trans('Contact');
+
+ //Set description
+ $this->context['description'] = $this->translator->trans('Welcome to raphaël\'s developer diary contact page');
+
+ //Set keywords
+ $this->context['keywords'] = $this->translator->trans('contact');
+
+ //Set data
+ $data = [];
+
+ //With user
+ if ($user = $this->security->getUser()) {
+ //Set data
+ $data = [
+ 'name' => $user->getRecipientName(),
+ 'mail' => $user->getMail()
+ ];
+ }
+
+ //Create response
+ $response = new Response();
+
+ //Create the form according to the FormType created previously.
+ //And give the proper parameters
+ $form = $this->createForm('Rapsys\BlogBundle\Form\ContactType', $data, [
+ 'action' => $this->generateUrl('rapsys_blog_contact'),
+ 'method' => 'POST'
+ ]);
+
+ if ($request->isMethod('POST')) {
+ // Refill the fields in case the form is not valid.
+ $form->handleRequest($request);
+
+ if ($form->isSubmitted() && $form->isValid()) {
+ //Get data
+ $data = $form->getData();
+
+ //Set context
+ $context = [
+ 'subject' => $data['subject'],
+ 'message' => strip_tags($data['message']),
+ ]+$this->context;
+
+ //Create message
+ $message = (new TemplatedEmail())
+ //Set sender
+ ->from(new Address($data['mail'], $data['name']))
+ //Set recipient
+ ->to(new Address($this->config['contact']['address'], $this->config['contact']['name']))
+ //Set subject
+ ->subject($data['subject'])
+
+ //Set path to twig templates
+ ->htmlTemplate('@RapsysBlog/mail/contact.html.twig')
+ ->textTemplate('@RapsysBlog/mail/contact.text.twig')
+
+ //Set context
+ ->context($context);
+
+ //Try sending message
+ //XXX: mail delivery may silently fail
+ try {
+ //Send message
+ $this->mailer->send($message);
+
+ //Redirect on the same route with sent=1 to cleanup form
+ return $this->redirectToRoute($request->get('_route'), ['sent' => 1]+$request->get('_route_params'));
+ //Catch obvious transport exception
+ } catch(TransportExceptionInterface $e) {
+ //Add error message mail unreachable
+ $form->get('mail')->addError(new FormError($this->translator->trans('Unable to reach account')));