コード例 #1
0
ファイル: FormSubscriber.php プロジェクト: dongilbert/mautic
 /**
  * @param Events\SubmissionEvent $event
  */
 public function onFormSubmitActionRepost(Events\SubmissionEvent $event)
 {
     if (!$event->checkContext('form.repost')) {
         return;
     }
     $post = $event->getPost();
     $results = $event->getResults();
     $config = $event->getActionConfig();
     $fields = $event->getFields();
     $lead = $event->getSubmission()->getLead();
     $matchedFields = [];
     $payload = ['mautic_contact' => $lead->getProfileFields(), 'mautic_form' => ['id' => $post['formId'], 'name' => $post['formName'], 'url' => $post['return']]];
     $fieldTypes = [];
     foreach ($fields as $field) {
         $fieldTypes[$field['alias']] = $field['type'];
         if (!isset($post[$field['alias']]) || 'button' == $field['type']) {
             continue;
         }
         $key = !empty($config[$field['alias']]) ? $config[$field['alias']] : $field['alias'];
         // Use the cleaned value by default - but if set to not save result, get from post
         $value = isset($results[$field['alias']]) ? $results[$field['alias']] : $post[$field['alias']];
         $matchedFields[$key] = $field['alias'];
         $payload[$key] = $value;
     }
     $headers = ['X-Forwarded-For' => $event->getSubmission()->getIpAddress()->getIpAddress()];
     if (!empty($config['authorization_header'])) {
         if (strpos($config['authorization_header'], ':') !== false) {
             list($key, $value) = explode(':', $config['authorization_header']);
         } else {
             $key = 'Authorization';
             $value = $config['authorization_header'];
         }
         $headers[trim($key)] = trim($value);
     }
     try {
         $client = new Client(['timeout' => 15]);
         $response = $client->post($config['post_url'], ['form_params' => $payload, 'headers' => $headers]);
         if ($redirect = $this->parseResponse($response, $matchedFields)) {
             $event->setPostSubmitCallbackResponse('form.repost', new RedirectResponse($redirect));
         }
     } catch (ServerException $exception) {
         $this->parseResponse($exception->getResponse(), $matchedFields);
     } catch (\Exception $exception) {
         if ($exception instanceof ValidationException) {
             if ($violations = $exception->getViolations()) {
                 throw $exception;
             }
         }
         $email = $config['failure_email'];
         // Failed so send email if applicable
         if (!empty($email)) {
             // Remove Mautic values and password fields
             foreach ($post as $key => $value) {
                 if (in_array($key, ['messenger', 'submit', 'formId', 'formid', 'formName', 'return'])) {
                     unset($post[$key]);
                 }
                 if (isset($fieldTypes[$key]) && in_array($fieldTypes[$key], ['password'])) {
                     $post[$key] = '*********';
                 }
             }
             $post['mautic_contact'] = array_filter($payload['mautic_contact']);
             $post['mautic_form'] = $payload['mautic_form'];
             $results = $this->postToHtml($post);
             $submission = $event->getSubmission();
             $emails = $emails = $this->getEmailsFromString($email);
             $this->mailer->setTo($emails);
             $this->mailer->setSubject($this->translator->trans('mautic.form.action.repost.failed_subject', ['%form%' => $submission->getForm()->getName()]));
             $this->mailer->setBody($this->translator->trans('mautic.form.action.repost.failed_message', ['%link%' => $this->router->generate('mautic_form_results', ['objectId' => $submission->getForm()->getId(), 'result' => $submission->getId()], UrlGeneratorInterface::ABSOLUTE_URL), '%message%' => $exception->getMessage(), '%results%' => $results]));
             $this->mailer->parsePlainText();
             $this->mailer->send();
         }
     }
 }