You are here

protected function WebformCivicrmBase::loadContact in Webform CiviCRM Integration 8.5

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 WebformCivicrmBase::loadContact()
WebformAjax::contactAjax in src/WebformAjax.php
Load one or more contacts via ajax
WebformCivicrmPostProcess::isFieldHiddenByExistingContactSettings in src/WebformCivicrmPostProcess.php
Test whether a field has been hidden due to existing contact settings
WebformCivicrmPreProcess::alterForm in src/WebformCivicrmPreProcess.php
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

src/WebformCivicrmBase.php, line 123
Front-end form handler base class.

Class

WebformCivicrmBase
Class WebformCivicrmBase

Namespace

Drupal\webform_civicrm

Code

protected function loadContact($c, $exclude = []) {
  if (!empty($this->loadedContacts[$c])) {
    return $this->loadedContacts[$c];
  }
  $info = [];
  $utils = \Drupal::service('webform_civicrm.utils');
  $cid = $this->ent['contact'][$c]['id'];
  if (!$cid) {
    return $info;
  }
  $contact = $this->data['contact'][$c];
  $prefix = 'civicrm_' . $c . '_contact_1_';
  $existing_contact_field = $this->node
    ->getElement($prefix . 'contact_existing');
  $element_manager = \Drupal::getContainer()
    ->get('plugin.manager.webform.element');
  $existing_component_plugin = $element_manager
    ->getElementInstance($existing_contact_field);
  $element_exclude = $existing_component_plugin
    ->getElementProperty($existing_contact_field, 'no_autofill');
  $exclude = array_merge($exclude, $element_exclude);
  foreach (array_merge([
    'contact',
  ], $utils
    ->wf_crm_location_fields()) as $ent) {
    if (!empty($contact['number_of_' . $ent]) && !in_array($ent, $exclude) || $ent == 'contact') {
      $params = [
        'contact_id' => $cid,
      ];
      if ($ent != 'contact' && $ent != 'website') {
        $params['options']['sort'] = 'is_primary DESC';
      }
      $result = $utils
        ->wf_civicrm_api($ent, 'get', $params);

      // Handle location field sorting
      if (in_array($ent, $utils
        ->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([
          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($utils
              ->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'] = $utils
              ->wf_crm_user_cid($cid, 'contact');
          }

          // Hack for gender as textfield. More general solution needed for all pseudoconsant fields
          $gender_field = $this->node
            ->getElement("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'] = $utils
                ->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 += array_diff_key($this
          ->getCustomData($cid), array_flip($exclude));
        break;
      }
    }
  }

  // Retrieve group and tag data
  if (!in_array('other', $exclude)) {
    $api = [
      '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] = $utils
            ->wf_civicrm_api($api[$ent], 'get', [
            '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 = $utils
      ->wf_crm_enabled_fields($this->node);
    for ($r = 1; $r <= $contact['number_of_relationship']; ++$r) {
      $types = [];
      $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;
      }
    }
  }
  $this->loadedContacts[$c] = $info;
  return $info;
}