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\Contracts\Translation\TranslatorInterface;
  6. use Pimcore\Model\DataObject;
  7. use Pimcore\Model\Document;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Pimcore\Model\Asset;
  11. use Pimcore\Model\Asset\Folder as AssetFolder;
  12. use Pimcore\Model\Asset\Image;
  13. use Pimcore\Model\WebsiteSetting;
  14. use App\Form\ContactFormType;
  15. use Pimcore\Mail;
  16. use Pimcore\Model\DataObject\ConcoursPaddle;
  17. use Pimcore\Model\DataObject\Folder;
  18. use Symfony\Component\HttpFoundation\RedirectResponse;
  19. class ContactController extends FrontendController
  20. {
  21.     /* @Template
  22.     * @param Request $request
  23.     * @Route("/{_locale}/success-concours", name="successConcours")
  24.     * @return array
  25.     */
  26.     public function successAction(Request $request)
  27.     {
  28.         return $this->render('default/success-concours.html.twig',[
  29.         ]);
  30.     }
  31.     /**
  32.      * @Template
  33.      * @param Request $request
  34.      *
  35.      * @param DataObject $object
  36.      *
  37.      * @return Response
  38.      *
  39.      */
  40.     public function defaultAction(Request $request)
  41.     {
  42.         $formStatus false;
  43.         // initialize form and handle request data
  44.         $formContact $this->createForm(ContactFormType::class, null);
  45.         $formContact->handleRequest($request);
  46.         // On vérifie si un POST a bien été effectué et donc si le formulaire a été envoyé
  47.         if ($formContact->isSubmitted() && $formContact ->isValid()) {
  48.             $data $formContact->getData();
  49.             $email $data['email'];
  50.             $lastname $data['lastname'];
  51.             $firstname $data['firstname'];
  52.             $choixmultiple $data['choixmultiple'];
  53.             $message nl2br($data['message']);
  54.             $img $data['preuvePaiement'];
  55.             $date = new \DateTime('now');
  56.             $gdpr $data['gdpr'];
  57.             $reglement $data['reglement'];
  58.             // On vérifie que tous les champs sont bien complétés
  59.             if($email !== "" && $lastname !== "" && $firstname !== "" && $choixmultiple && $message !== ""){
  60.                 // reCaptcha verification
  61.                 $recaptchaKeyPrivate WebsiteSetting::getByName('recaptchaKeyPrivate');
  62.                 $recaptcha_url 'https://www.google.com/recaptcha/api/siteverify';
  63.                 $recaptcha_response $data['recaptcha'];
  64.                 $recaptcha file_get_contents($recaptcha_url '?secret=' $recaptchaKeyPrivate->getData() . '&response=' $recaptcha_response);
  65.                 $recaptcha json_decode($recaptcha);
  66.                 if ($recaptcha->success){
  67.                     /* Condition pour qu'un seul participant participe
  68.                     $nbParticipant = DataObject\ConcoursPaddle::getByEmail($email);
  69.                     if(count($nbParticipant)===0){
  70.                     */
  71.                         if($img != "" && $gdpr == "1" && $reglement  == "1") {
  72.                             $gdpr true;
  73.                             $reglement true;
  74.                             // condition pour changer le folder d'enregistrement de concours dans les assets
  75.                             $concoursDocumentPath Folder::getByPath("/ConcoursDefault");
  76.                             if($this->document->getProperty('concoursFolder')) {
  77.                                 $concoursDocumentPath Folder::getByPath($this->document->getProperty('concoursFolder'));
  78.                             }
  79.                             $folder AssetFolder::getByPath("/preuves-achat");
  80.                             if(!$folder){
  81.                                 $folder = new AssetFolder();
  82.                                 $folder->setKey($email.uniqid());
  83.                                 $folder->setParent(AssetFolder::getByPath("/preuves-achat"));
  84.                                 $folder->save();
  85.                             }
  86.                             $assetImg = new Image();
  87.                             $assetImg->setKey(uniqid().'.'.$img->getClientOriginalExtension());
  88.                             $assetImg->setParent($folder);
  89.                             $assetImg->setData(file_get_contents($img->getPath().'/'.$img->getFileName()));
  90.                             $assetImg->save();
  91.                             $participant = new DataObject\ConcoursPaddle();
  92.                             $participant->setLastname($lastname);
  93.                             $participant->setFirstname($firstname);
  94.                             $participant->setEmail($email);
  95.                             $participant->setAnswer($choixmultiple);
  96.                             $participant->setSubsidiary($message);
  97.                             $participant->setDateSub(date('Y-m-d H:i:s'));
  98.                             $participant->setIp($_SERVER['HTTP_X_FORWARDED_FOR']);
  99.                             $participant->setLang($request->getLocale());
  100.                             $participant->setPreuvePaiement($assetImg);
  101.                             $participant->setGdpr($gdpr);
  102.                             $participant->setReglement($reglement);
  103.                             $participant->setKey(\Pimcore\Model\Element\Service::getValidKey($email.'_'.uniqid(),"object"));
  104.                             $participant->setParent($concoursDocumentPath);
  105.                             $participant->setClassName("ConcoursPaddle");
  106.                             $participant->setPublished(true);
  107.                             $participant->save();
  108.                             // Email Confirmation
  109.                             $mailConfirmation = new Mail();
  110.                             $emailConfirmationDocument Document::getByPath('/emails/'$request->getLocale() . '/contact-confirmation');
  111.                             $mailConfirmation->setDocument($emailConfirmationDocument);
  112.                             if (filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
  113.                                 $mailConfirmation->to($data['email']);
  114.                                 $mailConfirmation->setParams($data);
  115.                                 $mailConfirmation->send();
  116.                             }
  117.                             $formStatus 'success';
  118.                             return $this->redirect("/"$request->getLocale() . "/success");
  119.                         } else {
  120.                             $formStatus 'not-filled';
  121.                         }
  122.                         /*
  123.                     }else{
  124.                         $formStatus = 'alreadyPlayed';
  125.                     }
  126.                     */
  127.                 }else{
  128.                     $formStatus 'robot';
  129.                 }
  130.             }else{
  131.                 $formStatus 'invalid';
  132.             }
  133.         }
  134.         
  135.         return $this->render('default/concours.html.twig',[
  136.             'form' => $formContact->createView(),
  137.             'formStatus' => $formStatus
  138.         ]);
  139.     }
  140. }