You are here

protected function WebformCivicrmBase::getRelationship in Webform CiviCRM Integration 8.5

Fetch relationship for a pair of contacts

Parameters

$r_types: Array of relationship type ids

$cid1: Contact id

$cid2: Contact id

$active_only: if TRUE - only active relationships are returned.

Return value

array

3 calls to WebformCivicrmBase::getRelationship()
WebformCivicrmBase::loadContact in src/WebformCivicrmBase.php
Fetch all relevant data for a given contact Used to load contacts for pre-filling a webform, and also to fill in a contact via ajax
WebformCivicrmPostProcess::expireRelationship in src/WebformCivicrmPostProcess.php
End relationship for a pair of contacts
WebformCivicrmPostProcess::processRelationship in src/WebformCivicrmPostProcess.php
Add/update relationship for a pair of contacts

File

src/WebformCivicrmBase.php, line 501
Front-end form handler base class.

Class

WebformCivicrmBase
Class WebformCivicrmBase

Namespace

Drupal\webform_civicrm

Code

protected function getRelationship($r_types, $cid1, $cid2, $active_only = FALSE) {
  $found = [];
  if (!$active_only) {
    $active_only = !empty($this->settings['create_new_relationship']);
  }
  $utils = \Drupal::service('webform_civicrm.utils');
  if ($r_types && $cid1 && $cid2) {
    $types = [];
    foreach ($r_types as $r_type) {
      list($type, $side) = explode('_', $r_type);
      $types[$type] = $type;
    }
    $params = [
      'contact_id_a' => [
        'IN' => [
          $cid1,
          $cid2,
        ],
      ],
      'contact_id_b' => [
        'IN' => [
          $cid1,
          $cid2,
        ],
      ],
      'relationship_type_id' => [
        'IN' => $types,
      ],
    ];
    if ($active_only) {
      $params['is_active'] = 1;
      $params['options']['sort'] = 'is_active DESC, end_date ASC';
    }
    foreach ($utils
      ->wf_crm_apivalues('relationship', 'get', $params) as $rel) {
      $type = $rel['relationship_type_id'];
      $side = $rel['contact_id_a'] == $cid1 ? 'a' : 'b';
      if ((in_array("{$type}_{$side}", $r_types) || in_array("{$type}_r", $r_types)) && ($rel['contact_id_a'] != $rel['contact_id_b'] || $cid1 == $cid2) && (empty($rel['end_date']) || !$active_only || strtotime($rel['end_date']) > time())) {

        // Support multi-valued relationship type fields, fudge the rest
        $found['relationship_type_id'][] = in_array("{$type}_r", $r_types) ? "{$type}_r" : "{$type}_{$side}";
        $found['relationship_permission'] = (!empty($rel['is_permission_a_b']) ? 1 : 0) + (!empty($rel['is_permission_b_a']) ? 2 : 0);
        $found += $rel;
      }
    }
  }
  return $found;
}