You are here

function wf_crm_find_relations in Webform CiviCRM Integration 7.4

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

Get a contact's relations of certain types

Parameters

int cid: Contact id

array types: Array of relationship_type_ids

bool $current: Limit to current & enabled relations?

Return value

array

2 calls to wf_crm_find_relations()
wf_crm_contact_search in includes/contact_component.inc
Returns a list of contacts based on component settings.
wf_crm_webform_base::findContact in includes/wf_crm_webform_base.inc
Find an existing contact based on matching criteria Used to populate a webform existing contact field

File

includes/contact_component.inc, line 889

Code

function wf_crm_find_relations($cid, $types = array(), $current = TRUE) {
  $found = $allowed = array();
  $cid = (int) $cid;
  static $employer_type = 0;
  if ($cid) {
    if (!$employer_type && $current) {
      $employer_type = wf_crm_aval(wf_civicrm_api('relationshipType', 'get', array(
        'name_a_b' => 'Employee of',
        'return' => 'id',
      )), 'id');
    }
    $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);
        $employer = $dao->id == $cid ? $dao->employer_id : $dao->id;
        while ($dao
          ->fetch()) {
          $found[$employer] = $employer;
        }
        $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';
      }
    }
    $typeClause = '';
    if ($type_ids) {
      $typeClause = "AND relationship_type_id IN ({$type_ids})";
    }
    $sql = "SELECT relationship_type_id, contact_id_a, contact_id_b\n      FROM civicrm_relationship\n      WHERE (contact_id_a = {$cid} OR contact_id_b = {$cid}) {$typeClause}";
    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 (!$allowed || in_array($dao->relationship_type_id . '_' . $a_b, $allowed)) {
        $c = $dao->{"contact_id_{$a_b}"};
        $found[$c] = $c;
      }
    }
    $dao
      ->free();
  }
  return $found;
}