Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: root
  5.  * Date: 16/01/18
  6.  * Time: 22:21
  7.  */
  8.  
  9. namespace MainBundle\Controller;
  10.  
  11. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Routing\Annotation\Route;
  14.  
  15.  
  16. class ContactController extends Controller
  17. {
  18.     public function sendEmail($data, $subject)
  19.     {
  20.  
  21. //        $subject = implode("','",$subject);
  22.  
  23.         $message = (new \Swift_Message($subject))
  24.             ->setFrom(array($data['email'] => $data["name"]))
  25.             ->setTo('contact@conceptsiteweb.com')
  26.             ->setBody(
  27.                 $this->renderView(
  28.                     '@Main/Contact/Creation/email.html.twig',
  29.                     ['message'=>$data['message'], 'email' => $data['email'], 'firstname' => $data['firstname'], 'name' => $data['name']]
  30.                 ),
  31.                 'text/html'
  32.             );
  33.  
  34.  
  35.  
  36.  
  37.         return $this->get('mailer')->send($message);
  38.     }
  39.  
  40.     /**
  41.      * @Route("/mail-creation.html/",  name="mail_creation_de_site")
  42.      */
  43.     public function formMailAction(Request $request)
  44.     {
  45.  
  46.  
  47.  
  48.  
  49.         $form = $this->createForm('MainBundle\Form\Contact\ContactCreaType',null,array(
  50.             // To set the action use $this->generateUrl('route_identifier')
  51.             'action' => $this->generateUrl('mail_creation_de_site'),
  52.             'method' => 'POST'
  53.         ));
  54.         if ($request->isMethod('POST')) {
  55.             // Refill the fields in case the form is not valid.
  56.             $request->getSession()
  57.                 ->getFlashBag()
  58.                 ->add('success', 'Message envoyé')
  59.             ;
  60.  
  61.             $form->handleRequest($request);
  62.  
  63.             if($form->isValid()){
  64.  
  65.  
  66.                 $url = ($request->headers->get('referer'));
  67.                 $routeName = $this->getRouteNameFromUrl($url);
  68.  
  69.                 if($routeName === 'creation_de_site'){
  70.                     $subject= 'Création de site';
  71.  
  72.                 }elseif (($routeName==='referencement')){
  73.                     $subject= 'Référencement';
  74.  
  75.  
  76.                 }elseif ('depannage'){
  77.                     $subject= 'depannage';
  78.  
  79.                 }
  80.  
  81.                 // Send mail
  82.                 if($this->sendEmail($form->getData(), $subject))
  83.                 {
  84.  
  85.                     // Everything OK, redirect to wherever you want ! :
  86.  
  87.  
  88.                     return $this->redirectToRoute($routeName);
  89.                 }else{
  90.                     // An error ocurred, handle
  91.                     var_dump("Errooooor :(");
  92.                 }
  93.             }
  94.         }
  95.  
  96.         return $this->render('@Main/Contact/Creation/sendmail.html.twig', array(
  97.             'form' => $form->createView(),
  98.  
  99.  
  100.         ));
  101.  
  102.     }
  103.  
  104.  
  105.     private function getRouteNameFromUrl($url)
  106.     {
  107.         $parsed = parse_url($url);
  108.         if (false === isset($parsed['path'])) {
  109.             return null;
  110.         }
  111.  
  112.         $route = $this->get('router')->match($parsed['path']);
  113.  
  114.         return $route['_route'] ?? null;
  115.     }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement