I don't know of a tutorial, because the exact code would depend on the structure of the file, but the basic snippet code would look something like this for your file:
<?php
/* open the user file and read the lines in a loop */
$file = "full/path/to/userfile.txt";
$output = "";
$fp = fopen($file, "r");
if ($fp) {
/* skip first line */
$line = fgets($fp, 4096);
while (($line = fgets($fp, 4096)) !== false) {
/* skip empty lines */
if (empty($line)) {
continue;
}
/* process line */
$fields = explode(';', $line);
if (count($fields != 6)) {
$output .= "" . 'malformed user info: ' . $fields[0] . ' not created';
continue;
}
$userFields = array();
$userFields['username'] = $fields[0];
$userFields['password'] = $fields[2];
$userFields['active'] = ($fields[4] == 'active');
$newUser = $modx->newObject('modUser', $userFields);
$newUser->save();
/* not sure this next line is necessary, but it can't hurt */
$newUser = getObject('modUser', array('username' => $userFields[0]));
$profile = $newUser->getOne('Profile');
$profile->set('fullname', $fields[1]);
$profile->set('email', $fields[3]);
$newUser->joinGroup($fields[5]);
$saveUser = $newUser->save();
$saveProfile = $profile->save();
if ($saveUser && $saveProfile) {
$output .= "" . 'created user: ' . $fields[0];
} else {
$output .= "" . 'error creating user: ' . $fields[0];
}
}
fclose($fp);
} else {
return 'Failed to open file: ' . $file;
}
return $output;
I'd try it with a small group of users first (and delete them before each run). The code will run for a while before anything appears on the screen, especially with the full 1000.
I see the forum has mangled some of the code. There are br tags inside double quotes at the beginning of the messages to put them on a new line and the forum has removed them.
[ed. note: BobRay last edited this post 12 years, 4 months ago.]