You are here

function joomla_import_users in Joomla to Drupal 7

Same name and namespace in other branches
  1. 6 joomla.module \joomla_import_users()
2 calls to joomla_import_users()
joomla_cron in ./joomla.module
joomla_import_form_submit in ./joomla.module
3 string references to 'joomla_import_users'
joomla_cron in ./joomla.module
joomla_import_form_checkboxes in ./joomla.module
These checkboxes are used on both the admin and import forms
joomla_uninstall in ./joomla.install
Implementation of hook_uninstall().

File

./joomla.module, line 359
The joomla module used for migrate Joomla to Drupal.

Code

function joomla_import_users($joomla_update_duplicate = NULL) {
  joomla_database_init();
  if ($joomla_update_duplicate === NULL) {
    $joomla_update_duplicate = variable_get('joomla_update_duplicate', JOOMLA_UPDATE_DUPLICATE);
  }
  $joomla_prefix = variable_get('joomla_prefix', JOOMLA_PREFIX);

  //Add Realname to Profile
  if (db_query("SELECT COUNT(*) FROM {profile_field} WHERE name='profile_realname'")
    ->fetchField() == 0) {
    db_query(" INSERT INTO {profile_field} (title,name,type,weight) VALUES ('Real Name','profile_realname','textfield','0')");
  }
  $results_fid = db_query("SELECT fid from {profile_field} WHERE name='profile_realname'");
  foreach ($results_fid as $data_fid) {
    $fid = $data_fid->fid;

    //Check Users
    $accounts_total = 0;
    $accounts_updated = 0;
    $accounts_new = 0;
    $accounts_failed = 0;
    $accounts_duplicate = 0;
    db_set_active('joomla');
    $results_user = db_query("SELECT * FROM {$joomla_prefix}users ORDER BY id");
    foreach ($results_user as $data) {
      $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;
      }
      $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 for d7
      if ($account->access < 0) {
        $account->access = 0;
      }
      if ($account->created < 0) {
        $account->created = 0;
      }

      /**
       * 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 (strlen($data->password) == 32) {
        $account->pass = $data->password;
      }
      $res = FALSE;
      if (!$uid) {

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

          //the username already exists
          $accounts_duplicate++;
          continue;
        }

        // d7 uid is not auto-increment so we must make it

        //get biggest uid
        $biggest_uid = db_query("SELECT MAX(uid) AS maxuid FROM {users}")
          ->fetchField();
        $account->uid = intval($biggest_uid);
        $account->uid++;
        $res = drupal_write_record('users', $account);
      }
      elseif ($joomla_update_duplicate) {
        $res = drupal_write_record('users', $account, 'uid');
      }
      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);
        }

        //Check and Update Realname
        $profile_value = new stdClass();
        $profile_value->fid = $fid;
        $profile_value->uid = $account->uid;
        $profile_value->value = $data->name;
        if ($uid) {
          drupal_write_record('profile_value', $profile_value, array(
            'fid',
            'uid',
          ));
        }
        else {
          drupal_write_record('profile_value', $profile_value);
        }
      }
      switch ($res) {
        case SAVED_NEW:
          $accounts_new++;
          break;
        case SAVED_UPDATED:
          $accounts_updated++;
          break;
        default:
          $accounts_failed++;
          break;
      }

      // Hook to allow other modules to modify the user
      module_invoke_all('joomla', 'user', $account, $data->id);
      joomla_sleep($accounts_total);
    }
  }
  drupal_set_message(t('Processed @users_total users (@users_new new, @users_duplicate duplicates found, @users_updated updated, @users_failed errors)', array(
    '@users_total' => $accounts_total,
    '@users_duplicate' => $accounts_duplicate,
    '@users_new' => $accounts_new,
    '@users_updated' => $accounts_updated,
    '@users_failed' => $accounts_failed,
  )));
}