src/Controller/Concours2023Controller.php line 35

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