You are here

function sf_user_import in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 5.2 sf_user/sf_user.module \sf_user_import()

Imports data from Salesforce into a user.

Parameters

$sf_data: The Salesforce Object OR The Salesforce ID of the object to be imported.

$name: The name of the fieldmap to use to create the export object.

$uid: The uid of the user to update. If left NULL, a new user will be created.

Return value

The uid of the imported user or FALSE on failure.

1 call to sf_user_import()
sf_user_salesforce_form_submit in sf_user/sf_user.module

File

sf_user/sf_user.module, line 594
Integrates the core user object and profile module with the Salesforce API.

Code

function sf_user_import($sf_data, $name, $uid = NULL, $options = array()) {

  // Retrieve the object from Salesforce.
  $sf = salesforce_api_connect();
  if (!$sf) {
    if (user_access('administer salesforce')) {
      drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
        '!url' => url(SALESFORCE_PATH_ADMIN),
      )));
    }
    return FALSE;
  }
  if (is_sfid($sf_data)) {
    $sf_data = salesforce_api_retrieve(array(
      $sf_data,
    ), $name);

    // Check to see if sf_data is an array of objects
    if (is_array($sf_data) && count($sf_data) > 0) {
      $sf_data = $sf_data[0];
    }
  }
  elseif (is_array($sf_data)) {
    $sf_data = (object) $sf_data;
  }
  if (empty($sf_data)) {
    return FALSE;
  }

  // Load the fieldmap data.
  $map = salesforce_api_fieldmap_load($name);

  // Load the object definitions.
  $drupal_object_definition = salesforce_api_fieldmap_objects_load('drupal', $map->drupal);
  $salesforce_object_definition = salesforce_api_fieldmap_objects_load('salesforce', $map->salesforce);

  // If a user was specified, attempt to load it.
  $account = user_load(array(
    'uid' => $uid,
  ));
  if (empty($uid) || empty($account)) {

    // Look for any matching records which we might want to update instead of creating duplicates.
    $matches = salesforce_api_search_for_duplicates('import', 'user', $sf_data, $name);
    if (!empty($matches)) {
      $uid = reset($matches);
      if (!empty($uid)) {
        $account = user_load($uid);
      }
    }
  }

  // Loop through the fields on the fieldmap.
  $changes = array();
  foreach ($map->fields as $sf_fieldname => $drupal_fieldname) {

    // If a handler is specified for importing a value from Salesforce.
    if (isset($drupal_object_definition['fields'][$drupal_fieldname]['import'])) {
      $drupal_field_import_handler = $drupal_object_definition['fields'][$drupal_fieldname]['import'];
      $drupal_field_definition = $drupal_object_definition['fields'][$drupal_fieldname];
      $sf_field_definition = $salesforce_object_definition['fields'][$sf_fieldname];

      // Get the value for the field from the handler function.
      $change = $drupal_field_import_handler($account, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition);
      if (is_array($changes) && is_array($change)) {
        $changes = array_merge($changes, $change);
      }
      else {
        salesforce_api_log(SALESFORCE_LOG_ALL, t('@import_handler import handler did not return an array!'), array(
          '@import_handler' => $drupal_field_import_handler,
        ), WATCHDOG_ERROR);
      }
    }
    elseif (isset($sf_data->{$sf_fieldname}) && !empty($drupal_fieldname)) {
      $changes[$drupal_fieldname] = $sf_data->{$sf_fieldname};
    }
  }
  foreach (module_implements('salesforce_api_pre_import') as $module) {
    $function = $module . '_salesforce_api_pre_import';
    $continue = $function($account, $name, $sf_data, $changes);
    if ($continue === FALSE) {
      return;
    }
  }

  // Check for changes made to the account
  $num_changes = 0;
  foreach ($changes as $key => $value) {
    if (!empty($value)) {
      if (!isset($account->{$key})) {
        $num_changes++;
      }
      elseif ($account->{$key} != $value) {
        $num_changes++;
      }
    }
  }

  // If no changes have been made, there is no need to save
  if ($num_changes == 0) {

    // Nothing changed, so do not do the save
    // Return the user ID to signal a "successful" save
    return $account->uid;
  }

  //drupal_set_message('<pre>'. print_r($changes, TRUE) .'</pre>');

  // We don't want to trigger an export while we're running import.
  // sf_user_skip_export is a flag to tell hook_user not to export this object.
  $account->sf_user_skip_export = TRUE;

  // If the user exists, update the existing user.
  if ($account->uid) {
    $account = user_save($account, $changes);
    if ($account == FALSE) {

      // User account not saved
      if (user_access('administer salesforce')) {
        drupal_set_message(t('A user account could not be imported for update. A required field for Drupal may be missing. <pre>%sf_data</pre>', array(
          '%sf_data' => print_r($sf_data, TRUE),
        )), 'error');
      }
      salesforce_api_log(SALESFORCE_LOG_ALL, t('A user account could not be imported for update. A required field for Drupal may be missing. <pre>%sf_data</pre>', array(
        '%sf_data' => print_r($sf_data, TRUE),
      )), WATCHDOG_ERROR);
      return FALSE;
    }
    $create = FALSE;
  }
  else {

    // Pass a dummy account object for inserts.
    $account = (object) array(
      'sf_user_skip_export' => TRUE,
      'uid' => FALSE,
    );
    $account = user_save($account, $changes);
    if ($account == FALSE) {

      // User account not saved
      if (user_access('administer salesforce')) {
        drupal_set_message(t('A user account could not be imported for creation. A required field for Drupal may be missing. <pre>%sf_data</pre>', array(
          '%sf_data' => print_r($sf_data),
        )), 'error');
      }
      salesforce_api_log(SALESFORCE_LOG_ALL, t('A user account could not be imported for creation. A required field for Drupal may be missing. <pre>%sf_data</pre>'), array(
        '%sf_data' => print_r($sf_data, TRUE),
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    $create = TRUE;
  }
  $sfid = $sf_data->Id;
  if ($map->automatic & SALESFORCE_AUTO_SYNC_CREATE && $create || $map->automatic & SALESFORCE_AUTO_SYNC_UPDATE && !$create || !empty($options['extra-linked']) && $options['extra-linked'] == TRUE) {

    // Store the Salesforce ID for the node and return TRUE.
    salesforce_api_id_save('user', $account->uid, $sfid, $name, 'import');
  }
  module_invoke_all('salesforce_api_post_import', $account, $name, $sf_data, $create, $changes);
  unset($account->sf_user_skip_export);
  return $account->uid;
}