src/Controller/System/ContactController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller\System;
  3. use App\Controller\BaseController;
  4. use App\Service\EmailService;
  5. use App\Validator\ContactFormValidator;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. final class ContactController extends BaseController
  9. {
  10.     public function indexAction(Request $request): Response
  11.     {
  12.         $desktop true;
  13.         $desktopView $this->checkDesktopView($request);
  14.         if ($this->isMobile && !$desktopView) {
  15.             $desktop false;
  16.         }
  17.         $message $request->get('message');
  18.         $render '@templates/Front/contact.html.twig';
  19.         return $this->render($render, [
  20.             'message' => $message,
  21.             'desktop' => $desktop
  22.         ]);
  23.     }
  24.     public function submitAction(Request $request): Response
  25.     {
  26.         $errors null;
  27.         $session $request->getSession();
  28.         $contactValidator = new ContactFormValidator($this->getEntityManager(), $this->getSerializer());
  29.         if ($contactValidator->checkForm($request)) {
  30.             $errors json_encode(array('success' => true));
  31.             $email $request->get('contactEmail');
  32.             $renderView $this->renderView('@templates/EmailTemplate/contactEmail.html.twig', array(
  33.                 'contactPerson' => $request->get('contactPerson'),
  34.                 'contactEmail' => $request->get('contactEmail'),
  35.                 'contactPhone' => $request->get('contactPhone'),
  36.                 'contactMessage' => $request->get('contactMessage')
  37.             ));
  38.             $this->sendFeedbackEmail($renderView);
  39.         } else {
  40.             if ($session->has('contactErrors')) {
  41.                 $errors $session->get('contactErrors');
  42.             }
  43.         }
  44.         return new Response($errors);
  45.     }
  46.     public function sendFeedbackEmail($renderView)
  47.     {
  48.         $message = (new \Swift_Message('Mesaj nou formular de contact roviniete.ro'))
  49.             ->setFrom($_ENV['EMAIL_USERNAME'])
  50.             ->setTo($_ENV['EMAIL_SCALA'])
  51.             ->addCc($_ENV['EMAIL_OFFICE_SCALA'])
  52.             ->setBody($renderView)
  53.             ->setContentType("text/html");
  54.         if (!EmailService::sendEmail($message$_ENV['EMAIL_SCALA'])) {
  55.             $message->setFrom($_ENV['EMAIL_USERNAME_2']);
  56.             EmailService::sendEmailUsingSecondaryService($message$_ENV['EMAIL_SCALA']);
  57.         }
  58.     }
  59.     function checkDesktopView(Request $request)
  60.     {
  61.         $session $request->getSession();
  62.         $desktopView false;
  63.         if ($session->has('switchDesktop')) {
  64.             $desktopView true;
  65.         }
  66.         return $desktopView;
  67.     }
  68. }