<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\Translation\TranslatorInterface;
use Pimcore\Model\DataObject;
use Pimcore\Model\Document;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Pimcore\Model\Asset;
use Pimcore\Model\Asset\Folder as AssetFolder;
use Pimcore\Model\Asset\Image;
use Pimcore\Model\WebsiteSetting;
use App\Form\Concours2023FormType;
use Pimcore\Mail;
use Pimcore\Model\DataObject\Concours2023;
use Pimcore\Model\DataObject\Folder;
use Symfony\Component\HttpFoundation\RedirectResponse;
class Concours2023Controller extends FrontendController
{
/**
* @Template
* @param Request $request
*
* @param DataObject $object
*
* @return Response
*
*/
public function defaultAction(Request $request)
{
$formStatus = false;
// initialize form and handle request data
$formContact = $this->createForm(Concours2023FormType::class, null);
$formContact->handleRequest($request);
// On vérifie si un POST a bien été effectué et donc si le formulaire a été envoyé
if ($formContact->isSubmitted() && $formContact ->isValid()) {
$data = $formContact->getData();
$email = $data['email'];
$lastname = $data['lastname'];
$firstname = $data['firstname'];
$choixmultiple = $data['choixmultiple'];
$message = nl2br($data['message']);
$img = $data['preuvePaiement'];
$date = new \DateTime('now');
$gdpr = $data['gdpr'];
$reglement = $data['reglement'];
// On vérifie que tous les champs sont bien complétés
if($email !== "" && $lastname !== "" && $firstname !== "" && $choixmultiple && $message !== ""){
// reCaptcha verification
$recaptchaKeyPrivate = WebsiteSetting::getByName('recaptchaKeyPrivate');
$recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
$recaptcha_response = $data['recaptcha'];
$recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptchaKeyPrivate->getData() . '&response=' . $recaptcha_response);
$recaptcha = json_decode($recaptcha);
if ($recaptcha->success){
/* Condition pour qu'un seul participant participe
$nbParticipant = DataObject\ConcoursPaddle::getByEmail($email);
if(count($nbParticipant)===0){
*/
if($img != "" && $gdpr == "1" && $reglement == "1") {
$gdpr = true;
$reglement = true;
// condition pour changer le folder d'enregistrement de concours dans les assets
$concoursDocumentPath = Folder::getByPath("/Concours2023");
if($this->document->getProperty('concoursFolder')) {
$concoursDocumentPath = Folder::getByPath($this->document->getProperty('concoursFolder'));
}
$folder = AssetFolder::getByPath("/preuves-achat");
if(!$folder){
$folder = new AssetFolder();
$folder->setKey($email.uniqid());
$folder->setParent(AssetFolder::getByPath("/preuves-achat"));
$folder->save();
}
$assetImg = new Image();
$assetImg->setKey(uniqid().'.'.$img->getClientOriginalExtension());
$assetImg->setParent($folder);
$assetImg->setData(file_get_contents($img->getPath().'/'.$img->getFileName()));
$assetImg->save();
$participant = new DataObject\Concours2023();
$participant->setLastname($lastname);
$participant->setFirstname($firstname);
$participant->setEmail($email);
$participant->setAnswer($choixmultiple);
$participant->setSubsidiary($message);
$participant->setDateSub(date('Y-m-d H:i:s'));
$participant->setIp($_SERVER['HTTP_X_FORWARDED_FOR']);
$participant->setLang($request->getLocale());
$participant->setPreuvePaiement($assetImg);
$participant->setGdpr($gdpr);
$participant->setReglement($reglement);
$participant->setKey(\Pimcore\Model\Element\Service::getValidKey($email.'_'.uniqid(),"object"));
$participant->setParent($concoursDocumentPath);
$participant->setClassName("Concours2023");
$participant->setPublished(true);
$participant->save();
// Email Confirmation
$mailConfirmation = new Mail();
$emailConfirmationDocument = Document::getByPath('/emails/'. $request->getLocale() . '/contact-confirmation');
$mailConfirmation->setDocument($emailConfirmationDocument);
if (filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
$mailConfirmation->to($data['email']);
$mailConfirmation->setParams($data);
$mailConfirmation->send();
}
$formStatus = 'success';
} else {
$formStatus = 'not-filled';
}
/*
}else{
$formStatus = 'alreadyPlayed';
}
*/
}else{
$formStatus = 'robot';
}
}else{
$formStatus = 'invalid';
}
}
return $this->render('default/concours-2023.html.twig',[
'form' => $formContact->createView(),
'formStatus' => $formStatus
]);
}
}