You are here

public function MigrateDestinationUser::import in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/user.inc \MigrateDestinationUser::import()

Import a single user.

Parameters

$account: Account object to build. Prefilled with any fields mapped in the Migration.

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

Return value

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

Overrides MigrateDestination::import

File

plugins/destinations/user.inc, line 153
Support for user destinations.

Class

MigrateDestinationUser
Destination class implementing migration into users.

Code

public function import(stdClass $account, stdClass $row) {
  $migration = Migration::currentMigration();

  // Updating previously-migrated content?
  if (isset($row->migrate_map_destid1)) {

    // Make sure is_new is off
    $account->is_new = FALSE;
    if (isset($account->uid)) {
      if ($account->uid != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming uid !uid and map destination uid !destid1 don't match", array(
          '!uid' => $account->uid,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $account->uid = $row->migrate_map_destid1;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($account->uid)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination uid provided'));
    }
    $old_account = user_load($account->uid, TRUE);
    if (empty($old_account)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but user !uid does not exist', array(
        '!uid' => $account->uid,
      )));
    }
  }
  else {
    $old_account = $account;
  }

  // Roles must be arrays keyed by the role id, which isn't how the data
  // naturally comes in. Fix them up.
  // First, if names instead of IDs are presented, translate them
  if (!empty($account->role_names)) {
    $role_names = is_array($account->role_names) ? $account->role_names : array(
      $account->role_names,
    );
    foreach ($role_names as $role_name) {
      $role = user_role_load_by_name($role_name);
      if ($role) {
        $account->roles[] = $role->rid;
      }
    }
  }
  if (!empty($account->roles)) {
    if (!is_array($account->roles)) {
      $account->roles = array(
        $account->roles,
      );
    }
    $account->roles = drupal_map_assoc($account->roles);
  }
  if (empty($account->roles) && empty($old_account->roles)) {
    $account->roles = array();
  }
  $this
    ->prepare($account, $row);
  if (isset($account->uid) && !(isset($account->is_new) && $account->is_new)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }

  // While user_save is happy to see a fid in $account->picture on insert,
  // when updating an existing account it wants a file object.
  if ($updating && isset($account->picture) && ($fid = $account->picture)) {
    $account->picture = file_load($fid);
  }

  // Normalize MD5 passwords to lowercase, as generated by Drupal 6 and previous
  if ($this->md5Passwords) {
    $account->pass = drupal_strtolower($account->pass);
  }

  // If any datetime values were included, ensure that they're in timestamp format.
  if (isset($account->created)) {
    $account->created = MigrationBase::timestamp($account->created);
  }
  if (isset($account->access)) {
    $account->access = MigrationBase::timestamp($account->access);
  }
  if (isset($account->login)) {
    $account->login = MigrationBase::timestamp($account->login);
  }

  // Validate field data prior to saving.
  MigrateDestinationEntity::fieldAttachValidate('user', $account);
  migrate_instrument_start('user_save');
  $newaccount = user_save($old_account, (array) $account);
  migrate_instrument_stop('user_save');
  if ($newaccount) {
    if ($this->md5Passwords && !empty($account->pass)) {

      // Ape the Drupal 6 -> Drupal 7 upgrade, which encrypts the MD5 text in the
      // modern way, and marks it with a prepended U so it recognizes and fixes it
      // up at login time.
      $password = 'U' . $newaccount->pass;
      db_update('users')
        ->fields(array(
        'pass' => $password,
      ))
        ->condition('uid', $newaccount->uid)
        ->execute();
    }

    // Unlike nodes and taxonomy terms, core does not automatically save an
    // alias in a user entity, we must do it ourselves.
    if (module_exists('path')) {
      if (!empty($account->path['alias'])) {
        $path = array(
          'source' => 'user/' . $account->uid,
          'alias' => $account->path['alias'],
        );
        migrate_instrument_start('path_save');
        path_save($path);
        migrate_instrument_stop('path_save');
      }
    }
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;

      // user_save() doesn't update file_usage on account creation, we have
      // to do it ourselves.
      if (!empty($newaccount->picture)) {
        $file = file_load($newaccount->picture);
        if (is_object($file)) {
          file_usage_add($file, 'user', 'user', $newaccount->uid);
        }
      }
    }
    $this
      ->complete($newaccount, $row);
    $return = array(
      $newaccount->uid,
    );
  }
  else {
    $return = FALSE;
  }
  return $return;
}