src/Controller/ContactController.php line 160
<?phpnamespace App\Controller;use App\Controller\Traits\HreflangsControllerTrait;use App\Controller\Traits\MetatagsControllerTrait;use App\Controller\Traits\StaticPageHelperTrait;use App\Controller\Traits\ValidateCatpchaControllerTrait;use App\Entity\Newsletter;use App\Service\EmailService;use App\Entity\OpenPosition;use App\Entity\RecipeType;use App\Form\CustomerServiceType;use App\Form\WorkWithUsType;use App\Repository\ProductRepository;use App\Repository\ProductLineRepository;use App\Repository\OpenPositionRepository;use App\Repository\RecipeRepository;use App\Repository\RecipeTypeRepository;use Doctrine\ORM\EntityManagerInterface;use Exception;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\JsonResponse;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\Mailer\Mailer;use Symfony\Component\Mailer\MailerInterface;use Symfony\Component\Routing\Annotation\Route;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Contracts\Translation\TranslatorInterface;use App\Entity\Recipe;//#[Route(path: [// '/contatti',// 'en' => '/en/contact',// 'es' => '/es/contactos'//])]class ContactController extends AbstractController{use MetatagsControllerTrait, HreflangsControllerTrait, StaticPageHelperTrait, ValidateCatpchaControllerTrait;private $em;private $translator;private $emailService;public function __construct(EntityManagerInterface $em, TranslatorInterface $translator, EmailService $emailService){$this->em = $em;$this->translator = $translator;$this->emailService = $emailService;}// #[Route(path: ['/',// 'en' => '/',// 'es' => '/'],// name: 'app_contacts')]// public function contactsIndex(Request $request, EntityManagerInterface $em)// {// list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, $this->getParameter('locales'));// return $this->render('contact/contactIndex.html.twig', [// 'metatags' => $metatags,// 'hreflangs' => $hreflangs// ]);// }#[Route(path: ['/contatti/servizio-clienti','en' => '/en/contact/customer-service',// 'es' => '/es/contactos/atencion-al-cliente'],name: 'app_customer_service')]public function customerService(Request $request, EntityManagerInterface $em){list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, $this->getParameter('locales'));$form = $this->createForm(CustomerServiceType::class);$form->handleRequest($request);if($request->isMethod('POST')){/** VALIDATING CAPTCHA */$validCaptcha = false;if ($recaptcha = $request->get('input')['recaptcha']) {if ($this->validateCaptcha($recaptcha,$this->getParameter('app_recaptcha_secret_v3'))) {$validCaptcha = true;}}if ($form->isSubmitted() && $form->isValid() && $validCaptcha) {$data = $form->getData();$email = $data['email'];$name = $data['name'];$surname = $data['surname'];$phone = $data['phone'];$reason = $data['reason'];$agreeType = 0;// if($data['data'] && $data['data2']){// $agreeType = 3;// } elseif($data['data'] && !$data['data2']){// $agreeType = 1;// } elseif(!$data['data'] && $data['data2']){// $agreeType = 2;// } else {// $agreeType = 0;// }if($data['data'])$agreeType = 1;if($agreeType){$newsletter = new Newsletter;$newsletter->setEmail($email);$newsletter->setName($name);$newsletter->setSurname($surname);$newsletter->setPhone($phone);$newsletter->setAgree($agreeType);$em->persist($newsletter);$em->flush();}$this->emailService->sendContactEmail($data);return new JsonResponse('success', 200);}if ($form->isSubmitted() && $form->getErrors() && $validCaptcha) {$errors = [];foreach ($form->getErrors(true) as $e) {// dd($e->getOrigin()->getName());$errors[$e->getOrigin()->getName()] = $e->getMessage();}return new JsonResponse(['errors' => $errors,], 406);}if(!$validCaptcha){return new JsonResponse('captcha not valid', 403);}}return $this->render('contact/customerService.html.twig', ['form' => $form->createView(),'metatags' => $metatags,'hreflangs' => $hreflangs]);}#[Route(path: ['it' => '/contatti/lavora-con-noi',// 'en' => '/en/contact/work-with-us',// 'es' => '/es/contactos/trabaja-con-nosotros'],name: 'app_work_with_us')]public function workWithus(Request $request, EntityManagerInterface $em){list ($metatags, $hreflangs) = $this->getStaticPageMetatagsAndHreflangs($request, $em, ['it']);/** @var EntityManagerInterface $em */$em = $this->em ;$openPositions = $em->getRepository(OpenPosition::class)->findBy(['locale' => $request->getLocale(),'isEnabled' => 1]);$form = $this->createForm(WorkWithUsType::class);$form->handleRequest($request);if($request->isMethod('POST')){/** VALIDATING CAPTCHA */$validCaptcha = false;if ($recaptcha = $request->get('input')['recaptcha']) {if ($this->validateCaptcha($recaptcha,$this->getParameter('app_recaptcha_secret_v3'))) {$validCaptcha = true;}}if ($form->isSubmitted() && $form->isValid() && $validCaptcha) {$data = $form->getData();$this->emailService->sendWorkWithUsEmail($data);return new JsonResponse('success', 200);}if ($form->isSubmitted() && $form->getErrors() && $validCaptcha) {$errors = [];foreach ($form->getErrors(true) as $e) {$errors[] = $e->getMessage();}return new JsonResponse(['errors' => $errors,], 406);}if(!$validCaptcha){return new JsonResponse('captcha not valid', 403);}}return $this->render('contact/workWithUs.html.twig', ['form' => $form->createView(),'openPositions' => $openPositions,'metatags' => $metatags,'hreflangs' => $hreflangs]);}}