src/Controller/Uniqskills/ContactFormController.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Uniqskills;
  3. use App\Entity\Gos\PortalSettings;
  4. use App\Entity\Gos\Uniqskills\ContactMessage;
  5. use App\Entity\Gos\Uniqskills\FmCompanySettings;
  6. use App\Form\Frontend\Uniqskills\ContactMessageType;
  7. use App\Utils\ContactFormService;
  8. use App\Utils\RecaptchaService;
  9. use App\Utils\Uniqskills\ContactFormUtils;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\JsonResponse;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * @Route("/{_locale}")
  19.  */
  20. class ContactFormController extends AbstractController
  21. {
  22.     private EntityManagerInterface $em;
  23.     private ContactFormUtils $contactUtils;
  24.     private ContactFormService $contactFormService;
  25.     public function __construct(
  26.         EntityManagerInterface $em,
  27.         ContactFormUtils $cfUtil,
  28.         ContactFormService $contactFormService
  29.     ) {
  30.         $this->em $em;
  31.         $this->contactUtils $cfUtil;
  32.         $this->contactFormService $contactFormService;
  33.     }
  34.     /**
  35.      * @Route("/contact", name="fmUniqskillsContactForm")
  36.      */
  37.     public function indexAction(Request $request)
  38.     {
  39.         if ($_ENV['DEFAULT_LOCALE'] === 'en')
  40.         {
  41.             $contactMessage = new ContactMessage();
  42.             $options = [
  43.                 'topics' => $this->contactUtils->getTopics(),
  44.                 'locale' => $request->getLocale(),
  45.                 'fmCompanySettings' => $this->em->getRepository(FmCompanySettings::class)
  46.                     ->findOneByPortalSettings($request->getSession()->get('domain'))
  47.             ];
  48.             $form $this->createForm(ContactMessageType::class, $contactMessage$options)
  49.                 ->handleRequest($request);
  50.             if ($form->isSubmitted() && $form->isValid())
  51.             {
  52.                 $this->contactUtils->sendAndSaveQuestion($contactMessage);
  53.                 return $this->redirectToRoute('fmUniqskillsContactMessageSent');
  54.             }
  55.             return $this->render('/uniqskills/contact/index.html.twig', [
  56.                 'form' => $form->createView()
  57.             ]);
  58.         }
  59.         $portal $this->em->getRepository(PortalSettings::class)->findOneByHash($_ENV['US_PORTAL_SETTINGS']);
  60.         $contactForm $this->contactFormService->getForm($portal);
  61.         return $this->render('/uniqskills/contact-new/index.html.twig', [
  62.             'dataForm' => $contactForm,
  63.             'recaptchaPublicKey' => $_ENV['GOOGLE_RECAPTCHA_US_SITE_KEY'],
  64.             'regulation' => $portal->getFmCompanySettings()->getTextAcceptanceRegulations()[$_ENV['DEFAULT_LOCALE']]
  65.         ]);
  66.     }
  67.     /**
  68.      * @Route("/ajaxExecuteContactForm", name="ajaxExecuteContactForm", methods={"POST"})
  69.      */
  70.     public function ajaxExecuteContactFormAction(Request $request): JsonResponse
  71.     {
  72.         $data $request->request->all();
  73.         $data['privateKeyRecaptcha'] = $_ENV['GOOGLE_RECAPTCHA_US_SECRET'];
  74.         /** @var PortalSettings $portal */
  75.         $portal $this->em->getRepository(PortalSettings::class)->findOneByHash($_ENV['US_PORTAL_SETTINGS']);
  76.         $message $this->contactFormService->checkValidFormAndSendData($data$portal);
  77.         return $this->json($message);
  78.     }
  79.     /**
  80.      * @Route("/contact/thank-you", name="fmUniqskillsContactMessageSent")
  81.      */
  82.     public function messageSentAction(Request $request): Response
  83.     {
  84.         $domain             $request->getSession()->get('domain');
  85.         $locale             $request->getLocale();
  86.         $fmCompanysettings  $this->em->getRepository(FmCompanySettings::class)->findOneByPortalSettings($domain);
  87.         $thankYouMessage    $fmCompanysettings->getTextContactThankYou()[$locale] ?? '';
  88.         $catalogueRoute     $locale === 'pl' 'fmUniqskillsFrontendPlCourseCataloguePage' 'fmUniqskillsFrontendCourseCataloguePage';
  89.         return $this->render('/uniqskills/contact/thanks.html.twig', [
  90.             'thankYouMessage'    => $thankYouMessage,
  91.             'courseCatalogueUrl' => $this->generateUrl($catalogueRoute)
  92.         ]);
  93.     }
  94.     /**
  95.      * @Route("/ajaxContactLanding", name="fmUniqskillsAjaxLandingContactForm", methods={"POST"})
  96.      */
  97.     public function ajaxContactLanding(Request $requestRecaptchaService $recaptchaServiceTranslatorInterface $translator): JsonResponse
  98.     {
  99.         $recaptcha $request->request->get('g-recaptcha-response');
  100.         $message $translator->trans('uniqskills.form.contact.errors.recaptcha');
  101.         $status 'error';
  102.         if (is_null($recaptcha))
  103.             return $this->json(['message' => $message'status' => $status]);
  104.         $recaptchaPricateKey $this->getParameter('ewz_recaptcha.private_key');
  105.         $ip $_SERVER['REMOTE_ADDR'];
  106.         $verifyReCaptcha $recaptchaService->verifyReCaptcha($recaptcha$recaptchaPricateKey$ip);
  107.         if ($verifyReCaptcha['success'] === false)
  108.             return $this->json(['message' => $message'status' => $status]);
  109.         $name $request->request->get('landing-contact-name');
  110.         $email $request->request->get('landing-contact-email');
  111.         $message $request->request->get('landing-contact-message');
  112.         $landingUrl $request->request->get('url');
  113.         $contactMessage = new ContactMessage();
  114.         $contactMessage->setName($name);
  115.         $contactMessage->setSender($email);
  116.         $contactMessage->setBody($message);
  117.         $contactMessage->setCourseName($landingUrl);
  118.         $contactMessage->setTopic(4);
  119.         $this->contactUtils->sendAndSaveQuestion($contactMessage);
  120.         $message $translator->trans('uniqskills.form.contact.messageSent');
  121.         $status 'success';
  122.         return $this->json(['message' => $message'status' => $status]);
  123.     }
  124. }