You are here

protected function wf_crm_webform_base::getRelationship in Webform CiviCRM Integration 7.4

Same name and namespace in other branches
  1. 7.5 includes/wf_crm_webform_base.inc \wf_crm_webform_base::getRelationship()

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_webform_base::getRelationship()
wf_crm_webform_base::loadContact in includes/wf_crm_webform_base.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_webform_postprocess::processRelationship in includes/wf_crm_webform_postprocess.inc
Add/update relationship for a pair of contacts

File

includes/wf_crm_webform_base.inc, line 496

Class

wf_crm_webform_base
Class wf_crm_webform_base

Code

protected function getRelationship($r_types, $cid1, $cid2) {
  $found = array();
  $active_clause = '';
  if (!empty($this->settings['create_new_relationship'])) {
    $active_clause = 'AND is_active = 1 AND (end_date IS NULL OR end_date >= now())';
  }
  if ($r_types && $cid1 && $cid2) {
    $types = array();
    foreach ($r_types as $r_type) {
      list($type, $side) = explode('_', $r_type);
      $types[$type] = $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})) {$active_clause}\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' && $k != 'relationship_type_id') {
            $found[$k] = $v;
          }
        }

        // 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'] = ($found['is_permission_a_b'] ? 1 : 0) + ($found['is_permission_b_a'] ? 2 : 0);
      }
    }
    $dao
      ->free();
  }
  return $found;
}