src/Controller/ContactController.php line 71

  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\Traits\HreflangsControllerTrait;
  4. use App\Controller\Traits\MetatagsControllerTrait;
  5. use App\Controller\Traits\StaticPageHelperTrait;
  6. use App\Controller\Traits\ValidateCatpchaControllerTrait;
  7. use App\Entity\Newsletter;
  8. use App\Service\EmailService;
  9. use App\Entity\OpenPosition;
  10. use App\Entity\RecipeType;
  11. use App\Form\CustomerServiceType;
  12. use App\Form\WorkWithUsType;
  13. use App\Repository\ProductRepository;
  14. use App\Repository\ProductLineRepository;
  15. use App\Repository\OpenPositionRepository;
  16. use App\Repository\RecipeRepository;
  17. use App\Repository\RecipeTypeRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Exception;
  20. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Component\Mailer\Mailer;
  24. use Symfony\Component\Mailer\MailerInterface;
  25. use Symfony\Component\Routing\Annotation\Route;
  26. use Symfony\Component\HttpFoundation\RedirectResponse;
  27. use Symfony\Contracts\Translation\TranslatorInterface;
  28. use App\Entity\Recipe;
  29. //#[Route(path: [
  30. //    '/contatti',
  31. //    'en' => '/en/contact',
  32. //    'es' => '/es/contactos'
  33. //])]
  34. class ContactController extends AbstractController
  35. {
  36.     use MetatagsControllerTraitHreflangsControllerTraitStaticPageHelperTraitValidateCatpchaControllerTrait;
  37.     private $em;
  38.     private $translator;
  39.     private $emailService;
  40.     public function __construct(EntityManagerInterface $emTranslatorInterface $translatorEmailService $emailService){
  41.         $this->em $em;
  42.         $this->translator $translator;
  43.         $this->emailService $emailService;
  44.     }
  45.     
  46.     // #[Route(path: ['/',
  47.     //                 'en' => '/',
  48.     //                 'es' => '/'], 
  49.     //         name: 'app_contacts')]
  50.     // public function contactsIndex(Request $request, EntityManagerInterface $em)
  51.     // {
  52.     //     list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, $this->getParameter('locales'));
  53.     //     return $this->render('contact/contactIndex.html.twig', [
  54.     //         'metatags' => $metatags,
  55.     //         'hreflangs' => $hreflangs
  56.     //     ]);
  57.     // }
  58.     #[Route(path: ['/contatti/servizio-clienti',
  59.                     'en' => '/en/contact/customer-service',
  60.                     'es' => '/es/contactos/atencion-al-cliente'
  61.     ],
  62.             name'app_customer_service')]
  63.     public function customerService(Request $requestEntityManagerInterface $em)
  64.     {
  65.         list ($metatags$hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request$em$this->getParameter('locales'));
  66.         $form $this->createForm(CustomerServiceType::class);
  67.         $form->handleRequest($request);
  68.         if($request->isMethod('POST')){
  69.             /** VALIDATING CAPTCHA */
  70.             $validCaptcha false;
  71.             if ($recaptcha $request->get('input')['recaptcha']) {
  72.                 if (
  73.                 $this->validateCaptcha(
  74.                     $recaptcha,
  75.                     $this->getParameter('app_recaptcha_secret_v3')
  76.                 )
  77.                 ) {
  78.                     $validCaptcha true;
  79.                 }
  80.             }
  81.             if ($form->isSubmitted() && $form->isValid() && $validCaptcha) {
  82.                 $data $form->getData();
  83.                 $email $data['email'];
  84.                 $name $data['name'];
  85.                 $surname $data['surname'];
  86.                 $phone $data['phone'];
  87.                 $reason $data['reason'];
  88.                 $agreeType 0;
  89.                 // if($data['data'] && $data['data2']){
  90.                 //     $agreeType = 3;
  91.                 // } elseif($data['data'] && !$data['data2']){
  92.                 //     $agreeType = 1;
  93.                 // } elseif(!$data['data'] && $data['data2']){
  94.                 //     $agreeType = 2;
  95.                 // } else {
  96.                 //     $agreeType = 0;
  97.                 // }
  98.                 if($data['data'])
  99.                     $agreeType 1;
  100.                 if($agreeType){
  101.                     $newsletter = new Newsletter;
  102.                     $newsletter->setEmail($email);
  103.                     $newsletter->setName($name);
  104.                     $newsletter->setSurname($surname);
  105.                     $newsletter->setPhone($phone);
  106.                     $newsletter->setAgree($agreeType);
  107.                     $em->persist($newsletter);
  108.                     $em->flush();
  109.                 }
  110.                 $this->emailService->sendContactEmail($data);
  111.                 return new JsonResponse('success'200);
  112.             }
  113.             if ($form->isSubmitted() && $form->getErrors() && $validCaptcha) {
  114.                 $errors = [];
  115.                 foreach ($form->getErrors(true) as $e) {
  116.                     // dd($e->getOrigin()->getName());
  117.                     $errors[$e->getOrigin()->getName()] = $e->getMessage();
  118.                 }
  119.                 return new JsonResponse([
  120.                     'errors' => $errors,
  121.                 ], 406);
  122.             }
  123.             if(!$validCaptcha){
  124.                 return new JsonResponse('captcha not valid'403);
  125.             }
  126.         }
  127.         return $this->render('contact/customerService.html.twig', [
  128.             'form' => $form->createView(),
  129.             'metatags' => $metatags,
  130.             'hreflangs' => $hreflangs
  131.         ]);
  132.     }
  133.     #[Route(path: ['it' => '/contatti/lavora-con-noi',
  134. //        'en' => '/en/contact/work-with-us',
  135. //        'es' => '/es/contactos/trabaja-con-nosotros'
  136.     ],
  137.     name'app_work_with_us')]
  138.     public function workWithus(Request $requestEntityManagerInterface $em)
  139.     {
  140.         list ($metatags$hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request$em, ['it']);
  141.         /** @var EntityManagerInterface $em */
  142.         $em $this->em ;
  143.         $openPositions $em->getRepository(OpenPosition::class)->findBy([
  144.             'locale' => $request->getLocale(),
  145.             'isEnabled' => 1
  146.         ]);
  147.         $form $this->createForm(WorkWithUsType::class);
  148.         $form->handleRequest($request);
  149.         if($request->isMethod('POST')){
  150.             /** VALIDATING CAPTCHA */
  151.             $validCaptcha false;
  152.             if ($recaptcha $request->get('input')['recaptcha']) {
  153.                 if (
  154.                 $this->validateCaptcha(
  155.                     $recaptcha,
  156.                     $this->getParameter('app_recaptcha_secret_v3')
  157.                 )
  158.                 ) {
  159.                     $validCaptcha true;
  160.                 }
  161.             }
  162.             if ($form->isSubmitted() && $form->isValid() && $validCaptcha) {
  163.                 $data $form->getData();
  164.                 $this->emailService->sendWorkWithUsEmail($data);
  165.                 return new JsonResponse('success'200);
  166.             }
  167.             if ($form->isSubmitted() && $form->getErrors() && $validCaptcha) {
  168.                 $errors = [];
  169.                 foreach ($form->getErrors(true) as $e) {
  170.                     $errors[] = $e->getMessage();
  171.                 }
  172.                 return new JsonResponse([
  173.                     'errors' => $errors,
  174.                 ], 406);
  175.             }
  176.             if(!$validCaptcha){
  177.                 return new JsonResponse('captcha not valid'403);
  178.             }
  179.         }
  180.         return $this->render('contact/workWithUs.html.twig', [
  181.             'form' => $form->createView(),
  182.             'openPositions' => $openPositions,
  183.             'metatags' => $metatags,
  184.             'hreflangs' => $hreflangs
  185.         ]);
  186.     }
  187.     
  188. }