src/Controller/ContactController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore\Controller\FrontendController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\RequestStack;
  6. use Symfony\Contracts\Translation\TranslatorInterface;
  7. use Pimcore\Model\DataObject;
  8. use Pimcore\Model\Document;
  9. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Pimcore\Model\WebsiteSetting;
  12. use App\Form\ExpertiseFormType;
  13. use App\Form\ContactFormType;
  14. use App\Form\ContactModalFormType;
  15. use Pimcore\Mail;
  16. use Carbon\Carbon;
  17. use Pimcore\Model\DataObject\Folder;
  18. class ContactController extends FrontendController
  19. {
  20.     /**
  21.      * @Template
  22.      * 
  23.      * @param Request $request
  24.      * 
  25.      * @param DataObject $object
  26.      * 
  27.      * @return response
  28.      *
  29.      */
  30.     private function formContactAction(Request $request)
  31.     {
  32.         //Formulaire de contact
  33.         $formStatus false;
  34.          
  35.         // initialize form and handle request data
  36.         $formContact $this->createForm(ContactFormType::class, null);
  37.         $formContact->handleRequest($request);
  38.             
  39.         // On vérifie si un POST a bien été effectué et donc si le formulaire a été envoyé
  40.         if ($formContact->isSubmitted() && $formContact->isValid()) {
  41.             $data $formContact->getData();
  42.             
  43.             $email                 $data['email'];
  44.             $phone                 $data['phone'];
  45.             $firstname             $data['firstname'];
  46.             $lastname             $data['lastname'];
  47.             $birthday             = new Carbon($data['birthday']);
  48.             $message             nl2br($data['message']);
  49.             $sang                 $data['sang'];
  50.             $plasma                $data['plasma'];
  51.             $gdpr                 $data['gdpr'];
  52.             $data['birthdayFormat']    = $birthday->format('d/m/Y');
  53.             // On vérifie que tous les champs sont bien complétés
  54.             if($firstname !== "" && $lastname !== "" && $email !== "" && $message !== ""){
  55.                 // reCaptcha verification
  56.                 $recaptchaKeyPrivate WebsiteSetting::getByName('recaptchaKeyPrivate');
  57.                 $recaptcha_url 'https://www.google.com/recaptcha/api/siteverify';
  58.                 $recaptcha_response $data['recaptcha'];
  59.                 $recaptcha file_get_contents($recaptcha_url '?secret=' $recaptchaKeyPrivate->getData() . '&response=' $recaptcha_response);
  60.                 $recaptcha json_decode($recaptcha);
  61.                 if ($recaptcha->success)
  62.                 {
  63.                     if (filter_var($emailFILTER_VALIDATE_EMAIL)) {
  64.                         // condition pour changer le folder d'enregistrement de concours dans les assets
  65.                         $folderPath Folder::getByPath("/Contact");
  66.                         if(!$folderPath){
  67.                             $folderPath = new Folder();
  68.                             $folderPath->setKey("Contact");
  69.                             $folderPath->setParentId(1);
  70.                             $folderPath->save();
  71.                         }
  72.                         if($this->document->getProperty('objectFormFolder')) {
  73.                             $folderPath Folder::getByPath($this->document->getProperty('objectFormFolder'));
  74.                         }
  75.                         $contact = new DataObject\Contact();
  76.                         $contact->setLastname($lastname);
  77.                         $contact->setFirstname($firstname);
  78.                         $contact->setEmail($email);
  79.                         $contact->setPhone($phone);
  80.                         $contact->setMessage($message);
  81.                         $contact->setBirthday($birthday);
  82.                         $contact->setSang($sang);
  83.                         $contact->setPlasma($plasma);
  84.                         $contact->setGdpr($gdpr);
  85.                         /*$contact->setLang($request->getLocale());*/
  86.                         $contact->setKey($email.'_'.uniqid());
  87.                         $contact->setParent($folderPath);
  88.                         $contact->setPublished(true);
  89.                         $contact->save();
  90.                         //email à CED
  91.                         $mail = new \Pimcore\Mail();
  92.                         $mail->setDocument('/emails/contact');
  93.                         $mail->replyTo($email);
  94.                         $mail->setParams($data);
  95.                         $mail->send();
  96.                         //Copie au visiteur
  97.                         $mail = new \Pimcore\Mail();
  98.                         $mail->setDocument('/emails/'.$request->getLocale().'/contact-confirmation');
  99.                         $mail->to($email);
  100.                         $mail->setParams($data);
  101.                         $mail->send();
  102.                         $formStatus 'success';
  103.                     }else{
  104.                         $formStatus 'invalid-email';
  105.                     }
  106.                 }else{
  107.                     $formStatus 'robot';
  108.                 }
  109.             }else{
  110.                 $formStatus 'required-empty';
  111.             }
  112.         }
  113.         $viewContactForm $formContact->createView();
  114.         return[
  115.             'form' => $viewContactForm,
  116.             'formStatus' => $formStatus
  117.         ];
  118.     }
  119.     /**
  120.      * @Template
  121.      * 
  122.      * @param RequestStack $request
  123.      * 
  124.      * @param DataObject $object
  125.      * 
  126.      * @return response
  127.      *
  128.      */
  129.     public function defaultContactAction(RequestStack $request)
  130.     {
  131.         $formDefault =$this->formContactAction($request->getMasterRequest());
  132.         return $this->render('contact/default.html.twig',[
  133.             'form' => $formDefault['form'],
  134.             'formStatus' => $formDefault['formStatus'],
  135.         ]);
  136.     }
  137.     /**
  138.      * @Template
  139.      * 
  140.      * @param RequestStack $request
  141.      * 
  142.      * @param DataObject $object
  143.      * 
  144.      * @return response
  145.      *
  146.      */
  147.     public function snippetContactAction(RequestStack $request)
  148.     {
  149.         $formSnippet =$this->formContactAction($request->getMasterRequest());
  150.         return $this->render('snippets/contact.html.twig',[
  151.             'form' => $formSnippet['form'],
  152.             'formStatus' => $formSnippet['formStatus'],
  153.         ]);
  154.     }
  155. }