<?php
namespace App\Controller;
use App\Api\SalesforceRest\Controllers\ApiContactController;
use App\Entity\ContactMail;
use App\Service\AccountManager;
use App\Service\ContactMailManager;
use App\Service\ContactManager;
use App\Service\LanguageManager;
use App\Service\MailManager;
use App\Service\NotificationManager;
use App\Service\NotificationService;
use App\Service\RequestManager;
use Doctrine\ORM\EntityManagerInterface;
use ReCaptcha\ReCaptcha;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
class FrontController extends EntityController
{
private AccountManager $accountManager;
private ContactManager $contactManager;
private MailManager $mailManager;
private ContactMailManager $contactMailManager;
private RequestManager $requestManager;
/**
* FrontController constructor.
* @param EntityManagerInterface $entityManager
* @param NotificationManager $notificationManager
* @param RequestStack $request
* @param LanguageManager $languageManager
* @param AccountManager $accountManager
* @param ContactManager $contactManager
* @param MailManager $mailManager
* @param ContactMailManager $contactMailManager
*/
public function __construct(EntityManagerInterface $entityManager,
NotificationManager $notificationManager,
RequestStack $request,
LanguageManager $languageManager,
AccountManager $accountManager,
ContactManager $contactManager,
MailManager $mailManager,
ContactMailManager $contactMailManager,
RequestManager $requestManager,
NotificationService $notificationService
)
{
//<<<<<<< HEAD
// parent::__construct( $entityManager, $notificationManager, $request, $languageManager, $session, $notificationService);
//=======
parent::__construct($entityManager, $notificationManager, $request, $languageManager, $notificationService);
//>>>>>>> 92-achat-unique
$this->accountManager = $accountManager;
$this->contactManager = $contactManager;
$this->mailManager = $mailManager;
$this->contactMailManager = $contactMailManager;
$this->requestManager = $requestManager;
}
/**
* The default page
* @return Response
*/
public function indexNoLocale() : Response
{
$lang = ['_locale'=> 'fr'];
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$browserLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if($browserLanguage == 'fr' || $browserLanguage == 'it') {
$lang = ['_locale' => $browserLanguage];
}
}
return $this->redirectToRoute('Index', $lang);
}
/**
* The default page
* @return Response
*/
public function index(Request $request) : Response
{
// check if the contact is connected first
// get the contact from session
$referent = $request->getSession()->get('contact', null);
if(!is_null($referent)){
// if contact has accounts and is not null
if(count($referent->getAccounts()) > 0){
// if the contact has only one account, we redirect to it
$response = $this->redirectToRoute('AccountIndex', ['salesforceAccountId' => $referent->getAccounts()[0]->getSalesforceId()]);
}
else{
$response = $this->redirectToRoute('Logout');
}
}
// If not connected, redirect to login route
else{
$response = $this->redirectToRoute('PreLogin');
}
return $response;
}
/**
* To contact us
* @param Request $request
* @return Response
*/
public function viewContactMail(Request $request) : Response
{
$contactFormSubmitted = $request->request->get('submitContact', null);
// get the motives from Salesforce
$motives = ApiContactController::getContactMotives();
// If the contact form has been submitted
if(!is_null($contactFormSubmitted)){
$recaptcha = new ReCaptcha($_ENV['G_RECAPTCHA_SECRET_KEY']);
$resp = $recaptcha->verify($request->request->get('g-recaptcha-response'), $request->getClientIp());
if (!$resp->isSuccess()) {
$this->notificationManager::addError('Le reCAPTCHA est incorrect. Veuillez réessayer.');
}
else {
$motive = $request->request->get('motive', null);
$mailMessage = $request->request->get('mailMessage', null);
$email = $request->request->get('email', null);
$this->requestManager->saveNotConnectedRequest($motive, $mailMessage, $email, $motives);
}
}
// get the motives from Salesforce
$this->addViewDataArray('motives', $motives);
$this->createFlashs();
// if the form is submitted
return $this->render('contact_us.html.twig', $this->viewData);
}
}