You are here

function joomla_user_save in Joomla to Drupal 7.2

1 call to joomla_user_save()
joomla_batch_save in ./joomla.batch.inc

File

./joomla.batch.inc, line 82

Code

function joomla_user_save(&$context) {
  $joomla_update_duplicate = $context['sandbox']['joomla_update_duplicate'];
  $offset =& $context['sandbox']['users_offset'];
  db_set_active('joomla');
  $users = db_select('users', 'u')
    ->fields('u')
    ->orderBy('u.id', 'DESC')
    ->range($offset, 10)
    ->execute()
    ->fetchAll();
  foreach ($users as $num => $data) {
    $context['sandbox']['progress']++;
    $context['results']['accounts_total']++;
    db_set_active();
    $uid = db_query("SELECT uid FROM {joomla_users} WHERE juid = :juid", array(
      ':juid' => $data->id,
    ))
      ->fetchField();
    $converted = db_query("SELECT converted FROM {joomla_users} WHERE juid = :juid", array(
      ':juid' => $data->id,
    ))
      ->fetchField();

    // Check if the user has selected to update previously imported users
    if ($uid && !$joomla_update_duplicate) {
      continue;
    }

    //if this user has his password converted to drupals hash then we must not update him
    if ($converted && $joomla_update_duplicate) {
      continue;
    }
    $account = new stdClass();

    // Set uid if we are updating an existing record
    if ($uid) {
      $account->uid = $uid;
    }
    else {

      //check username to be unique
      $duplicate = db_query_range("SELECT name FROM {users} WHERE name = :name", 0, 1, array(
        ':name' => $data->username,
      ))
        ->fetchField();
      if (!empty($duplicate)) {

        //the username already exists
        $context['results']['accounts_duplicate']++;
        continue;
      }
      $account->is_new = TRUE;
    }
    $account->name = $data->username;
    $account->mail = $data->email;
    $account->status = !$data->block;
    $account->created = strtotime($data->registerdate);
    $account->access = strtotime($data->lastvisitdate);

    //no negative values
    $account->access = $account->access < 0 ? NULL : $account->access;
    $account->created = $account->created < 0 ? NULL : $account->created;
    if ($real_name_field = variable_get('joomla_real_name_field', JOOMLA_REAL_NAME_FIELD)) {
      $lang = field_language('user', $account, $real_name_field);
      $account->{$real_name_field}[$lang][0]['value'] = $data->name;
    }

    /**
     * Older versions of Joomla used an unsalted MD5 password hash.  If this
     * is the case we can use this hash as the Drupal password.
     */
    if (drupal_strlen($data->password) == 32) {
      $account->pass = $data->password;
    }
    $res = user_save($account);
    if ($res) {

      // Write into the joomla -> drupal user mapping table
      $joomla_user = new stdClass();
      $joomla_user->uid = $account->uid;
      $joomla_user->juid = $data->id;
      $joomla_user->password = $data->password;
      if ($uid) {
        drupal_write_record('joomla_users', $joomla_user, 'uid');
      }
      else {
        drupal_write_record('joomla_users', $joomla_user);
      }
    }
    if ($uid && $res) {
      $context['results']['accounts_updated']++;
    }
    elseif (!$uid && $res) {
      $context['results']['accounts_new']++;
    }
    else {
      $context['results']['accounts_failed']++;
    }

    // Hook to allow other modules to modify the term
    module_invoke_all('joomla', 'user', $account, $data);
    $context['message'] = t('Now processing %user', array(
      '%user' => $data->name,
    ));
  }
  $offset += 10;
}