You are here

private function WebformCivicrmPostProcess::processRelationship in Webform CiviCRM Integration 8.5

Add/update relationship for a pair of contacts

Parameters

$params: Params array for relationship api

$cid1: Contact id

$cid2: Contact id

1 call to WebformCivicrmPostProcess::processRelationship()
WebformCivicrmPostProcess::saveRelationships in src/WebformCivicrmPostProcess.php
Save relationships for a contact

File

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

Class

WebformCivicrmPostProcess

Namespace

Drupal\webform_civicrm

Code

private function processRelationship($params, $cid1, $cid2) {
  $utils = \Drupal::service('webform_civicrm.utils');
  if (!empty($params['relationship_type_id']) && $cid2 && $cid1 != $cid2) {
    list($type, $side) = explode('_', $params['relationship_type_id']);
    $existing = $this
      ->getRelationship([
      $params['relationship_type_id'],
    ], $cid1, $cid2);
    $perm = wf_crm_aval($params, 'relationship_permission');

    // Swap contacts if this is an inverse relationship
    if ($side == 'b' || $existing && $existing['contact_id_a'] != $cid1) {
      list($cid1, $cid2) = [
        $cid2,
        $cid1,
      ];
      if ($perm == 1 || $perm == 2) {
        $perm = $perm == 1 ? 2 : 1;
      }
    }

    //initialise start_date for create action.
    if (empty($existing) && !array_key_exists('start_date', $params)) {
      $params['start_date'] = 'now';
    }
    $params += $existing;
    $params['contact_id_a'] = $cid1;
    $params['contact_id_b'] = $cid2;
    $params['relationship_type_id'] = $type;
    if ($perm) {
      $params['is_permission_a_b'] = $params['is_permission_b_a'] = $perm == 3 ? 1 : 0;
      if ($perm == 1 || $perm == 2) {
        $params['is_permission_' . ($perm == 1 ? 'a_b' : 'b_a')] = 1;
      }
    }
    unset($params['relationship_permission']);
    $utils
      ->wf_civicrm_api('relationship', 'create', $params);
  }
}