You are here

function drush_sf_import in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 7.2 sf_import/sf_import.drush.inc \drush_sf_import()

Import data from Salesforce

File

sf_import/sf_import.drush.inc, line 195
Drush integration for Salesforce Import module. Provides commands to import Salesforce data.

Code

function drush_sf_import($fieldmap_key) {

  // Initial checks
  if (!$fieldmap_key) {
    drush_die('Please provide a mapping to use');
    return;
  }
  _sf_import_drush_check_soap();
  $map = salesforce_api_fieldmap_load($fieldmap_key);
  if (empty($map)) {
    drush_die('Invalid fieldmap');
    return;
  }

  // Read options
  $where = drush_get_option('where', FALSE);
  $start = intval(drush_get_option('start', 0));
  $count = intval(drush_get_option('count', -1));
  $no_new = drush_get_option('no-new', FALSE);
  $no_update = drush_get_option('no-update', FALSE);
  $check_date = drush_get_option('check-date', FALSE);

  // Read existing entries
  $result = db_query("\n    SELECT oid, sfid\n    FROM   {salesforce_object_map}\n    WHERE  name = '%s'", $map->name);
  while ($row = db_fetch_array($result)) {
    $existing[$row['sfid']] = $row['oid'];
  }

  // Read entries from salesforce
  $fields = array_keys($map->fields);
  if ($check_date && !in_array('LastModifiedDate', $fields)) {
    $fields[] = 'LastModifiedDate';
  }
  if (!in_array('Id', $fields)) {
    $fields[] = 'Id';
  }
  $soql = "\n    SELECT " . implode(', ', $fields) . "\n    FROM   " . $map->salesforce . "\n  ";
  if ($where) {
    $soql .= " WHERE " . $where;
  }
  try {
    $sf = salesforce_api_connect();
    $query = $sf->client
      ->query($soql);
  } catch (Exception $e) {
    drush_die("Error while querying Salesforce : " . $e
      ->getMessage());
  }

  // Prepare for import
  $type = $map->drupal;
  if (strpos($type, 'node_') === 0) {
    $type = 'node';
  }
  $function = 'sf_' . $type . '_import';
  if (!function_exists($function)) {
    drush_die("Could not find import function for type " . $type);
  }
  $drupal_object = salesforce_api_fieldmap_objects_load('drupal', $map->drupal);
  if ($count == -1) {
    $count = count($query->records) - $start;
  }
  if ($start < 0 || $start + $count > count($query->records)) {
    drush_die("Start/count exceed possible rows. Number of rows in Salesfoce : " . count($query->records));
  }

  // Run the import
  $created = 0;
  $updated = 0;
  $skipped = 0;
  $failed = 0;
  $imported = 0;
  for ($i = $start; $i < $start + $count; $i++) {
    $record = $query->records[$i];

    // Check if we know this record
    $exists = isset($existing[$record->Id]);
    if ($exists && $no_update) {
      drush_print("Record Id {$record->Id} already exists, skipping");
      $skipped++;
      continue;
    }
    if (!$exists && $no_new) {
      drush_print("Record Id {$record->Id} does not exist, skipping");
      $skipped++;
      continue;
    }
    if ($type == 'node' && $exists && $check_date) {
      $changed = db_result(db_query("\n        SELECT changed\n        FROM   {node}\n        WHERE  nid = %d", $existing[$record->Id]));
      if ($changed >= strtotime($record->LastModifiedDate)) {
        drush_print("Record Id {$record->Id} has not been modified (date check)");
        $skipped++;
        continue;
      }
    }
    if ($exists) {
      $existing_oid = $existing[$record->Id];
    }
    else {
      $existing_oid = NULL;
    }
    $oid = FALSE;
    try {
      $oid = $function($record->Id, $map->name, $existing_oid);
    } catch (Exception $e) {
      drush_log("Error importing record {$record->Id} : " . $e
        ->getMessage(), 'error');
    }
    if (!$oid) {
      drush_log("Error importing record {$record->Id}", 'error');
      $failed++;
    }
    else {
      if (!$exists) {
        drush_print("Created object {$oid}");
        $created++;
      }
      else {
        drush_print("Updated object {$oid}");
        $updated++;
      }
      $imported++;
    }
  }

  // Report
  drush_print("Number of created objects : " . $created);
  drush_print("Number of updated objects : " . $updated);
  drush_print("Number of skipped objects : " . $skipped);
  drush_print("Number of failed imports : " . $failed);
}