You are here

protected function wf_crm_webform_base::loadContact 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::loadContact()

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

int $c: Contact #

array $exclude: Fields to ignore

Return value

array Contact data

3 calls to wf_crm_webform_base::loadContact()
wf_crm_webform_ajax::contactAjax in includes/wf_crm_webform_ajax.inc
Load one or more contacts via ajax
wf_crm_webform_postprocess::isFieldHiddenByExistingContactSettings in includes/wf_crm_webform_postprocess.inc
Test whether a field has been hidden due to existing contact settings
wf_crm_webform_preprocess::alterForm in includes/wf_crm_webform_preprocess.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

includes/wf_crm_webform_base.inc, line 93

Class

wf_crm_webform_base
Class wf_crm_webform_base

Code

protected function loadContact($c, $exclude = array()) {
  if (!empty($this->loadedContacts[$c])) {
    return $this->loadedContacts[$c];
  }
  $info = array();
  $cid = $this->ent['contact'][$c]['id'];
  if (!$cid) {
    return $info;
  }
  $contact = $this->data['contact'][$c];
  $prefix = 'civicrm_' . $c . '_contact_1_';
  $existing_contact_field = $this
    ->getComponent($prefix . 'contact_existing');
  $exclude = array_merge($exclude, wf_crm_aval($existing_contact_field['extra'], 'no_autofill', array()));
  foreach (array_merge(array(
    'contact',
  ), wf_crm_location_fields()) as $ent) {
    if (!empty($contact['number_of_' . $ent]) && !in_array($ent, $exclude) || $ent == 'contact') {
      $params = array(
        'contact_id' => $cid,
      );
      if ($ent != 'contact' && $ent != 'website') {
        $params['options']['sort'] = 'is_primary DESC';
      }
      $result = wf_civicrm_api($ent, 'get', $params);

      // Handle location field sorting
      if (in_array($ent, wf_crm_location_fields())) {
        $result['values'] = $this
          ->reorderByLocationType($c, $ent, $result['values']);
      }
      if (!empty($result['values'])) {

        // Index array from 1 instead of 0
        $result = array_merge(array(
          0,
        ), array_values($result['values']));
        unset($result[0]);
        if ($ent == 'contact') {

          // Exclude name fields
          if (in_array('name', $exclude)) {
            unset($result[1]['first_name'], $result[1]['middle_name'], $result[1]['last_name'], $result[1]['formal_title'], $result[1]['prefix_id'], $result[1]['suffix_id'], $result[1]['nick_name'], $result[1]['organization_name'], $result[1]['household_name']);
          }

          // Privacy fields
          if (isset($this->enabled[$prefix . 'contact_privacy'])) {
            foreach (array_keys(wf_crm_get_privacy_options()) as $key) {
              if (!empty($result[1][$key])) {
                $result[1]['privacy'][] = $key;
              }
            }
          }

          // User id
          if (isset($this->enabled[$prefix . 'contact_user_id'])) {
            $result[1]['user_id'] = wf_crm_user_cid($cid, 'contact');
          }

          // Hack for gender as textfield. More general solution needed for all pseudoconsant fields
          $gender_field = $this
            ->getComponent("civicrm_{$c}_contact_1_contact_gender_id");
          if ($gender_field && $gender_field['type'] == 'textfield') {
            $result[1]['gender_id'] = wf_crm_aval($result[1], 'gender');
          }
        }

        // Extra processing for addresses
        if ($ent == '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
            if (isset($address['id'])) {
              $custom = $this
                ->getCustomData($address['id'], 'address');
              if (!empty($custom['address'])) {
                $address += $custom['address'][1];
              }
            }
          }
        }
        $info[$ent] = $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 += $this
          ->getCustomData($cid);
        break;
      }
    }
  }

  // Retrieve group and tag data
  if (!in_array('other', $exclude)) {
    $api = array(
      'tag' => 'entity_tag',
      'group' => 'group_contact',
    );
    foreach (array_keys($this->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'])) {
    $this->enabled = wf_crm_enabled_fields($this->node);
    for ($r = 1; $r <= $contact['number_of_relationship']; ++$r) {
      $types = array();
      $prefix = "civicrm_{$c}_contact_{$r}_relationship_";
      if (!empty($this->ent['contact'][$r]['id'])) {
        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($this->enabled[$prefix . 'relationship_type_id'])) {
          $types += array_keys($this
            ->getExposedOptions($prefix . 'relationship_type_id'));
        }
      }
      $rel = $this
        ->getRelationship($types, $cid, wf_crm_aval($this->ent['contact'], "{$r}:id"));
      if ($rel) {
        $info['relationship'][$r] = $rel;

        // Fetch custom data
        $len = strlen($prefix . 'custom_');
        foreach ($this->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;
          }
        }
      }
    }
  }
  $this->loadedContacts[$c] = $info;
  return $info;
}