You are here

function wf_crm_contact_get in Webform CiviCRM Integration 7.3

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

Parameters

$node: Node object

$enabled: Array of crm webform fields

$c: Contact #

$cids: All known contact ids for this form

array $exclude: Fields to ignore

Return value

array of contact data

2 calls to wf_crm_contact_get()
wf_crm_ajax in ./contact_component.inc
Drupal page callback to serve AJAX requests.
_wf_crm_frontend_form_alter in ./webform_civicrm_forms.inc
Alter front-end of webforms: Called by hook_form_alter() when rendering a civicrm-enabled webform Add custom prefix. Display messages. Block users who should not have access. Set webform default values.

File

./webform_civicrm_forms.inc, line 308

Code

function wf_crm_contact_get($node, $enabled, $c, $cids, $exclude = array()) {
  $info = array();
  $cid = $cids[$c];
  $data = $node->webform_civicrm['data'];
  $contact = $data['contact'][$c];
  $prefix = 'civicrm_' . $c . '_contact_1_';
  foreach (array(
    'contact',
    'address',
    'email',
    'phone',
    'website',
  ) as $field) {
    if (!empty($contact['number_of_' . $field]) && !in_array($field, $exclude) || $field == 'contact') {
      $params = array(
        'contact_id' => $cid,
      );
      if ($field != 'contact' && $field != 'website') {
        $params['options']['sort'] = 'is_primary DESC';
      }
      $result = wf_civicrm_api($field, 'get', $params);
      if (!empty($result['values'])) {
        $result = array_merge(array(
          0,
        ), array_values($result['values']));
        unset($result[0]);
        if ($field == 'contact') {

          // Privacy fields
          foreach (array_keys(wf_crm_get_options('privacy')) as $key) {
            if (!empty($result[1][$key])) {
              $result[1]['privacy'][] = $key;
            }
          }
        }

        // Extra processing for addresses
        if ($field == 'address') {
          foreach ($result as &$address) {

            // Translate to abbr
            if (!empty($address['state_province_id'])) {
              $address['state_province_id'] = wf_crm_state_abbr($address['state_province_id']);
            }

            // Load custom data
            $custom = wf_crm_get_custom($address['id'], 'address');
            if (!empty($custom['address'])) {
              $address += $custom['address'][1];
            }
          }
        }
        $info[$field] = $result;
      }
    }
  }

  // Get custom contact data if needed
  foreach ($contact as $k => $v) {
    if (substr($k, 0, 12) == 'number_of_cg' && !empty($v)) {
      if (!in_array(substr($k, 10), $exclude)) {
        $info += wf_crm_get_custom($cid);
        break;
      }
    }
  }

  // Communication prefs are not fetched by default by the api
  if (isset($enabled[$prefix . 'contact_preferred_communication_method']) || isset($enabled[$prefix . 'contact_preferred_language'])) {
    if (!in_array('contact', $exclude)) {
      $result = wf_civicrm_api('contact', 'get', array(
        'contact_id' => $cid,
        'return.preferred_communication_method' => 1,
        'return.preferred_language' => 1,
      ));
      $info['contact'][1] += $result['values'][$cid];
    }
  }

  // Retrieve group and tag data
  if (!in_array('other', $exclude)) {
    $api = array(
      'tag' => 'entity_tag',
      'group' => 'group_contact',
    );
    foreach (array_keys($enabled) as $fid) {

      // This way we support multiple tag fields (for tagsets)
      if (strpos($fid, $prefix . 'other') !== FALSE) {
        list(, , , , , $ent) = explode('_', $fid);
        list(, , , , , $field) = explode('_', $fid, 6);

        // Cheap way to avoid fetching the same data twice from the api
        if (!is_array($api[$ent])) {
          $api[$ent] = wf_civicrm_api($api[$ent], 'get', array(
            'contact_id' => $cid,
          ));
        }
        foreach (wf_crm_aval($api[$ent], 'values') as $val) {
          $info['other'][1][$field][] = $val[$ent . '_id'];
        }
      }
    }
  }

  // Retrieve relationship data
  if (!in_array('relationship', $exclude) && !empty($contact['number_of_relationship'])) {
    $enabled = wf_crm_enabled_fields($node);
    for ($r = 1; $r <= $contact['number_of_relationship']; ++$r) {
      $types = array();
      $prefix = "civicrm_{$c}_contact_{$r}_relationship_";
      if (!empty($cids[$r])) {
        if (!empty($contact['relationship'][$r]['relationship_type_id']) && $contact['relationship'][$r]['relationship_type_id'] != 'create_civicrm_webform_element') {
          $types = array(
            $contact['relationship'][$r]['relationship_type_id'],
          );
        }
        if (!empty($enabled[$prefix . 'relationship_type_id'])) {
          $types = array_keys(wf_crm_exposed_options($node, $enabled[$prefix . 'relationship_type_id']));
        }
      }
      $rel = wf_crm_relationship_get($types, $cid, $cids[$r]);
      if ($rel) {
        $info['relationship'][$r] = $rel;

        // Fetch custom data
        $len = strlen($prefix . 'custom_');
        foreach ($enabled as $k => $v) {
          if (substr($k, 0, $len) == $prefix . 'custom_') {
            $custom = wf_civicrm_api('custom_value', 'get', array(
              'entity_id' => $rel['id'],
              'entity_table' => 'Relationship',
            ));
            foreach ($custom['values'] as $k => $v) {
              if (isset($v[0])) {
                $info['relationship'][$r]["custom_{$k}"] = $v[0];
              }
            }
            break;
          }
        }
      }
    }
  }
  return $info;
}