$csvpath = MODX_ASSETS_PATH . 'imports/';
$csvfile = $csvpath . 'userimport.csv'; // make this a snippet property
$file = new SplFileObject($csvfile);
$delimiter = ',';
$enclosure = "\n";
$file->setCsvControl($delimiter, $enclosure);
while ($file->valid()) {
$data = $file->fgetcsv();
$email = $data[2];
// check for email already existing
$result = $modx->query('SELECT id FROM modx_user_attributes WHERE email = ' . $email);
if (!is_object($result)) {
// user needs to be created
$username = strstr($email, '@', true); // As of PHP 5.3.0
$fullname = $data[0] . ' ' . $data[1];
// we already have $email
$active = '1';
...
}This question has been answered by multiple community members. See the first response.
//create the new user object
$userToSave = $modx->newObject('modUser', array ('username'=>$theUsersNewName));
//give a password
$userToSave->set('password', $theUsersNewPassword);
//setup the users profile
$profile = $modx->newObject('modUserProfile');
$profile->set('email',$theUsersNewEmail);
$profile->set('fullname',$theUsersFullName);
$success = $userToSave->addOne($profile);
if ($success) {
$userToSave->save();
}else {
$output.= '<p>failed to add profile. User not saved.</p>';
}
$response = $modx->runProcessor('security/user/create',array(
'username' => $credentials['username'],
'fullname' => $juser->name,
'email' => $juser->email,
'phone' => $juser->telnr ? $juser->telnr:'0612345678',
'newpassword' => TRUE,
/* Changed theses params */
'specifiedpassword' => $credentials['password'],
'confirmpassword' => $credentials['password'],
/* End of change */
'passwordnotifymethod' => 'e'
)); // Update the quote with the new user_id
if ($newuser = $response->getObject()){
$object->set('user_id',$modx->getOption('id',$newuser));
$object->save();
// Add the new user to the Parents group
$user = $modx->getObject('modUser', $modx->getOption('id',$newuser));
$user->joinGroup(3, 1);
// Create and save the parent details record also
$parent = $modx->newObject('ParentDetails');
$parent->set('parent_id',$modx->getOption('id',$newuser));
$parent->set('coach_id', $object->get('coach_id'));
$parent->set('hourly_rate', $object->get('cost_per_hour'));
$parent->set('send_invoice', 'Y');
$parent->set('num_lessons', '8');
$parent->save();