src/Controller/FormController.php line 133

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Service\NewsletterService;
  4. use App\Twig\LayoutExtension;
  5. use CustomerManagementFrameworkBundle\DataValidator\EmailValidator;
  6. use Egulias\EmailValidator\Validation\RFCValidation;
  7. use Elements\Bundle\HashCashBundle\Service\HashCashService;
  8. use Pimcore\Mail;
  9. use Pimcore\Model\DataObject\ContactTopic;
  10. use Pimcore\Model\DataObject\CustomerSegment;
  11. use Pimcore\Model\DataObject\Region;
  12. use Pimcore\Model\Document;
  13. use Pimcore\Model\Document\Email;
  14. use Pimcore\Model\Document\Page;
  15. use Pimcore\Translation\Translator;
  16. use Symfony\Component\HttpFoundation\Request;
  17. class FormController extends AbstractController
  18. {
  19.     const COMPONENT 'FormController';
  20.     private Translator $translator;
  21.     public function __construct(Translator $translator, protected LayoutExtension $layoutExtension)
  22.     {
  23.         $this->translator $translator;
  24.     }
  25.     /**
  26.      * @param Request $request
  27.      * @param HashCashService $hashCashService
  28.      * @return \Symfony\Component\HttpFoundation\Response
  29.      * @throws \Exception
  30.      */
  31.     public function contactAction(Request $requestHashCashService $hashCashService)
  32.     {
  33.         $errors = [];
  34.         $success null;
  35.         $mailValidator = new EmailValidator();
  36.         if ($request->isMethod('POST') && $request->get('potNumber') == '') {
  37.             if ($hashCashService->validateProcessFrom()) {
  38.                 $required = ['salutation''firstname''lastname''email''message'];
  39.                 if (!$this->getDocumentEditable('relations''topics')->isEmpty()) {
  40.                     $required[] = 'topic';
  41.                 }
  42.                 if (!$this->getDocumentEditable('relations''regions')->isEmpty()) {
  43.                     $required[] = 'region';
  44.                 }
  45.                 $params array_merge($required, ['title''company''phone''gdpr-text''street''zip''city''country']);
  46.                 unset($params['elhc_stamp']);
  47.                 unset($params['elhc_difficulty']);
  48.                 unset($params['elhc_nonce']);
  49.                 foreach ($required as $param) {
  50.                     if ($request->get($param) == '') {
  51.                         $errors[$param] = 'missing';
  52.                     } else if ($param == 'email' && !$mailValidator->isValid($request->get('email'), new RFCValidation())) {
  53.                         $errors[$param] = 'invalid';
  54.                     }
  55.                 }
  56.                 if (empty($errors)) {
  57.                     $mailDoc $this->getDocumentEditable('relation''email')->getElement();
  58.                     if ($mailDoc instanceof Email) {
  59.                         $mailParams = [];
  60.                         foreach ($params as $param) {
  61.                             $mailParams[$param] = $request->get($param);
  62.                         }
  63.                         array_filter($mailParams);
  64.                         if ($mailParams['salutation']) {
  65.                             $mailParams['salutation'] = $this->translator->trans($mailParams['salutation']);
  66.                         }
  67.                         $mailParams['items'] = ['salutation', ($this->getDocumentEditable('checkbox''use_company')?->isChecked() ? 'company' 'title'), 'firstname''lastname''email''phone''street''zip''city''country''message'];
  68.                         $mail = new Mail();
  69.                         $mail->setDocument($mailDoc);
  70.                         $mail->setParams($mailParams);
  71.                         // calculate To address based on region and topic
  72.                         $toAddress false;
  73.                         $region = isset($mailParams['region']) ? Region::getById($mailParams['region']) : null;
  74.                         if (!$region instanceof Region) {
  75.                             $region $this->document->getProperty('region');
  76.                         }
  77.                         if ($topic = isset($mailParams['topic']) ? ContactTopic::getById($mailParams['topic']) : false) {
  78.                             foreach ($topic->getEmailOverride() as $item) {
  79.                                 if ($item['email']->getData() != '' && $item['region']->getData() != '' && $item['region']->getData()->getId() == $region->getId()) {
  80.                                     $toAddress $item['email']->getData();
  81.                                     break;
  82.                                 }
  83.                             }
  84.                         }
  85.                         if (!$toAddress) {
  86.                             $toAddress $region->getContactEmail();
  87.                         }
  88.                         if ($mailValidator->isValid($toAddress, new RFCValidation())) {
  89.                             $mail->addTo($toAddress);
  90.                             $mail->send();
  91.                         }
  92.                     }
  93.                     $successPage $this->getDocumentEditable('relation''success')->getElement();
  94.                     if ($successPage instanceof Page) {
  95.                         return $this->redirect((string)$successPage);
  96.                     }
  97.                     $success true;
  98.                 } else {
  99.                     $success false;
  100.                 }
  101.             } else {
  102.                 $errors[] = 'hashcash not valid';
  103.             }
  104.         }
  105.         return $this->renderTemplate('Form/contact.html.twig', [
  106.             'success' => $success,
  107.             'errors' => $errors
  108.         ]);
  109.     }
  110.     /**
  111.      * @param Request $request
  112.      * @param NewsletterService $newsletterService
  113.      * @param HashCashService $hashCashService
  114.      * @return \Symfony\Component\HttpFoundation\Response
  115.      * @throws \Exception
  116.      */
  117.     public function newsletterAction(Request $requestNewsletterService $newsletterServiceHashCashService $hashCashService)
  118.     {
  119.         $errors = [];
  120.         if ($request->isMethod('post') && $request->get('potNumber') == '' && empty($request->get('ignore-post'))) {
  121.             if ($hashCashService->validateProcessFrom()) {
  122.                 $params $request->request->all();
  123.                 unset($params['elhc_stamp']);
  124.                 unset($params['elhc_difficulty']);
  125.                 unset($params['elhc_nonce']);
  126.                 $params['customerLanguage'] = $request->getLocale();
  127.                 $selectedSegmentIds $request->get('segments', []);
  128.                 $selectedSegmentsArray = [];
  129.                 if (is_array($selectedSegmentIds) && count($selectedSegmentIds)) {
  130.                     $selectedSegments = new CustomerSegment\Listing();
  131.                     $selectedSegments->addConditionParam('o_id IN (' implode(','$selectedSegmentIds) . ')');
  132.                     $selectedSegmentsArray $selectedSegments->load();
  133.                 }
  134.                 $nl_list $this->getDocumentEditable('select''nl_list')->getData();
  135.                 if (!in_array($nl_list$newsletterService::ALLOWED_NEWSLETTER_LISTS)) {
  136.                     $nl_list $newsletterService::EVALANCHE_LIST;
  137.                 }
  138.                 //outsource allowed segments to experienceconfig
  139.                 if ($request->get('numbirds')) {
  140.                     if ($newsletterService->subscribeNumbirds($params['email'], $params$selectedSegmentsArray$this->getDocumentEditable('relations''segments')->getData())) {
  141.                         $this->addNewsletterTrackingArray($params);
  142.                         if ($this->getDocumentEditable('relation''success')->getData() && $this->getDocumentEditable('relation''success')->getData()['id']) {
  143.                             $document Document::getById($this->getDocumentEditable('relation''success')->getData()['id']);
  144.                             if ($document) {
  145.                                 return $this->redirect($document);
  146.                             }
  147.                         }
  148.                         return $this->redirect("?success=1");
  149.                     }
  150.                 } elseif ($newsletterService->subscribe($params['email'], $params$nl_list$selectedSegmentsArray$this->getDocumentEditable('relations''segments')->getData(), $this->getDocumentEditable('relation''nl_attributeValue')->getElement() ?: null)) {
  151.                     $this->addNewsletterTrackingArray($params);
  152.                     if ($this->getDocumentEditable('relation''success')->getData() && $this->getDocumentEditable('relation''success')->getData()['id']) {
  153.                         $document Document::getById($this->getDocumentEditable('relation''success')->getData()['id']);
  154.                         if ($document) {
  155.                             return $this->redirect($document);
  156.                         }
  157.                     }
  158.                     return $this->redirect("?success=1");
  159.                 }
  160.             } else {
  161.                 $errors[] = 'hashcash not valid';
  162.             }
  163.         }
  164.         return $this->renderTemplate('Form/newsletter.html.twig', [
  165.             'defaultPool' => $newsletterService::EVALANCHE_LIST,
  166.             'errors' => array_merge($errors$newsletterService->getErrors() ?: [])
  167.         ]);
  168.     }
  169.     public function brandCooperationAction(Request $requestHashCashService $hashCashService)
  170.     {
  171.         $errors = [];
  172.         $success null;
  173.         $mailValidator = new EmailValidator();
  174.         if ($request->isMethod('POST') && $request->get('potNumber') == '') {
  175.             if ($hashCashService->validateProcessFrom()) {
  176.                 $requiredFields = [
  177.                     'company''contactPerson''street''zip''city''email''partnership'
  178.                 ];
  179.                 $params = [
  180.                     'message' => $request->get('message'),
  181.                     'gdpr' => $request->get('gdpr-text'),
  182.                     'phone' => $request->get('phone'),
  183.                     'country' => $request->get('country'),
  184.                 ];
  185.                 foreach ($requiredFields as $field) {
  186.                     $params[$field] = $request->get($field);
  187.                     if (empty($params[$field])) {
  188.                         $errors[$field] = 'missing';
  189.                     }
  190.                 }
  191.                 if (!empty($params['email']) && !$mailValidator->isValid($params['email'], new RFCValidation())) {
  192.                     $errors['email'] = 'invalid';
  193.                 }
  194.                 if (empty($errors)) {
  195.                     $adminMail $this->getDocumentEditable('relation''notificationmail')->getElement();
  196.                     $userMail $this->getDocumentEditable('relation''usermail')->getElement();
  197.                     if ($adminMail instanceof Email) {
  198.                         $mail = new Mail();
  199.                         $adminMailParams $params;
  200.                         $adminMailParams['items'] = array_keys($params);
  201.                         try {
  202.                             $mail->setParams($adminMailParams);
  203.                             $mail->setDocument($adminMail);
  204.                             $mail->send();
  205.                         } catch (\Exception $e) {
  206.                             $this->logger->error("Sending admin Mail failed\n" $e->getMessage(), ['component' => self::COMPONENT]);
  207.                         }
  208.                     }
  209.                     if ($userMail instanceof Email) {
  210.                         $mail = new Mail();
  211.                         try {
  212.                             $mail->addTo($params['email']);
  213.                             $mail->setDocument($userMail);
  214.                             $mail->send();
  215.                         } catch (\Exception $e) {
  216.                             $this->logger->error("Sending user Mail failed\n" $e->getMessage(), ['component' => self::COMPONENT]);
  217.                         }
  218.                     }
  219.                     $successPage $this->getDocumentEditable('relation''successPage')->getElement();
  220.                     if ($successPage instanceof Page) {
  221.                         return $this->redirect((string)$successPage->getFullPath());
  222.                     }
  223.                 } else {
  224.                     $errors['hashcash'] = 'HashCash validation failed.';
  225.                 }
  226.             }
  227.             return $this->renderTemplate('Form/brandCooperation.html.twig', [
  228.                 'errors' => $errors,
  229.                 'success' => $success
  230.             ]);
  231.         }
  232.         return $this->renderTemplate('Form/brandCooperation.html.twig', [
  233.             'errors' => $errors,
  234.             'success' => $success
  235.         ]);
  236.     }
  237.     protected function addNewsletterTrackingArray($params): void
  238.     {
  239.         $event = [
  240.             'event' => 'newsletter_signup',
  241.             'leadsUserData' => [
  242.                 'address' => [
  243.                     'sha256_first_name' => hash('sha256'$params['firstname'] ?? ''),
  244.                     'sha256_last_name' => hash('sha256'$params['lastname'] ?? '')
  245.                 ],
  246.                 'sha256_email_address' => hash('sha256'$params['email'])
  247.             ]
  248.         ];
  249.         $this->addFlash('trackingData'$event);
  250.     }
  251. }