private function WebformCivicrmPostProcess::saveContactLocation in Webform CiviCRM Integration 8.5
Save location data for a contact
Parameters
array $contact:
int $cid:
int $c:
1 call to WebformCivicrmPostProcess::saveContactLocation()
- 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 816 - Front-end form validation and post-processing.
Class
Namespace
Drupal\webform_civicrmCode
private function saveContactLocation($contact, $cid, $c) {
$utils = \Drupal::service('webform_civicrm.utils');
// Check which location_type_id is to be set as is_primary=1;
$is_primary_address_location_type = wf_crm_aval($contact, 'address:1:location_type_id');
$is_primary_email_location_type = wf_crm_aval($contact, 'email:1:location_type_id');
foreach ($utils
->wf_crm_location_fields() as $location) {
if (!empty($contact[$location])) {
$existing = [];
$params = [
'contact_id' => $cid,
];
if ($location != 'website') {
$params['options'] = [
'sort' => 'is_primary DESC',
];
}
$result = $utils
->wf_civicrm_api($location, 'get', $params);
if (!empty($result['values'])) {
$contact[$location] = self::reorderLocationValues($contact[$location], $result['values'], $location);
// start array index at 1
$existing = array_merge([
[],
], $result['values']);
}
foreach ($contact[$location] as $i => $params) {
// Translate state/prov abbr to id
if (!empty($params['state_province_id'])) {
$default_country = $utils
->wf_crm_get_civi_setting('defaultContactCountry', 1228);
if (!($params['state_province_id'] = $utils
->wf_crm_state_abbr($params['state_province_id'], 'id', wf_crm_aval($params, 'country_id', $default_country)))) {
$params['state_province_id'] = '';
}
}
// Substitute county stub ('-' is a hack to get around required field when there are no available counties)
if (isset($params['county_id']) && $params['county_id'] === '-') {
$params['county_id'] = '';
}
// Check if anything was changed, else skip the update
if (!empty($existing[$i])) {
$same = TRUE;
foreach ($params as $param => $val) {
if ($val != (string) wf_crm_aval($existing[$i], $param, '')) {
$same = FALSE;
}
}
if ($same) {
continue;
}
}
if ($location == 'address') {
// Store shared addresses for later since we haven't necessarily processed
// the contact this address is shared with yet.
if (!empty($params['master_id'])) {
$this->shared_address[$cid][$i] = [
'id' => wf_crm_aval($existing, "{$i}:id"),
'mc' => $params['master_id'],
'loc' => $params['location_type_id'],
'pri' => $params['location_type_id'] == $is_primary_address_location_type,
];
continue;
}
// Reset calculated values when updating an address
$params['master_id'] = $params['geo_code_1'] = $params['geo_code_2'] = 'null';
}
$params['contact_id'] = $cid;
if (!empty($existing[$i])) {
$params['id'] = $existing[$i]['id'];
}
if ($this
->locationIsEmpty($location, $params)) {
// Delete this location if nothing was entered and this is a known contact
if (!empty($this->existing_contacts[$c]) && !empty($params['id'])) {
$utils
->wf_civicrm_api($location, 'delete', $params);
}
continue;
}
if ($location != 'website') {
if (empty($params['location_type_id'])) {
$params['location_type_id'] = wf_crm_aval($existing, "{$i}:location_type_id", 1);
}
$params['is_primary'] = $i == 1 ? 1 : 0;
// Override that just in cases we know
if ($location == 'address' && $params['location_type_id'] == $is_primary_address_location_type) {
$params['is_primary'] = 1;
}
if ($location == 'email' && $params['location_type_id'] == $is_primary_email_location_type) {
$params['is_primary'] = 1;
}
}
$utils
->wf_civicrm_api($location, 'create', $params);
}
}
}
}