You are here

public function MigrateDestinationCRMCoreContact::import in CRM Core 7

Import a single contact.

Parameters

object $contact: Contact object to import. Pre-filled with any fields mapped in the Migration.

object $row: Raw source data object - passed through to prepare/complete handlers.

Return value

array Array of key fields (contact_id only in this case) of the contact that was saved if successful. FALSE on failure.

Throws

MigrateException

Overrides MigrateDestination::import

File

modules/crm_core_contact/includes/crm_core_contact.migrate.inc, line 101
Class MigrateDestinationCRMCoreContact.

Class

MigrateDestinationCRMCoreContact
CRM Contact destination handler.

Code

public function import(stdClass $contact, stdClass $row) {
  $migration = Migration::currentMigration();
  if (isset($row->migrate_map_destid1)) {

    // This is an update to an existing contact.
    if (isset($contact->contact_id)) {
      if ($contact->contact_id != $row->migrate_map_destid1) {
        $params = array(
          '@contact_id' => $contact->contact_id,
          '@destid1' => $row->migrate_map_destid1,
        );
        $message = t('Incoming contact ID "@contact_id" and map destination contact ID "@destid1" do not match.', $params);
        throw new MigrateException($message);
      }
    }
    else {
      $contact->contact_id = $row->migrate_map_destid1;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($contact->contact_id)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination contact_id provided'));
    }
    $old_contact = crm_core_contact_load($contact->contact_id);
    if (empty($old_contact)) {
      $params = array(
        '@contact_id' => $contact->contact_id,
      );
      $message = t('System-of-record is DESTINATION, but contact with ID "@contact_id" does not exist.', $params);
      throw new MigrateException($message);
    }
  }
  if (!isset($contact->type)) {

    // Default the type to our designated destination bundle (by doing this
    // conditionally, we permit some flexibility in terms of implementing
    // migrations which can affect more than one type).
    $contact->type = $this->bundle;
  }

  // Set default properties.
  if ($migration
    ->getSystemOfRecord() == Migration::SOURCE) {
    if (!isset($contact->created)) {
      $contact->created = REQUEST_TIME;
    }
  }
  $this
    ->prepare($contact, $row);
  if (!empty($contact->contact_id)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  migrate_instrument_start('contact_save');
  crm_core_contact_save($contact);
  migrate_instrument_stop('contact_save');
  if (!empty($contact->contact_id)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    $return = array(
      $contact->contact_id,
    );
  }
  else {
    $return = FALSE;
  }
  $this
    ->complete($contact, $row);
  return $return;
}