You are here

function sf_entity_import in Salesforce Suite 7

Same name and namespace in other branches
  1. 7.2 sf_entity/sf_entity.module \sf_entity_import()

Imports data from Salesforce into an entity

Parameters

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

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

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

Return value

The id of the imported node or FALSE on failure.

File

sf_entity/sf_entity.module, line 766
Integrates fieldable entities with the Salesforce API.

Code

function sf_entity_import($sf_data, $fieldmap, $id = NULL) {

  // Retrieve the object from Salesforce.
  $sf = salesforce_api_connect();
  if (!$sf) {
    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 = $sf
      ->retrieve(array(
      $sf_data,
    ), $fieldmap);
  }
  elseif (is_array($sf_data)) {
    $sf_data = (object) $sf_data;
  }
  if (empty($sf_data)) {
    return FALSE;
  }
  $sfid = $sf_data->Id;

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

  // Load the object definitions.
  $drupal_object_definition = salesforce_api_fieldmap_objects_load('drupal', $map['drupal_entity'], $map['drupal_bundle']);
  $salesforce_object_definition = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce'], $map['salesforce']);

  // If the node exists, simply update the existing node.
  $entities = entity_load($map['drupal_entity'], $id);
  if (is_array($entities)) {
    $entity = current($entities);
  }
  else {
    $entity = $entities;
  }
  if (empty($id) || empty($entity)) {

    // Look for any matching records which we might want to update instead of creating duplicates.
    $matches = salesforce_api_search_for_duplicates('import', $sf_data, $fieldmap);
    if (!empty($matches)) {
      $id = reset($matches);
      if (!empty($id)) {
        $entities = entity_load($map['drupal_entity'], $id);
        if (is_array($entities)) {
          $entity = current($entities);
        }
        else {
          $entity = $entities;
        }
      }
    }
    if (empty($entity)) {
      $ids = array(
        NULL,
        NULL,
        $map['drupal_bundle'],
      );
      $entity = entity_create_stub_entity($map['drupal_type'], $ids);
      $entity->is_new = TRUE;
    }
  }
  list($id, $vid, $bundle) = entity_extract_ids($type, $entity);

  // Loop through the fields on the fieldmap.
  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];

      // Let the handler function set the value for the field on the node.
      $drupal_field_import_handler($entity, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition);
    }
    elseif (isset($sf_data->{$sf_fieldname})) {

      // Otherwise set the field on the export object to the value of the source
      // field if it's present on the source object.
      $entity->{$drupal_fieldname} = $sf_data->{$sf_fieldname};
    }
  }
  $entity->sf_entity_skip_export = TRUE;

  // It would be nice if we could just call entity_save($entity), but there is
  // no entity_save (wtf?). Fortunately core modules all implement their save
  // functions in almost the exact same way, and they all call entity_invoke()
  // so that our hook_entity_update and hook_entity_insert will fire properly.
  // I really don't understand why node_save still exists since we now have
  // entity_invoke.
  $function = $map['drupal_entity'] . '_save';
  if (function_exists($function)) {
    $function($entity);
  }
  else {
    if ($entity->is_new || empty($id)) {
      entity_invoke('insert', $map['drupal_entity'], $entity);
    }
    else {
      entity_invoke('update', $map['drupal_entity'], $entity);
    }
  }
  list($id, $vid, $bundle) = entity_extract_ids($type, $entity);
  if ($map['automatic'] && !empty($id)) {

    // Store the Salesforce ID for the node and return TRUE.
    salesforce_api_id_save($map['drupal_entity'], $bundle, $id, $sfid, $fieldmap);
  }
  unset($entity->sf_entity_skip_export);
  return $id;
}