You are here

public function wf_crm_webform_postprocess::preSave in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_webform_postprocess.inc \wf_crm_webform_postprocess::preSave()

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

Parameters

stdClass $submission:

See also

webform_civicrm_webform_submission_presave

File

includes/wf_crm_webform_postprocess.inc, line 112

Class

wf_crm_webform_postprocess

Code

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

  // 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 (!empty($this->submission->is_draft)) {
    return;
  }
  $this
    ->fillDataFromSubmission();

  // 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 (empty($this->ent['contribution'][1]['id']) && $this->totalContribution) {
    $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;
    }
    $this
      ->saveCurrentEmployer($contact, $cid);
    $this
      ->saveCustomData($contact, $cid, 'Contact', !empty($this->existing_contacts[$c]), $c);
    $this
      ->fillHiddenContactFields($cid, $c);
    $this
      ->saveContactLocation($contact, $cid, $c);
    $this
      ->saveGroupsAndTags($contact, $cid, $c);

    // Process relationships
    foreach (wf_crm_aval($contact, 'relationship', array()) as $n => $params) {
      $relationship_type_id = wf_crm_aval($params, 'relationship_type_id');
      if ($relationship_type_id) {
        foreach ((array) $relationship_type_id as $params['relationship_type_id']) {
          $this
            ->processRelationship($params, $cid, $this->ent['contact'][$n]['id']);
        }
      }
    }

    // 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']);
    }
  }
}