You are here

function wf_crm_find_relations in Webform CiviCRM Integration 7.3

Same name and namespace in other branches
  1. 7.5 includes/contact_component.inc \wf_crm_find_relations()
  2. 7.4 includes/contact_component.inc \wf_crm_find_relations()

Get a contact's relations of certain types

Parameters

cid: Contact id

types: Array of relationship_type_ids

$current: Limit to current & enabled relations?

Return value

array

1 call to wf_crm_find_relations()
wf_crm_find_contact in ./contact_component.inc
Find an existing contact based on matching criteria Used to autopopulate a webform existing contact field

File

./contact_component.inc, line 887

Code

function wf_crm_find_relations($cid, $types, $current = TRUE) {
  $found = $allowed = array();
  $cid = (int) $cid;
  static $employer_type = 0;
  if ($cid) {
    if (!$employer_type && $current) {
      $employer_type = CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_relationship_type WHERE name_a_b = 'Employee of'");
    }
    $type_ids = '';
    foreach ($types as $t) {
      list($type, $a_b) = explode('_', $t);

      // Put current employer first in the list
      if ($type == $employer_type && $current) {
        $sql = "SELECT id, employer_id\n        FROM civicrm_contact\n        WHERE id = {$cid} OR employer_id = {$cid}";
        $dao =& CRM_Core_DAO::executeQuery($sql);
        while ($dao
          ->fetch()) {
          $found[] = $dao->id == $cid ? $dao->employer_id : $dao->id;
        }
        $dao
          ->free();
      }
      $type_ids .= ($type_ids ? ',' : '') . $type;
      if ($a_b == 'a' || $a_b == 'r') {
        $allowed[] = $type . '_a';
      }
      if ($a_b == 'b' || $a_b == 'r') {
        $allowed[] = $type . '_b';
      }
    }
    if ($type_ids) {
      $sql = "SELECT relationship_type_id, contact_id_a, contact_id_b\n        FROM civicrm_relationship\n        WHERE relationship_type_id IN ({$type_ids}) AND (contact_id_a = {$cid} OR contact_id_b = {$cid})";
      if ($current) {
        $sql .= " AND is_active = 1 AND (end_date > CURDATE() OR end_date IS NULL)";
      }
      $dao =& CRM_Core_DAO::executeQuery($sql);
      while ($dao
        ->fetch()) {
        $a_b = $dao->contact_id_a == $cid ? 'b' : 'a';
        if (in_array($dao->relationship_type_id . '_' . $a_b, $allowed)) {
          $c = $dao->{"contact_id_{$a_b}"};
          $found[$c] = $c;
        }
      }
      $dao
        ->free();
    }
  }
  return $found;
}