You are here

public function WebformCivicrmPostProcess::preSave in Webform CiviCRM Integration 8.5

Process webform submission when it is about to be saved. Called by the following hook:

Parameters

\Drupal\webform\WebformSubmissionInterface $webform_submission:

Overrides WebformCivicrmPostProcessInterface::preSave

See also

webform_civicrm_webform_submission_presave

File

src/WebformCivicrmPostProcess.php, line 179
Front-end form validation and post-processing.

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

public function preSave(WebformSubmissionInterface $webform_submission) {
  $this->submission = $webform_submission;
  $this->data = $this->settings['data'];
  $this
    ->modifyWebformSubmissionData($webform_submission);

  // Check for existing submission
  // $this->setUpdateParam();
  // Fill $this->id from existing contacts
  $this
    ->getExistingContactIds();

  // While saving a draft, just skip to postSave and write the record
  if ($this->submission
    ->isDraft()) {
    return;
  }
  $this
    ->fillDataFromSubmission();

  //Fill Custom Contact reference fields.
  $this
    ->fillContactRefs(TRUE);

  // Create/update contacts
  foreach ($this->data['contact'] as $c => $contact) {
    if (empty($this->ent['contact'][$c]['id'])) {

      // Don't create contact if we don't have a name or email
      if ($this
        ->isContactEmpty($contact)) {
        $this->ent['contact'][$c]['id'] = 0;
        continue;
      }
      $this->ent['contact'][$c]['id'] = $this
        ->findDuplicateContact($contact);
    }

    // Current employer must wait for ContactRef ids to be filled
    unset($contact['contact'][1]['employer_id']);
    $newContact = empty($this->ent['contact'][$c]['id']);

    // Create new contact
    if ($newContact) {
      $this->ent['contact'][$c]['id'] = $this
        ->createContact($contact);
    }
    if ($c == 1) {
      $this
        ->setLoggingContact();
    }

    // Update existing contact
    if (!$newContact) {
      $this
        ->updateContact($contact, $c);
    }
  }

  // $this->ent['contact'] will now contain all contacts in order, with 0 as a placeholder id for any contact not saved
  ksort($this->ent['contact']);

  // Once all contacts are saved we can fill contact ref fields
  $this
    ->fillContactRefs();

  // Save a non-live transaction
  if ($this->totalContribution && empty($this->ent['contribution'][1]['id'])) {
    $this
      ->createDeferredPayment();
  }

  // Create/update other data associated with contacts
  foreach ($this->data['contact'] as $c => $contact) {
    $cid = $this->ent['contact'][$c]['id'];
    if (!$cid) {
      continue;
    }
    if (!empty($contact['update_contact_ref'])) {
      $this
        ->saveContactRefs($contact, $cid);
    }
    $this
      ->saveCurrentEmployer($contact, $cid);
    $this
      ->fillHiddenContactFields($cid, $c);
    $this
      ->saveContactLocation($contact, $cid, $c);
    $this
      ->saveGroupsAndTags($contact, $cid, $c);
    $this
      ->saveRelationships($contact, $cid, $c);

    // Process event participation
    if (isset($this->all_sets['participant']) && !empty($this->data['participant_reg_type'])) {
      $this
        ->processParticipants($c, $cid);
    }
  }

  // We do this after all contacts and addresses exist
  $this
    ->processSharedAddresses();

  // Process memberships after relationships have been created
  foreach ($this->ent['contact'] as $c => $contact) {
    if ($contact['id'] && isset($this->all_sets['membership']) && !empty($this->data['membership'][$c]['number_of_membership'])) {
      $this
        ->processMemberships($c, $contact['id']);
    }
  }
}