src/Controller/Front/MessageController.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Front;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  8. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  9. use App\Form\ContactFormType;
  10. use App\Form\ContactAgentFormType;
  11. use App\Form\ContactRecrutementFormType;
  12. use App\Service\MailSender;
  13. use App\Entity\Agent;
  14. class MessageController extends AbstractController
  15. {
  16.     public function __construct(ParameterBagInterface $params)
  17.     {
  18.         $this->message_receiver = $params->get('message_receiver');
  19.         $this->contact_recipients = array_values($params->get('contact_recipients'));
  20.         $this->contact_recipient_names = array_keys($params->get('contact_recipients'));
  21.         $this->subject_choices = $params->get('subject_choices');
  22.         $this->menu = $params->get('main_menu');
  23.     }
  24.     
  25.     /**
  26.      * include 
  27.      */
  28.     public function contactForm( $context=null, $display="default", $agent_id=null , $refannonce=null ): Response
  29.     {   
  30.         dump([$context , $display,  $agent_id]);
  31.         switch ($context) {
  32.             case 'agent':
  33.                 $form = $this->createForm(ContactAgentFormType::class, ['agent_id' => $agent_id, 'refannonce' => $refannonce]);
  34.             break;
  35.             case 'devenir_conseiller_':
  36.                 $form = $this->createForm(ContactRecrutementFormType::class);
  37.             break;
  38.             default:
  39.                 $form = $this->createForm(ContactFormType::class);
  40.             break;
  41.         }
  42.         return $this->render('front/message/_contact_form.html.twig', [
  43.             'form' => $form->createView(),
  44.             'context' => $context,
  45.             'display' => $display,
  46.         ]);
  47.     }
  48.     /**
  49.      * include 
  50.      */
  51.     public function contactFormModal( $context=null ): Response
  52.     {
  53.         $form = $this->createForm(ContactFormType::class);
  54.         return $this->render('front/message/_contact_form_modal.html.twig', [
  55.             'form' => $form->createView(),
  56.             'context' => $context,
  57.             'modale_name' => $context == 'agent' ? 'contact_modal_agent' : 'contact_modal'
  58.         ]);
  59.     }
  60.     
  61.     /**
  62.      * include 
  63.      */
  64.     public function contactFormFooter( $context=null ): Response
  65.     {
  66.         switch ($context) {
  67.             case 'devenir_conseiller_':
  68.                 $form = $this->createForm(ContactRecrutementFormType::class);
  69.             break;
  70.             default:
  71.                 $form = $this->createForm(ContactFormType::class);
  72.             break;
  73.         }
  74.         return $this->render('front/message/_contact_form_footer.html.twig', [
  75.             'form' => $form->createView(),
  76.             'context' => $context,
  77.         ]);
  78.     }
  79.     /** Ajax
  80.      * @Route("/contact", name="contact") 
  81.      */    
  82.     public function contact( Request $request, MailSender $mail_sender ): Response
  83.     {
  84.         $context = $request->request->has('context') ? $request->request->get('context') : null;
  85.         switch ($context) {
  86.             case 'agent':
  87.                 $form = $this->createForm(ContactAgentFormType::class);
  88.             break;
  89.             case 'devenir_conseiller_':
  90.                 $form = $this->createForm(ContactRecrutementFormType::class);
  91.             break;
  92.             default:
  93.                 $form = $this->createForm(ContactFormType::class);
  94.             break;
  95.         }
  96.         $form->handleRequest($request);
  97.         if ( $request->isXmlHttpRequest() && $form->isSubmitted() ) {
  98.             if ( $form->isValid() ) {
  99.                 $data = $form->getData();
  100.                 if($context == 'agent'){
  101.                     $agent = $this->getDoctrine()->getRepository(Agent::class)->find( $data['agent_id'] );
  102.                     $to = $agent->getEmail();
  103.                     $from = $data['email'];   
  104.                     $subject = "Prise de contact depuis www.sublimons.com";    
  105.                     $bcc = null;
  106.                 }
  107.                 elseif($context == 'devenir_conseiller_'){
  108.                     $to = $this->contact_recipients['Sublimons Morbihan'];
  109.                     $recipient_name = $this->contact_recipient_names[$data['Sublimons Morbihan']];
  110.                     $from = $data['email'];  
  111.                     $subject = "Prise de contact [Recrutement] depuis www.sublimons.com - " . $recipient_name ; 
  112.                     $bcc = 'contact@sublimons.com';
  113.                 }
  114.                 else{
  115.                     $to = $this->contact_recipients[0];
  116.                     $recipient_name = $this->contact_recipient_names[0];
  117.                     $from = $data['email'];  
  118.                     // $subject = "Prise de contact [".$this->getPageTitle($context)."] depuis www.sublimons.com - " . $recipient_name;
  119.                     $subject = "Prise de contact depuis www.sublimons.com - [".$this->getPageTitle($context)."]"; 
  120.                     $bcc = 'contact@sublimons.com';
  121.                     $data['sujet'] = "Je souhaite " .$this->subject_choices[$data['sujet']];
  122.                 }
  123.                 
  124.                 $data['page_title'] = $this->getPageTitle($context);
  125.                 $message = $this->render('front/message/_contact.html.twig', $data)->getContent();
  126.                 $result = $mail_sender->send($to, $from, $subject, $message, [], 'default', $bcc);  
  127.                 if($result['antifa']){
  128.                     $output['success'] = true;
  129.                     $output['msg'] = "Votre message a bien été envoyé à la <b>POLICE</b>.";   
  130.                 }                
  131.                 elseif($result['success']){
  132.                     $output['success'] = true;
  133.                     $output['msg'] = "Votre message a bien été envoyé";   
  134.                 }
  135.                 else{
  136.                     $output['error'] = true;
  137.                     $output['msg'] = $result['error'];
  138.                 }
  139.             }
  140.             else{
  141.                 $output['error'] = true;
  142.                 $output['msg'] = 'Formulaire invalide';
  143.                 $output['form'] = $this->render('front/message/_contact_form.html.twig', ['form' => $form->createView(), 'context' => $context])->getContent();
  144.             }
  145.            return $this->json($output);
  146.         }
  147.         else{
  148.             throw new AccessDeniedHttpException("L'action demandée est impossible");
  149.         }
  150.     }
  151.     public function getPageTitle($context)
  152.     {   
  153.         switch ($context) {
  154.             case 'homepage':
  155.                 $page_title = "Accueil";
  156.             break;
  157.             case 'agent':
  158.                 $page_title = "Agent";
  159.             break;
  160.             case 'show_agent':
  161.                 $page_title = "Agent";
  162.             break;
  163.             default:
  164.                 $page_title = $this->menu[$context];
  165.             break;
  166.         }
  167.         return $page_title;
  168.     }
  169. }