You are here

private function WebformCivicrmPostProcess::updateContact in Webform CiviCRM Integration 8.5

Update a contact

Parameters

array $contact:

int $c:

1 call to WebformCivicrmPostProcess::updateContact()
WebformCivicrmPostProcess::preSave in src/WebformCivicrmPostProcess.php
Process webform submission when it is about to be saved. Called by the following hook:

File

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

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function updateContact($contact, $c) {
  $params = $contact['contact'][1];
  $utils = \Drupal::service('webform_civicrm.utils');
  unset($params['contact_type'], $params['contact_id']);

  // Fetch data from existing multivalued fields
  $fetch = $multi = [];
  foreach ($this->all_fields as $fid => $field) {
    if (!empty($field['extra']['multiple']) && substr($fid, 0, 7) == 'contact') {
      list(, $name) = explode('_', $fid, 2);
      if ($name != 'privacy' && isset($params[$name])) {
        $fetch["return.{$name}"] = 1;
        $multi[] = $name;
      }
    }
  }

  // Merge data from existing multivalued fields
  if ($multi) {
    $existing = $utils
      ->wf_civicrm_api('contact', 'get', [
      'id' => $this->ent['contact'][$c]['id'],
    ] + $fetch);
    $existing = wf_crm_aval($existing, 'values:' . $this->ent['contact'][$c]['id'], []);
    foreach ($multi as $name) {
      $exist_to_merge = (array) wf_crm_aval($existing, $name, []);
      $exist = array_filter(array_combine($exist_to_merge, $exist_to_merge));

      // Only known contacts are allowed to empty a field
      if (!empty($this->existing_contacts[$c])) {
        foreach ($this
          ->getExposedOptions("civicrm_{$c}_contact_1_contact_{$name}") as $k => $v) {
          unset($exist[$k]);
        }
      }
      $params[$name] = array_unique(array_map("strtolower", array_merge($params[$name], $exist)));
    }
  }
  $params['id'] = $this->ent['contact'][$c]['id'];
  $utils
    ->wf_civicrm_api('contact', 'create', $params);
}