Skip to content

Instantly share code, notes, and snippets.

@denysbutenko
Created March 2, 2013 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save denysbutenko/5071913 to your computer and use it in GitHub Desktop.
Save denysbutenko/5071913 to your computer and use it in GitHub Desktop.
[modx revo]import users from csv
<?php
require_once '/absolute_path/config.core.php';
require_once MODX_CORE_PATH.'model/modx.class.php';
$modx = new modX();
$modx->initialize('web');
$modx->getService('error','error.modError');
//To get Base Path
$basePath = $modx->config['base_path'];
// Prepare the data
if (file_exists($basePath."users.csv")){
$fp = fopen($basePath."users.csv","r");
$ausers =array();
while($data=fgetcsv($fp,900000,",")){
$datas =array(
'email' => $data[0],
'password' => $data[1],
// Will be encrypted automatically in the below process
);
array_push($ausers,$datas);
}
}
foreach ($ausers as $fields) {
$email=$fields['email'];
$passwd=$fields['password'];
// Query to check Email address is unique,not exists in database.
$stmt = $modx->query("select email from modx_users
where email = $email");
$result = $stmt->fetchAll(PDO::FETCH_COLUMN);
if(count($result)==0 )
{
/* To create user and profile */
$user = $modx->newObject('modUser');
$profile = $modx->newObject('modUserProfile');
/* To override class key */
if (empty($fields['class_key'])) $fields['class_key'] = 'modUser';
/* set user and profile */
$user->fromArray($fields);
$user->set('username',email);
$user->set('active',1); // 1 to set user as active,0 for inactive
$version = $modx->getVersionData();
$user->set('password',$passwd);
$profile->fromArray($fields);
$profile->set('internalKey',0);
$user->addOne($profile,'Profile');
/* To save user */
if (!$user->save()) {
$modx->log(modX::LOG_LEVEL_ERROR,'Could not save registered user:
'.$user->get('id').' with username:
'.$user->get('username'));
return 'Error saving user '.print_r($scriptProperties,true);
}else
{
$userId =$user->get('id');
// To get saved user id
$user = (!empty($userId)) ? $modx->getObject('modUser', $userId)
: $modx->user;
/* To store extended data */
$extendedFields = array();
$extendedFields = $profile->get('extended');
$extendedFields['gender'] = $fields['gender'];
$profile->set('extended', $extendedFields);
$profile->save();
/* To send mail to users */
$scriptProperties =array();
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY, $body);
$modx->mail->set(modMail::MAIL_FROM, 'admin@example.com');
$modx->mail->set(modMail::MAIL_FROM_NAME, 'admin');
$modx->mail->set(modMail::MAIL_SENDER, $email);
$modx->mail->set(modMail::MAIL_SUBJECT, 'Your Account has been created');
$modx->mail->address('to', $email, $user->get('username'));
$modx->mail->setHTML(true);
$sent = $modx->mail->send();
}
}else{
continue;// If user already exists
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment