You are here

function wf_crm_relationship_get in Webform CiviCRM Integration 7.3

Fetch relationship for a pair of contacts

Parameters

$r_types: Array of relationship type ids

$cid1: Contact id

$cid2: Contact id

Return value

array

2 calls to wf_crm_relationship_get()
wf_crm_contact_get in ./webform_civicrm_forms.inc
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
wf_crm_process_relationship in ./webform_civicrm_forms.inc
Dispatch function to add/update relationship for a pair of contacts Called during webform submission

File

./webform_civicrm_forms.inc, line 1506

Code

function wf_crm_relationship_get($r_types, $cid1, $cid2) {
  $found = array();
  if ($r_types && $cid1 && $cid2) {
    $types = array();
    foreach ($r_types as $r_type) {
      list($type, $side) = explode('_', $r_type);
      $types[] = $type;
    }
    $sql = "SELECT * FROM civicrm_relationship\n      WHERE relationship_type_id IN (" . implode(',', $types) . ")\n      AND ((contact_id_a = {$cid1} AND contact_id_b = {$cid2}) OR (contact_id_a = {$cid2} AND contact_id_b = {$cid1}))\n      ORDER BY is_active DESC, IF(end_date, 1, 0), end_date DESC";
    $dao = CRM_Core_DAO::executeQuery($sql);
    while ($dao
      ->fetch()) {
      $type = $dao->relationship_type_id;
      $side = $dao->contact_id_a == $cid1 ? 'a' : 'b';

      // Verify this is the correct orientation for the relationship
      if (in_array("{$type}_{$side}", $r_types) || in_array("{$type}_r", $r_types)) {

        // Discard metadata from the query
        foreach ((array) $dao as $k => $v) {
          if ($k[0] != '_' && $k != 'N') {
            $found[$k] = $v;
          }
        }
        $found['relationship_type_id'] = in_array("{$type}_r", $r_types) ? "{$type}_r" : "{$type}_{$side}";
        $found['relationship_permission'] = ($found['is_permission_a_b'] ? 1 : 0) + ($found['is_permission_b_a'] ? 2 : 0);
        break;
      }
    }
    $dao
      ->free();
  }
  return $found;
}