src/Controller/FormsController.php line 193

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Elements.at New Media Solutions GmbH
  4.  *
  5.  */
  6. namespace App\Controller;
  7. use App\Twig\ConfigHelper;
  8. use Elements\Bundle\CmsToolsBundle\Tool\Helper\MailHelper;
  9. use Elements\Bundle\HashCashBundle\Service\HashCashService;
  10. use Pimcore\Log\ApplicationLogger;
  11. use Pimcore\Mail;
  12. use Pimcore\Model\DataObject\SiteConfig;
  13. use Pimcore\Model\Document\Email;
  14. use Pimcore\Model\Document\Page;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Contracts\Translation\TranslatorInterface;
  18. class FormsController extends AbstractController
  19. {
  20.     public function __construct(private readonly HashCashService $hashCashService, private readonly TranslatorInterface $translator, private readonly ConfigHelper $configHelper)
  21.     {
  22.     }
  23.     /**
  24.      *
  25.      * @param Request $request
  26.      *
  27.      * @return Response
  28.      */
  29.     public function contactAction(Request $request): Response
  30.     {
  31.         \Pimcore\Cache::disable();
  32.         $errors = [];
  33.         $success null;
  34.         $siteConfig $this->configHelper->getSiteConfig();
  35.         if ($request->isMethod('POST')) {
  36.             if ($siteConfig instanceof SiteConfig) {
  37.                 $successPage $siteConfig->getContactSuccessPage();
  38.                 $resort $this->document->getProperty('resort');
  39.                 if ($resort == 'construction') {
  40.                     $adminMail $siteConfig->getContactConstructionAdminMail();
  41.                     $userMail $siteConfig->getContactConstructionUserMail();
  42.                     $mailAddress $siteConfig->getContactConstructionMailAddress();
  43.                 } elseif ($resort == 'loader') {
  44.                     $adminMail $siteConfig->getContactLoaderAdminMail();
  45.                     $userMail $siteConfig->getContactLoaderUserMail();
  46.                     $mailAddress $siteConfig->getContactLoaderMailAddress();
  47.                 } else {
  48.                     $adminMail $siteConfig->getContactGroupAdminMail();
  49.                     $userMail $siteConfig->getContactGroupUserMail();
  50.                     $mailAddress $siteConfig->getContactGroupMailAddress();
  51.                 }
  52.             }
  53.             $required = ['firstname''lastname''email''message''gdpr'];
  54.             if (!$request->get('person'false)) {
  55.                 $required[] = 'company';
  56.             }
  57.             $params $this->checkForm($request$required);
  58.             if (!empty($params['errors'])) {
  59.                 $errors $params['errors'];
  60.                 $success false;
  61.             } else {
  62.                 $adminMailSuccess false;
  63.                 $userMailSuccess false;
  64.                 if (isset($adminMail) && isset($mailAddress) && $adminMail instanceof Email) {
  65.                     $mail = new Mail();
  66.                     try {
  67.                         $mail->setParams($params);
  68.                         $mail->setDocument($adminMail);
  69.                         $mail->addTo($mailAddress);
  70.                         $mail->send();
  71.                         $adminMailSuccess true;
  72.                     } catch(\Exception $e) {
  73.                         $errors[] = 'sending admin mail failed';
  74.                     }
  75.                 }
  76.                 if (isset($userMail) && $userMail instanceof Email) {
  77.                     $mail = new Mail();
  78.                     try {
  79.                         $mail->addTo($params['email']);
  80.                         $mail->setDocument($userMail);
  81.                         $mail->send();
  82.                         $userMailSuccess true;
  83.                     } catch(\Exception $e) {
  84.                         $errors[] = 'sending user mail failed';
  85.                     }
  86.                 }
  87.                 if ($adminMailSuccess && $userMailSuccess && isset($successPage) && $successPage instanceof Page) {
  88.                     return $this->redirect((string)$successPage);
  89.                 }
  90.             }
  91.         }
  92.         return $this->renderTemplate('forms/contact.html.twig', [
  93.             'errors' => $errors,
  94.             'success' => $success,
  95.         ]);
  96.     }
  97.     /**
  98.      *
  99.      * @param Request $request
  100.      * @param ApplicationLogger $logger
  101.      * @return Response
  102.      * @throws \Exception
  103.      */
  104.     public function whistleBlowerContactAction(Request $requestApplicationLogger $logger): Response
  105.     {
  106.         \Pimcore\Cache::disable();
  107.         $errors = [];
  108.         $success null;
  109.         if ($request->isMethod('POST')) {
  110.             $adminMail $this->getDocumentEditable('relation''email-admin')->getElement();
  111.             $adminMailAddress $this->getDocumentEditable('input''admin-address')->getData();
  112.             $successPage $this->getDocumentEditable('relation''success')->getElement();
  113.             $required = ['message'];
  114.             $params $this->checkForm($request$required);
  115.             if (!empty($params['errors'])) {
  116.                 $errors $params['errors'];
  117.                 $success false;
  118.             } else {
  119.                 $adminMailSuccess false;
  120.                 $mailInfos null;
  121.                 if ($adminMail instanceof Email) {
  122.                     $mail = new Mail();
  123.                     try {
  124.                         $mail->setParams($params);
  125.                         $mail->addTo($adminMailAddress);
  126.                         $mail->setDocument($adminMail);
  127.                         $mailInfos $mail->send();
  128.                         $adminMailSuccess true;
  129.                     } catch(\Exception $e) {
  130.                         $errors[] = 'sending admin mail failed';
  131.                     }
  132.                 }
  133.                 if ($adminMailSuccess) {
  134.                     $documentId $mailInfos?->getDocumentId() ?? null;
  135.                     $requestUri $request->getRequestUri();
  136.                     try {
  137.                         $db \Pimcore\Db::get();
  138.                         $db->delete('email_log', [
  139.                             'documentId' => $documentId,
  140.                             'requestUri' => $requestUri,
  141.                             '`to' => $adminMailAddress,
  142.                         ]);
  143.                     } catch (\Exception $e) {
  144.                         $logger->error("Could not delete whistleblower email: " $e->getFile() . $e->getLine() . $e->getMessage());
  145.                     }
  146.                     if ($successPage instanceof Page) {
  147.                         return $this->redirect((string)$successPage);
  148.                     }
  149.                 }
  150.             }
  151.         }
  152.         return $this->renderTemplate('forms/whistleBlowerContact.html.twig', [
  153.             'errors' => $errors,
  154.             'success' => $success,
  155.         ]);
  156.     }
  157.     /**
  158.      *
  159.      * @param Request $request
  160.      *
  161.      * @return Response
  162.      */
  163.     public function ePartsAction(Request $request): Response
  164.     {
  165.         \Pimcore\Cache::disable();
  166.         $errors = [];
  167.         $success null;
  168.         if ($request->isMethod('POST')) {
  169.             $adminMail $this->getDocumentEditable('relation''email-admin')->getElement();
  170.             $adminMailAddress $this->getDocumentEditable('input''admin-address')->getData();
  171.             $successPage $this->getDocumentEditable('relation''success')->getElement();
  172.             $required = ['company''street''zip''city''country''email''phone''contactPerson''gdpr'];
  173.             $params $this->checkForm($request$required);
  174.             if (!empty($params['errors'])) {
  175.                 $errors $params['errors'];
  176.                 $success false;
  177.             } else {
  178.                 $adminMailSuccess false;
  179.                 if ($adminMail instanceof Email) {
  180.                     $mail = new Mail();
  181.                     try {
  182.                         $mail->setParams($params);
  183.                         $mail->addTo($adminMailAddress);
  184.                         $mail->setDocument($adminMail);
  185.                         $mail->send();
  186.                         $adminMailSuccess true;
  187.                         $success true;
  188.                     } catch(\Exception $e) {
  189.                         $errors[] = 'sending admin mail failed';
  190.                     }
  191.                 }
  192.                 if ($adminMailSuccess && $successPage instanceof Page) {
  193.                     return $this->redirect((string)$successPage);
  194.                 }
  195.             }
  196.         }
  197.         return $this->renderTemplate('forms/eParts.html.twig', [
  198.             'errors' => $errors,
  199.             'success' => $success,
  200.         ]);
  201.     }
  202.     /**
  203.      * @param Request $request
  204.      * @param mixed $required
  205.      *
  206.      * @return mixed
  207.      */
  208.     private function checkForm(Request $requestmixed $required): mixed
  209.     {
  210.         $validHashCash $this->hashCashService->validateProcessFrom();
  211.         if ($validHashCash) {
  212.             $params $request->request->all();
  213.             unset($params['elhc_stamp'], $params['elhc_difficulty'], $params['elhc_nonce']);
  214.             $errors = [];
  215.             foreach ($required as $param) {
  216.                 if ($request->get($param) == '') {
  217.                     $errors[$param] =  $param ' is missing';
  218.                 }
  219.                 if ($param == 'email' && !MailHelper::isValidEmailAddress($request->get($param))) {
  220.                     $errors[$param] = 'email invalid';
  221.                 }
  222.             }
  223.             if (!empty($errors)) {
  224.                 $params['errors'] = $errors;
  225.             }
  226.             if (isset($params['salutation'])) {
  227.                 $params['salutation'] = $this->translator->trans('form.salutation.' $params['salutation'], [], null$request->getLocale());
  228.             }
  229.             $params['items'] = $params;
  230.         } else {
  231.             $params['errors'] = ['recaptcha' => 'invalid captcha'];
  232.         }
  233.         return $params;
  234.     }
  235. }