<?php
namespace App\Controller\System;
use App\Controller\BaseController;
use App\Service\EmailService;
use App\Validator\ContactFormValidator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
final class ContactController extends BaseController
{
public function indexAction(Request $request): Response
{
$desktop = true;
$desktopView = $this->checkDesktopView($request);
if ($this->isMobile && !$desktopView) {
$desktop = false;
}
$message = $request->get('message');
$render = '@templates/Front/contact.html.twig';
return $this->render($render, [
'message' => $message,
'desktop' => $desktop
]);
}
public function submitAction(Request $request): Response
{
$errors = null;
$session = $request->getSession();
$contactValidator = new ContactFormValidator($this->getEntityManager(), $this->getSerializer());
if ($contactValidator->checkForm($request)) {
$errors = json_encode(array('success' => true));
$email = $request->get('contactEmail');
$renderView = $this->renderView('@templates/EmailTemplate/contactEmail.html.twig', array(
'contactPerson' => $request->get('contactPerson'),
'contactEmail' => $request->get('contactEmail'),
'contactPhone' => $request->get('contactPhone'),
'contactMessage' => $request->get('contactMessage')
));
$this->sendFeedbackEmail($renderView);
} else {
if ($session->has('contactErrors')) {
$errors = $session->get('contactErrors');
}
}
return new Response($errors);
}
public function sendFeedbackEmail($renderView)
{
$message = (new \Swift_Message('Mesaj nou formular de contact roviniete.ro'))
->setFrom($_ENV['EMAIL_USERNAME'])
->setTo($_ENV['EMAIL_SCALA'])
->addCc($_ENV['EMAIL_OFFICE_SCALA'])
->setBody($renderView)
->setContentType("text/html");
if (!EmailService::sendEmail($message, $_ENV['EMAIL_SCALA'])) {
$message->setFrom($_ENV['EMAIL_USERNAME_2']);
EmailService::sendEmailUsingSecondaryService($message, $_ENV['EMAIL_SCALA']);
}
}
function checkDesktopView(Request $request)
{
$session = $request->getSession();
$desktopView = false;
if ($session->has('switchDesktop')) {
$desktopView = true;
}
return $desktopView;
}
}