src/Document/Areabrick/FormConfigurator.php line 170

Open in your IDE?
  1. <?php
  2. // src/App/Document/Areabrick/Iframe.php
  3. namespace App\Document\Areabrick;
  4. use Pimcore\Model\Document\Editable\Area\Info;
  5. use Symfony\Component\Form\Form;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. class FormConfigurator extends AbstractAreabrick
  9. {
  10.     protected $translator;
  11.     public function getName()
  12.     {
  13.         return 'form-configurator';
  14.     }
  15.     public function getDescription()
  16.     {
  17.         return 'Show form';
  18.     }
  19.     public function getTemplateLocation()
  20.     {
  21.         return static::TEMPLATE_LOCATION_GLOBAL;
  22.     }
  23.     public function getViewTemplate()
  24.     {
  25.         return 'areas/form-configurator/view.html.twig';
  26.     }
  27.     public function getEditTemplate()
  28.     {
  29.         return 'areas/form-configurator/edit.html.twig';
  30.     }
  31.     /**
  32.      * @inheritDoc
  33.      */
  34.     public function hasEditTemplate()
  35.     {
  36.         return true;
  37.     }
  38.     // other methods defined above
  39.     //
  40.     public function action(Info $info)
  41.     {
  42.         $this->translator $this->container->get('translator');
  43.         $formType $info->getDocumentElement('form_type');
  44.         if (!$formType) {
  45.             return;
  46.         }
  47.         $class $this->getFormClass($formType);
  48.         if (!$class) {
  49.             $info->setParam('error''Can not find form class for "'.$formType.'"');
  50.             return;
  51.         }
  52.         $data = [];
  53.         $options = [];
  54.         /** @var Form */
  55.         $form $this->container->get('form.factory')->create($class$data$options);
  56.         if ($info->getRequest()->getMethod() == 'POST') {
  57.             $form->handleRequest($info->getRequest());
  58.         }
  59.         if ($form->isSubmitted() && $form->isValid()) {
  60.             $innerFormType $form->getConfig()->getType()->getInnerType();
  61.             if (method_exists($innerFormType'persist') && is_callable([$innerFormType'persist'])) {
  62.                 $status false;
  63.                 if (method_exists($innerFormType'getHasRecaptcha') && is_callable([$innerFormType'getHasRecaptcha'])) {
  64.                     $hasRecaptcha $innerFormType->getHasRecaptcha();
  65.                     if ($hasRecaptcha) {
  66.                         $data $form->getData();
  67.                         if (!$this->isValidRepatcha($data)) {
  68.                             $this->addFlashMessage($info->getRequest(), 'error''stoelting.recaptcha-failed.error');
  69.                             return $this->redirectBack($info->getRequest());
  70.                         }
  71.                     }
  72.                 }
  73.                 try {
  74.                     $status $innerFormType->persist($form$info$this);
  75.                 } catch (\Exception $e) {
  76.                     $this->addFlashMessage($info->getRequest(), 'error'$e->getMessage());
  77.                 }
  78.                 if ($status) {
  79.                     $this->buildFlashMessage($info->getRequest(), 'success'$formType);
  80.                     return $this->redirectBack($info->getRequest(), ['X-show-thankyou-page' => 'true']);
  81.                 }
  82.             } else {
  83.                 $this->addFlashMessage($info->getRequest(), 'error''stoelting.missing-form-handler.error');
  84.             }
  85.         }
  86.         $info->setParam('formTemplate'$this->getFormTemplate($formType));
  87.         $info->setParam('formObject'$form->createView());
  88.     }
  89.     protected function redirectBack(Request $request, array $headers = [])
  90.     {
  91.         $referer $request->headers->get('referer''/');
  92.         return new RedirectResponse($referer302$headers);
  93.     }
  94.     protected function buildFlashMessage(Request $requeststring $messageType$formSlug)
  95.     {
  96.         $this->addFlashMessage($request$messageType'stoelting.'.$formSlug.'.'.$messageType);
  97.     }
  98.     protected function addFlashMessage(Request $requeststring $messageTypestring $message)
  99.     {
  100.         $request->getSession()
  101.             ->getFlashBag()
  102.             ->add(
  103.                 $messageType,
  104.                 $this->translator->trans($message)
  105.             );
  106.     }
  107.     protected function isValidRepatcha(array $data)
  108.     {
  109.         $captcha $data['g-recaptcha-response'];
  110.         $action $data['action'];
  111.         $secret   $this->container->getParameter('recaptcha_v3_secret_key');
  112.         $ch curl_init();
  113.         curl_setopt($chCURLOPT_URL,"https://www.google.com/recaptcha/api/siteverify");
  114.         curl_setopt($chCURLOPT_POST1);
  115.         curl_setopt($chCURLOPT_POSTFIELDShttp_build_query(array('secret' => $secret'response' => $captcha)));
  116.         curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
  117.         $response curl_exec($ch);
  118.         curl_close($ch);
  119.         $arrResponse json_decode($responsetrue);
  120.         // verify the response
  121.         return ($arrResponse["success"] == true && $arrResponse["score"] >= 0.8 && $arrResponse['action'] == $action);
  122.     }
  123.     protected function getFormTemplate($name)
  124.     {
  125.         return 'Forms/FormConfigurator/'.$name.'.html.twig';
  126.     }
  127.     protected function getFormClass($name)
  128.     {
  129.         $classBaseName implode(''array_map('ucfirst'array_values(array_filter(explode('-'$name)))));
  130.         $className "\\App\\Form\\FormConfigurator\\{$classBaseName}";
  131.         if (!class_exists($className)) {
  132.             return false;
  133.         }
  134.         return $className;
  135.     }
  136. }