<?php
namespace App\Controller;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
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\WebsiteSetting;
use App\Form\ExpertiseFormType;
use App\Form\ContactFormType;
use App\Form\ContactModalFormType;
use Pimcore\Mail;
use Carbon\Carbon;
use Pimcore\Model\DataObject\Folder;
class ContactController extends FrontendController
{
/**
* @Template
*
* @param Request $request
*
* @param DataObject $object
*
* @return response
*
*/
private function formContactAction(Request $request)
{
//Formulaire de contact
$formStatus = false;
// initialize form and handle request data
$formContact = $this->createForm(ContactFormType::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'];
$phone = $data['phone'];
$firstname = $data['firstname'];
$lastname = $data['lastname'];
$birthday = new Carbon($data['birthday']);
$message = nl2br($data['message']);
$sang = $data['sang'];
$plasma = $data['plasma'];
$gdpr = $data['gdpr'];
$data['birthdayFormat'] = $birthday->format('d/m/Y');
// On vérifie que tous les champs sont bien complétés
if($firstname !== "" && $lastname !== "" && $email !== "" && $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)
{
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
// condition pour changer le folder d'enregistrement de concours dans les assets
$folderPath = Folder::getByPath("/Contact");
if(!$folderPath){
$folderPath = new Folder();
$folderPath->setKey("Contact");
$folderPath->setParentId(1);
$folderPath->save();
}
if($this->document->getProperty('objectFormFolder')) {
$folderPath = Folder::getByPath($this->document->getProperty('objectFormFolder'));
}
$contact = new DataObject\Contact();
$contact->setLastname($lastname);
$contact->setFirstname($firstname);
$contact->setEmail($email);
$contact->setPhone($phone);
$contact->setMessage($message);
$contact->setBirthday($birthday);
$contact->setSang($sang);
$contact->setPlasma($plasma);
$contact->setGdpr($gdpr);
/*$contact->setLang($request->getLocale());*/
$contact->setKey($email.'_'.uniqid());
$contact->setParent($folderPath);
$contact->setPublished(true);
$contact->save();
//email à CED
$mail = new \Pimcore\Mail();
$mail->setDocument('/emails/contact');
$mail->replyTo($email);
$mail->setParams($data);
$mail->send();
//Copie au visiteur
$mail = new \Pimcore\Mail();
$mail->setDocument('/emails/'.$request->getLocale().'/contact-confirmation');
$mail->to($email);
$mail->setParams($data);
$mail->send();
$formStatus = 'success';
}else{
$formStatus = 'invalid-email';
}
}else{
$formStatus = 'robot';
}
}else{
$formStatus = 'required-empty';
}
}
$viewContactForm = $formContact->createView();
return[
'form' => $viewContactForm,
'formStatus' => $formStatus
];
}
/**
* @Template
*
* @param RequestStack $request
*
* @param DataObject $object
*
* @return response
*
*/
public function defaultContactAction(RequestStack $request)
{
$formDefault =$this->formContactAction($request->getMasterRequest());
return $this->render('contact/default.html.twig',[
'form' => $formDefault['form'],
'formStatus' => $formDefault['formStatus'],
]);
}
/**
* @Template
*
* @param RequestStack $request
*
* @param DataObject $object
*
* @return response
*
*/
public function snippetContactAction(RequestStack $request)
{
$formSnippet =$this->formContactAction($request->getMasterRequest());
return $this->render('snippets/contact.html.twig',[
'form' => $formSnippet['form'],
'formStatus' => $formSnippet['formStatus'],
]);
}
}